dungeon_crawler

view prototype/src/main.cc @ 55:4c427e28ca00

music playback bugfixing
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Sep 2012 08:19:10 +0300
parents bcdea26c8f27
children f9b8bbebc9b3
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"
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"))) {
112 music->play(PlayMode::loop);
113 } else {
114 delete music;
115 music = 0;
116 }
117 }
119 rend = new DeferredRenderer();
120 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
121 printf("falling back to crappy renderer...\n");
123 rend = new FwdRenderer();
124 if(!rend->init(xsz, ysz)) {
125 fprintf(stderr, "failed to create renderer\n");
126 return false;
127 }
128 }
130 if(!init_cmdcon()) {
131 return false;
132 }
134 psys_texture_loader(load_psys_tex, 0, 0);
136 // load a tileset
137 tileset = new TileSet;
138 printf("loading tileset: %s\n", cfg.tileset_file);
139 if(!tileset->load(datafile_path(cfg.tileset_file))) {
140 return false;
141 }
142 set_active_tileset(tileset);
144 level = new Level;
145 printf("loading level: %s\n", cfg.level_file);
146 if(!level->load(datafile_path(cfg.level_file))) {
147 return false;
148 }
150 cam.input_move(0, 0.5, 0);
152 return true;
153 }
155 void cleanup()
156 {
157 delete level;
158 delete tileset;
159 delete rend;
161 cleanup_cmdcon();
163 if(cfg.sound) {
164 delete music;
165 delete move_sound;
166 destroy_audio();
167 }
168 }
170 void idle()
171 {
172 glutPostRedisplay();
173 }
175 void disp()
176 {
177 update(get_time_msec());
179 if(cfg.stereo) {
180 glDrawBuffer(GL_BACK_LEFT);
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 proj_matrix(-1);
185 glMatrixMode(GL_MODELVIEW);
186 glLoadIdentity();
187 view_matrix(-1);
189 draw();
191 glDrawBuffer(GL_BACK_RIGHT);
193 glMatrixMode(GL_PROJECTION);
194 glLoadIdentity();
195 proj_matrix(1);
196 glMatrixMode(GL_MODELVIEW);
197 glLoadIdentity();
198 view_matrix(1);
200 draw();
201 } else {
202 glMatrixMode(GL_PROJECTION);
203 glLoadIdentity();
204 proj_matrix(0);
205 glMatrixMode(GL_MODELVIEW);
206 glLoadIdentity();
207 view_matrix(0);
209 draw();
210 }
212 glutSwapBuffers();
213 assert(glGetError() == GL_NO_ERROR);
214 }
216 void draw()
217 {
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
220 rend->render(level);
222 if(show_con) {
223 draw_cmdcon();
224 }
225 }
227 void view_matrix(int eye)
228 {
229 float offs = stereo_eye_sep * eye * 0.5;
230 glTranslatef(-offs, 0, 0);
231 cam.use_inverse();
232 }
234 void proj_matrix(int eye)
235 {
236 static const float fov = M_PI / 4.0;
237 static const float near_clip = 0.1;
238 static const float far_clip = 100.0;
240 float top = near_clip * tan(fov * 0.5);
241 float right = top * (float)cfg.width / (float)cfg.height;
243 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
244 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
245 }
248 void update(unsigned long msec)
249 {
250 static unsigned long last_upd;
252 if(last_upd == 0) {
253 last_upd = msec;
254 }
255 float dt = (float)(msec - last_upd) / 1000.0;
257 float offs = 2.5 * dt;
258 float dx = 0, dy = 0;
260 // handle key input
261 bool did_move = false;
262 if(keystate['w'] || keystate['W']) {
263 dy -= offs;
264 did_move = true;
265 }
266 if(keystate['s'] || keystate['S']) {
267 dy += offs;
268 did_move = true;
269 }
270 if(keystate['d'] || keystate['D']) {
271 dx += offs;
272 did_move = true;
273 }
274 if(keystate['a'] || keystate['A']) {
275 dx -= offs;
276 did_move = true;
277 }
279 cam.input_move(dx, 0, dy);
281 tileset->update_tiles(msec);
283 level->set_player_position(cam.get_position());
284 level->update(msec, dt);
286 if(cfg.sound) {
287 // set the listener matrix
288 set_audio_listener(cam.matrix());
290 // play the walking sound if we're walking
291 int cellx, celly;
292 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
294 const AudioSample *move_sample;
295 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
296 if(move_sample != move_sound->get_sample()) {
297 move_sound->stop();
298 move_sound->set_sample(move_sample);
299 move_sound->play();
300 }
301 } else {
302 if(move_sound->get_sample()) {
303 move_sound->stop();
304 move_sound->set_sample(0);
305 }
306 }
307 }
309 last_upd = msec;
310 }
312 void reshape(int x, int y)
313 {
314 glViewport(0, 0, x, y);
315 cfg.width = x;
316 cfg.height = y;
318 rend->resize(x, y);
319 }
321 static bool stereo_shift_pressed;
323 void keyb(unsigned char key, int x, int y)
324 {
325 switch(key) {
326 case 27:
327 exit(0);
329 case '`':
330 show_con = true;
331 glutKeyboardFunc(keyb_con);
332 glutKeyboardUpFunc(0);
333 glutPostRedisplay();
334 break;
336 case 'z':
337 stereo_shift_pressed = true;
338 break;
340 case 'p':
341 {
342 Vector3 pos = cam.get_position();
343 int cell_x, cell_y;
344 level->get_cell_coords_at(pos, &cell_x, &cell_y);
345 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
346 cell_x, cell_y);
347 }
348 break;
350 case 'P':
351 {
352 Vector3 pos = cam.get_position();
353 int cell_x, cell_y;
354 level->get_cell_coords_at(pos, &cell_x, &cell_y);
355 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
356 printf("walk sample: %p\n", (void*)sample);
357 }
358 break;
361 case '\n':
362 case '\r':
363 {
364 static bool fullscr;
365 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
366 fullscr = !fullscr;
367 if(fullscr) {
368 glutFullScreen();
369 } else {
370 glutPositionWindow(20, 20);
371 }
372 }
373 }
374 break;
376 default:
377 break;
378 }
380 keystate[key] = true;
381 }
383 void key_release(unsigned char key, int x, int y)
384 {
385 switch(key) {
386 case 'z':
387 stereo_shift_pressed = false;
388 break;
390 default:
391 break;
392 }
394 keystate[key] = false;
395 }
397 void keyb_con(unsigned char key, int x, int y)
398 {
399 if(key == '`' || key == 27) {
400 show_con = false;
401 glutKeyboardFunc(keyb);
402 glutKeyboardUpFunc(key_release);
403 glutPostRedisplay();
404 } else {
405 cmdcon_keypress(key);
406 }
407 }
409 static int prev_x, prev_y;
410 static bool bnstate[32];
412 void mouse(int bn, int state, int x, int y)
413 {
414 prev_x = x;
415 prev_y = y;
416 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
417 }
419 void motion(int x, int y)
420 {
421 int dx = x - prev_x;
422 int dy = y - prev_y;
423 prev_x = x;
424 prev_y = y;
426 if(stereo_shift_pressed) {
427 if(dy != 0) {
428 stereo_focus_dist += dy * 0.01;
429 stereo_eye_sep = stereo_focus_dist / 30.0;
430 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
431 glutPostRedisplay();
432 }
433 return;
434 }
436 if(bnstate[0]) {
437 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
438 glutPostRedisplay();
439 }
440 if(bnstate[2]) {
441 cam.input_zoom(dy * 0.1);
442 glutPostRedisplay();
443 }
444 }
446 unsigned int load_psys_tex(const char *fname, void *cls)
447 {
448 TextureSet *texset = tileset->get_textures();
449 return texset->get(fname);
450 }