nuclear@0: #include nuclear@0: #include "opengl.h" nuclear@0: #include "game.h" nuclear@1: #include "vein.h" nuclear@1: #include "ship.h" nuclear@1: #include "camera.h" nuclear@0: nuclear@1: static Vein *vein; nuclear@1: static Ship ship; nuclear@1: nuclear@1: static int keystate[256]; nuclear@2: static int dbg_inside = false; nuclear@1: nuclear@1: static OrbitCamera dbgcam; nuclear@1: nuclear@1: static unsigned long msec, start_time = -1; nuclear@0: nuclear@0: bool game_init() nuclear@0: { nuclear@0: glEnable(GL_DEPTH_TEST); nuclear@0: glEnable(GL_CULL_FACE); nuclear@0: glEnable(GL_LIGHTING); nuclear@0: glEnable(GL_LIGHT0); nuclear@0: nuclear@0: float lpos[] = {-1, 1, 1, 0}; nuclear@0: glLightfv(GL_LIGHT0, GL_POSITION, lpos); nuclear@0: nuclear@1: // initialize the only level (vein) nuclear@1: vein = new Vein; nuclear@1: nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: void game_shutdown() nuclear@0: { nuclear@0: exit(0); nuclear@0: } nuclear@0: nuclear@1: void game_update(unsigned long ms) nuclear@0: { nuclear@1: static unsigned long last_upd; nuclear@1: nuclear@0: if(start_time == 0) { nuclear@1: start_time = ms; nuclear@0: } nuclear@1: msec = ms - start_time; nuclear@1: nuclear@1: time_sec_t dt = (msec - last_upd) / 1000.0; nuclear@1: nuclear@1: // handle key input nuclear@1: if(keystate['w'] || keystate['W']) { nuclear@1: ship.accelerate(1.0 * dt); nuclear@1: } nuclear@1: if(keystate['s'] || keystate['S']) { nuclear@1: ship.accelerate(-1.0 * dt); nuclear@1: } nuclear@1: // XXX change to mouselook nuclear@1: if(keystate['d'] || keystate['D']) { nuclear@1: ship.turn(-1.0 * dt, 0.0); nuclear@1: } nuclear@1: if(keystate['a'] || keystate['A']) { nuclear@1: ship.turn(1.0 * dt, 0.0); nuclear@1: } nuclear@1: if(keystate['e'] || keystate['E']) { nuclear@2: ship.turn(0.0, 1.0 * dt); nuclear@1: } nuclear@1: if(keystate['c'] || keystate['C']) { nuclear@2: ship.turn(0.0, -1.0 * dt); nuclear@1: } nuclear@1: nuclear@1: ship.update(dt); nuclear@1: nuclear@1: last_upd = msec; nuclear@0: } nuclear@0: nuclear@0: void game_draw() nuclear@0: { nuclear@0: glMatrixMode(GL_MODELVIEW); nuclear@1: dbgcam.use(); nuclear@0: nuclear@2: if(dbg_inside) { nuclear@2: load_gl_matrix(ship.get_matrix().inverse()); nuclear@2: } nuclear@2: nuclear@1: vein->draw(ship.get_position()); nuclear@1: ship.dbg_draw(); nuclear@0: } nuclear@0: nuclear@0: void game_input_keyb(int key, int state) nuclear@0: { nuclear@0: if(state) { nuclear@0: switch(key) { nuclear@0: case 27: nuclear@0: game_shutdown(); nuclear@1: break; nuclear@0: nuclear@2: case '\b': nuclear@2: dbg_inside = !dbg_inside; nuclear@2: break; nuclear@2: nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@1: nuclear@1: if(key < 256) { nuclear@1: keystate[key] = state; nuclear@1: } nuclear@0: } nuclear@1: nuclear@1: static int bnstate[16]; nuclear@1: static int prev_x, prev_y; nuclear@1: nuclear@1: void game_input_mbutton(int bn, int state, int x, int y) nuclear@1: { nuclear@1: if(bn < 16) { nuclear@1: bnstate[bn] = state; nuclear@1: } nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@1: } nuclear@1: nuclear@1: void game_input_mmotion(int x, int y) nuclear@1: { nuclear@1: int dx = x - prev_x; nuclear@1: int dy = y - prev_y; nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@1: nuclear@1: if(bnstate[0]) { nuclear@1: dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0); nuclear@1: } nuclear@1: if(bnstate[2]) { nuclear@1: dbgcam.input_zoom(10.0 * dy / win_ysz); nuclear@1: } nuclear@1: }