intravenous

view src/game.cc @ 3:94d4c60af435

some progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 03:35:18 +0300
parents 472c28b8b875
children c6a6a64df6de
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"
7 #include "cockpit.h"
9 static Vein *vein;
10 static Ship ship;
12 static int keystate[256];
13 static int dbg_inside = false;
15 static OrbitCamera dbgcam;
17 static unsigned long msec, start_time = -1;
19 bool game_init()
20 {
21 glEnable(GL_DEPTH_TEST);
22 glEnable(GL_CULL_FACE);
24 float lpos[] = {-1, 1, 1, 0};
25 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
27 // initialize the cockpit
28 if(!cockpit_init()) {
29 return false;
30 }
32 // initialize the only level
33 vein = new Vein;
34 if(!vein->init()) {
35 return false;
36 }
38 return true;
39 }
41 void game_shutdown()
42 {
43 delete vein;
44 cockpit_destroy();
45 exit(0);
46 }
48 void game_update(unsigned long ms)
49 {
50 static unsigned long last_upd;
52 if(start_time == 0) {
53 start_time = ms;
54 }
55 msec = ms - start_time;
57 time_sec_t dt = (msec - last_upd) / 1000.0;
59 // handle key input
60 if(keystate['w'] || keystate['W']) {
61 ship.accelerate(1.0 * dt);
62 }
63 if(keystate['s'] || keystate['S']) {
64 ship.accelerate(-1.0 * dt);
65 }
66 if(keystate['d'] || keystate['D']) {
67 ship.accelerate_side(1.0 * dt);
68 }
69 if(keystate['a'] || keystate['A']) {
70 ship.accelerate_side(-1.0 * dt);
71 }
72 /*if(keystate['e'] || keystate['E']) {
73 ship.turn(0.0, 1.0 * dt);
74 }
75 if(keystate['c'] || keystate['C']) {
76 ship.turn(0.0, -1.0 * dt);
77 }*/
79 ship.update(dt);
81 last_upd = msec;
82 }
84 void game_draw()
85 {
86 glMatrixMode(GL_MODELVIEW);
88 if(dbg_inside) {
89 load_gl_matrix(ship.get_matrix().inverse());
90 } else {
91 dbgcam.use();
92 }
94 vein->draw(ship.get_position());
96 if(!dbg_inside) {
97 ship.dbg_draw();
98 } else {
99 cockpit_draw();
100 }
101 }
103 void game_input_keyb(int key, int state)
104 {
105 if(state) {
106 switch(key) {
107 case 27:
108 game_shutdown();
109 break;
111 case '\b':
112 dbg_inside = !dbg_inside;
113 if(dbg_inside) {
114 glutPassiveMotionFunc(game_input_mmotion);
115 glutSetCursor(GLUT_CURSOR_NONE);
116 } else {
117 glutPassiveMotionFunc(0);
118 glutSetCursor(GLUT_CURSOR_INHERIT);
119 }
120 break;
122 default:
123 break;
124 }
125 }
127 if(key < 256) {
128 keystate[key] = state;
129 }
130 }
132 static int bnstate[16];
133 static int prev_x, prev_y;
135 void game_input_mbutton(int bn, int state, int x, int y)
136 {
137 if(bn < 16) {
138 bnstate[bn] = state;
139 }
140 prev_x = x;
141 prev_y = y;
142 }
144 void game_input_mmotion(int x, int y)
145 {
146 static bool ignore_next;
147 int dx = x - prev_x;
148 int dy = y - prev_y;
149 prev_x = x;
150 prev_y = y;
152 if(ignore_next) {
153 ignore_next = false;
154 return;
155 }
157 if(dbg_inside) {
158 ship.turn((float)-dx / win_xsz, (float)-dy / win_ysz);
160 ignore_next = true;
161 glutWarpPointer(win_xsz / 2, win_ysz / 2);
162 } else {
163 if(bnstate[0]) {
164 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
165 }
166 if(bnstate[2]) {
167 dbgcam.input_zoom(10.0 * dy / win_ysz);
168 }
169 }
170 }