intravenous

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