dungeon_crawler

annotate prototype/src/main.cc @ 72:a27528035e20

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