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@3
|
7 #include "cockpit.h"
|
nuclear@0
|
8
|
nuclear@5
|
9 static const Vector3 fog_color{0.64, 0.1, 0.1};
|
nuclear@5
|
10
|
nuclear@1
|
11 static Vein *vein;
|
nuclear@1
|
12 static Ship ship;
|
nuclear@1
|
13
|
nuclear@1
|
14 static int keystate[256];
|
nuclear@2
|
15 static int dbg_inside = false;
|
nuclear@4
|
16 static bool ignore_next_motion;
|
nuclear@1
|
17
|
nuclear@1
|
18 static OrbitCamera dbgcam;
|
nuclear@1
|
19
|
nuclear@1
|
20 static unsigned long msec, start_time = -1;
|
nuclear@0
|
21
|
nuclear@0
|
22 bool game_init()
|
nuclear@0
|
23 {
|
nuclear@0
|
24 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
25 glEnable(GL_CULL_FACE);
|
nuclear@0
|
26
|
nuclear@0
|
27 float lpos[] = {-1, 1, 1, 0};
|
nuclear@0
|
28 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@0
|
29
|
nuclear@3
|
30 // initialize the cockpit
|
nuclear@3
|
31 if(!cockpit_init()) {
|
nuclear@3
|
32 return false;
|
nuclear@3
|
33 }
|
nuclear@3
|
34
|
nuclear@3
|
35 // initialize the only level
|
nuclear@1
|
36 vein = new Vein;
|
nuclear@3
|
37 if(!vein->init()) {
|
nuclear@3
|
38 return false;
|
nuclear@3
|
39 }
|
nuclear@5
|
40 vein->set_fog_color(fog_color);
|
nuclear@5
|
41
|
nuclear@5
|
42 glClearColor(fog_color.x, fog_color.y, fog_color.z, 1);
|
nuclear@1
|
43
|
nuclear@0
|
44 return true;
|
nuclear@0
|
45 }
|
nuclear@0
|
46
|
nuclear@0
|
47 void game_shutdown()
|
nuclear@0
|
48 {
|
nuclear@3
|
49 delete vein;
|
nuclear@3
|
50 cockpit_destroy();
|
nuclear@0
|
51 exit(0);
|
nuclear@0
|
52 }
|
nuclear@0
|
53
|
nuclear@1
|
54 void game_update(unsigned long ms)
|
nuclear@0
|
55 {
|
nuclear@1
|
56 static unsigned long last_upd;
|
nuclear@1
|
57
|
nuclear@0
|
58 if(start_time == 0) {
|
nuclear@1
|
59 start_time = ms;
|
nuclear@0
|
60 }
|
nuclear@1
|
61 msec = ms - start_time;
|
nuclear@1
|
62
|
nuclear@1
|
63 time_sec_t dt = (msec - last_upd) / 1000.0;
|
nuclear@1
|
64
|
nuclear@1
|
65 // handle key input
|
nuclear@1
|
66 if(keystate['w'] || keystate['W']) {
|
nuclear@1
|
67 ship.accelerate(1.0 * dt);
|
nuclear@1
|
68 }
|
nuclear@1
|
69 if(keystate['s'] || keystate['S']) {
|
nuclear@1
|
70 ship.accelerate(-1.0 * dt);
|
nuclear@1
|
71 }
|
nuclear@1
|
72 if(keystate['d'] || keystate['D']) {
|
nuclear@3
|
73 ship.accelerate_side(1.0 * dt);
|
nuclear@1
|
74 }
|
nuclear@1
|
75 if(keystate['a'] || keystate['A']) {
|
nuclear@3
|
76 ship.accelerate_side(-1.0 * dt);
|
nuclear@1
|
77 }
|
nuclear@3
|
78 /*if(keystate['e'] || keystate['E']) {
|
nuclear@2
|
79 ship.turn(0.0, 1.0 * dt);
|
nuclear@1
|
80 }
|
nuclear@1
|
81 if(keystate['c'] || keystate['C']) {
|
nuclear@2
|
82 ship.turn(0.0, -1.0 * dt);
|
nuclear@3
|
83 }*/
|
nuclear@1
|
84
|
nuclear@1
|
85 ship.update(dt);
|
nuclear@1
|
86
|
nuclear@1
|
87 last_upd = msec;
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 void game_draw()
|
nuclear@0
|
91 {
|
nuclear@0
|
92 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
93
|
nuclear@2
|
94 if(dbg_inside) {
|
nuclear@2
|
95 load_gl_matrix(ship.get_matrix().inverse());
|
nuclear@3
|
96 } else {
|
nuclear@3
|
97 dbgcam.use();
|
nuclear@2
|
98 }
|
nuclear@2
|
99
|
nuclear@1
|
100 vein->draw(ship.get_position());
|
nuclear@3
|
101
|
nuclear@3
|
102 if(!dbg_inside) {
|
nuclear@3
|
103 ship.dbg_draw();
|
nuclear@3
|
104 } else {
|
nuclear@3
|
105 cockpit_draw();
|
nuclear@3
|
106 }
|
nuclear@0
|
107 }
|
nuclear@0
|
108
|
nuclear@0
|
109 void game_input_keyb(int key, int state)
|
nuclear@0
|
110 {
|
nuclear@0
|
111 if(state) {
|
nuclear@0
|
112 switch(key) {
|
nuclear@0
|
113 case 27:
|
nuclear@0
|
114 game_shutdown();
|
nuclear@1
|
115 break;
|
nuclear@0
|
116
|
nuclear@2
|
117 case '\b':
|
nuclear@2
|
118 dbg_inside = !dbg_inside;
|
nuclear@3
|
119 if(dbg_inside) {
|
nuclear@3
|
120 glutPassiveMotionFunc(game_input_mmotion);
|
nuclear@3
|
121 glutSetCursor(GLUT_CURSOR_NONE);
|
nuclear@4
|
122
|
nuclear@4
|
123 ignore_next_motion = true;
|
nuclear@4
|
124 glutWarpPointer(win_xsz / 2, win_ysz / 2);
|
nuclear@3
|
125 } else {
|
nuclear@3
|
126 glutPassiveMotionFunc(0);
|
nuclear@3
|
127 glutSetCursor(GLUT_CURSOR_INHERIT);
|
nuclear@3
|
128 }
|
nuclear@2
|
129 break;
|
nuclear@2
|
130
|
nuclear@0
|
131 default:
|
nuclear@0
|
132 break;
|
nuclear@0
|
133 }
|
nuclear@0
|
134 }
|
nuclear@1
|
135
|
nuclear@1
|
136 if(key < 256) {
|
nuclear@1
|
137 keystate[key] = state;
|
nuclear@1
|
138 }
|
nuclear@0
|
139 }
|
nuclear@1
|
140
|
nuclear@1
|
141 static int bnstate[16];
|
nuclear@1
|
142 static int prev_x, prev_y;
|
nuclear@1
|
143
|
nuclear@1
|
144 void game_input_mbutton(int bn, int state, int x, int y)
|
nuclear@1
|
145 {
|
nuclear@1
|
146 if(bn < 16) {
|
nuclear@1
|
147 bnstate[bn] = state;
|
nuclear@1
|
148 }
|
nuclear@1
|
149 prev_x = x;
|
nuclear@1
|
150 prev_y = y;
|
nuclear@1
|
151 }
|
nuclear@1
|
152
|
nuclear@8
|
153 #define EDGE_PIXELS (win_ysz / 4)
|
nuclear@8
|
154
|
nuclear@1
|
155 void game_input_mmotion(int x, int y)
|
nuclear@1
|
156 {
|
nuclear@1
|
157 int dx = x - prev_x;
|
nuclear@1
|
158 int dy = y - prev_y;
|
nuclear@1
|
159 prev_x = x;
|
nuclear@1
|
160 prev_y = y;
|
nuclear@1
|
161
|
nuclear@8
|
162 if(ignore_next_motion || abs(dx) >= win_xsz / 2 - EDGE_PIXELS ||
|
nuclear@8
|
163 abs(dy) >= win_ysz / 2 - EDGE_PIXELS) {
|
nuclear@4
|
164 ignore_next_motion = false;
|
nuclear@3
|
165 return;
|
nuclear@1
|
166 }
|
nuclear@3
|
167
|
nuclear@3
|
168 if(dbg_inside) {
|
nuclear@3
|
169 ship.turn((float)-dx / win_xsz, (float)-dy / win_ysz);
|
nuclear@3
|
170
|
nuclear@8
|
171 if(x < EDGE_PIXELS || x >= win_xsz - EDGE_PIXELS || y < EDGE_PIXELS || y >= win_ysz - EDGE_PIXELS) {
|
nuclear@8
|
172 glutWarpPointer(win_xsz / 2, win_ysz / 2);
|
nuclear@8
|
173 }
|
nuclear@3
|
174 } else {
|
nuclear@3
|
175 if(bnstate[0]) {
|
nuclear@3
|
176 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
|
nuclear@3
|
177 }
|
nuclear@3
|
178 if(bnstate[2]) {
|
nuclear@3
|
179 dbgcam.input_zoom(10.0 * dy / win_ysz);
|
nuclear@3
|
180 }
|
nuclear@1
|
181 }
|
nuclear@1
|
182 }
|