conworlds

diff src/game.cc @ 0:b326d53321f7

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 Apr 2014 05:20:53 +0300
parents
children cd7755e4663a
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/game.cc	Fri Apr 25 05:20:53 2014 +0300
     1.3 @@ -0,0 +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 \ No newline at end of file