dungeon_crawler

view prototype/src/main.cc @ 49:303743485aba

pretty much implemented the positional torch sound sources
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 00:34:29 +0300
parents aa9e28670ae2
children c40efa9cf844
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 if(cfg.sound) {
147 delete move_sound;
148 destroy_audio();
149 }
151 delete level;
152 delete tileset;
153 delete rend;
155 cleanup_cmdcon();
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 // play the walking sound if we're walking
275 if(cfg.sound) {
276 int cellx, celly;
277 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
279 const AudioSample *move_sample;
280 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
281 if(move_sample != move_sound->get_sample()) {
282 move_sound->stop();
283 move_sound->set_sample(move_sample);
284 move_sound->play();
285 }
286 } else {
287 if(move_sound->get_sample()) {
288 move_sound->stop();
289 move_sound->set_sample(0);
290 }
291 }
292 }
294 last_upd = msec;
295 }
297 void reshape(int x, int y)
298 {
299 glViewport(0, 0, x, y);
300 cfg.width = x;
301 cfg.height = y;
303 rend->resize(x, y);
304 }
306 static bool stereo_shift_pressed;
308 void keyb(unsigned char key, int x, int y)
309 {
310 switch(key) {
311 case 27:
312 exit(0);
314 case '`':
315 show_con = true;
316 glutKeyboardFunc(keyb_con);
317 glutKeyboardUpFunc(0);
318 glutPostRedisplay();
319 break;
321 case 'z':
322 stereo_shift_pressed = true;
323 break;
325 case 'p':
326 {
327 Vector3 pos = cam.get_position();
328 int cell_x, cell_y;
329 level->get_cell_coords_at(pos, &cell_x, &cell_y);
330 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
331 cell_x, cell_y);
332 }
333 break;
335 case 'P':
336 {
337 Vector3 pos = cam.get_position();
338 int cell_x, cell_y;
339 level->get_cell_coords_at(pos, &cell_x, &cell_y);
340 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
341 printf("walk sample: %p\n", (void*)sample);
342 }
343 break;
346 case '\n':
347 case '\r':
348 {
349 static bool fullscr;
350 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
351 fullscr = !fullscr;
352 if(fullscr) {
353 glutFullScreen();
354 } else {
355 glutPositionWindow(20, 20);
356 }
357 }
358 }
359 break;
361 default:
362 break;
363 }
365 keystate[key] = true;
366 }
368 void key_release(unsigned char key, int x, int y)
369 {
370 switch(key) {
371 case 'z':
372 stereo_shift_pressed = false;
373 break;
375 default:
376 break;
377 }
379 keystate[key] = false;
380 }
382 void keyb_con(unsigned char key, int x, int y)
383 {
384 if(key == '`' || key == 27) {
385 show_con = false;
386 glutKeyboardFunc(keyb);
387 glutKeyboardUpFunc(key_release);
388 glutPostRedisplay();
389 } else {
390 cmdcon_keypress(key);
391 }
392 }
394 static int prev_x, prev_y;
395 static bool bnstate[32];
397 void mouse(int bn, int state, int x, int y)
398 {
399 prev_x = x;
400 prev_y = y;
401 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
402 }
404 void motion(int x, int y)
405 {
406 int dx = x - prev_x;
407 int dy = y - prev_y;
408 prev_x = x;
409 prev_y = y;
411 if(stereo_shift_pressed) {
412 if(dy != 0) {
413 stereo_focus_dist += dy * 0.01;
414 stereo_eye_sep = stereo_focus_dist / 30.0;
415 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
416 glutPostRedisplay();
417 }
418 return;
419 }
421 if(bnstate[0]) {
422 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
423 glutPostRedisplay();
424 }
425 if(bnstate[2]) {
426 cam.input_zoom(dy * 0.1);
427 glutPostRedisplay();
428 }
429 }
431 unsigned int load_psys_tex(const char *fname, void *cls)
432 {
433 TextureSet *texset = tileset->get_textures();
434 return texset->get(fname);
435 }