intravenous

view src/game.cc @ 10:8fbdc6f84f64

fixed after the change in vmath
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 May 2013 01:30:14 +0300
parents 0f305fc0b548 2723dc026c4f
children
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 const Vector3 fog_color(0.64, 0.1, 0.1);
11 static Vein *vein;
12 static Ship *ship;
14 static int keystate[256];
15 static int dbg_inside = false;
16 static bool ignore_next_motion;
18 static OrbitCamera dbgcam;
20 static unsigned long msec, start_time = -1;
22 bool game_init()
23 {
24 glEnable(GL_DEPTH_TEST);
25 glEnable(GL_CULL_FACE);
27 float lpos[] = {-1, 1, 1, 0};
28 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
30 // initialize the cockpit
31 if(!cockpit_init()) {
32 return false;
33 }
35 // initialize the only level
36 vein = new Vein;
37 if(!vein->init()) {
38 return false;
39 }
40 vein->set_fog_color(fog_color);
42 // create the player's ship
43 ship = new Ship(vein);
45 glClearColor(fog_color.x, fog_color.y, fog_color.z, 1);
47 return true;
48 }
50 void game_shutdown()
51 {
52 delete vein;
53 cockpit_destroy();
54 exit(0);
55 }
57 void game_update(unsigned long ms)
58 {
59 static unsigned long last_upd;
61 if(start_time == 0) {
62 start_time = ms;
63 }
64 msec = ms - start_time;
66 time_sec_t dt = (msec - last_upd) / 1000.0;
68 // handle key input
69 if(keystate['w'] || keystate['W']) {
70 ship->accelerate(4.0 * dt);
71 }
72 if(keystate['s'] || keystate['S']) {
73 ship->accelerate(-4.0 * dt);
74 }
75 if(keystate['d'] || keystate['D']) {
76 ship->accelerate_side(4.0 * dt);
77 }
78 if(keystate['a'] || keystate['A']) {
79 ship->accelerate_side(-4.0 * dt);
80 }
82 ship->update(dt);
84 last_upd = msec;
85 }
87 void game_draw()
88 {
89 glMatrixMode(GL_MODELVIEW);
91 if(dbg_inside) {
92 load_gl_matrix(ship->get_matrix().inverse());
93 } else {
94 dbgcam.use();
95 }
97 vein->draw(ship->get_position());
99 if(!dbg_inside) {
100 ship->dbg_draw();
101 } else {
102 cockpit_draw();
103 }
104 }
106 void game_input_keyb(int key, int state)
107 {
108 if(state) {
109 switch(key) {
110 case 27:
111 game_shutdown();
112 break;
114 case '\b':
115 case 127:
116 dbg_inside = !dbg_inside;
117 if(dbg_inside) {
118 glutPassiveMotionFunc(game_input_mmotion);
119 glutSetCursor(GLUT_CURSOR_NONE);
121 ignore_next_motion = true;
122 glutWarpPointer(win_xsz / 2, win_ysz / 2);
123 } else {
124 glutPassiveMotionFunc(0);
125 glutSetCursor(GLUT_CURSOR_INHERIT);
126 }
127 break;
129 default:
130 break;
131 }
132 }
134 if(key < 256) {
135 keystate[key] = state;
136 }
137 }
139 static int bnstate[16];
140 static int prev_x, prev_y;
142 void game_input_mbutton(int bn, int state, int x, int y)
143 {
144 if(bn < 16) {
145 bnstate[bn] = state;
146 }
147 prev_x = x;
148 prev_y = y;
149 }
151 #define EDGE_PIXELS (win_ysz / 4)
153 void game_input_mmotion(int x, int y)
154 {
155 int dx = x - prev_x;
156 int dy = y - prev_y;
157 prev_x = x;
158 prev_y = y;
160 if(ignore_next_motion || abs(dx) >= win_xsz / 2 - EDGE_PIXELS ||
161 abs(dy) >= win_ysz / 2 - EDGE_PIXELS) {
162 ignore_next_motion = false;
163 return;
164 }
166 if(dbg_inside) {
167 ship->turn((float)-dx / win_xsz, (float)-dy / win_ysz);
169 if(x < EDGE_PIXELS || x >= win_xsz - EDGE_PIXELS || y < EDGE_PIXELS || y >= win_ysz - EDGE_PIXELS) {
170 glutWarpPointer(win_xsz / 2, win_ysz / 2);
171 }
172 } else {
173 if(bnstate[0]) {
174 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
175 }
176 if(bnstate[2]) {
177 dbgcam.input_zoom(10.0 * dy / win_ysz);
178 }
179 }
180 }