dungeon_crawler

annotate prototype/src/main.cc @ 70:f55ddab0f3a4

"fixed" the disappearing lights issue
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 09 Oct 2012 02:54:01 +0300
parents 7f52d6310317
children a27528035e20
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <stdlib.h>
nuclear@1 3 #include <assert.h>
nuclear@1 4 #include "opengl.h"
nuclear@46 5 #include "psys/psys.h"
nuclear@1 6 #include "level.h"
nuclear@48 7 #include "texture.h"
nuclear@1 8 #include "camera.h"
nuclear@5 9 #include "datapath.h"
nuclear@5 10 #include "tileset.h"
nuclear@15 11 #include "renderer.h"
nuclear@41 12 #include "renderer_deferred.h"
nuclear@60 13 #include "renderer_multipass.h"
nuclear@24 14 #include "cmdcon.h"
nuclear@18 15 #include "cfg.h"
nuclear@38 16 #include "timer.h"
nuclear@48 17 #include "audio/audio.h"
nuclear@48 18 #include "audio/source.h"
nuclear@55 19 #include "audio/ovstream.h"
nuclear@1 20
nuclear@17 21 bool init(int xsz, int ysz);
nuclear@5 22 void cleanup();
nuclear@7 23 void idle();
nuclear@1 24 void disp();
nuclear@15 25 void draw();
nuclear@18 26 void view_matrix(int eye);
nuclear@18 27 void proj_matrix(int eye);
nuclear@7 28 void update(unsigned long msec);
nuclear@1 29 void reshape(int x, int y);
nuclear@1 30 void keyb(unsigned char key, int x, int y);
nuclear@7 31 void key_release(unsigned char key, int x, int y);
nuclear@24 32 void keyb_con(unsigned char key, int x, int y);
nuclear@1 33 void mouse(int bn, int state, int x, int y);
nuclear@1 34 void motion(int x, int y);
nuclear@46 35 unsigned int load_psys_tex(const char *fname, void *cls);
nuclear@1 36
nuclear@5 37 static TileSet *tileset;
nuclear@1 38 static Level *level;
nuclear@7 39
nuclear@7 40 static FpsCamera cam;
nuclear@7 41 static bool keystate[256];
nuclear@1 42
nuclear@18 43 static float stereo_focus_dist = 0.25;
nuclear@18 44 static float stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@18 45
nuclear@24 46 static bool show_con;
nuclear@5 47
nuclear@48 48 static AudioSource *move_sound;
nuclear@55 49 static OggVorbisStream *music;
nuclear@48 50
nuclear@1 51 int main(int argc, char **argv)
nuclear@1 52 {
nuclear@1 53 glutInit(&argc, argv);
nuclear@5 54
nuclear@18 55 if(!cfg.parse_args(argc, argv)) {
nuclear@18 56 return 1;
nuclear@5 57 }
nuclear@5 58
nuclear@18 59 glutInitWindowSize(cfg.width, cfg.height);
nuclear@18 60 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
nuclear@18 61 glutCreateWindow("dungeon crawler prototype");
nuclear@1 62
nuclear@7 63 glutIdleFunc(idle);
nuclear@1 64 glutDisplayFunc(disp);
nuclear@1 65 glutReshapeFunc(reshape);
nuclear@1 66 glutKeyboardFunc(keyb);
nuclear@7 67 glutKeyboardUpFunc(key_release);
nuclear@1 68 glutMouseFunc(mouse);
nuclear@1 69 glutMotionFunc(motion);
nuclear@1 70
nuclear@1 71 glewInit();
nuclear@1 72
nuclear@18 73 if(!init(cfg.width, cfg.height)) {
nuclear@5 74 return 1;
nuclear@5 75 }
nuclear@39 76 atexit(cleanup);
nuclear@5 77
nuclear@5 78 glutMainLoop();
nuclear@5 79 }
nuclear@5 80
nuclear@17 81 bool init(int xsz, int ysz)
nuclear@5 82 {
nuclear@41 83 // backup light for the forward crappy renderer
nuclear@1 84 glEnable(GL_LIGHTING);
nuclear@1 85 glEnable(GL_LIGHT0);
nuclear@41 86
nuclear@41 87 float ldir[] = {0, 0, -0.5, 1};
nuclear@1 88 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@41 89 float lcol[] = {1, 1, 1, 1};
nuclear@41 90 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
nuclear@41 91 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
nuclear@1 92
nuclear@1 93 glEnable(GL_DEPTH_TEST);
nuclear@1 94 glEnable(GL_CULL_FACE);
nuclear@1 95 glEnable(GL_MULTISAMPLE);
nuclear@1 96
nuclear@25 97 add_data_path(".");
nuclear@5 98 add_data_path("data");
nuclear@48 99 add_data_path("data/audio");
nuclear@16 100 add_data_path("sdr");
nuclear@5 101
nuclear@48 102 if(cfg.sound && !init_audio()) {
nuclear@48 103 fprintf(stderr, "failed to initialize audio, continuing silently\n");
nuclear@48 104 cfg.sound = false;
nuclear@48 105 }
nuclear@48 106 if(cfg.sound) {
nuclear@48 107 move_sound = new AudioSource;
nuclear@52 108 move_sound->set_volume(0.4);
nuclear@55 109
nuclear@55 110 music = new OggVorbisStream;
nuclear@63 111 if(music->open(datafile_path("bgtrack.ogg").c_str())) {
nuclear@56 112 music->set_volume(0.6);
nuclear@55 113 music->play(PlayMode::loop);
nuclear@55 114 } else {
nuclear@55 115 delete music;
nuclear@55 116 music = 0;
nuclear@55 117 }
nuclear@48 118 }
nuclear@48 119
nuclear@60 120 switch(cfg.rend) {
nuclear@60 121 case Config::Renderer::mrt:
nuclear@60 122 rend = new DeferredRenderer;
nuclear@60 123 if(!rend->init(xsz, ysz)) {
nuclear@60 124 fprintf(stderr, "failed to initialize deferred mrt renderer\n");
nuclear@60 125 delete rend;
nuclear@60 126 return false;
nuclear@60 127 }
nuclear@60 128 break;
nuclear@41 129
nuclear@60 130 case Config::Renderer::multipass:
nuclear@60 131 rend = new MultipassRenderer;
nuclear@41 132 if(!rend->init(xsz, ysz)) {
nuclear@60 133 fprintf(stderr, "failed to initialize multipass deferred renderer\n");
nuclear@60 134 delete rend;
nuclear@41 135 return false;
nuclear@41 136 }
nuclear@60 137 break;
nuclear@60 138
nuclear@60 139 case Config::Renderer::fwd:
nuclear@60 140 rend = new FwdRenderer;
nuclear@60 141 if(!rend->init(xsz, ysz)) {
nuclear@60 142 fprintf(stderr, "failed to initialize forward renderer\n");
nuclear@60 143 delete rend;
nuclear@60 144 return false;
nuclear@60 145 }
nuclear@60 146 break;
nuclear@60 147
nuclear@60 148 default:
nuclear@60 149 // try each in turn falling back to progressively worse renderers
nuclear@60 150 rend = new DeferredRenderer;
nuclear@60 151 if(!rend->init(xsz, ysz)) {
nuclear@60 152 printf("falling back to multipass renderer...\n");
nuclear@60 153
nuclear@60 154 delete rend;
nuclear@60 155 rend = new MultipassRenderer();
nuclear@60 156 if(!rend->init(xsz, ysz)) {
nuclear@60 157 printf("falling back to crappy renderer...\n");
nuclear@60 158
nuclear@60 159 rend = new FwdRenderer();
nuclear@60 160 if(!rend->init(xsz, ysz)) {
nuclear@60 161 fprintf(stderr, "failed to create renderer\n");
nuclear@60 162 return false;
nuclear@60 163 }
nuclear@60 164 }
nuclear@60 165 }
nuclear@15 166 }
nuclear@15 167
nuclear@26 168 if(!init_cmdcon()) {
nuclear@26 169 return false;
nuclear@26 170 }
nuclear@26 171
nuclear@46 172 psys_texture_loader(load_psys_tex, 0, 0);
nuclear@46 173
nuclear@5 174 // load a tileset
nuclear@5 175 tileset = new TileSet;
nuclear@18 176 printf("loading tileset: %s\n", cfg.tileset_file);
nuclear@63 177 if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) {
nuclear@5 178 return false;
nuclear@5 179 }
nuclear@5 180 set_active_tileset(tileset);
nuclear@5 181
nuclear@1 182 level = new Level;
nuclear@18 183 printf("loading level: %s\n", cfg.level_file);
nuclear@63 184 if(!level->load(datafile_path(cfg.level_file).c_str())) {
nuclear@5 185 return false;
nuclear@1 186 }
nuclear@1 187
nuclear@7 188 cam.input_move(0, 0.5, 0);
nuclear@7 189
nuclear@5 190 return true;
nuclear@1 191 }
nuclear@1 192
nuclear@15 193 void cleanup()
nuclear@15 194 {
nuclear@15 195 delete level;
nuclear@15 196 delete tileset;
nuclear@41 197 delete rend;
nuclear@26 198
nuclear@26 199 cleanup_cmdcon();
nuclear@51 200
nuclear@51 201 if(cfg.sound) {
nuclear@55 202 delete music;
nuclear@51 203 delete move_sound;
nuclear@51 204 destroy_audio();
nuclear@51 205 }
nuclear@15 206 }
nuclear@15 207
nuclear@7 208 void idle()
nuclear@7 209 {
nuclear@7 210 glutPostRedisplay();
nuclear@7 211 }
nuclear@7 212
nuclear@1 213 void disp()
nuclear@1 214 {
nuclear@38 215 update(get_time_msec());
nuclear@7 216
nuclear@18 217 if(cfg.stereo) {
nuclear@18 218 glDrawBuffer(GL_BACK_LEFT);
nuclear@1 219
nuclear@18 220 glMatrixMode(GL_PROJECTION);
nuclear@18 221 glLoadIdentity();
nuclear@18 222 proj_matrix(-1);
nuclear@18 223 glMatrixMode(GL_MODELVIEW);
nuclear@18 224 glLoadIdentity();
nuclear@18 225 view_matrix(-1);
nuclear@1 226
nuclear@24 227 draw();
nuclear@18 228
nuclear@18 229 glDrawBuffer(GL_BACK_RIGHT);
nuclear@18 230
nuclear@18 231 glMatrixMode(GL_PROJECTION);
nuclear@18 232 glLoadIdentity();
nuclear@18 233 proj_matrix(1);
nuclear@18 234 glMatrixMode(GL_MODELVIEW);
nuclear@18 235 glLoadIdentity();
nuclear@18 236 view_matrix(1);
nuclear@18 237
nuclear@24 238 draw();
nuclear@18 239 } else {
nuclear@18 240 glMatrixMode(GL_PROJECTION);
nuclear@18 241 glLoadIdentity();
nuclear@18 242 proj_matrix(0);
nuclear@18 243 glMatrixMode(GL_MODELVIEW);
nuclear@18 244 glLoadIdentity();
nuclear@18 245 view_matrix(0);
nuclear@18 246
nuclear@24 247 draw();
nuclear@18 248 }
nuclear@1 249
nuclear@1 250 glutSwapBuffers();
nuclear@1 251 assert(glGetError() == GL_NO_ERROR);
nuclear@7 252 }
nuclear@7 253
nuclear@24 254 void draw()
nuclear@24 255 {
nuclear@41 256 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@29 257
nuclear@41 258 rend->render(level);
nuclear@24 259
nuclear@24 260 if(show_con) {
nuclear@24 261 draw_cmdcon();
nuclear@24 262 }
nuclear@24 263 }
nuclear@24 264
nuclear@18 265 void view_matrix(int eye)
nuclear@18 266 {
nuclear@18 267 float offs = stereo_eye_sep * eye * 0.5;
nuclear@18 268 glTranslatef(-offs, 0, 0);
nuclear@18 269 cam.use_inverse();
nuclear@18 270 }
nuclear@18 271
nuclear@18 272 void proj_matrix(int eye)
nuclear@18 273 {
nuclear@18 274 static const float fov = M_PI / 4.0;
nuclear@18 275 static const float near_clip = 0.1;
nuclear@18 276 static const float far_clip = 100.0;
nuclear@18 277
nuclear@18 278 float top = near_clip * tan(fov * 0.5);
nuclear@18 279 float right = top * (float)cfg.width / (float)cfg.height;
nuclear@18 280
nuclear@18 281 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
nuclear@18 282 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
nuclear@18 283 }
nuclear@18 284
nuclear@18 285
nuclear@7 286 void update(unsigned long msec)
nuclear@7 287 {
nuclear@7 288 static unsigned long last_upd;
nuclear@7 289
nuclear@7 290 if(last_upd == 0) {
nuclear@7 291 last_upd = msec;
nuclear@7 292 }
nuclear@7 293 float dt = (float)(msec - last_upd) / 1000.0;
nuclear@7 294
nuclear@56 295 float offs = 2.0 * dt;
nuclear@7 296 float dx = 0, dy = 0;
nuclear@7 297
nuclear@7 298 // handle key input
nuclear@48 299 bool did_move = false;
nuclear@7 300 if(keystate['w'] || keystate['W']) {
nuclear@7 301 dy -= offs;
nuclear@48 302 did_move = true;
nuclear@7 303 }
nuclear@7 304 if(keystate['s'] || keystate['S']) {
nuclear@7 305 dy += offs;
nuclear@48 306 did_move = true;
nuclear@7 307 }
nuclear@7 308 if(keystate['d'] || keystate['D']) {
nuclear@7 309 dx += offs;
nuclear@48 310 did_move = true;
nuclear@7 311 }
nuclear@7 312 if(keystate['a'] || keystate['A']) {
nuclear@7 313 dx -= offs;
nuclear@48 314 did_move = true;
nuclear@7 315 }
nuclear@7 316
nuclear@7 317 cam.input_move(dx, 0, dy);
nuclear@7 318
nuclear@46 319 tileset->update_tiles(msec);
nuclear@49 320
nuclear@49 321 level->set_player_position(cam.get_position());
nuclear@38 322 level->update(msec, dt);
nuclear@38 323
nuclear@49 324 if(cfg.sound) {
nuclear@50 325 // set the listener matrix
nuclear@50 326 set_audio_listener(cam.matrix());
nuclear@50 327
nuclear@50 328 // play the walking sound if we're walking
nuclear@49 329 int cellx, celly;
nuclear@49 330 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
nuclear@48 331
nuclear@49 332 const AudioSample *move_sample;
nuclear@49 333 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
nuclear@49 334 if(move_sample != move_sound->get_sample()) {
nuclear@49 335 move_sound->stop();
nuclear@49 336 move_sound->set_sample(move_sample);
nuclear@49 337 move_sound->play();
nuclear@49 338 }
nuclear@49 339 } else {
nuclear@49 340 if(move_sound->get_sample()) {
nuclear@49 341 move_sound->stop();
nuclear@49 342 move_sound->set_sample(0);
nuclear@49 343 }
nuclear@48 344 }
nuclear@48 345 }
nuclear@48 346
nuclear@7 347 last_upd = msec;
nuclear@1 348 }
nuclear@1 349
nuclear@1 350 void reshape(int x, int y)
nuclear@1 351 {
nuclear@1 352 glViewport(0, 0, x, y);
nuclear@18 353 cfg.width = x;
nuclear@18 354 cfg.height = y;
nuclear@30 355
nuclear@41 356 rend->resize(x, y);
nuclear@1 357 }
nuclear@1 358
nuclear@18 359 static bool stereo_shift_pressed;
nuclear@18 360
nuclear@1 361 void keyb(unsigned char key, int x, int y)
nuclear@1 362 {
nuclear@1 363 switch(key) {
nuclear@1 364 case 27:
nuclear@1 365 exit(0);
nuclear@18 366
nuclear@24 367 case '`':
nuclear@24 368 show_con = true;
nuclear@24 369 glutKeyboardFunc(keyb_con);
nuclear@24 370 glutKeyboardUpFunc(0);
nuclear@24 371 glutPostRedisplay();
nuclear@24 372 break;
nuclear@24 373
nuclear@18 374 case 'z':
nuclear@18 375 stereo_shift_pressed = true;
nuclear@18 376 break;
nuclear@18 377
nuclear@48 378 case 'p':
nuclear@48 379 {
nuclear@48 380 Vector3 pos = cam.get_position();
nuclear@48 381 int cell_x, cell_y;
nuclear@48 382 level->get_cell_coords_at(pos, &cell_x, &cell_y);
nuclear@48 383 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
nuclear@48 384 cell_x, cell_y);
nuclear@48 385 }
nuclear@48 386 break;
nuclear@48 387
nuclear@48 388 case 'P':
nuclear@48 389 {
nuclear@48 390 Vector3 pos = cam.get_position();
nuclear@48 391 int cell_x, cell_y;
nuclear@48 392 level->get_cell_coords_at(pos, &cell_x, &cell_y);
nuclear@48 393 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
nuclear@48 394 printf("walk sample: %p\n", (void*)sample);
nuclear@48 395 }
nuclear@48 396 break;
nuclear@48 397
nuclear@48 398
nuclear@18 399 case '\n':
nuclear@18 400 case '\r':
nuclear@18 401 {
nuclear@18 402 static bool fullscr;
nuclear@18 403 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
nuclear@18 404 fullscr = !fullscr;
nuclear@18 405 if(fullscr) {
nuclear@18 406 glutFullScreen();
nuclear@18 407 } else {
nuclear@18 408 glutPositionWindow(20, 20);
nuclear@18 409 }
nuclear@18 410 }
nuclear@18 411 }
nuclear@18 412 break;
nuclear@18 413
nuclear@18 414 default:
nuclear@18 415 break;
nuclear@1 416 }
nuclear@7 417
nuclear@7 418 keystate[key] = true;
nuclear@7 419 }
nuclear@7 420
nuclear@7 421 void key_release(unsigned char key, int x, int y)
nuclear@7 422 {
nuclear@18 423 switch(key) {
nuclear@18 424 case 'z':
nuclear@18 425 stereo_shift_pressed = false;
nuclear@18 426 break;
nuclear@18 427
nuclear@18 428 default:
nuclear@18 429 break;
nuclear@18 430 }
nuclear@18 431
nuclear@7 432 keystate[key] = false;
nuclear@1 433 }
nuclear@1 434
nuclear@24 435 void keyb_con(unsigned char key, int x, int y)
nuclear@24 436 {
nuclear@24 437 if(key == '`' || key == 27) {
nuclear@24 438 show_con = false;
nuclear@24 439 glutKeyboardFunc(keyb);
nuclear@24 440 glutKeyboardUpFunc(key_release);
nuclear@24 441 glutPostRedisplay();
nuclear@24 442 } else {
nuclear@24 443 cmdcon_keypress(key);
nuclear@24 444 }
nuclear@24 445 }
nuclear@24 446
nuclear@1 447 static int prev_x, prev_y;
nuclear@1 448 static bool bnstate[32];
nuclear@1 449
nuclear@1 450 void mouse(int bn, int state, int x, int y)
nuclear@1 451 {
nuclear@1 452 prev_x = x;
nuclear@1 453 prev_y = y;
nuclear@1 454 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@1 455 }
nuclear@1 456
nuclear@1 457 void motion(int x, int y)
nuclear@1 458 {
nuclear@1 459 int dx = x - prev_x;
nuclear@1 460 int dy = y - prev_y;
nuclear@1 461 prev_x = x;
nuclear@1 462 prev_y = y;
nuclear@1 463
nuclear@18 464 if(stereo_shift_pressed) {
nuclear@18 465 if(dy != 0) {
nuclear@18 466 stereo_focus_dist += dy * 0.01;
nuclear@18 467 stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@18 468 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
nuclear@18 469 glutPostRedisplay();
nuclear@18 470 }
nuclear@18 471 return;
nuclear@18 472 }
nuclear@18 473
nuclear@1 474 if(bnstate[0]) {
nuclear@56 475 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
nuclear@1 476 glutPostRedisplay();
nuclear@1 477 }
nuclear@1 478 if(bnstate[2]) {
nuclear@1 479 cam.input_zoom(dy * 0.1);
nuclear@1 480 glutPostRedisplay();
nuclear@1 481 }
nuclear@1 482 }
nuclear@46 483
nuclear@46 484 unsigned int load_psys_tex(const char *fname, void *cls)
nuclear@46 485 {
nuclear@46 486 TextureSet *texset = tileset->get_textures();
nuclear@48 487 return texset->get(fname);
nuclear@46 488 }