vrchess

diff src/game.cc @ 2:879194e4b1f0

fixed line-endings
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 Apr 2014 05:44:09 +0300
parents cd7755e4663a
children a797e426e309
line diff
     1.1 --- a/src/game.cc	Fri Apr 25 05:43:26 2014 +0300
     1.2 +++ b/src/game.cc	Fri Apr 25 05:44:09 2014 +0300
     1.3 @@ -1,180 +1,180 @@
     1.4 -#include "game.h"
     1.5 -#include "opengl.h"
     1.6 -#include "camera.h"
     1.7 -#include "texture.h"
     1.8 -//#include "OVR_CAPI_GL.h"
     1.9 -
    1.10 -static void draw_scene();
    1.11 -
    1.12 -static const float move_speed = 10.0f;
    1.13 -
    1.14 -static int fb_width, fb_height;
    1.15 -static FlyCamera cam;
    1.16 -static Texture floor_tex;
    1.17 -static bool keystate[256];
    1.18 -
    1.19 -bool game_init()
    1.20 -{
    1.21 -	glEnable(GL_DEPTH_TEST);
    1.22 -	glEnable(GL_CULL_FACE);
    1.23 -	glEnable(GL_LIGHTING);
    1.24 -	glEnable(GL_LIGHT0);
    1.25 -
    1.26 -	glClearColor(0.1, 0.1, 0.1, 1);
    1.27 -
    1.28 -	if(!floor_tex.load("data/tiles.png")) {
    1.29 -		return false;
    1.30 -	}
    1.31 -
    1.32 -	cam.input_move(0, 0, 5);
    1.33 -	return true;
    1.34 -}
    1.35 -
    1.36 -void game_cleanup()
    1.37 -{
    1.38 -	floor_tex.destroy();
    1.39 -}
    1.40 -
    1.41 -
    1.42 -void game_update(unsigned int msec)
    1.43 -{
    1.44 -	static unsigned int prev_msec;
    1.45 -	float dt = (msec - prev_msec) / 1000.0f;
    1.46 -	float offs = dt * move_speed;
    1.47 -	prev_msec = msec;
    1.48 -
    1.49 -	Vector3 move;
    1.50 -	float roll = 0.0f;
    1.51 -
    1.52 -	if(keystate['d'] || keystate['D']) {
    1.53 -		move.x += offs;
    1.54 -	}
    1.55 -	if(keystate['a'] || keystate['A']) {
    1.56 -		move.x -= offs;
    1.57 -	}
    1.58 -	if(keystate['s'] || keystate['S']) {
    1.59 -		move.z += offs;
    1.60 -	}
    1.61 -	if(keystate['w'] || keystate['W']) {
    1.62 -		move.z -= offs;
    1.63 -	}
    1.64 -	if(keystate['e'] || keystate['E']) {
    1.65 -		roll += dt;
    1.66 -	}
    1.67 -	if(keystate['q'] || keystate['Q']) {
    1.68 -		roll -= dt;
    1.69 -	}
    1.70 -
    1.71 -	cam.input_move(move.x, move.y, move.z);
    1.72 -	cam.input_rotate(0, 0, roll);
    1.73 -}
    1.74 -
    1.75 -void game_render(int eye)
    1.76 -{
    1.77 -	Matrix4x4 view_matrix = cam.get_matrix().inverse();
    1.78 -
    1.79 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.80 -
    1.81 -	glMatrixMode(GL_PROJECTION);
    1.82 -	glLoadIdentity();
    1.83 -	gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
    1.84 -
    1.85 -	glMatrixMode(GL_MODELVIEW);
    1.86 -	glLoadIdentity();
    1.87 -	glLoadTransposeMatrixf(view_matrix[0]);
    1.88 -
    1.89 -	draw_scene();
    1.90 -}
    1.91 -
    1.92 -void game_reshape(int x, int y)
    1.93 -{
    1.94 -	glViewport(0, 0, x, y);
    1.95 -	fb_width = x;
    1.96 -	fb_height = y;
    1.97 -}
    1.98 -
    1.99 -void game_keyboard(int key, bool pressed, int x, int y)
   1.100 -{
   1.101 -	if(pressed) {
   1.102 -		switch(key) {
   1.103 -		case 27:
   1.104 -			exit(0);
   1.105 -		}
   1.106 -	}
   1.107 -
   1.108 -	if(key < 256) {
   1.109 -		keystate[key] = pressed;
   1.110 -	}
   1.111 -}
   1.112 -
   1.113 -static int prev_x, prev_y;
   1.114 -static bool bnstate[32];
   1.115 -
   1.116 -void game_mouse(int bn, bool pressed, int x, int y)
   1.117 -{
   1.118 -	bnstate[bn] = pressed;
   1.119 -	prev_x = x;
   1.120 -	prev_y = y;
   1.121 -}
   1.122 -
   1.123 -void game_motion(int x, int y)
   1.124 -{
   1.125 -	int dx = x - prev_x;
   1.126 -	int dy = y - prev_y;
   1.127 -	prev_x = x;
   1.128 -	prev_y = y;
   1.129 -
   1.130 -	if(!dx && !dy) return;
   1.131 -
   1.132 -	if(bnstate[0]) {
   1.133 -		float xrot = dy * 0.5;
   1.134 -		float yrot = dx * 0.5;
   1.135 -		cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
   1.136 -		cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
   1.137 -	}
   1.138 -}
   1.139 -
   1.140 -void game_6dof_move(float x, float y, float z)
   1.141 -{
   1.142 -	cam.input_move(x, y, z);
   1.143 -}
   1.144 -
   1.145 -void game_6dof_rotate(float x, float y, float z)
   1.146 -{
   1.147 -	cam.input_rotate(x, y, z);
   1.148 -}
   1.149 -
   1.150 -static void draw_scene()
   1.151 -{
   1.152 -	glMatrixMode(GL_MODELVIEW);
   1.153 -	glTranslatef(0, -1.5, 0);
   1.154 -
   1.155 -	float lpos[] = {-20, 30, 10, 1};
   1.156 -	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   1.157 -
   1.158 -	glEnable(GL_TEXTURE_2D);
   1.159 -	floor_tex.bind();
   1.160 -
   1.161 -	glMatrixMode(GL_TEXTURE);
   1.162 -	glScalef(8, 8, 8);
   1.163 -
   1.164 -	glBegin(GL_QUADS);
   1.165 -	glNormal3f(0, 1, 0);
   1.166 -	glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
   1.167 -	glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
   1.168 -	glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
   1.169 -	glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
   1.170 -	glEnd();
   1.171 -	glDisable(GL_TEXTURE_2D);
   1.172 -	glLoadIdentity();
   1.173 -
   1.174 -	glMatrixMode(GL_MODELVIEW);
   1.175 -	glPushMatrix();
   1.176 -	glTranslatef(0, 0.75, 0);
   1.177 -
   1.178 -	glFrontFace(GL_CW);
   1.179 -	glutSolidTeapot(1.0);
   1.180 -	glFrontFace(GL_CCW);
   1.181 -
   1.182 -	glPopMatrix();
   1.183 -}
   1.184 +#include "game.h"
   1.185 +#include "opengl.h"
   1.186 +#include "camera.h"
   1.187 +#include "texture.h"
   1.188 +//#include "OVR_CAPI_GL.h"
   1.189 +
   1.190 +static void draw_scene();
   1.191 +
   1.192 +static const float move_speed = 10.0f;
   1.193 +
   1.194 +static int fb_width, fb_height;
   1.195 +static FlyCamera cam;
   1.196 +static Texture floor_tex;
   1.197 +static bool keystate[256];
   1.198 +
   1.199 +bool game_init()
   1.200 +{
   1.201 +	glEnable(GL_DEPTH_TEST);
   1.202 +	glEnable(GL_CULL_FACE);
   1.203 +	glEnable(GL_LIGHTING);
   1.204 +	glEnable(GL_LIGHT0);
   1.205 +
   1.206 +	glClearColor(0.1, 0.1, 0.1, 1);
   1.207 +
   1.208 +	if(!floor_tex.load("data/tiles.png")) {
   1.209 +		return false;
   1.210 +	}
   1.211 +
   1.212 +	cam.input_move(0, 0, 5);
   1.213 +	return true;
   1.214 +}
   1.215 +
   1.216 +void game_cleanup()
   1.217 +{
   1.218 +	floor_tex.destroy();
   1.219 +}
   1.220 +
   1.221 +
   1.222 +void game_update(unsigned int msec)
   1.223 +{
   1.224 +	static unsigned int prev_msec;
   1.225 +	float dt = (msec - prev_msec) / 1000.0f;
   1.226 +	float offs = dt * move_speed;
   1.227 +	prev_msec = msec;
   1.228 +
   1.229 +	Vector3 move;
   1.230 +	float roll = 0.0f;
   1.231 +
   1.232 +	if(keystate['d'] || keystate['D']) {
   1.233 +		move.x += offs;
   1.234 +	}
   1.235 +	if(keystate['a'] || keystate['A']) {
   1.236 +		move.x -= offs;
   1.237 +	}
   1.238 +	if(keystate['s'] || keystate['S']) {
   1.239 +		move.z += offs;
   1.240 +	}
   1.241 +	if(keystate['w'] || keystate['W']) {
   1.242 +		move.z -= offs;
   1.243 +	}
   1.244 +	if(keystate['e'] || keystate['E']) {
   1.245 +		roll += dt;
   1.246 +	}
   1.247 +	if(keystate['q'] || keystate['Q']) {
   1.248 +		roll -= dt;
   1.249 +	}
   1.250 +
   1.251 +	cam.input_move(move.x, move.y, move.z);
   1.252 +	cam.input_rotate(0, 0, roll);
   1.253 +}
   1.254 +
   1.255 +void game_render(int eye)
   1.256 +{
   1.257 +	Matrix4x4 view_matrix = cam.get_matrix().inverse();
   1.258 +
   1.259 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.260 +
   1.261 +	glMatrixMode(GL_PROJECTION);
   1.262 +	glLoadIdentity();
   1.263 +	gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
   1.264 +
   1.265 +	glMatrixMode(GL_MODELVIEW);
   1.266 +	glLoadIdentity();
   1.267 +	glLoadTransposeMatrixf(view_matrix[0]);
   1.268 +
   1.269 +	draw_scene();
   1.270 +}
   1.271 +
   1.272 +void game_reshape(int x, int y)
   1.273 +{
   1.274 +	glViewport(0, 0, x, y);
   1.275 +	fb_width = x;
   1.276 +	fb_height = y;
   1.277 +}
   1.278 +
   1.279 +void game_keyboard(int key, bool pressed, int x, int y)
   1.280 +{
   1.281 +	if(pressed) {
   1.282 +		switch(key) {
   1.283 +		case 27:
   1.284 +			exit(0);
   1.285 +		}
   1.286 +	}
   1.287 +
   1.288 +	if(key < 256) {
   1.289 +		keystate[key] = pressed;
   1.290 +	}
   1.291 +}
   1.292 +
   1.293 +static int prev_x, prev_y;
   1.294 +static bool bnstate[32];
   1.295 +
   1.296 +void game_mouse(int bn, bool pressed, int x, int y)
   1.297 +{
   1.298 +	bnstate[bn] = pressed;
   1.299 +	prev_x = x;
   1.300 +	prev_y = y;
   1.301 +}
   1.302 +
   1.303 +void game_motion(int x, int y)
   1.304 +{
   1.305 +	int dx = x - prev_x;
   1.306 +	int dy = y - prev_y;
   1.307 +	prev_x = x;
   1.308 +	prev_y = y;
   1.309 +
   1.310 +	if(!dx && !dy) return;
   1.311 +
   1.312 +	if(bnstate[0]) {
   1.313 +		float xrot = dy * 0.5;
   1.314 +		float yrot = dx * 0.5;
   1.315 +		cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
   1.316 +		cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
   1.317 +	}
   1.318 +}
   1.319 +
   1.320 +void game_6dof_move(float x, float y, float z)
   1.321 +{
   1.322 +	cam.input_move(x, y, z);
   1.323 +}
   1.324 +
   1.325 +void game_6dof_rotate(float x, float y, float z)
   1.326 +{
   1.327 +	cam.input_rotate(x, y, z);
   1.328 +}
   1.329 +
   1.330 +static void draw_scene()
   1.331 +{
   1.332 +	glMatrixMode(GL_MODELVIEW);
   1.333 +	glTranslatef(0, -1.5, 0);
   1.334 +
   1.335 +	float lpos[] = {-20, 30, 10, 1};
   1.336 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   1.337 +
   1.338 +	glEnable(GL_TEXTURE_2D);
   1.339 +	floor_tex.bind();
   1.340 +
   1.341 +	glMatrixMode(GL_TEXTURE);
   1.342 +	glScalef(8, 8, 8);
   1.343 +
   1.344 +	glBegin(GL_QUADS);
   1.345 +	glNormal3f(0, 1, 0);
   1.346 +	glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
   1.347 +	glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
   1.348 +	glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
   1.349 +	glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
   1.350 +	glEnd();
   1.351 +	glDisable(GL_TEXTURE_2D);
   1.352 +	glLoadIdentity();
   1.353 +
   1.354 +	glMatrixMode(GL_MODELVIEW);
   1.355 +	glPushMatrix();
   1.356 +	glTranslatef(0, 0.75, 0);
   1.357 +
   1.358 +	glFrontFace(GL_CW);
   1.359 +	glutSolidTeapot(1.0);
   1.360 +	glFrontFace(GL_CCW);
   1.361 +
   1.362 +	glPopMatrix();
   1.363 +}