dungeon_crawler

view prototype/src/main.cc @ 63:7f52d6310317

fixed design issue with datafile_path
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 02 Oct 2012 04:52:59 +0300
parents aa86119e3295
children 45172d087ebe
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "psys/psys.h"
7 #include "level.h"
8 #include "texture.h"
9 #include "camera.h"
10 #include "datapath.h"
11 #include "tileset.h"
12 #include "renderer.h"
13 #include "renderer_deferred.h"
14 #include "renderer_multipass.h"
15 #include "cmdcon.h"
16 #include "cfg.h"
17 #include "timer.h"
18 #include "audio/audio.h"
19 #include "audio/source.h"
20 #include "audio/ovstream.h"
22 bool init(int xsz, int ysz);
23 void cleanup();
24 void idle();
25 void disp();
26 void draw();
27 void view_matrix(int eye);
28 void proj_matrix(int eye);
29 void update(unsigned long msec);
30 void reshape(int x, int y);
31 void keyb(unsigned char key, int x, int y);
32 void key_release(unsigned char key, int x, int y);
33 void keyb_con(unsigned char key, int x, int y);
34 void mouse(int bn, int state, int x, int y);
35 void motion(int x, int y);
36 unsigned int load_psys_tex(const char *fname, void *cls);
38 static TileSet *tileset;
39 static Level *level;
41 static FpsCamera cam;
42 static bool keystate[256];
44 static float stereo_focus_dist = 0.25;
45 static float stereo_eye_sep = stereo_focus_dist / 30.0;
47 static bool show_con;
49 static AudioSource *move_sound;
50 static OggVorbisStream *music;
52 int main(int argc, char **argv)
53 {
54 glutInit(&argc, argv);
56 if(!cfg.parse_args(argc, argv)) {
57 return 1;
58 }
60 glutInitWindowSize(cfg.width, cfg.height);
61 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
62 glutCreateWindow("dungeon crawler prototype");
64 glutIdleFunc(idle);
65 glutDisplayFunc(disp);
66 glutReshapeFunc(reshape);
67 glutKeyboardFunc(keyb);
68 glutKeyboardUpFunc(key_release);
69 glutMouseFunc(mouse);
70 glutMotionFunc(motion);
72 glewInit();
74 if(!init(cfg.width, cfg.height)) {
75 return 1;
76 }
77 atexit(cleanup);
79 glutMainLoop();
80 }
82 bool init(int xsz, int ysz)
83 {
84 // backup light for the forward crappy renderer
85 glEnable(GL_LIGHTING);
86 glEnable(GL_LIGHT0);
88 float ldir[] = {0, 0, -0.5, 1};
89 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
90 float lcol[] = {1, 1, 1, 1};
91 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
92 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
94 glEnable(GL_DEPTH_TEST);
95 glEnable(GL_CULL_FACE);
96 glEnable(GL_MULTISAMPLE);
98 add_data_path(".");
99 add_data_path("data");
100 add_data_path("data/audio");
101 add_data_path("sdr");
103 if(cfg.sound && !init_audio()) {
104 fprintf(stderr, "failed to initialize audio, continuing silently\n");
105 cfg.sound = false;
106 }
107 if(cfg.sound) {
108 move_sound = new AudioSource;
109 move_sound->set_volume(0.4);
111 music = new OggVorbisStream;
112 if(music->open(datafile_path("bgtrack.ogg").c_str())) {
113 music->set_volume(0.6);
114 music->play(PlayMode::loop);
115 } else {
116 delete music;
117 music = 0;
118 }
119 }
121 switch(cfg.rend) {
122 case Config::Renderer::mrt:
123 rend = new DeferredRenderer;
124 if(!rend->init(xsz, ysz)) {
125 fprintf(stderr, "failed to initialize deferred mrt renderer\n");
126 delete rend;
127 return false;
128 }
129 break;
131 case Config::Renderer::multipass:
132 rend = new MultipassRenderer;
133 if(!rend->init(xsz, ysz)) {
134 fprintf(stderr, "failed to initialize multipass deferred renderer\n");
135 delete rend;
136 return false;
137 }
138 break;
140 case Config::Renderer::fwd:
141 rend = new FwdRenderer;
142 if(!rend->init(xsz, ysz)) {
143 fprintf(stderr, "failed to initialize forward renderer\n");
144 delete rend;
145 return false;
146 }
147 break;
149 default:
150 // try each in turn falling back to progressively worse renderers
151 rend = new DeferredRenderer;
152 if(!rend->init(xsz, ysz)) {
153 printf("falling back to multipass renderer...\n");
155 delete rend;
156 rend = new MultipassRenderer();
157 if(!rend->init(xsz, ysz)) {
158 printf("falling back to crappy renderer...\n");
160 rend = new FwdRenderer();
161 if(!rend->init(xsz, ysz)) {
162 fprintf(stderr, "failed to create renderer\n");
163 return false;
164 }
165 }
166 }
167 }
169 if(!init_cmdcon()) {
170 return false;
171 }
173 psys_texture_loader(load_psys_tex, 0, 0);
175 // load a tileset
176 tileset = new TileSet;
177 printf("loading tileset: %s\n", cfg.tileset_file);
178 if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) {
179 return false;
180 }
181 set_active_tileset(tileset);
183 level = new Level;
184 printf("loading level: %s\n", cfg.level_file);
185 if(!level->load(datafile_path(cfg.level_file).c_str())) {
186 return false;
187 }
189 cam.input_move(0, 0.5, 0);
191 return true;
192 }
194 void cleanup()
195 {
196 delete level;
197 delete tileset;
198 delete rend;
200 cleanup_cmdcon();
202 if(cfg.sound) {
203 delete music;
204 delete move_sound;
205 destroy_audio();
206 }
207 }
209 void idle()
210 {
211 glutPostRedisplay();
212 }
214 void disp()
215 {
216 update(get_time_msec());
218 if(cfg.stereo) {
219 glDrawBuffer(GL_BACK_LEFT);
221 glMatrixMode(GL_PROJECTION);
222 glLoadIdentity();
223 proj_matrix(-1);
224 glMatrixMode(GL_MODELVIEW);
225 glLoadIdentity();
226 view_matrix(-1);
228 draw();
230 glDrawBuffer(GL_BACK_RIGHT);
232 glMatrixMode(GL_PROJECTION);
233 glLoadIdentity();
234 proj_matrix(1);
235 glMatrixMode(GL_MODELVIEW);
236 glLoadIdentity();
237 view_matrix(1);
239 draw();
240 } else {
241 glMatrixMode(GL_PROJECTION);
242 glLoadIdentity();
243 proj_matrix(0);
244 glMatrixMode(GL_MODELVIEW);
245 glLoadIdentity();
246 view_matrix(0);
248 draw();
249 }
251 glutSwapBuffers();
252 assert(glGetError() == GL_NO_ERROR);
253 }
255 void draw()
256 {
257 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
259 rend->render(level);
261 if(show_con) {
262 draw_cmdcon();
263 }
264 }
266 void view_matrix(int eye)
267 {
268 float offs = stereo_eye_sep * eye * 0.5;
269 glTranslatef(-offs, 0, 0);
270 cam.use_inverse();
271 }
273 void proj_matrix(int eye)
274 {
275 static const float fov = M_PI / 4.0;
276 static const float near_clip = 0.1;
277 static const float far_clip = 100.0;
279 float top = near_clip * tan(fov * 0.5);
280 float right = top * (float)cfg.width / (float)cfg.height;
282 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
283 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
284 }
287 void update(unsigned long msec)
288 {
289 static unsigned long last_upd;
291 if(last_upd == 0) {
292 last_upd = msec;
293 }
294 float dt = (float)(msec - last_upd) / 1000.0;
296 float offs = 2.0 * dt;
297 float dx = 0, dy = 0;
299 // handle key input
300 bool did_move = false;
301 if(keystate['w'] || keystate['W']) {
302 dy -= offs;
303 did_move = true;
304 }
305 if(keystate['s'] || keystate['S']) {
306 dy += offs;
307 did_move = true;
308 }
309 if(keystate['d'] || keystate['D']) {
310 dx += offs;
311 did_move = true;
312 }
313 if(keystate['a'] || keystate['A']) {
314 dx -= offs;
315 did_move = true;
316 }
318 cam.input_move(dx, 0, dy);
320 tileset->update_tiles(msec);
322 level->set_player_position(cam.get_position());
323 level->update(msec, dt);
325 if(cfg.sound) {
326 // set the listener matrix
327 set_audio_listener(cam.matrix());
329 // play the walking sound if we're walking
330 int cellx, celly;
331 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
333 const AudioSample *move_sample;
334 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
335 if(move_sample != move_sound->get_sample()) {
336 move_sound->stop();
337 move_sound->set_sample(move_sample);
338 move_sound->play();
339 }
340 } else {
341 if(move_sound->get_sample()) {
342 move_sound->stop();
343 move_sound->set_sample(0);
344 }
345 }
346 }
348 last_upd = msec;
349 }
351 void reshape(int x, int y)
352 {
353 glViewport(0, 0, x, y);
354 cfg.width = x;
355 cfg.height = y;
357 rend->resize(x, y);
358 }
360 static bool stereo_shift_pressed;
362 void keyb(unsigned char key, int x, int y)
363 {
364 switch(key) {
365 case 27:
366 exit(0);
368 case '`':
369 show_con = true;
370 glutKeyboardFunc(keyb_con);
371 glutKeyboardUpFunc(0);
372 glutPostRedisplay();
373 break;
375 case 'z':
376 stereo_shift_pressed = true;
377 break;
379 case 'p':
380 {
381 Vector3 pos = cam.get_position();
382 int cell_x, cell_y;
383 level->get_cell_coords_at(pos, &cell_x, &cell_y);
384 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
385 cell_x, cell_y);
386 }
387 break;
389 case 'P':
390 {
391 Vector3 pos = cam.get_position();
392 int cell_x, cell_y;
393 level->get_cell_coords_at(pos, &cell_x, &cell_y);
394 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
395 printf("walk sample: %p\n", (void*)sample);
396 }
397 break;
400 case '\n':
401 case '\r':
402 {
403 static bool fullscr;
404 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
405 fullscr = !fullscr;
406 if(fullscr) {
407 glutFullScreen();
408 } else {
409 glutPositionWindow(20, 20);
410 }
411 }
412 }
413 break;
415 default:
416 break;
417 }
419 keystate[key] = true;
420 }
422 void key_release(unsigned char key, int x, int y)
423 {
424 switch(key) {
425 case 'z':
426 stereo_shift_pressed = false;
427 break;
429 default:
430 break;
431 }
433 keystate[key] = false;
434 }
436 void keyb_con(unsigned char key, int x, int y)
437 {
438 if(key == '`' || key == 27) {
439 show_con = false;
440 glutKeyboardFunc(keyb);
441 glutKeyboardUpFunc(key_release);
442 glutPostRedisplay();
443 } else {
444 cmdcon_keypress(key);
445 }
446 }
448 static int prev_x, prev_y;
449 static bool bnstate[32];
451 void mouse(int bn, int state, int x, int y)
452 {
453 prev_x = x;
454 prev_y = y;
455 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
456 }
458 void motion(int x, int y)
459 {
460 int dx = x - prev_x;
461 int dy = y - prev_y;
462 prev_x = x;
463 prev_y = y;
465 if(stereo_shift_pressed) {
466 if(dy != 0) {
467 stereo_focus_dist += dy * 0.01;
468 stereo_eye_sep = stereo_focus_dist / 30.0;
469 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
470 glutPostRedisplay();
471 }
472 return;
473 }
475 if(bnstate[0]) {
476 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
477 glutPostRedisplay();
478 }
479 if(bnstate[2]) {
480 cam.input_zoom(dy * 0.1);
481 glutPostRedisplay();
482 }
483 }
485 unsigned int load_psys_tex(const char *fname, void *cls)
486 {
487 TextureSet *texset = tileset->get_textures();
488 return texset->get(fname);
489 }