nuclear@2: #include nuclear@2: #include nuclear@17: #include nuclear@2: #include "opengl.h" nuclear@2: #include "game.h" nuclear@13: #include "gameopt.h" nuclear@6: #include "vr/vr.h" nuclear@2: nuclear@17: #define ANYPOS SDL_WINDOWPOS_UNDEFINED nuclear@17: nuclear@2: static bool init(); nuclear@2: static void cleanup(); nuclear@17: static void handle_event(SDL_Event *ev); nuclear@2: nuclear@17: static SDL_Window *win; nuclear@17: static SDL_GLContext ctx; nuclear@12: nuclear@2: int main(int argc, char **argv) nuclear@2: { nuclear@13: if(!parse_args(argc, argv)) { nuclear@13: return 1; nuclear@13: } nuclear@13: nuclear@17: SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); nuclear@2: nuclear@17: unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; nuclear@17: if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { nuclear@17: fprintf(stderr, "failed to create window\n"); nuclear@17: return 1; nuclear@17: } nuclear@17: nuclear@17: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); nuclear@17: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); nuclear@17: nuclear@17: if(!(ctx = SDL_GL_CreateContext(win))) { nuclear@17: fprintf(stderr, "failed to create OpenGL context\n"); nuclear@17: return 1; nuclear@17: } nuclear@2: nuclear@2: if(!init()) { nuclear@2: return 1; nuclear@2: } nuclear@2: atexit(cleanup); nuclear@2: nuclear@17: int xsz, ysz; nuclear@17: SDL_GetWindowSize(win, &xsz, &ysz); nuclear@17: game_reshape(xsz, ysz); nuclear@17: nuclear@17: for(;;) { nuclear@17: SDL_Event ev; nuclear@17: nuclear@17: while(SDL_PollEvent(&ev)) { nuclear@17: handle_event(&ev); nuclear@17: } nuclear@17: nuclear@17: unsigned int msec = SDL_GetTicks(); nuclear@17: nuclear@17: game_update(msec); nuclear@17: game_render(); nuclear@17: } nuclear@17: nuclear@2: return 0; nuclear@2: } nuclear@2: nuclear@2: static bool init() nuclear@2: { nuclear@2: glewInit(); nuclear@2: nuclear@2: if(!game_init()) { nuclear@2: return false; nuclear@2: } nuclear@7: nuclear@13: if(opt.vr) { nuclear@13: int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); nuclear@13: int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); nuclear@13: if(win_xsz && win_ysz) { nuclear@17: SDL_SetWindowSize(win, win_xsz, win_ysz); nuclear@13: } nuclear@7: } nuclear@2: return true; nuclear@2: } nuclear@2: nuclear@2: static void cleanup() nuclear@2: { nuclear@2: game_cleanup(); nuclear@2: } nuclear@2: nuclear@17: static void handle_event(SDL_Event *ev) nuclear@2: { nuclear@11: static bool fullscr; nuclear@12: static int prev_xpos, prev_ypos; nuclear@11: static int prev_xsz, prev_ysz; nuclear@11: nuclear@17: switch(ev->type) { nuclear@17: case SDL_KEYDOWN: nuclear@17: case SDL_KEYUP: nuclear@17: if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { nuclear@17: fullscr = !fullscr; nuclear@17: if(fullscr) { nuclear@17: int xoffs, yoffs; nuclear@11: nuclear@17: SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); nuclear@17: SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); nuclear@11: nuclear@17: xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); nuclear@17: yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); nuclear@17: if(xoffs || yoffs) { nuclear@17: printf("repositioning: %d,%d\n", xoffs, yoffs); nuclear@17: SDL_SetWindowPosition(win, xoffs, yoffs); nuclear@17: } nuclear@17: SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); nuclear@17: } else { nuclear@17: SDL_SetWindowFullscreen(win, 0); nuclear@17: /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); nuclear@17: SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ nuclear@11: } nuclear@17: break; nuclear@11: } nuclear@17: nuclear@17: game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); nuclear@17: break; nuclear@17: nuclear@17: case SDL_MOUSEBUTTONDOWN: nuclear@17: case SDL_MOUSEBUTTONUP: nuclear@17: game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, nuclear@17: ev->button.x, ev->button.y); nuclear@17: break; nuclear@17: nuclear@17: case SDL_MOUSEWHEEL: nuclear@17: game_mwheel(ev->wheel.y); nuclear@17: break; nuclear@17: nuclear@17: case SDL_MOUSEMOTION: nuclear@17: game_motion(ev->motion.x, ev->motion.y); nuclear@17: break; nuclear@17: nuclear@17: case SDL_WINDOWEVENT: nuclear@17: switch(ev->window.event) { nuclear@17: case SDL_WINDOWEVENT_RESIZED: nuclear@17: game_reshape(ev->window.data1, ev->window.data2); nuclear@17: break; nuclear@17: nuclear@17: default: nuclear@17: break; nuclear@17: } nuclear@17: nuclear@17: default: nuclear@11: break; nuclear@11: } nuclear@2: }