intravenous

diff src/game.cc @ 1:3ea290d35984

it's never going to finish but wth :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Apr 2012 22:42:43 +0300
parents 2b30f261f641
children 472c28b8b875
line diff
     1.1 --- a/src/game.cc	Sat Apr 21 06:56:33 2012 +0300
     1.2 +++ b/src/game.cc	Sat Apr 21 22:42:43 2012 +0300
     1.3 @@ -1,8 +1,18 @@
     1.4  #include <stdlib.h>
     1.5  #include "opengl.h"
     1.6  #include "game.h"
     1.7 +#include "vein.h"
     1.8 +#include "ship.h"
     1.9 +#include "camera.h"
    1.10  
    1.11 -static unsigned long tmsec, start_time = -1;
    1.12 +static Vein *vein;
    1.13 +static Ship ship;
    1.14 +
    1.15 +static int keystate[256];
    1.16 +
    1.17 +static OrbitCamera dbgcam;
    1.18 +
    1.19 +static unsigned long msec, start_time = -1;
    1.20  
    1.21  bool game_init()
    1.22  {
    1.23 @@ -14,6 +24,9 @@
    1.24  	float lpos[] = {-1, 1, 1, 0};
    1.25  	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.26  
    1.27 +	// initialize the only level (vein)
    1.28 +	vein = new Vein;
    1.29 +
    1.30  	return true;
    1.31  }
    1.32  
    1.33 @@ -22,24 +35,51 @@
    1.34  	exit(0);
    1.35  }
    1.36  
    1.37 -void game_update(unsigned long msec)
    1.38 +void game_update(unsigned long ms)
    1.39  {
    1.40 +	static unsigned long last_upd;
    1.41 +
    1.42  	if(start_time == 0) {
    1.43 -		start_time = msec;
    1.44 +		start_time = ms;
    1.45  	}
    1.46 -	tmsec = msec - start_time;
    1.47 +	msec = ms - start_time;
    1.48 +
    1.49 +	time_sec_t dt = (msec - last_upd) / 1000.0;
    1.50 +
    1.51 +	// handle key input
    1.52 +	if(keystate['w'] || keystate['W']) {
    1.53 +		ship.accelerate(1.0 * dt);
    1.54 +	}
    1.55 +	if(keystate['s'] || keystate['S']) {
    1.56 +		ship.accelerate(-1.0 * dt);
    1.57 +	}
    1.58 +	// XXX change to mouselook
    1.59 +	if(keystate['d'] || keystate['D']) {
    1.60 +		ship.turn(-1.0 * dt, 0.0);
    1.61 +	}
    1.62 +	if(keystate['a'] || keystate['A']) {
    1.63 +		ship.turn(1.0 * dt, 0.0);
    1.64 +	}
    1.65 +	if(keystate['e'] || keystate['E']) {
    1.66 +		ship.turn(0.0, -1.0 * dt);
    1.67 +	}
    1.68 +	if(keystate['c'] || keystate['C']) {
    1.69 +		ship.turn(0.0, 1.0 * dt);
    1.70 +	}
    1.71 +
    1.72 +	ship.update(dt);
    1.73 +
    1.74 +	last_upd = msec;
    1.75  }
    1.76  
    1.77  void game_draw()
    1.78  {
    1.79  	glMatrixMode(GL_MODELVIEW);
    1.80 -	glLoadIdentity();
    1.81 -	glTranslatef(0, 0, -8);
    1.82 -	glRotatef(20, 1, 0, 0);
    1.83 +	dbgcam.use();
    1.84  
    1.85 -	glFrontFace(GL_CW);
    1.86 -	glutSolidTeapot(1.0);
    1.87 -	glFrontFace(GL_CCW);
    1.88 +	vein->draw(ship.get_position());
    1.89 +
    1.90 +	ship.dbg_draw();
    1.91  }
    1.92  
    1.93  void game_input_keyb(int key, int state)
    1.94 @@ -48,9 +88,41 @@
    1.95  		switch(key) {
    1.96  		case 27:
    1.97  			game_shutdown();
    1.98 +			break;
    1.99  
   1.100  		default:
   1.101  			break;
   1.102  		}
   1.103  	}
   1.104 +
   1.105 +	if(key < 256) {
   1.106 +		keystate[key] = state;
   1.107 +	}
   1.108  }
   1.109 +
   1.110 +static int bnstate[16];
   1.111 +static int prev_x, prev_y;
   1.112 +
   1.113 +void game_input_mbutton(int bn, int state, int x, int y)
   1.114 +{
   1.115 +	if(bn < 16) {
   1.116 +		bnstate[bn] = state;
   1.117 +	}
   1.118 +	prev_x = x;
   1.119 +	prev_y = y;
   1.120 +}
   1.121 +
   1.122 +void game_input_mmotion(int x, int y)
   1.123 +{
   1.124 +	int dx = x - prev_x;
   1.125 +	int dy = y - prev_y;
   1.126 +	prev_x = x;
   1.127 +	prev_y = y;
   1.128 +
   1.129 +	if(bnstate[0]) {
   1.130 +		dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
   1.131 +	}
   1.132 +	if(bnstate[2]) {
   1.133 +		dbgcam.input_zoom(10.0 * dy / win_ysz);
   1.134 +	}
   1.135 +}