nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@7: #include nuclear@1: #include "opengl.h" nuclear@1: #include "level.h" nuclear@1: #include "camera.h" nuclear@5: #include "datapath.h" nuclear@5: #include "tileset.h" nuclear@15: #include "renderer.h" nuclear@24: #include "cmdcon.h" nuclear@18: #include "cfg.h" nuclear@1: nuclear@17: bool init(int xsz, int ysz); nuclear@5: void cleanup(); nuclear@7: void idle(); nuclear@1: void disp(); nuclear@15: void draw(); nuclear@18: void view_matrix(int eye); nuclear@18: void proj_matrix(int eye); nuclear@7: void update(unsigned long msec); nuclear@1: void reshape(int x, int y); nuclear@1: void keyb(unsigned char key, int x, int y); nuclear@7: void key_release(unsigned char key, int x, int y); nuclear@24: void keyb_con(unsigned char key, int x, int y); nuclear@1: void mouse(int bn, int state, int x, int y); nuclear@1: void motion(int x, int y); nuclear@1: nuclear@5: static TileSet *tileset; nuclear@1: static Level *level; nuclear@7: nuclear@7: static FpsCamera cam; nuclear@7: static bool keystate[256]; nuclear@1: nuclear@18: static float stereo_focus_dist = 0.25; nuclear@18: static float stereo_eye_sep = stereo_focus_dist / 30.0; nuclear@18: nuclear@24: static bool show_con; nuclear@5: nuclear@1: int main(int argc, char **argv) nuclear@1: { nuclear@1: glutInit(&argc, argv); nuclear@5: nuclear@18: if(!cfg.parse_args(argc, argv)) { nuclear@18: return 1; nuclear@5: } nuclear@5: nuclear@18: glutInitWindowSize(cfg.width, cfg.height); nuclear@18: glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0)); nuclear@18: glutCreateWindow("dungeon crawler prototype"); nuclear@1: nuclear@7: glutIdleFunc(idle); nuclear@1: glutDisplayFunc(disp); nuclear@1: glutReshapeFunc(reshape); nuclear@1: glutKeyboardFunc(keyb); nuclear@7: glutKeyboardUpFunc(key_release); nuclear@1: glutMouseFunc(mouse); nuclear@1: glutMotionFunc(motion); nuclear@1: nuclear@1: glewInit(); nuclear@1: nuclear@18: if(!init(cfg.width, cfg.height)) { nuclear@5: return 1; nuclear@5: } nuclear@5: nuclear@5: glutMainLoop(); nuclear@5: } nuclear@5: nuclear@17: bool init(int xsz, int ysz) nuclear@5: { nuclear@1: glEnable(GL_LIGHTING); nuclear@1: glEnable(GL_LIGHT0); nuclear@1: float ldir[] = {-1, 1, 2, 0}; nuclear@1: glLightfv(GL_LIGHT0, GL_POSITION, ldir); nuclear@1: glEnable(GL_NORMALIZE); nuclear@1: nuclear@1: glEnable(GL_DEPTH_TEST); nuclear@1: glEnable(GL_CULL_FACE); nuclear@1: glEnable(GL_MULTISAMPLE); nuclear@1: nuclear@25: add_data_path("."); nuclear@5: add_data_path("data"); nuclear@16: add_data_path("sdr"); nuclear@5: nuclear@17: if(!init_renderer(xsz, ysz)) { nuclear@15: return false; nuclear@15: } nuclear@15: nuclear@26: if(!init_cmdcon()) { nuclear@26: return false; nuclear@26: } nuclear@26: nuclear@5: // load a tileset nuclear@5: tileset = new TileSet; nuclear@18: printf("loading tileset: %s\n", cfg.tileset_file); nuclear@18: if(!tileset->load(datafile_path(cfg.tileset_file))) { nuclear@5: return false; nuclear@5: } nuclear@5: set_active_tileset(tileset); nuclear@5: nuclear@1: level = new Level; nuclear@18: printf("loading level: %s\n", cfg.level_file); nuclear@18: if(!level->load(datafile_path(cfg.level_file))) { nuclear@5: return false; nuclear@1: } nuclear@1: nuclear@7: cam.input_move(0, 0.5, 0); nuclear@7: nuclear@5: return true; nuclear@1: } nuclear@1: nuclear@15: void cleanup() nuclear@15: { nuclear@15: delete level; nuclear@15: delete tileset; nuclear@15: nuclear@15: destroy_renderer(); nuclear@26: nuclear@26: cleanup_cmdcon(); nuclear@15: } nuclear@15: nuclear@7: void idle() nuclear@7: { nuclear@7: glutPostRedisplay(); nuclear@7: } nuclear@7: nuclear@1: void disp() nuclear@1: { nuclear@7: update(glutGet(GLUT_ELAPSED_TIME)); nuclear@7: nuclear@18: if(cfg.stereo) { nuclear@18: glDrawBuffer(GL_BACK_LEFT); nuclear@1: nuclear@18: glMatrixMode(GL_PROJECTION); nuclear@18: glLoadIdentity(); nuclear@18: proj_matrix(-1); nuclear@18: glMatrixMode(GL_MODELVIEW); nuclear@18: glLoadIdentity(); nuclear@18: view_matrix(-1); nuclear@1: nuclear@24: draw(); nuclear@18: nuclear@18: glDrawBuffer(GL_BACK_RIGHT); nuclear@18: nuclear@18: glMatrixMode(GL_PROJECTION); nuclear@18: glLoadIdentity(); nuclear@18: proj_matrix(1); nuclear@18: glMatrixMode(GL_MODELVIEW); nuclear@18: glLoadIdentity(); nuclear@18: view_matrix(1); nuclear@18: nuclear@24: draw(); nuclear@18: } else { nuclear@18: glMatrixMode(GL_PROJECTION); nuclear@18: glLoadIdentity(); nuclear@18: proj_matrix(0); nuclear@18: glMatrixMode(GL_MODELVIEW); nuclear@18: glLoadIdentity(); nuclear@18: view_matrix(0); nuclear@18: nuclear@24: draw(); nuclear@18: } nuclear@1: nuclear@1: glutSwapBuffers(); nuclear@1: assert(glGetError() == GL_NO_ERROR); nuclear@7: nuclear@7: usleep(10000); nuclear@7: } nuclear@7: nuclear@24: void draw() nuclear@24: { nuclear@29: glClear(GL_COLOR_BUFFER_BIT); nuclear@29: nuclear@29: render_deferred(level); nuclear@24: nuclear@24: if(show_con) { nuclear@24: draw_cmdcon(); nuclear@24: } nuclear@24: } nuclear@24: nuclear@18: void view_matrix(int eye) nuclear@18: { nuclear@18: float offs = stereo_eye_sep * eye * 0.5; nuclear@18: glTranslatef(-offs, 0, 0); nuclear@18: cam.use_inverse(); nuclear@18: } nuclear@18: nuclear@18: void proj_matrix(int eye) nuclear@18: { nuclear@18: static const float fov = M_PI / 4.0; nuclear@18: static const float near_clip = 0.1; nuclear@18: static const float far_clip = 100.0; nuclear@18: nuclear@18: float top = near_clip * tan(fov * 0.5); nuclear@18: float right = top * (float)cfg.width / (float)cfg.height; nuclear@18: nuclear@18: float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist); nuclear@18: glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip); nuclear@18: } nuclear@18: nuclear@18: nuclear@7: void update(unsigned long msec) nuclear@7: { nuclear@7: static unsigned long last_upd; nuclear@7: nuclear@7: if(last_upd == 0) { nuclear@7: last_upd = msec; nuclear@7: } nuclear@7: float dt = (float)(msec - last_upd) / 1000.0; nuclear@7: nuclear@9: float offs = 2.5 * dt; nuclear@7: float dx = 0, dy = 0; nuclear@7: nuclear@7: // handle key input nuclear@7: if(keystate['w'] || keystate['W']) { nuclear@7: dy -= offs; nuclear@7: } nuclear@7: if(keystate['s'] || keystate['S']) { nuclear@7: dy += offs; nuclear@7: } nuclear@7: if(keystate['d'] || keystate['D']) { nuclear@7: dx += offs; nuclear@7: } nuclear@7: if(keystate['a'] || keystate['A']) { nuclear@7: dx -= offs; nuclear@7: } nuclear@7: nuclear@7: cam.input_move(dx, 0, dy); nuclear@7: nuclear@7: last_upd = msec; nuclear@1: } nuclear@1: nuclear@1: void reshape(int x, int y) nuclear@1: { nuclear@1: glViewport(0, 0, x, y); nuclear@18: cfg.width = x; nuclear@18: cfg.height = y; nuclear@1: } nuclear@1: nuclear@18: static bool stereo_shift_pressed; nuclear@18: nuclear@1: void keyb(unsigned char key, int x, int y) nuclear@1: { nuclear@1: switch(key) { nuclear@1: case 27: nuclear@1: exit(0); nuclear@18: nuclear@24: case '`': nuclear@24: show_con = true; nuclear@24: glutKeyboardFunc(keyb_con); nuclear@24: glutKeyboardUpFunc(0); nuclear@24: glutPostRedisplay(); nuclear@24: break; nuclear@24: nuclear@18: case 'z': nuclear@18: stereo_shift_pressed = true; nuclear@18: break; nuclear@18: nuclear@18: case '\n': nuclear@18: case '\r': nuclear@18: { nuclear@18: static bool fullscr; nuclear@18: if(glutGetModifiers() & GLUT_ACTIVE_ALT) { nuclear@18: fullscr = !fullscr; nuclear@18: if(fullscr) { nuclear@18: glutFullScreen(); nuclear@18: } else { nuclear@18: glutPositionWindow(20, 20); nuclear@18: } nuclear@18: } nuclear@18: } nuclear@18: break; nuclear@18: nuclear@18: default: nuclear@18: break; nuclear@1: } nuclear@7: nuclear@7: keystate[key] = true; nuclear@7: } nuclear@7: nuclear@7: void key_release(unsigned char key, int x, int y) nuclear@7: { nuclear@18: switch(key) { nuclear@18: case 'z': nuclear@18: stereo_shift_pressed = false; nuclear@18: break; nuclear@18: nuclear@18: default: nuclear@18: break; nuclear@18: } nuclear@18: nuclear@7: keystate[key] = false; nuclear@1: } nuclear@1: nuclear@24: void keyb_con(unsigned char key, int x, int y) nuclear@24: { nuclear@24: if(key == '`' || key == 27) { nuclear@24: show_con = false; nuclear@24: glutKeyboardFunc(keyb); nuclear@24: glutKeyboardUpFunc(key_release); nuclear@24: glutPostRedisplay(); nuclear@24: } else { nuclear@24: cmdcon_keypress(key); nuclear@24: } nuclear@24: } nuclear@24: nuclear@1: static int prev_x, prev_y; nuclear@1: static bool bnstate[32]; nuclear@1: nuclear@1: void mouse(int bn, int state, int x, int y) nuclear@1: { nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@1: bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN; nuclear@1: } nuclear@1: nuclear@1: void motion(int x, int y) nuclear@1: { nuclear@1: int dx = x - prev_x; nuclear@1: int dy = y - prev_y; nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@1: nuclear@18: if(stereo_shift_pressed) { nuclear@18: if(dy != 0) { nuclear@18: stereo_focus_dist += dy * 0.01; nuclear@18: stereo_eye_sep = stereo_focus_dist / 30.0; nuclear@18: printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep); nuclear@18: glutPostRedisplay(); nuclear@18: } nuclear@18: return; nuclear@18: } nuclear@18: nuclear@1: if(bnstate[0]) { nuclear@7: cam.input_rotate(dy * 0.01, dx * 0.01, 0); nuclear@1: glutPostRedisplay(); nuclear@1: } nuclear@1: if(bnstate[2]) { nuclear@1: cam.input_zoom(dy * 0.1); nuclear@1: glutPostRedisplay(); nuclear@1: } nuclear@1: }