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@1: nuclear@5: bool init(); nuclear@5: void cleanup(); nuclear@7: void idle(); nuclear@1: void disp(); 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@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@5: static const char *level_file = "0.level"; nuclear@5: nuclear@1: int main(int argc, char **argv) nuclear@1: { nuclear@1: glutInit(&argc, argv); nuclear@5: nuclear@5: if(argc > 1) { nuclear@5: level_file = argv[1]; nuclear@5: } nuclear@5: nuclear@1: glutInitWindowSize(800, 600); nuclear@1: glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); nuclear@1: glutCreateWindow("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@5: if(!init()) { nuclear@5: return 1; nuclear@5: } nuclear@5: nuclear@5: glutMainLoop(); nuclear@5: } nuclear@5: nuclear@5: bool init() 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@5: add_data_path("data"); nuclear@5: nuclear@5: // load a tileset nuclear@5: tileset = new TileSet; nuclear@5: if(!tileset->load(datafile_path("default.tileset"))) { nuclear@5: return false; nuclear@5: } nuclear@5: set_active_tileset(tileset); nuclear@5: nuclear@1: level = new Level; nuclear@7: printf("loading level: %s\n", level_file); nuclear@5: if(!level->load(datafile_path(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@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@1: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@1: nuclear@1: glMatrixMode(GL_MODELVIEW); nuclear@1: glLoadIdentity(); nuclear@7: cam.use_inverse(); nuclear@1: nuclear@1: level->draw(); nuclear@1: nuclear@1: glutSwapBuffers(); nuclear@1: assert(glGetError() == GL_NO_ERROR); nuclear@7: nuclear@7: usleep(10000); nuclear@7: } nuclear@7: 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@7: float offs = 2.0 * 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@1: glMatrixMode(GL_PROJECTION); nuclear@1: glLoadIdentity(); nuclear@7: gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0); nuclear@1: } nuclear@1: 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@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@7: keystate[key] = false; nuclear@1: } nuclear@1: 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@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: }