dungeon_crawler

view prototype/src/main.cc @ 48:aa9e28670ae2

added sound playback, more to do
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 17 Sep 2012 08:40:59 +0300
parents d52711f2b9a1
children 303743485aba
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);
270 level->update(msec, dt);
272 // play the walking sound if we're walking
273 int cellx, celly;
274 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
276 const AudioSample *move_sample;
277 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
278 if(move_sample != move_sound->get_sample()) {
279 move_sound->stop();
280 move_sound->set_sample(move_sample);
281 move_sound->play();
282 }
283 } else {
284 if(move_sound->get_sample()) {
285 move_sound->stop();
286 move_sound->set_sample(0);
287 }
288 }
290 last_upd = msec;
291 }
293 void reshape(int x, int y)
294 {
295 glViewport(0, 0, x, y);
296 cfg.width = x;
297 cfg.height = y;
299 rend->resize(x, y);
300 }
302 static bool stereo_shift_pressed;
304 void keyb(unsigned char key, int x, int y)
305 {
306 switch(key) {
307 case 27:
308 exit(0);
310 case '`':
311 show_con = true;
312 glutKeyboardFunc(keyb_con);
313 glutKeyboardUpFunc(0);
314 glutPostRedisplay();
315 break;
317 case 'z':
318 stereo_shift_pressed = true;
319 break;
321 case 'p':
322 {
323 Vector3 pos = cam.get_position();
324 int cell_x, cell_y;
325 level->get_cell_coords_at(pos, &cell_x, &cell_y);
326 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
327 cell_x, cell_y);
328 }
329 break;
331 case 'P':
332 {
333 Vector3 pos = cam.get_position();
334 int cell_x, cell_y;
335 level->get_cell_coords_at(pos, &cell_x, &cell_y);
336 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
337 printf("walk sample: %p\n", (void*)sample);
338 }
339 break;
342 case '\n':
343 case '\r':
344 {
345 static bool fullscr;
346 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
347 fullscr = !fullscr;
348 if(fullscr) {
349 glutFullScreen();
350 } else {
351 glutPositionWindow(20, 20);
352 }
353 }
354 }
355 break;
357 default:
358 break;
359 }
361 keystate[key] = true;
362 }
364 void key_release(unsigned char key, int x, int y)
365 {
366 switch(key) {
367 case 'z':
368 stereo_shift_pressed = false;
369 break;
371 default:
372 break;
373 }
375 keystate[key] = false;
376 }
378 void keyb_con(unsigned char key, int x, int y)
379 {
380 if(key == '`' || key == 27) {
381 show_con = false;
382 glutKeyboardFunc(keyb);
383 glutKeyboardUpFunc(key_release);
384 glutPostRedisplay();
385 } else {
386 cmdcon_keypress(key);
387 }
388 }
390 static int prev_x, prev_y;
391 static bool bnstate[32];
393 void mouse(int bn, int state, int x, int y)
394 {
395 prev_x = x;
396 prev_y = y;
397 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
398 }
400 void motion(int x, int y)
401 {
402 int dx = x - prev_x;
403 int dy = y - prev_y;
404 prev_x = x;
405 prev_y = y;
407 if(stereo_shift_pressed) {
408 if(dy != 0) {
409 stereo_focus_dist += dy * 0.01;
410 stereo_eye_sep = stereo_focus_dist / 30.0;
411 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
412 glutPostRedisplay();
413 }
414 return;
415 }
417 if(bnstate[0]) {
418 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
419 glutPostRedisplay();
420 }
421 if(bnstate[2]) {
422 cam.input_zoom(dy * 0.1);
423 glutPostRedisplay();
424 }
425 }
427 unsigned int load_psys_tex(const char *fname, void *cls)
428 {
429 TextureSet *texset = tileset->get_textures();
430 return texset->get(fname);
431 }