nuclear@0: #include nuclear@0: #include nuclear@1: #include nuclear@0: #include "opengl.h" nuclear@0: #include "game.h" nuclear@0: #include "opt.h" nuclear@0: nuclear@0: static void disp(); nuclear@1: static void idle(); nuclear@0: static void reshape(int x, int y); nuclear@0: static void keydown(unsigned char key, int x, int y); nuclear@0: static void keyup(unsigned char key, int x, int y); nuclear@0: static void skeydown(int key, int x, int y); nuclear@0: static void skeyup(int key, int x, int y); nuclear@0: static void mouse(int bn, int state, int x, int y); nuclear@0: static void motion(int x, int y); nuclear@0: static void spacemove(int x, int y, int z); nuclear@0: static void spacerot(int x, int y, int z); nuclear@0: static void spacebut(int bn, int state); nuclear@0: nuclear@1: static int win_xsz, win_ysz, centerx, centery; nuclear@1: static unsigned int prev_msec; nuclear@1: nuclear@0: int main(int argc, char **argv) nuclear@0: { nuclear@0: glutInit(&argc, argv); nuclear@0: nuclear@0: if(!parse_opt(argc, argv)) { nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); nuclear@1: glutInitWindowSize(opt.xsz, opt.ysz); nuclear@0: glutCreateWindow("bloboland"); nuclear@0: nuclear@0: glutDisplayFunc(disp); nuclear@1: glutIdleFunc(idle); nuclear@0: glutReshapeFunc(reshape); nuclear@0: glutKeyboardFunc(keydown); nuclear@0: glutKeyboardUpFunc(keyup); nuclear@0: glutSpecialFunc(skeydown); nuclear@0: glutSpecialUpFunc(skeyup); nuclear@0: glutMouseFunc(mouse); nuclear@0: glutMotionFunc(motion); nuclear@0: glutSpaceballMotionFunc(spacemove); nuclear@0: glutSpaceballRotateFunc(spacerot); nuclear@0: glutSpaceballButtonFunc(spacebut); nuclear@0: nuclear@0: glewInit(); nuclear@0: nuclear@0: if(!game_init()) { nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: glutMainLoop(); nuclear@0: } nuclear@0: nuclear@0: static void disp() nuclear@0: { nuclear@0: unsigned int msec = glutGet(GLUT_ELAPSED_TIME); nuclear@0: game_iter((msec - prev_msec) / 1000.0); nuclear@0: nuclear@0: nuclear@1: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@0: nuclear@0: game_render(); nuclear@0: nuclear@0: glutSwapBuffers(); nuclear@1: assert(glGetError() == GL_NO_ERROR); nuclear@1: } nuclear@1: nuclear@1: static void idle() nuclear@1: { nuclear@1: glutPostRedisplay(); nuclear@0: } nuclear@0: nuclear@0: static void reshape(int x, int y) nuclear@0: { nuclear@0: glViewport(0, 0, x, y); nuclear@0: nuclear@0: glMatrixMode(GL_PROJECTION); nuclear@0: glLoadIdentity(); nuclear@0: gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); nuclear@1: nuclear@1: win_xsz = x; nuclear@1: win_ysz = y; nuclear@1: nuclear@1: centerx = x / 2; nuclear@1: centery = y / 2; nuclear@0: } nuclear@0: nuclear@0: static void keydown(unsigned char key, int x, int y) nuclear@0: { nuclear@0: skeydown(key, x, y); nuclear@0: } nuclear@0: nuclear@0: static void keyup(unsigned char key, int x, int y) nuclear@0: { nuclear@0: skeyup(key, x, y); nuclear@0: } nuclear@0: nuclear@0: static void skeydown(int key, int x, int y) nuclear@0: { nuclear@0: if(key == 27) { nuclear@0: exit(0); nuclear@0: } nuclear@0: nuclear@1: if(key < GAME_MAX_KEYS) { nuclear@0: keystate[key] = true; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: static void skeyup(int key, int x, int y) nuclear@0: { nuclear@1: if(key < GAME_MAX_KEYS) { nuclear@0: keystate[key] = false; nuclear@0: } nuclear@0: } nuclear@0: nuclear@1: nuclear@1: static int prev_x, prev_y; nuclear@1: nuclear@0: static void mouse(int bn, int state, int x, int y) nuclear@0: { nuclear@1: int idx = bn - GLUT_LEFT_BUTTON; nuclear@1: nuclear@1: if(idx < GAME_MAX_BUTTONS) { nuclear@1: bnstate[idx] = state == GLUT_DOWN; nuclear@1: nuclear@1: if(state == GLUT_DOWN) { nuclear@1: game_input_shoot(idx); nuclear@1: } nuclear@1: } nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@0: } nuclear@0: nuclear@1: nuclear@0: static void motion(int x, int y) nuclear@0: { nuclear@1: /* nuclear@1: int dx = x - centerx; nuclear@1: int dy = y - centery; nuclear@1: nuclear@1: if(!dx && !dy) { nuclear@1: return; nuclear@1: } nuclear@1: game_input_rot(dx * 0.5, dy * 0.5); nuclear@1: nuclear@1: glutWarpPointer(centerx, centery); nuclear@1: */ nuclear@1: int dx = x - prev_x; nuclear@1: int dy = y - prev_y; nuclear@1: nuclear@1: prev_x = x; nuclear@1: prev_y = y; nuclear@1: nuclear@1: if(bnstate[0]) { nuclear@1: game_input_rot((float)dx / win_xsz, (float)dy / win_ysz); nuclear@1: } nuclear@0: } nuclear@0: nuclear@0: static void spacemove(int x, int y, int z) nuclear@0: { nuclear@1: game_input_move(x * 0.1, y * 0.1, z * 0.1); nuclear@0: } nuclear@0: nuclear@0: static void spacerot(int x, int y, int z) nuclear@0: { nuclear@1: game_input_rot(y * 0.1, x * 0.1); nuclear@0: } nuclear@0: nuclear@0: static void spacebut(int bn, int state) nuclear@0: { nuclear@1: game_input_shoot(bn); nuclear@0: } nuclear@0: