dungeon_crawler

annotate prototype/src/main.cc @ 63:7f52d6310317

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