dungeon_crawler

annotate prototype/src/main.cc @ 74:5981917093ff

color grading palette output done, all is left is the input
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 21 Oct 2012 15:56:47 +0300
parents a27528035e20
children b05ab29cd17d
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@74 46 static bool save_grade_shot;
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::fwd:
nuclear@60 131 rend = new FwdRenderer;
nuclear@60 132 if(!rend->init(xsz, ysz)) {
nuclear@60 133 fprintf(stderr, "failed to initialize forward renderer\n");
nuclear@60 134 delete rend;
nuclear@60 135 return false;
nuclear@60 136 }
nuclear@60 137 break;
nuclear@60 138
nuclear@60 139 default:
nuclear@60 140 // try each in turn falling back to progressively worse renderers
nuclear@60 141 rend = new DeferredRenderer;
nuclear@60 142 if(!rend->init(xsz, ysz)) {
nuclear@60 143 printf("falling back to multipass renderer...\n");
nuclear@60 144
nuclear@60 145 delete rend;
nuclear@72 146 rend = new FwdRenderer();
nuclear@60 147 if(!rend->init(xsz, ysz)) {
nuclear@72 148 fprintf(stderr, "failed to create renderer\n");
nuclear@72 149 return false;
nuclear@60 150 }
nuclear@60 151 }
nuclear@15 152 }
nuclear@15 153
nuclear@26 154 if(!init_cmdcon()) {
nuclear@26 155 return false;
nuclear@26 156 }
nuclear@26 157
nuclear@46 158 psys_texture_loader(load_psys_tex, 0, 0);
nuclear@46 159
nuclear@5 160 // load a tileset
nuclear@5 161 tileset = new TileSet;
nuclear@18 162 printf("loading tileset: %s\n", cfg.tileset_file);
nuclear@63 163 if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) {
nuclear@5 164 return false;
nuclear@5 165 }
nuclear@5 166 set_active_tileset(tileset);
nuclear@5 167
nuclear@1 168 level = new Level;
nuclear@18 169 printf("loading level: %s\n", cfg.level_file);
nuclear@63 170 if(!level->load(datafile_path(cfg.level_file).c_str())) {
nuclear@5 171 return false;
nuclear@1 172 }
nuclear@1 173
nuclear@7 174 cam.input_move(0, 0.5, 0);
nuclear@7 175
nuclear@5 176 return true;
nuclear@1 177 }
nuclear@1 178
nuclear@15 179 void cleanup()
nuclear@15 180 {
nuclear@15 181 delete level;
nuclear@15 182 delete tileset;
nuclear@41 183 delete rend;
nuclear@26 184
nuclear@26 185 cleanup_cmdcon();
nuclear@51 186
nuclear@51 187 if(cfg.sound) {
nuclear@55 188 delete music;
nuclear@51 189 delete move_sound;
nuclear@51 190 destroy_audio();
nuclear@51 191 }
nuclear@15 192 }
nuclear@15 193
nuclear@7 194 void idle()
nuclear@7 195 {
nuclear@7 196 glutPostRedisplay();
nuclear@7 197 }
nuclear@7 198
nuclear@1 199 void disp()
nuclear@1 200 {
nuclear@38 201 update(get_time_msec());
nuclear@7 202
nuclear@18 203 if(cfg.stereo) {
nuclear@18 204 glDrawBuffer(GL_BACK_LEFT);
nuclear@1 205
nuclear@18 206 glMatrixMode(GL_PROJECTION);
nuclear@18 207 glLoadIdentity();
nuclear@18 208 proj_matrix(-1);
nuclear@18 209 glMatrixMode(GL_MODELVIEW);
nuclear@18 210 glLoadIdentity();
nuclear@18 211 view_matrix(-1);
nuclear@1 212
nuclear@24 213 draw();
nuclear@18 214
nuclear@18 215 glDrawBuffer(GL_BACK_RIGHT);
nuclear@18 216
nuclear@18 217 glMatrixMode(GL_PROJECTION);
nuclear@18 218 glLoadIdentity();
nuclear@18 219 proj_matrix(1);
nuclear@18 220 glMatrixMode(GL_MODELVIEW);
nuclear@18 221 glLoadIdentity();
nuclear@18 222 view_matrix(1);
nuclear@18 223
nuclear@24 224 draw();
nuclear@18 225 } else {
nuclear@18 226 glMatrixMode(GL_PROJECTION);
nuclear@18 227 glLoadIdentity();
nuclear@18 228 proj_matrix(0);
nuclear@18 229 glMatrixMode(GL_MODELVIEW);
nuclear@18 230 glLoadIdentity();
nuclear@18 231 view_matrix(0);
nuclear@18 232
nuclear@24 233 draw();
nuclear@18 234 }
nuclear@1 235
nuclear@1 236 glutSwapBuffers();
nuclear@1 237 assert(glGetError() == GL_NO_ERROR);
nuclear@7 238 }
nuclear@7 239
nuclear@24 240 void draw()
nuclear@24 241 {
nuclear@41 242 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@29 243
nuclear@41 244 rend->render(level);
nuclear@24 245
nuclear@24 246 if(show_con) {
nuclear@24 247 draw_cmdcon();
nuclear@24 248 }
nuclear@74 249
nuclear@74 250 if(save_grade_shot) {
nuclear@74 251 GradePalette *pal = rend->get_grade_palette();
nuclear@74 252 if(!pal->save_shot("gradeshot.ppm")) {
nuclear@74 253 fprintf(stderr, "failed to save color grading shot\n");
nuclear@74 254 }
nuclear@74 255 save_grade_shot = false;
nuclear@74 256 }
nuclear@24 257 }
nuclear@24 258
nuclear@18 259 void view_matrix(int eye)
nuclear@18 260 {
nuclear@18 261 float offs = stereo_eye_sep * eye * 0.5;
nuclear@18 262 glTranslatef(-offs, 0, 0);
nuclear@18 263 cam.use_inverse();
nuclear@18 264 }
nuclear@18 265
nuclear@18 266 void proj_matrix(int eye)
nuclear@18 267 {
nuclear@18 268 static const float fov = M_PI / 4.0;
nuclear@18 269 static const float near_clip = 0.1;
nuclear@18 270 static const float far_clip = 100.0;
nuclear@18 271
nuclear@18 272 float top = near_clip * tan(fov * 0.5);
nuclear@18 273 float right = top * (float)cfg.width / (float)cfg.height;
nuclear@18 274
nuclear@18 275 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
nuclear@18 276 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
nuclear@18 277 }
nuclear@18 278
nuclear@18 279
nuclear@7 280 void update(unsigned long msec)
nuclear@7 281 {
nuclear@7 282 static unsigned long last_upd;
nuclear@7 283
nuclear@7 284 if(last_upd == 0) {
nuclear@7 285 last_upd = msec;
nuclear@7 286 }
nuclear@7 287 float dt = (float)(msec - last_upd) / 1000.0;
nuclear@7 288
nuclear@56 289 float offs = 2.0 * dt;
nuclear@7 290 float dx = 0, dy = 0;
nuclear@7 291
nuclear@7 292 // handle key input
nuclear@48 293 bool did_move = false;
nuclear@7 294 if(keystate['w'] || keystate['W']) {
nuclear@7 295 dy -= offs;
nuclear@48 296 did_move = true;
nuclear@7 297 }
nuclear@7 298 if(keystate['s'] || keystate['S']) {
nuclear@7 299 dy += offs;
nuclear@48 300 did_move = true;
nuclear@7 301 }
nuclear@7 302 if(keystate['d'] || keystate['D']) {
nuclear@7 303 dx += offs;
nuclear@48 304 did_move = true;
nuclear@7 305 }
nuclear@7 306 if(keystate['a'] || keystate['A']) {
nuclear@7 307 dx -= offs;
nuclear@48 308 did_move = true;
nuclear@7 309 }
nuclear@7 310
nuclear@7 311 cam.input_move(dx, 0, dy);
nuclear@7 312
nuclear@46 313 tileset->update_tiles(msec);
nuclear@49 314
nuclear@49 315 level->set_player_position(cam.get_position());
nuclear@38 316 level->update(msec, dt);
nuclear@38 317
nuclear@49 318 if(cfg.sound) {
nuclear@50 319 // set the listener matrix
nuclear@50 320 set_audio_listener(cam.matrix());
nuclear@50 321
nuclear@50 322 // play the walking sound if we're walking
nuclear@49 323 int cellx, celly;
nuclear@49 324 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
nuclear@48 325
nuclear@49 326 const AudioSample *move_sample;
nuclear@49 327 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
nuclear@49 328 if(move_sample != move_sound->get_sample()) {
nuclear@49 329 move_sound->stop();
nuclear@49 330 move_sound->set_sample(move_sample);
nuclear@49 331 move_sound->play();
nuclear@49 332 }
nuclear@49 333 } else {
nuclear@49 334 if(move_sound->get_sample()) {
nuclear@49 335 move_sound->stop();
nuclear@49 336 move_sound->set_sample(0);
nuclear@49 337 }
nuclear@48 338 }
nuclear@48 339 }
nuclear@48 340
nuclear@7 341 last_upd = msec;
nuclear@1 342 }
nuclear@1 343
nuclear@1 344 void reshape(int x, int y)
nuclear@1 345 {
nuclear@1 346 glViewport(0, 0, x, y);
nuclear@18 347 cfg.width = x;
nuclear@18 348 cfg.height = y;
nuclear@30 349
nuclear@41 350 rend->resize(x, y);
nuclear@1 351 }
nuclear@1 352
nuclear@18 353 static bool stereo_shift_pressed;
nuclear@18 354
nuclear@1 355 void keyb(unsigned char key, int x, int y)
nuclear@1 356 {
nuclear@1 357 switch(key) {
nuclear@1 358 case 27:
nuclear@1 359 exit(0);
nuclear@18 360
nuclear@24 361 case '`':
nuclear@24 362 show_con = true;
nuclear@24 363 glutKeyboardFunc(keyb_con);
nuclear@24 364 glutKeyboardUpFunc(0);
nuclear@24 365 glutPostRedisplay();
nuclear@24 366 break;
nuclear@24 367
nuclear@18 368 case 'z':
nuclear@18 369 stereo_shift_pressed = true;
nuclear@18 370 break;
nuclear@18 371
nuclear@48 372 case 'p':
nuclear@48 373 {
nuclear@48 374 Vector3 pos = cam.get_position();
nuclear@48 375 int cell_x, cell_y;
nuclear@48 376 level->get_cell_coords_at(pos, &cell_x, &cell_y);
nuclear@48 377 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
nuclear@48 378 cell_x, cell_y);
nuclear@48 379 }
nuclear@48 380 break;
nuclear@48 381
nuclear@48 382 case 'P':
nuclear@48 383 {
nuclear@48 384 Vector3 pos = cam.get_position();
nuclear@48 385 int cell_x, cell_y;
nuclear@48 386 level->get_cell_coords_at(pos, &cell_x, &cell_y);
nuclear@48 387 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
nuclear@48 388 printf("walk sample: %p\n", (void*)sample);
nuclear@48 389 }
nuclear@48 390 break;
nuclear@48 391
nuclear@48 392
nuclear@18 393 case '\n':
nuclear@18 394 case '\r':
nuclear@18 395 {
nuclear@18 396 static bool fullscr;
nuclear@18 397 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
nuclear@18 398 fullscr = !fullscr;
nuclear@18 399 if(fullscr) {
nuclear@18 400 glutFullScreen();
nuclear@18 401 } else {
nuclear@18 402 glutPositionWindow(20, 20);
nuclear@18 403 }
nuclear@18 404 }
nuclear@18 405 }
nuclear@18 406 break;
nuclear@18 407
nuclear@74 408 case 'g':
nuclear@74 409 save_grade_shot = true;
nuclear@74 410 break;
nuclear@74 411
nuclear@18 412 default:
nuclear@18 413 break;
nuclear@1 414 }
nuclear@7 415
nuclear@7 416 keystate[key] = true;
nuclear@7 417 }
nuclear@7 418
nuclear@7 419 void key_release(unsigned char key, int x, int y)
nuclear@7 420 {
nuclear@18 421 switch(key) {
nuclear@18 422 case 'z':
nuclear@18 423 stereo_shift_pressed = false;
nuclear@18 424 break;
nuclear@18 425
nuclear@18 426 default:
nuclear@18 427 break;
nuclear@18 428 }
nuclear@18 429
nuclear@7 430 keystate[key] = false;
nuclear@1 431 }
nuclear@1 432
nuclear@24 433 void keyb_con(unsigned char key, int x, int y)
nuclear@24 434 {
nuclear@24 435 if(key == '`' || key == 27) {
nuclear@24 436 show_con = false;
nuclear@24 437 glutKeyboardFunc(keyb);
nuclear@24 438 glutKeyboardUpFunc(key_release);
nuclear@24 439 glutPostRedisplay();
nuclear@24 440 } else {
nuclear@24 441 cmdcon_keypress(key);
nuclear@24 442 }
nuclear@24 443 }
nuclear@24 444
nuclear@1 445 static int prev_x, prev_y;
nuclear@1 446 static bool bnstate[32];
nuclear@1 447
nuclear@1 448 void mouse(int bn, int state, int x, int y)
nuclear@1 449 {
nuclear@1 450 prev_x = x;
nuclear@1 451 prev_y = y;
nuclear@1 452 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@1 453 }
nuclear@1 454
nuclear@1 455 void motion(int x, int y)
nuclear@1 456 {
nuclear@1 457 int dx = x - prev_x;
nuclear@1 458 int dy = y - prev_y;
nuclear@1 459 prev_x = x;
nuclear@1 460 prev_y = y;
nuclear@1 461
nuclear@18 462 if(stereo_shift_pressed) {
nuclear@18 463 if(dy != 0) {
nuclear@18 464 stereo_focus_dist += dy * 0.01;
nuclear@18 465 stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@18 466 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
nuclear@18 467 glutPostRedisplay();
nuclear@18 468 }
nuclear@18 469 return;
nuclear@18 470 }
nuclear@18 471
nuclear@1 472 if(bnstate[0]) {
nuclear@56 473 cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
nuclear@1 474 glutPostRedisplay();
nuclear@1 475 }
nuclear@1 476 if(bnstate[2]) {
nuclear@1 477 cam.input_zoom(dy * 0.1);
nuclear@1 478 glutPostRedisplay();
nuclear@1 479 }
nuclear@1 480 }
nuclear@46 481
nuclear@46 482 unsigned int load_psys_tex(const char *fname, void *cls)
nuclear@46 483 {
nuclear@46 484 TextureSet *texset = tileset->get_textures();
nuclear@48 485 return texset->get(fname);
nuclear@46 486 }