dungeon_crawler

view prototype/src/main.cc @ 56:f9b8bbebc9b3

fixed the music playback
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Sep 2012 10:04:25 +0300
parents 4c427e28ca00
children aa86119e3295
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->set_volume(0.6);
113 music->play(PlayMode::loop);
114 } else {
115 delete music;
116 music = 0;
117 }
118 }
120 rend = new DeferredRenderer();
121 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
122 printf("falling back to crappy renderer...\n");
124 rend = new FwdRenderer();
125 if(!rend->init(xsz, ysz)) {
126 fprintf(stderr, "failed to create renderer\n");
127 return false;
128 }
129 }
131 if(!init_cmdcon()) {
132 return false;
133 }
135 psys_texture_loader(load_psys_tex, 0, 0);
137 // load a tileset
138 tileset = new TileSet;
139 printf("loading tileset: %s\n", cfg.tileset_file);
140 if(!tileset->load(datafile_path(cfg.tileset_file))) {
141 return false;
142 }
143 set_active_tileset(tileset);
145 level = new Level;
146 printf("loading level: %s\n", cfg.level_file);
147 if(!level->load(datafile_path(cfg.level_file))) {
148 return false;
149 }
151 cam.input_move(0, 0.5, 0);
153 return true;
154 }
156 void cleanup()
157 {
158 delete level;
159 delete tileset;
160 delete rend;
162 cleanup_cmdcon();
164 if(cfg.sound) {
165 delete music;
166 delete move_sound;
167 destroy_audio();
168 }
169 }
171 void idle()
172 {
173 glutPostRedisplay();
174 }
176 void disp()
177 {
178 update(get_time_msec());
180 if(cfg.stereo) {
181 glDrawBuffer(GL_BACK_LEFT);
183 glMatrixMode(GL_PROJECTION);
184 glLoadIdentity();
185 proj_matrix(-1);
186 glMatrixMode(GL_MODELVIEW);
187 glLoadIdentity();
188 view_matrix(-1);
190 draw();
192 glDrawBuffer(GL_BACK_RIGHT);
194 glMatrixMode(GL_PROJECTION);
195 glLoadIdentity();
196 proj_matrix(1);
197 glMatrixMode(GL_MODELVIEW);
198 glLoadIdentity();
199 view_matrix(1);
201 draw();
202 } else {
203 glMatrixMode(GL_PROJECTION);
204 glLoadIdentity();
205 proj_matrix(0);
206 glMatrixMode(GL_MODELVIEW);
207 glLoadIdentity();
208 view_matrix(0);
210 draw();
211 }
213 glutSwapBuffers();
214 assert(glGetError() == GL_NO_ERROR);
215 }
217 void draw()
218 {
219 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
221 rend->render(level);
223 if(show_con) {
224 draw_cmdcon();
225 }
226 }
228 void view_matrix(int eye)
229 {
230 float offs = stereo_eye_sep * eye * 0.5;
231 glTranslatef(-offs, 0, 0);
232 cam.use_inverse();
233 }
235 void proj_matrix(int eye)
236 {
237 static const float fov = M_PI / 4.0;
238 static const float near_clip = 0.1;
239 static const float far_clip = 100.0;
241 float top = near_clip * tan(fov * 0.5);
242 float right = top * (float)cfg.width / (float)cfg.height;
244 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
245 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
246 }
249 void update(unsigned long msec)
250 {
251 static unsigned long last_upd;
253 if(last_upd == 0) {
254 last_upd = msec;
255 }
256 float dt = (float)(msec - last_upd) / 1000.0;
258 float offs = 2.0 * dt;
259 float dx = 0, dy = 0;
261 // handle key input
262 bool did_move = false;
263 if(keystate['w'] || keystate['W']) {
264 dy -= offs;
265 did_move = true;
266 }
267 if(keystate['s'] || keystate['S']) {
268 dy += offs;
269 did_move = true;
270 }
271 if(keystate['d'] || keystate['D']) {
272 dx += offs;
273 did_move = true;
274 }
275 if(keystate['a'] || keystate['A']) {
276 dx -= offs;
277 did_move = true;
278 }
280 cam.input_move(dx, 0, dy);
282 tileset->update_tiles(msec);
284 level->set_player_position(cam.get_position());
285 level->update(msec, dt);
287 if(cfg.sound) {
288 // set the listener matrix
289 set_audio_listener(cam.matrix());
291 // play the walking sound if we're walking
292 int cellx, celly;
293 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
295 const AudioSample *move_sample;
296 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
297 if(move_sample != move_sound->get_sample()) {
298 move_sound->stop();
299 move_sound->set_sample(move_sample);
300 move_sound->play();
301 }
302 } else {
303 if(move_sound->get_sample()) {
304 move_sound->stop();
305 move_sound->set_sample(0);
306 }
307 }
308 }
310 last_upd = msec;
311 }
313 void reshape(int x, int y)
314 {
315 glViewport(0, 0, x, y);
316 cfg.width = x;
317 cfg.height = y;
319 rend->resize(x, y);
320 }
322 static bool stereo_shift_pressed;
324 void keyb(unsigned char key, int x, int y)
325 {
326 switch(key) {
327 case 27:
328 exit(0);
330 case '`':
331 show_con = true;
332 glutKeyboardFunc(keyb_con);
333 glutKeyboardUpFunc(0);
334 glutPostRedisplay();
335 break;
337 case 'z':
338 stereo_shift_pressed = true;
339 break;
341 case 'p':
342 {
343 Vector3 pos = cam.get_position();
344 int cell_x, cell_y;
345 level->get_cell_coords_at(pos, &cell_x, &cell_y);
346 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
347 cell_x, cell_y);
348 }
349 break;
351 case 'P':
352 {
353 Vector3 pos = cam.get_position();
354 int cell_x, cell_y;
355 level->get_cell_coords_at(pos, &cell_x, &cell_y);
356 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
357 printf("walk sample: %p\n", (void*)sample);
358 }
359 break;
362 case '\n':
363 case '\r':
364 {
365 static bool fullscr;
366 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
367 fullscr = !fullscr;
368 if(fullscr) {
369 glutFullScreen();
370 } else {
371 glutPositionWindow(20, 20);
372 }
373 }
374 }
375 break;
377 default:
378 break;
379 }
381 keystate[key] = true;
382 }
384 void key_release(unsigned char key, int x, int y)
385 {
386 switch(key) {
387 case 'z':
388 stereo_shift_pressed = false;
389 break;
391 default:
392 break;
393 }
395 keystate[key] = false;
396 }
398 void keyb_con(unsigned char key, int x, int y)
399 {
400 if(key == '`' || key == 27) {
401 show_con = false;
402 glutKeyboardFunc(keyb);
403 glutKeyboardUpFunc(key_release);
404 glutPostRedisplay();
405 } else {
406 cmdcon_keypress(key);
407 }
408 }
410 static int prev_x, prev_y;
411 static bool bnstate[32];
413 void mouse(int bn, int state, int x, int y)
414 {
415 prev_x = x;
416 prev_y = y;
417 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
418 }
420 void motion(int x, int y)
421 {
422 int dx = x - prev_x;
423 int dy = y - prev_y;
424 prev_x = x;
425 prev_y = y;
427 if(stereo_shift_pressed) {
428 if(dy != 0) {
429 stereo_focus_dist += dy * 0.01;
430 stereo_eye_sep = stereo_focus_dist / 30.0;
431 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
432 glutPostRedisplay();
433 }
434 return;
435 }
437 if(bnstate[0]) {
438 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
439 glutPostRedisplay();
440 }
441 if(bnstate[2]) {
442 cam.input_zoom(dy * 0.1);
443 glutPostRedisplay();
444 }
445 }
447 unsigned int load_psys_tex(const char *fname, void *cls)
448 {
449 TextureSet *texset = tileset->get_textures();
450 return texset->get(fname);
451 }