intravenous

annotate src/game.cc @ 2:472c28b8b875

I think I pretty much nailed the camera
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Apr 2012 23:03:36 +0300
parents 3ea290d35984
children 94d4c60af435
rev   line source
nuclear@0 1 #include <stdlib.h>
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "game.h"
nuclear@1 4 #include "vein.h"
nuclear@1 5 #include "ship.h"
nuclear@1 6 #include "camera.h"
nuclear@0 7
nuclear@1 8 static Vein *vein;
nuclear@1 9 static Ship ship;
nuclear@1 10
nuclear@1 11 static int keystate[256];
nuclear@2 12 static int dbg_inside = false;
nuclear@1 13
nuclear@1 14 static OrbitCamera dbgcam;
nuclear@1 15
nuclear@1 16 static unsigned long msec, start_time = -1;
nuclear@0 17
nuclear@0 18 bool game_init()
nuclear@0 19 {
nuclear@0 20 glEnable(GL_DEPTH_TEST);
nuclear@0 21 glEnable(GL_CULL_FACE);
nuclear@0 22 glEnable(GL_LIGHTING);
nuclear@0 23 glEnable(GL_LIGHT0);
nuclear@0 24
nuclear@0 25 float lpos[] = {-1, 1, 1, 0};
nuclear@0 26 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 27
nuclear@1 28 // initialize the only level (vein)
nuclear@1 29 vein = new Vein;
nuclear@1 30
nuclear@0 31 return true;
nuclear@0 32 }
nuclear@0 33
nuclear@0 34 void game_shutdown()
nuclear@0 35 {
nuclear@0 36 exit(0);
nuclear@0 37 }
nuclear@0 38
nuclear@1 39 void game_update(unsigned long ms)
nuclear@0 40 {
nuclear@1 41 static unsigned long last_upd;
nuclear@1 42
nuclear@0 43 if(start_time == 0) {
nuclear@1 44 start_time = ms;
nuclear@0 45 }
nuclear@1 46 msec = ms - start_time;
nuclear@1 47
nuclear@1 48 time_sec_t dt = (msec - last_upd) / 1000.0;
nuclear@1 49
nuclear@1 50 // handle key input
nuclear@1 51 if(keystate['w'] || keystate['W']) {
nuclear@1 52 ship.accelerate(1.0 * dt);
nuclear@1 53 }
nuclear@1 54 if(keystate['s'] || keystate['S']) {
nuclear@1 55 ship.accelerate(-1.0 * dt);
nuclear@1 56 }
nuclear@1 57 // XXX change to mouselook
nuclear@1 58 if(keystate['d'] || keystate['D']) {
nuclear@1 59 ship.turn(-1.0 * dt, 0.0);
nuclear@1 60 }
nuclear@1 61 if(keystate['a'] || keystate['A']) {
nuclear@1 62 ship.turn(1.0 * dt, 0.0);
nuclear@1 63 }
nuclear@1 64 if(keystate['e'] || keystate['E']) {
nuclear@2 65 ship.turn(0.0, 1.0 * dt);
nuclear@1 66 }
nuclear@1 67 if(keystate['c'] || keystate['C']) {
nuclear@2 68 ship.turn(0.0, -1.0 * dt);
nuclear@1 69 }
nuclear@1 70
nuclear@1 71 ship.update(dt);
nuclear@1 72
nuclear@1 73 last_upd = msec;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 void game_draw()
nuclear@0 77 {
nuclear@0 78 glMatrixMode(GL_MODELVIEW);
nuclear@1 79 dbgcam.use();
nuclear@0 80
nuclear@2 81 if(dbg_inside) {
nuclear@2 82 load_gl_matrix(ship.get_matrix().inverse());
nuclear@2 83 }
nuclear@2 84
nuclear@1 85 vein->draw(ship.get_position());
nuclear@1 86 ship.dbg_draw();
nuclear@0 87 }
nuclear@0 88
nuclear@0 89 void game_input_keyb(int key, int state)
nuclear@0 90 {
nuclear@0 91 if(state) {
nuclear@0 92 switch(key) {
nuclear@0 93 case 27:
nuclear@0 94 game_shutdown();
nuclear@1 95 break;
nuclear@0 96
nuclear@2 97 case '\b':
nuclear@2 98 dbg_inside = !dbg_inside;
nuclear@2 99 break;
nuclear@2 100
nuclear@0 101 default:
nuclear@0 102 break;
nuclear@0 103 }
nuclear@0 104 }
nuclear@1 105
nuclear@1 106 if(key < 256) {
nuclear@1 107 keystate[key] = state;
nuclear@1 108 }
nuclear@0 109 }
nuclear@1 110
nuclear@1 111 static int bnstate[16];
nuclear@1 112 static int prev_x, prev_y;
nuclear@1 113
nuclear@1 114 void game_input_mbutton(int bn, int state, int x, int y)
nuclear@1 115 {
nuclear@1 116 if(bn < 16) {
nuclear@1 117 bnstate[bn] = state;
nuclear@1 118 }
nuclear@1 119 prev_x = x;
nuclear@1 120 prev_y = y;
nuclear@1 121 }
nuclear@1 122
nuclear@1 123 void game_input_mmotion(int x, int y)
nuclear@1 124 {
nuclear@1 125 int dx = x - prev_x;
nuclear@1 126 int dy = y - prev_y;
nuclear@1 127 prev_x = x;
nuclear@1 128 prev_y = y;
nuclear@1 129
nuclear@1 130 if(bnstate[0]) {
nuclear@1 131 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
nuclear@1 132 }
nuclear@1 133 if(bnstate[2]) {
nuclear@1 134 dbgcam.input_zoom(10.0 * dy / win_ysz);
nuclear@1 135 }
nuclear@1 136 }