intravenous

view src/game.cc @ 4:c6a6a64df6de

normalmap
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 05:20:03 +0300
parents 94d4c60af435
children aab0d8ea21cd
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;
14 static bool ignore_next_motion;
16 static OrbitCamera dbgcam;
18 static unsigned long msec, start_time = -1;
20 bool game_init()
21 {
22 glEnable(GL_DEPTH_TEST);
23 glEnable(GL_CULL_FACE);
25 float lpos[] = {-1, 1, 1, 0};
26 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
28 // initialize the cockpit
29 if(!cockpit_init()) {
30 return false;
31 }
33 // initialize the only level
34 vein = new Vein;
35 if(!vein->init()) {
36 return false;
37 }
39 return true;
40 }
42 void game_shutdown()
43 {
44 delete vein;
45 cockpit_destroy();
46 exit(0);
47 }
49 void game_update(unsigned long ms)
50 {
51 static unsigned long last_upd;
53 if(start_time == 0) {
54 start_time = ms;
55 }
56 msec = ms - start_time;
58 time_sec_t dt = (msec - last_upd) / 1000.0;
60 // handle key input
61 if(keystate['w'] || keystate['W']) {
62 ship.accelerate(1.0 * dt);
63 }
64 if(keystate['s'] || keystate['S']) {
65 ship.accelerate(-1.0 * dt);
66 }
67 if(keystate['d'] || keystate['D']) {
68 ship.accelerate_side(1.0 * dt);
69 }
70 if(keystate['a'] || keystate['A']) {
71 ship.accelerate_side(-1.0 * dt);
72 }
73 /*if(keystate['e'] || keystate['E']) {
74 ship.turn(0.0, 1.0 * dt);
75 }
76 if(keystate['c'] || keystate['C']) {
77 ship.turn(0.0, -1.0 * dt);
78 }*/
80 ship.update(dt);
82 last_upd = msec;
83 }
85 void game_draw()
86 {
87 glMatrixMode(GL_MODELVIEW);
89 if(dbg_inside) {
90 load_gl_matrix(ship.get_matrix().inverse());
91 } else {
92 dbgcam.use();
93 }
95 vein->draw(ship.get_position());
97 if(!dbg_inside) {
98 ship.dbg_draw();
99 } else {
100 cockpit_draw();
101 }
102 }
104 void game_input_keyb(int key, int state)
105 {
106 if(state) {
107 switch(key) {
108 case 27:
109 game_shutdown();
110 break;
112 case '\b':
113 dbg_inside = !dbg_inside;
114 if(dbg_inside) {
115 glutPassiveMotionFunc(game_input_mmotion);
116 glutSetCursor(GLUT_CURSOR_NONE);
118 ignore_next_motion = true;
119 glutWarpPointer(win_xsz / 2, win_ysz / 2);
120 } else {
121 glutPassiveMotionFunc(0);
122 glutSetCursor(GLUT_CURSOR_INHERIT);
123 }
124 break;
126 default:
127 break;
128 }
129 }
131 if(key < 256) {
132 keystate[key] = state;
133 }
134 }
136 static int bnstate[16];
137 static int prev_x, prev_y;
139 void game_input_mbutton(int bn, int state, int x, int y)
140 {
141 if(bn < 16) {
142 bnstate[bn] = state;
143 }
144 prev_x = x;
145 prev_y = y;
146 }
148 void game_input_mmotion(int x, int y)
149 {
150 int dx = x - prev_x;
151 int dy = y - prev_y;
152 prev_x = x;
153 prev_y = y;
155 if(ignore_next_motion) {
156 ignore_next_motion = false;
157 return;
158 }
160 if(dbg_inside) {
161 ship.turn((float)-dx / win_xsz, (float)-dy / win_ysz);
163 ignore_next_motion = true;
164 glutWarpPointer(win_xsz / 2, win_ysz / 2);
165 } else {
166 if(bnstate[0]) {
167 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
168 }
169 if(bnstate[2]) {
170 dbgcam.input_zoom(10.0 * dy / win_ysz);
171 }
172 }
173 }