dungeon_crawler

view prototype/src/main.cc @ 51:d57df51f6b50

- fixed audio panning (listener direction) - particles had no fog - sound sources were not destroyed properly
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 09:40:56 +0300
parents c40efa9cf844
children bcdea26c8f27
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 "cmdcon.h"
15 #include "cfg.h"
16 #include "timer.h"
17 #include "audio/audio.h"
18 #include "audio/source.h"
20 bool init(int xsz, int ysz);
21 void cleanup();
22 void idle();
23 void disp();
24 void draw();
25 void view_matrix(int eye);
26 void proj_matrix(int eye);
27 void update(unsigned long msec);
28 void reshape(int x, int y);
29 void keyb(unsigned char key, int x, int y);
30 void key_release(unsigned char key, int x, int y);
31 void keyb_con(unsigned char key, int x, int y);
32 void mouse(int bn, int state, int x, int y);
33 void motion(int x, int y);
34 unsigned int load_psys_tex(const char *fname, void *cls);
36 static TileSet *tileset;
37 static Level *level;
39 static FpsCamera cam;
40 static bool keystate[256];
42 static float stereo_focus_dist = 0.25;
43 static float stereo_eye_sep = stereo_focus_dist / 30.0;
45 static bool show_con;
47 static AudioSource *move_sound;
49 int main(int argc, char **argv)
50 {
51 glutInit(&argc, argv);
53 if(!cfg.parse_args(argc, argv)) {
54 return 1;
55 }
57 glutInitWindowSize(cfg.width, cfg.height);
58 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
59 glutCreateWindow("dungeon crawler prototype");
61 glutIdleFunc(idle);
62 glutDisplayFunc(disp);
63 glutReshapeFunc(reshape);
64 glutKeyboardFunc(keyb);
65 glutKeyboardUpFunc(key_release);
66 glutMouseFunc(mouse);
67 glutMotionFunc(motion);
69 glewInit();
71 if(!init(cfg.width, cfg.height)) {
72 return 1;
73 }
74 atexit(cleanup);
76 glutMainLoop();
77 }
79 bool init(int xsz, int ysz)
80 {
81 // backup light for the forward crappy renderer
82 glEnable(GL_LIGHTING);
83 glEnable(GL_LIGHT0);
85 float ldir[] = {0, 0, -0.5, 1};
86 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
87 float lcol[] = {1, 1, 1, 1};
88 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
89 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
91 glEnable(GL_DEPTH_TEST);
92 glEnable(GL_CULL_FACE);
93 glEnable(GL_MULTISAMPLE);
95 add_data_path(".");
96 add_data_path("data");
97 add_data_path("data/audio");
98 add_data_path("sdr");
100 if(cfg.sound && !init_audio()) {
101 fprintf(stderr, "failed to initialize audio, continuing silently\n");
102 cfg.sound = false;
103 }
104 if(cfg.sound) {
105 move_sound = new AudioSource;
106 }
108 rend = new DeferredRenderer();
109 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
110 printf("falling back to crappy renderer...\n");
112 rend = new FwdRenderer();
113 if(!rend->init(xsz, ysz)) {
114 fprintf(stderr, "failed to create renderer\n");
115 return false;
116 }
117 }
119 if(!init_cmdcon()) {
120 return false;
121 }
123 psys_texture_loader(load_psys_tex, 0, 0);
125 // load a tileset
126 tileset = new TileSet;
127 printf("loading tileset: %s\n", cfg.tileset_file);
128 if(!tileset->load(datafile_path(cfg.tileset_file))) {
129 return false;
130 }
131 set_active_tileset(tileset);
133 level = new Level;
134 printf("loading level: %s\n", cfg.level_file);
135 if(!level->load(datafile_path(cfg.level_file))) {
136 return false;
137 }
139 cam.input_move(0, 0.5, 0);
141 return true;
142 }
144 void cleanup()
145 {
146 delete level;
147 delete tileset;
148 delete rend;
150 cleanup_cmdcon();
152 if(cfg.sound) {
153 delete move_sound;
154 destroy_audio();
155 }
156 }
158 void idle()
159 {
160 glutPostRedisplay();
161 }
163 void disp()
164 {
165 update(get_time_msec());
167 if(cfg.stereo) {
168 glDrawBuffer(GL_BACK_LEFT);
170 glMatrixMode(GL_PROJECTION);
171 glLoadIdentity();
172 proj_matrix(-1);
173 glMatrixMode(GL_MODELVIEW);
174 glLoadIdentity();
175 view_matrix(-1);
177 draw();
179 glDrawBuffer(GL_BACK_RIGHT);
181 glMatrixMode(GL_PROJECTION);
182 glLoadIdentity();
183 proj_matrix(1);
184 glMatrixMode(GL_MODELVIEW);
185 glLoadIdentity();
186 view_matrix(1);
188 draw();
189 } else {
190 glMatrixMode(GL_PROJECTION);
191 glLoadIdentity();
192 proj_matrix(0);
193 glMatrixMode(GL_MODELVIEW);
194 glLoadIdentity();
195 view_matrix(0);
197 draw();
198 }
200 glutSwapBuffers();
201 assert(glGetError() == GL_NO_ERROR);
202 }
204 void draw()
205 {
206 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
208 rend->render(level);
210 if(show_con) {
211 draw_cmdcon();
212 }
213 }
215 void view_matrix(int eye)
216 {
217 float offs = stereo_eye_sep * eye * 0.5;
218 glTranslatef(-offs, 0, 0);
219 cam.use_inverse();
220 }
222 void proj_matrix(int eye)
223 {
224 static const float fov = M_PI / 4.0;
225 static const float near_clip = 0.1;
226 static const float far_clip = 100.0;
228 float top = near_clip * tan(fov * 0.5);
229 float right = top * (float)cfg.width / (float)cfg.height;
231 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
232 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
233 }
236 void update(unsigned long msec)
237 {
238 static unsigned long last_upd;
240 if(last_upd == 0) {
241 last_upd = msec;
242 }
243 float dt = (float)(msec - last_upd) / 1000.0;
245 float offs = 2.5 * dt;
246 float dx = 0, dy = 0;
248 // handle key input
249 bool did_move = false;
250 if(keystate['w'] || keystate['W']) {
251 dy -= offs;
252 did_move = true;
253 }
254 if(keystate['s'] || keystate['S']) {
255 dy += offs;
256 did_move = true;
257 }
258 if(keystate['d'] || keystate['D']) {
259 dx += offs;
260 did_move = true;
261 }
262 if(keystate['a'] || keystate['A']) {
263 dx -= offs;
264 did_move = true;
265 }
267 cam.input_move(dx, 0, dy);
269 tileset->update_tiles(msec);
271 level->set_player_position(cam.get_position());
272 level->update(msec, dt);
274 if(cfg.sound) {
275 // set the listener matrix
276 set_audio_listener(cam.matrix());
278 // play the walking sound if we're walking
279 int cellx, celly;
280 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
282 const AudioSample *move_sample;
283 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
284 if(move_sample != move_sound->get_sample()) {
285 move_sound->stop();
286 move_sound->set_sample(move_sample);
287 move_sound->play();
288 }
289 } else {
290 if(move_sound->get_sample()) {
291 move_sound->stop();
292 move_sound->set_sample(0);
293 }
294 }
295 }
297 last_upd = msec;
298 }
300 void reshape(int x, int y)
301 {
302 glViewport(0, 0, x, y);
303 cfg.width = x;
304 cfg.height = y;
306 rend->resize(x, y);
307 }
309 static bool stereo_shift_pressed;
311 void keyb(unsigned char key, int x, int y)
312 {
313 switch(key) {
314 case 27:
315 exit(0);
317 case '`':
318 show_con = true;
319 glutKeyboardFunc(keyb_con);
320 glutKeyboardUpFunc(0);
321 glutPostRedisplay();
322 break;
324 case 'z':
325 stereo_shift_pressed = true;
326 break;
328 case 'p':
329 {
330 Vector3 pos = cam.get_position();
331 int cell_x, cell_y;
332 level->get_cell_coords_at(pos, &cell_x, &cell_y);
333 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
334 cell_x, cell_y);
335 }
336 break;
338 case 'P':
339 {
340 Vector3 pos = cam.get_position();
341 int cell_x, cell_y;
342 level->get_cell_coords_at(pos, &cell_x, &cell_y);
343 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
344 printf("walk sample: %p\n", (void*)sample);
345 }
346 break;
349 case '\n':
350 case '\r':
351 {
352 static bool fullscr;
353 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
354 fullscr = !fullscr;
355 if(fullscr) {
356 glutFullScreen();
357 } else {
358 glutPositionWindow(20, 20);
359 }
360 }
361 }
362 break;
364 default:
365 break;
366 }
368 keystate[key] = true;
369 }
371 void key_release(unsigned char key, int x, int y)
372 {
373 switch(key) {
374 case 'z':
375 stereo_shift_pressed = false;
376 break;
378 default:
379 break;
380 }
382 keystate[key] = false;
383 }
385 void keyb_con(unsigned char key, int x, int y)
386 {
387 if(key == '`' || key == 27) {
388 show_con = false;
389 glutKeyboardFunc(keyb);
390 glutKeyboardUpFunc(key_release);
391 glutPostRedisplay();
392 } else {
393 cmdcon_keypress(key);
394 }
395 }
397 static int prev_x, prev_y;
398 static bool bnstate[32];
400 void mouse(int bn, int state, int x, int y)
401 {
402 prev_x = x;
403 prev_y = y;
404 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
405 }
407 void motion(int x, int y)
408 {
409 int dx = x - prev_x;
410 int dy = y - prev_y;
411 prev_x = x;
412 prev_y = y;
414 if(stereo_shift_pressed) {
415 if(dy != 0) {
416 stereo_focus_dist += dy * 0.01;
417 stereo_eye_sep = stereo_focus_dist / 30.0;
418 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
419 glutPostRedisplay();
420 }
421 return;
422 }
424 if(bnstate[0]) {
425 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
426 glutPostRedisplay();
427 }
428 if(bnstate[2]) {
429 cam.input_zoom(dy * 0.1);
430 glutPostRedisplay();
431 }
432 }
434 unsigned int load_psys_tex(const char *fname, void *cls)
435 {
436 TextureSet *texset = tileset->get_textures();
437 return texset->get(fname);
438 }