dungeon_crawler
view prototype/src/main.cc @ 75:b05ab29cd17d
color grading done
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 22 Oct 2012 03:55:00 +0300 |
parents | 5981917093ff |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "psys/psys.h"
6 #include "level.h"
7 #include "texture.h"
8 #include "camera.h"
9 #include "datapath.h"
10 #include "tileset.h"
11 #include "renderer.h"
12 #include "renderer_deferred.h"
13 #include "cmdcon.h"
14 #include "cfg.h"
15 #include "timer.h"
16 #include "audio/audio.h"
17 #include "audio/source.h"
18 #include "audio/ovstream.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;
46 static bool save_grade_shot;
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").c_str())) {
112 music->set_volume(0.6);
113 music->play(PlayMode::loop);
114 } else {
115 delete music;
116 music = 0;
117 }
118 }
120 switch(cfg.rend) {
121 case Config::Renderer::mrt:
122 rend = new DeferredRenderer;
123 if(!rend->init(xsz, ysz)) {
124 fprintf(stderr, "failed to initialize deferred mrt renderer\n");
125 delete rend;
126 return false;
127 }
128 break;
130 case Config::Renderer::fwd:
131 rend = new FwdRenderer;
132 if(!rend->init(xsz, ysz)) {
133 fprintf(stderr, "failed to initialize forward renderer\n");
134 delete rend;
135 return false;
136 }
137 break;
139 default:
140 // try each in turn falling back to progressively worse renderers
141 rend = new DeferredRenderer;
142 if(!rend->init(xsz, ysz)) {
143 printf("falling back to multipass renderer...\n");
145 delete rend;
146 rend = new FwdRenderer();
147 if(!rend->init(xsz, ysz)) {
148 fprintf(stderr, "failed to create renderer\n");
149 return false;
150 }
151 }
152 }
154 if(!init_cmdcon()) {
155 return false;
156 }
158 psys_texture_loader(load_psys_tex, 0, 0);
160 // load a tileset
161 tileset = new TileSet;
162 printf("loading tileset: %s\n", cfg.tileset_file);
163 if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) {
164 return false;
165 }
166 set_active_tileset(tileset);
168 level = new Level;
169 printf("loading level: %s\n", cfg.level_file);
170 if(!level->load(datafile_path(cfg.level_file).c_str())) {
171 return false;
172 }
174 cam.input_move(0, 0.5, 0);
176 return true;
177 }
179 void cleanup()
180 {
181 delete level;
182 delete tileset;
183 delete rend;
185 cleanup_cmdcon();
187 if(cfg.sound) {
188 delete music;
189 delete move_sound;
190 destroy_audio();
191 }
192 }
194 void idle()
195 {
196 glutPostRedisplay();
197 }
199 void disp()
200 {
201 update(get_time_msec());
203 if(cfg.stereo) {
204 glDrawBuffer(GL_BACK_LEFT);
206 glMatrixMode(GL_PROJECTION);
207 glLoadIdentity();
208 proj_matrix(-1);
209 glMatrixMode(GL_MODELVIEW);
210 glLoadIdentity();
211 view_matrix(-1);
213 draw();
215 glDrawBuffer(GL_BACK_RIGHT);
217 glMatrixMode(GL_PROJECTION);
218 glLoadIdentity();
219 proj_matrix(1);
220 glMatrixMode(GL_MODELVIEW);
221 glLoadIdentity();
222 view_matrix(1);
224 draw();
225 } else {
226 glMatrixMode(GL_PROJECTION);
227 glLoadIdentity();
228 proj_matrix(0);
229 glMatrixMode(GL_MODELVIEW);
230 glLoadIdentity();
231 view_matrix(0);
233 draw();
234 }
236 glutSwapBuffers();
237 assert(glGetError() == GL_NO_ERROR);
238 }
240 void draw()
241 {
242 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
244 rend->render(level);
246 if(show_con) {
247 draw_cmdcon();
248 }
250 if(save_grade_shot) {
251 GradePalette *pal = rend->get_grade_palette();
252 if(!pal->save_shot("gradeshot.ppm")) {
253 fprintf(stderr, "failed to save color grading shot\n");
254 }
255 save_grade_shot = false;
256 }
257 }
259 void view_matrix(int eye)
260 {
261 float offs = stereo_eye_sep * eye * 0.5;
262 glTranslatef(-offs, 0, 0);
263 cam.use_inverse();
264 }
266 void proj_matrix(int eye)
267 {
268 static const float fov = M_PI / 4.0;
269 static const float near_clip = 0.1;
270 static const float far_clip = 100.0;
272 float top = near_clip * tan(fov * 0.5);
273 float right = top * (float)cfg.width / (float)cfg.height;
275 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
276 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
277 }
280 void update(unsigned long msec)
281 {
282 static unsigned long last_upd;
284 if(last_upd == 0) {
285 last_upd = msec;
286 }
287 float dt = (float)(msec - last_upd) / 1000.0;
289 float offs = 2.0 * dt;
290 float dx = 0, dy = 0;
292 // handle key input
293 bool did_move = false;
294 if(keystate['w'] || keystate['W']) {
295 dy -= offs;
296 did_move = true;
297 }
298 if(keystate['s'] || keystate['S']) {
299 dy += offs;
300 did_move = true;
301 }
302 if(keystate['d'] || keystate['D']) {
303 dx += offs;
304 did_move = true;
305 }
306 if(keystate['a'] || keystate['A']) {
307 dx -= offs;
308 did_move = true;
309 }
311 cam.input_move(dx, 0, dy);
313 tileset->update_tiles(msec);
315 level->set_player_position(cam.get_position());
316 level->update(msec, dt);
318 if(cfg.sound) {
319 // set the listener matrix
320 set_audio_listener(cam.matrix());
322 // play the walking sound if we're walking
323 int cellx, celly;
324 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
326 const AudioSample *move_sample;
327 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
328 if(move_sample != move_sound->get_sample()) {
329 move_sound->stop();
330 move_sound->set_sample(move_sample);
331 move_sound->play();
332 }
333 } else {
334 if(move_sound->get_sample()) {
335 move_sound->stop();
336 move_sound->set_sample(0);
337 }
338 }
339 }
341 last_upd = msec;
342 }
344 void reshape(int x, int y)
345 {
346 glViewport(0, 0, x, y);
347 cfg.width = x;
348 cfg.height = y;
350 rend->resize(x, y);
351 }
353 static bool stereo_shift_pressed;
355 void keyb(unsigned char key, int x, int y)
356 {
357 switch(key) {
358 case 27:
359 exit(0);
361 case '`':
362 show_con = true;
363 glutKeyboardFunc(keyb_con);
364 glutKeyboardUpFunc(0);
365 glutPostRedisplay();
366 break;
368 case 'z':
369 stereo_shift_pressed = true;
370 break;
372 case 'p':
373 {
374 Vector3 pos = cam.get_position();
375 int cell_x, cell_y;
376 level->get_cell_coords_at(pos, &cell_x, &cell_y);
377 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
378 cell_x, cell_y);
379 }
380 break;
382 case 'P':
383 {
384 Vector3 pos = cam.get_position();
385 int cell_x, cell_y;
386 level->get_cell_coords_at(pos, &cell_x, &cell_y);
387 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
388 printf("walk sample: %p\n", (void*)sample);
389 }
390 break;
393 case '\n':
394 case '\r':
395 {
396 static bool fullscr;
397 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
398 fullscr = !fullscr;
399 if(fullscr) {
400 glutFullScreen();
401 } else {
402 glutPositionWindow(20, 20);
403 }
404 }
405 }
406 break;
408 case 'G':
409 save_grade_shot = true;
410 break;
412 case 'g':
413 {
414 static bool do_grade;
415 do_grade = !do_grade;
417 GradePalette *gpal = rend->get_grade_palette();
418 if(do_grade) {
419 gpal->load_shot("gradeshot-proc.ppm");
420 } else {
421 gpal->create();
422 }
423 }
424 break;
426 default:
427 break;
428 }
430 keystate[key] = true;
431 }
433 void key_release(unsigned char key, int x, int y)
434 {
435 switch(key) {
436 case 'z':
437 stereo_shift_pressed = false;
438 break;
440 default:
441 break;
442 }
444 keystate[key] = false;
445 }
447 void keyb_con(unsigned char key, int x, int y)
448 {
449 if(key == '`' || key == 27) {
450 show_con = false;
451 glutKeyboardFunc(keyb);
452 glutKeyboardUpFunc(key_release);
453 glutPostRedisplay();
454 } else {
455 cmdcon_keypress(key);
456 }
457 }
459 static int prev_x, prev_y;
460 static bool bnstate[32];
462 void mouse(int bn, int state, int x, int y)
463 {
464 prev_x = x;
465 prev_y = y;
466 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
467 }
469 void motion(int x, int y)
470 {
471 int dx = x - prev_x;
472 int dy = y - prev_y;
473 prev_x = x;
474 prev_y = y;
476 if(stereo_shift_pressed) {
477 if(dy != 0) {
478 stereo_focus_dist += dy * 0.01;
479 stereo_eye_sep = stereo_focus_dist / 30.0;
480 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
481 glutPostRedisplay();
482 }
483 return;
484 }
486 if(bnstate[0]) {
487 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
488 glutPostRedisplay();
489 }
490 if(bnstate[2]) {
491 cam.input_zoom(dy * 0.1);
492 glutPostRedisplay();
493 }
494 }
496 unsigned int load_psys_tex(const char *fname, void *cls)
497 {
498 TextureSet *texset = tileset->get_textures();
499 return texset->get(fname);
500 }