dungeon_crawler

view prototype/src/main.cc @ 69:45172d087ebe

fixed some windows compatibility crap fixed a terrible stack overrun in psys (TODO: remember to fix in libpsys too)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Oct 2012 03:42:44 +0200
parents 7f52d6310317
children a27528035e20
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "psys/psys.h"
6 #include "level.h"
7 #include "texture.h"
8 #include "camera.h"
9 #include "datapath.h"
10 #include "tileset.h"
11 #include "renderer.h"
12 #include "renderer_deferred.h"
13 #include "renderer_multipass.h"
14 #include "cmdcon.h"
15 #include "cfg.h"
16 #include "timer.h"
17 #include "audio/audio.h"
18 #include "audio/source.h"
19 #include "audio/ovstream.h"
21 bool init(int xsz, int ysz);
22 void cleanup();
23 void idle();
24 void disp();
25 void draw();
26 void view_matrix(int eye);
27 void proj_matrix(int eye);
28 void update(unsigned long msec);
29 void reshape(int x, int y);
30 void keyb(unsigned char key, int x, int y);
31 void key_release(unsigned char key, int x, int y);
32 void keyb_con(unsigned char key, int x, int y);
33 void mouse(int bn, int state, int x, int y);
34 void motion(int x, int y);
35 unsigned int load_psys_tex(const char *fname, void *cls);
37 static TileSet *tileset;
38 static Level *level;
40 static FpsCamera cam;
41 static bool keystate[256];
43 static float stereo_focus_dist = 0.25;
44 static float stereo_eye_sep = stereo_focus_dist / 30.0;
46 static bool show_con;
48 static AudioSource *move_sound;
49 static OggVorbisStream *music;
51 int main(int argc, char **argv)
52 {
53 glutInit(&argc, argv);
55 if(!cfg.parse_args(argc, argv)) {
56 return 1;
57 }
59 glutInitWindowSize(cfg.width, cfg.height);
60 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
61 glutCreateWindow("dungeon crawler prototype");
63 glutIdleFunc(idle);
64 glutDisplayFunc(disp);
65 glutReshapeFunc(reshape);
66 glutKeyboardFunc(keyb);
67 glutKeyboardUpFunc(key_release);
68 glutMouseFunc(mouse);
69 glutMotionFunc(motion);
71 glewInit();
73 if(!init(cfg.width, cfg.height)) {
74 return 1;
75 }
76 atexit(cleanup);
78 glutMainLoop();
79 }
81 bool init(int xsz, int ysz)
82 {
83 // backup light for the forward crappy renderer
84 glEnable(GL_LIGHTING);
85 glEnable(GL_LIGHT0);
87 float ldir[] = {0, 0, -0.5, 1};
88 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
89 float lcol[] = {1, 1, 1, 1};
90 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
91 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
93 glEnable(GL_DEPTH_TEST);
94 glEnable(GL_CULL_FACE);
95 glEnable(GL_MULTISAMPLE);
97 add_data_path(".");
98 add_data_path("data");
99 add_data_path("data/audio");
100 add_data_path("sdr");
102 if(cfg.sound && !init_audio()) {
103 fprintf(stderr, "failed to initialize audio, continuing silently\n");
104 cfg.sound = false;
105 }
106 if(cfg.sound) {
107 move_sound = new AudioSource;
108 move_sound->set_volume(0.4);
110 music = new OggVorbisStream;
111 if(music->open(datafile_path("bgtrack.ogg").c_str())) {
112 music->set_volume(0.6);
113 music->play(PlayMode::loop);
114 } else {
115 delete music;
116 music = 0;
117 }
118 }
120 switch(cfg.rend) {
121 case Config::Renderer::mrt:
122 rend = new DeferredRenderer;
123 if(!rend->init(xsz, ysz)) {
124 fprintf(stderr, "failed to initialize deferred mrt renderer\n");
125 delete rend;
126 return false;
127 }
128 break;
130 case Config::Renderer::multipass:
131 rend = new MultipassRenderer;
132 if(!rend->init(xsz, ysz)) {
133 fprintf(stderr, "failed to initialize multipass deferred renderer\n");
134 delete rend;
135 return false;
136 }
137 break;
139 case Config::Renderer::fwd:
140 rend = new FwdRenderer;
141 if(!rend->init(xsz, ysz)) {
142 fprintf(stderr, "failed to initialize forward renderer\n");
143 delete rend;
144 return false;
145 }
146 break;
148 default:
149 // try each in turn falling back to progressively worse renderers
150 rend = new DeferredRenderer;
151 if(!rend->init(xsz, ysz)) {
152 printf("falling back to multipass renderer...\n");
154 delete rend;
155 rend = new MultipassRenderer();
156 if(!rend->init(xsz, ysz)) {
157 printf("falling back to crappy renderer...\n");
159 rend = new FwdRenderer();
160 if(!rend->init(xsz, ysz)) {
161 fprintf(stderr, "failed to create renderer\n");
162 return false;
163 }
164 }
165 }
166 }
168 if(!init_cmdcon()) {
169 return false;
170 }
172 psys_texture_loader(load_psys_tex, 0, 0);
174 // load a tileset
175 tileset = new TileSet;
176 printf("loading tileset: %s\n", cfg.tileset_file);
177 if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) {
178 return false;
179 }
180 set_active_tileset(tileset);
182 level = new Level;
183 printf("loading level: %s\n", cfg.level_file);
184 if(!level->load(datafile_path(cfg.level_file).c_str())) {
185 return false;
186 }
188 cam.input_move(0, 0.5, 0);
190 return true;
191 }
193 void cleanup()
194 {
195 delete level;
196 delete tileset;
197 delete rend;
199 cleanup_cmdcon();
201 if(cfg.sound) {
202 delete music;
203 delete move_sound;
204 destroy_audio();
205 }
206 }
208 void idle()
209 {
210 glutPostRedisplay();
211 }
213 void disp()
214 {
215 update(get_time_msec());
217 if(cfg.stereo) {
218 glDrawBuffer(GL_BACK_LEFT);
220 glMatrixMode(GL_PROJECTION);
221 glLoadIdentity();
222 proj_matrix(-1);
223 glMatrixMode(GL_MODELVIEW);
224 glLoadIdentity();
225 view_matrix(-1);
227 draw();
229 glDrawBuffer(GL_BACK_RIGHT);
231 glMatrixMode(GL_PROJECTION);
232 glLoadIdentity();
233 proj_matrix(1);
234 glMatrixMode(GL_MODELVIEW);
235 glLoadIdentity();
236 view_matrix(1);
238 draw();
239 } else {
240 glMatrixMode(GL_PROJECTION);
241 glLoadIdentity();
242 proj_matrix(0);
243 glMatrixMode(GL_MODELVIEW);
244 glLoadIdentity();
245 view_matrix(0);
247 draw();
248 }
250 glutSwapBuffers();
251 assert(glGetError() == GL_NO_ERROR);
252 }
254 void draw()
255 {
256 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258 rend->render(level);
260 if(show_con) {
261 draw_cmdcon();
262 }
263 }
265 void view_matrix(int eye)
266 {
267 float offs = stereo_eye_sep * eye * 0.5;
268 glTranslatef(-offs, 0, 0);
269 cam.use_inverse();
270 }
272 void proj_matrix(int eye)
273 {
274 static const float fov = M_PI / 4.0;
275 static const float near_clip = 0.1;
276 static const float far_clip = 100.0;
278 float top = near_clip * tan(fov * 0.5);
279 float right = top * (float)cfg.width / (float)cfg.height;
281 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
282 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
283 }
286 void update(unsigned long msec)
287 {
288 static unsigned long last_upd;
290 if(last_upd == 0) {
291 last_upd = msec;
292 }
293 float dt = (float)(msec - last_upd) / 1000.0;
295 float offs = 2.0 * dt;
296 float dx = 0, dy = 0;
298 // handle key input
299 bool did_move = false;
300 if(keystate['w'] || keystate['W']) {
301 dy -= offs;
302 did_move = true;
303 }
304 if(keystate['s'] || keystate['S']) {
305 dy += offs;
306 did_move = true;
307 }
308 if(keystate['d'] || keystate['D']) {
309 dx += offs;
310 did_move = true;
311 }
312 if(keystate['a'] || keystate['A']) {
313 dx -= offs;
314 did_move = true;
315 }
317 cam.input_move(dx, 0, dy);
319 tileset->update_tiles(msec);
321 level->set_player_position(cam.get_position());
322 level->update(msec, dt);
324 if(cfg.sound) {
325 // set the listener matrix
326 set_audio_listener(cam.matrix());
328 // play the walking sound if we're walking
329 int cellx, celly;
330 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
332 const AudioSample *move_sample;
333 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
334 if(move_sample != move_sound->get_sample()) {
335 move_sound->stop();
336 move_sound->set_sample(move_sample);
337 move_sound->play();
338 }
339 } else {
340 if(move_sound->get_sample()) {
341 move_sound->stop();
342 move_sound->set_sample(0);
343 }
344 }
345 }
347 last_upd = msec;
348 }
350 void reshape(int x, int y)
351 {
352 glViewport(0, 0, x, y);
353 cfg.width = x;
354 cfg.height = y;
356 rend->resize(x, y);
357 }
359 static bool stereo_shift_pressed;
361 void keyb(unsigned char key, int x, int y)
362 {
363 switch(key) {
364 case 27:
365 exit(0);
367 case '`':
368 show_con = true;
369 glutKeyboardFunc(keyb_con);
370 glutKeyboardUpFunc(0);
371 glutPostRedisplay();
372 break;
374 case 'z':
375 stereo_shift_pressed = true;
376 break;
378 case 'p':
379 {
380 Vector3 pos = cam.get_position();
381 int cell_x, cell_y;
382 level->get_cell_coords_at(pos, &cell_x, &cell_y);
383 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
384 cell_x, cell_y);
385 }
386 break;
388 case 'P':
389 {
390 Vector3 pos = cam.get_position();
391 int cell_x, cell_y;
392 level->get_cell_coords_at(pos, &cell_x, &cell_y);
393 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
394 printf("walk sample: %p\n", (void*)sample);
395 }
396 break;
399 case '\n':
400 case '\r':
401 {
402 static bool fullscr;
403 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
404 fullscr = !fullscr;
405 if(fullscr) {
406 glutFullScreen();
407 } else {
408 glutPositionWindow(20, 20);
409 }
410 }
411 }
412 break;
414 default:
415 break;
416 }
418 keystate[key] = true;
419 }
421 void key_release(unsigned char key, int x, int y)
422 {
423 switch(key) {
424 case 'z':
425 stereo_shift_pressed = false;
426 break;
428 default:
429 break;
430 }
432 keystate[key] = false;
433 }
435 void keyb_con(unsigned char key, int x, int y)
436 {
437 if(key == '`' || key == 27) {
438 show_con = false;
439 glutKeyboardFunc(keyb);
440 glutKeyboardUpFunc(key_release);
441 glutPostRedisplay();
442 } else {
443 cmdcon_keypress(key);
444 }
445 }
447 static int prev_x, prev_y;
448 static bool bnstate[32];
450 void mouse(int bn, int state, int x, int y)
451 {
452 prev_x = x;
453 prev_y = y;
454 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
455 }
457 void motion(int x, int y)
458 {
459 int dx = x - prev_x;
460 int dy = y - prev_y;
461 prev_x = x;
462 prev_y = y;
464 if(stereo_shift_pressed) {
465 if(dy != 0) {
466 stereo_focus_dist += dy * 0.01;
467 stereo_eye_sep = stereo_focus_dist / 30.0;
468 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
469 glutPostRedisplay();
470 }
471 return;
472 }
474 if(bnstate[0]) {
475 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
476 glutPostRedisplay();
477 }
478 if(bnstate[2]) {
479 cam.input_zoom(dy * 0.1);
480 glutPostRedisplay();
481 }
482 }
484 unsigned int load_psys_tex(const char *fname, void *cls)
485 {
486 TextureSet *texset = tileset->get_textures();
487 return texset->get(fname);
488 }