rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <assert.h>
|
nuclear@7
|
4 #include <unistd.h>
|
nuclear@1
|
5 #include "opengl.h"
|
nuclear@1
|
6 #include "level.h"
|
nuclear@1
|
7 #include "camera.h"
|
nuclear@5
|
8 #include "datapath.h"
|
nuclear@5
|
9 #include "tileset.h"
|
nuclear@15
|
10 #include "renderer.h"
|
nuclear@24
|
11 #include "cmdcon.h"
|
nuclear@18
|
12 #include "cfg.h"
|
nuclear@1
|
13
|
nuclear@17
|
14 bool init(int xsz, int ysz);
|
nuclear@5
|
15 void cleanup();
|
nuclear@7
|
16 void idle();
|
nuclear@1
|
17 void disp();
|
nuclear@15
|
18 void draw();
|
nuclear@18
|
19 void view_matrix(int eye);
|
nuclear@18
|
20 void proj_matrix(int eye);
|
nuclear@7
|
21 void update(unsigned long msec);
|
nuclear@1
|
22 void reshape(int x, int y);
|
nuclear@1
|
23 void keyb(unsigned char key, int x, int y);
|
nuclear@7
|
24 void key_release(unsigned char key, int x, int y);
|
nuclear@24
|
25 void keyb_con(unsigned char key, int x, int y);
|
nuclear@1
|
26 void mouse(int bn, int state, int x, int y);
|
nuclear@1
|
27 void motion(int x, int y);
|
nuclear@1
|
28
|
nuclear@5
|
29 static TileSet *tileset;
|
nuclear@1
|
30 static Level *level;
|
nuclear@7
|
31
|
nuclear@7
|
32 static FpsCamera cam;
|
nuclear@7
|
33 static bool keystate[256];
|
nuclear@1
|
34
|
nuclear@18
|
35 static float stereo_focus_dist = 0.25;
|
nuclear@18
|
36 static float stereo_eye_sep = stereo_focus_dist / 30.0;
|
nuclear@18
|
37
|
nuclear@24
|
38 static bool show_con;
|
nuclear@5
|
39
|
nuclear@1
|
40 int main(int argc, char **argv)
|
nuclear@1
|
41 {
|
nuclear@1
|
42 glutInit(&argc, argv);
|
nuclear@5
|
43
|
nuclear@18
|
44 if(!cfg.parse_args(argc, argv)) {
|
nuclear@18
|
45 return 1;
|
nuclear@5
|
46 }
|
nuclear@5
|
47
|
nuclear@18
|
48 glutInitWindowSize(cfg.width, cfg.height);
|
nuclear@18
|
49 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
|
nuclear@18
|
50 glutCreateWindow("dungeon crawler prototype");
|
nuclear@1
|
51
|
nuclear@7
|
52 glutIdleFunc(idle);
|
nuclear@1
|
53 glutDisplayFunc(disp);
|
nuclear@1
|
54 glutReshapeFunc(reshape);
|
nuclear@1
|
55 glutKeyboardFunc(keyb);
|
nuclear@7
|
56 glutKeyboardUpFunc(key_release);
|
nuclear@1
|
57 glutMouseFunc(mouse);
|
nuclear@1
|
58 glutMotionFunc(motion);
|
nuclear@1
|
59
|
nuclear@1
|
60 glewInit();
|
nuclear@1
|
61
|
nuclear@18
|
62 if(!init(cfg.width, cfg.height)) {
|
nuclear@5
|
63 return 1;
|
nuclear@5
|
64 }
|
nuclear@5
|
65
|
nuclear@5
|
66 glutMainLoop();
|
nuclear@5
|
67 }
|
nuclear@5
|
68
|
nuclear@17
|
69 bool init(int xsz, int ysz)
|
nuclear@5
|
70 {
|
nuclear@1
|
71 glEnable(GL_LIGHTING);
|
nuclear@1
|
72 glEnable(GL_LIGHT0);
|
nuclear@1
|
73 float ldir[] = {-1, 1, 2, 0};
|
nuclear@1
|
74 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
|
nuclear@1
|
75 glEnable(GL_NORMALIZE);
|
nuclear@1
|
76
|
nuclear@1
|
77 glEnable(GL_DEPTH_TEST);
|
nuclear@1
|
78 glEnable(GL_CULL_FACE);
|
nuclear@1
|
79 glEnable(GL_MULTISAMPLE);
|
nuclear@1
|
80
|
nuclear@25
|
81 add_data_path(".");
|
nuclear@5
|
82 add_data_path("data");
|
nuclear@16
|
83 add_data_path("sdr");
|
nuclear@5
|
84
|
nuclear@17
|
85 if(!init_renderer(xsz, ysz)) {
|
nuclear@15
|
86 return false;
|
nuclear@15
|
87 }
|
nuclear@15
|
88
|
nuclear@26
|
89 if(!init_cmdcon()) {
|
nuclear@26
|
90 return false;
|
nuclear@26
|
91 }
|
nuclear@26
|
92
|
nuclear@5
|
93 // load a tileset
|
nuclear@5
|
94 tileset = new TileSet;
|
nuclear@18
|
95 printf("loading tileset: %s\n", cfg.tileset_file);
|
nuclear@18
|
96 if(!tileset->load(datafile_path(cfg.tileset_file))) {
|
nuclear@5
|
97 return false;
|
nuclear@5
|
98 }
|
nuclear@5
|
99 set_active_tileset(tileset);
|
nuclear@5
|
100
|
nuclear@1
|
101 level = new Level;
|
nuclear@18
|
102 printf("loading level: %s\n", cfg.level_file);
|
nuclear@18
|
103 if(!level->load(datafile_path(cfg.level_file))) {
|
nuclear@5
|
104 return false;
|
nuclear@1
|
105 }
|
nuclear@1
|
106
|
nuclear@7
|
107 cam.input_move(0, 0.5, 0);
|
nuclear@7
|
108
|
nuclear@5
|
109 return true;
|
nuclear@1
|
110 }
|
nuclear@1
|
111
|
nuclear@15
|
112 void cleanup()
|
nuclear@15
|
113 {
|
nuclear@15
|
114 delete level;
|
nuclear@15
|
115 delete tileset;
|
nuclear@15
|
116
|
nuclear@15
|
117 destroy_renderer();
|
nuclear@26
|
118
|
nuclear@26
|
119 cleanup_cmdcon();
|
nuclear@15
|
120 }
|
nuclear@15
|
121
|
nuclear@7
|
122 void idle()
|
nuclear@7
|
123 {
|
nuclear@7
|
124 glutPostRedisplay();
|
nuclear@7
|
125 }
|
nuclear@7
|
126
|
nuclear@1
|
127 void disp()
|
nuclear@1
|
128 {
|
nuclear@7
|
129 update(glutGet(GLUT_ELAPSED_TIME));
|
nuclear@7
|
130
|
nuclear@18
|
131 if(cfg.stereo) {
|
nuclear@18
|
132 glDrawBuffer(GL_BACK_LEFT);
|
nuclear@1
|
133
|
nuclear@18
|
134 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
135 glLoadIdentity();
|
nuclear@18
|
136 proj_matrix(-1);
|
nuclear@18
|
137 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
138 glLoadIdentity();
|
nuclear@18
|
139 view_matrix(-1);
|
nuclear@1
|
140
|
nuclear@24
|
141 draw();
|
nuclear@18
|
142
|
nuclear@18
|
143 glDrawBuffer(GL_BACK_RIGHT);
|
nuclear@18
|
144
|
nuclear@18
|
145 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
146 glLoadIdentity();
|
nuclear@18
|
147 proj_matrix(1);
|
nuclear@18
|
148 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
149 glLoadIdentity();
|
nuclear@18
|
150 view_matrix(1);
|
nuclear@18
|
151
|
nuclear@24
|
152 draw();
|
nuclear@18
|
153 } else {
|
nuclear@18
|
154 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
155 glLoadIdentity();
|
nuclear@18
|
156 proj_matrix(0);
|
nuclear@18
|
157 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
158 glLoadIdentity();
|
nuclear@18
|
159 view_matrix(0);
|
nuclear@18
|
160
|
nuclear@24
|
161 draw();
|
nuclear@18
|
162 }
|
nuclear@1
|
163
|
nuclear@1
|
164 glutSwapBuffers();
|
nuclear@1
|
165 assert(glGetError() == GL_NO_ERROR);
|
nuclear@7
|
166
|
nuclear@7
|
167 usleep(10000);
|
nuclear@7
|
168 }
|
nuclear@7
|
169
|
nuclear@24
|
170 void draw()
|
nuclear@24
|
171 {
|
nuclear@29
|
172 glClear(GL_COLOR_BUFFER_BIT);
|
nuclear@29
|
173
|
nuclear@29
|
174 render_deferred(level);
|
nuclear@24
|
175
|
nuclear@24
|
176 if(show_con) {
|
nuclear@24
|
177 draw_cmdcon();
|
nuclear@24
|
178 }
|
nuclear@24
|
179 }
|
nuclear@24
|
180
|
nuclear@18
|
181 void view_matrix(int eye)
|
nuclear@18
|
182 {
|
nuclear@18
|
183 float offs = stereo_eye_sep * eye * 0.5;
|
nuclear@18
|
184 glTranslatef(-offs, 0, 0);
|
nuclear@18
|
185 cam.use_inverse();
|
nuclear@18
|
186 }
|
nuclear@18
|
187
|
nuclear@18
|
188 void proj_matrix(int eye)
|
nuclear@18
|
189 {
|
nuclear@18
|
190 static const float fov = M_PI / 4.0;
|
nuclear@18
|
191 static const float near_clip = 0.1;
|
nuclear@18
|
192 static const float far_clip = 100.0;
|
nuclear@18
|
193
|
nuclear@18
|
194 float top = near_clip * tan(fov * 0.5);
|
nuclear@18
|
195 float right = top * (float)cfg.width / (float)cfg.height;
|
nuclear@18
|
196
|
nuclear@18
|
197 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
|
nuclear@18
|
198 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
|
nuclear@18
|
199 }
|
nuclear@18
|
200
|
nuclear@18
|
201
|
nuclear@7
|
202 void update(unsigned long msec)
|
nuclear@7
|
203 {
|
nuclear@7
|
204 static unsigned long last_upd;
|
nuclear@7
|
205
|
nuclear@7
|
206 if(last_upd == 0) {
|
nuclear@7
|
207 last_upd = msec;
|
nuclear@7
|
208 }
|
nuclear@7
|
209 float dt = (float)(msec - last_upd) / 1000.0;
|
nuclear@7
|
210
|
nuclear@9
|
211 float offs = 2.5 * dt;
|
nuclear@7
|
212 float dx = 0, dy = 0;
|
nuclear@7
|
213
|
nuclear@7
|
214 // handle key input
|
nuclear@7
|
215 if(keystate['w'] || keystate['W']) {
|
nuclear@7
|
216 dy -= offs;
|
nuclear@7
|
217 }
|
nuclear@7
|
218 if(keystate['s'] || keystate['S']) {
|
nuclear@7
|
219 dy += offs;
|
nuclear@7
|
220 }
|
nuclear@7
|
221 if(keystate['d'] || keystate['D']) {
|
nuclear@7
|
222 dx += offs;
|
nuclear@7
|
223 }
|
nuclear@7
|
224 if(keystate['a'] || keystate['A']) {
|
nuclear@7
|
225 dx -= offs;
|
nuclear@7
|
226 }
|
nuclear@7
|
227
|
nuclear@7
|
228 cam.input_move(dx, 0, dy);
|
nuclear@7
|
229
|
nuclear@7
|
230 last_upd = msec;
|
nuclear@1
|
231 }
|
nuclear@1
|
232
|
nuclear@1
|
233 void reshape(int x, int y)
|
nuclear@1
|
234 {
|
nuclear@1
|
235 glViewport(0, 0, x, y);
|
nuclear@18
|
236 cfg.width = x;
|
nuclear@18
|
237 cfg.height = y;
|
nuclear@1
|
238 }
|
nuclear@1
|
239
|
nuclear@18
|
240 static bool stereo_shift_pressed;
|
nuclear@18
|
241
|
nuclear@1
|
242 void keyb(unsigned char key, int x, int y)
|
nuclear@1
|
243 {
|
nuclear@1
|
244 switch(key) {
|
nuclear@1
|
245 case 27:
|
nuclear@1
|
246 exit(0);
|
nuclear@18
|
247
|
nuclear@24
|
248 case '`':
|
nuclear@24
|
249 show_con = true;
|
nuclear@24
|
250 glutKeyboardFunc(keyb_con);
|
nuclear@24
|
251 glutKeyboardUpFunc(0);
|
nuclear@24
|
252 glutPostRedisplay();
|
nuclear@24
|
253 break;
|
nuclear@24
|
254
|
nuclear@18
|
255 case 'z':
|
nuclear@18
|
256 stereo_shift_pressed = true;
|
nuclear@18
|
257 break;
|
nuclear@18
|
258
|
nuclear@18
|
259 case '\n':
|
nuclear@18
|
260 case '\r':
|
nuclear@18
|
261 {
|
nuclear@18
|
262 static bool fullscr;
|
nuclear@18
|
263 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
|
nuclear@18
|
264 fullscr = !fullscr;
|
nuclear@18
|
265 if(fullscr) {
|
nuclear@18
|
266 glutFullScreen();
|
nuclear@18
|
267 } else {
|
nuclear@18
|
268 glutPositionWindow(20, 20);
|
nuclear@18
|
269 }
|
nuclear@18
|
270 }
|
nuclear@18
|
271 }
|
nuclear@18
|
272 break;
|
nuclear@18
|
273
|
nuclear@18
|
274 default:
|
nuclear@18
|
275 break;
|
nuclear@1
|
276 }
|
nuclear@7
|
277
|
nuclear@7
|
278 keystate[key] = true;
|
nuclear@7
|
279 }
|
nuclear@7
|
280
|
nuclear@7
|
281 void key_release(unsigned char key, int x, int y)
|
nuclear@7
|
282 {
|
nuclear@18
|
283 switch(key) {
|
nuclear@18
|
284 case 'z':
|
nuclear@18
|
285 stereo_shift_pressed = false;
|
nuclear@18
|
286 break;
|
nuclear@18
|
287
|
nuclear@18
|
288 default:
|
nuclear@18
|
289 break;
|
nuclear@18
|
290 }
|
nuclear@18
|
291
|
nuclear@7
|
292 keystate[key] = false;
|
nuclear@1
|
293 }
|
nuclear@1
|
294
|
nuclear@24
|
295 void keyb_con(unsigned char key, int x, int y)
|
nuclear@24
|
296 {
|
nuclear@24
|
297 if(key == '`' || key == 27) {
|
nuclear@24
|
298 show_con = false;
|
nuclear@24
|
299 glutKeyboardFunc(keyb);
|
nuclear@24
|
300 glutKeyboardUpFunc(key_release);
|
nuclear@24
|
301 glutPostRedisplay();
|
nuclear@24
|
302 } else {
|
nuclear@24
|
303 cmdcon_keypress(key);
|
nuclear@24
|
304 }
|
nuclear@24
|
305 }
|
nuclear@24
|
306
|
nuclear@1
|
307 static int prev_x, prev_y;
|
nuclear@1
|
308 static bool bnstate[32];
|
nuclear@1
|
309
|
nuclear@1
|
310 void mouse(int bn, int state, int x, int y)
|
nuclear@1
|
311 {
|
nuclear@1
|
312 prev_x = x;
|
nuclear@1
|
313 prev_y = y;
|
nuclear@1
|
314 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
|
nuclear@1
|
315 }
|
nuclear@1
|
316
|
nuclear@1
|
317 void motion(int x, int y)
|
nuclear@1
|
318 {
|
nuclear@1
|
319 int dx = x - prev_x;
|
nuclear@1
|
320 int dy = y - prev_y;
|
nuclear@1
|
321 prev_x = x;
|
nuclear@1
|
322 prev_y = y;
|
nuclear@1
|
323
|
nuclear@18
|
324 if(stereo_shift_pressed) {
|
nuclear@18
|
325 if(dy != 0) {
|
nuclear@18
|
326 stereo_focus_dist += dy * 0.01;
|
nuclear@18
|
327 stereo_eye_sep = stereo_focus_dist / 30.0;
|
nuclear@18
|
328 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
|
nuclear@18
|
329 glutPostRedisplay();
|
nuclear@18
|
330 }
|
nuclear@18
|
331 return;
|
nuclear@18
|
332 }
|
nuclear@18
|
333
|
nuclear@1
|
334 if(bnstate[0]) {
|
nuclear@7
|
335 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
|
nuclear@1
|
336 glutPostRedisplay();
|
nuclear@1
|
337 }
|
nuclear@1
|
338 if(bnstate[2]) {
|
nuclear@1
|
339 cam.input_zoom(dy * 0.1);
|
nuclear@1
|
340 glutPostRedisplay();
|
nuclear@1
|
341 }
|
nuclear@1
|
342 }
|