X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fmain.cc;h=fb8a2e20c3798e68aef7a471102755d3eb73f1cb;hb=3aacec4aee795e703e8eb9d6852ca47a3be065ee;hp=f9c9f3a0d88dd0e87aab3f946af2403dd7f5a523;hpb=dbcb9345c23c5c027d808915962843e7db2d14aa;p=laserbrain_demo diff --git a/src/main.cc b/src/main.cc index f9c9f3a..fb8a2e2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,58 +2,91 @@ #include #include #include -#ifdef __APPLE__ -#include -#else -#include -#endif +#include #include "app.h" -static bool init(); -static void display(); -static void idle(); -static void reshape(int x, int y); -static void key_press(unsigned char key, int x, int y); -static void key_release(unsigned char key, int x, int y); -static void mouse(int bn, int st, int x, int y); +static bool init(int argc, char **argv); +static void process_event(SDL_Event *ev); static void proc_modkeys(); +static SDL_Window *win; +static SDL_GLContext ctx; +static bool fullscreen, mouse_grabbed; +static bool quit; + static unsigned int start_time; static unsigned int modkeys; +static int scale_factor = 1; + int main(int argc, char **argv) { - glutInitWindowSize(1024, 768); - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); - glutCreateWindow("demo"); - - glutDisplayFunc(display); - glutIdleFunc(idle); - glutReshapeFunc(reshape); - glutKeyboardFunc(key_press); - glutKeyboardUpFunc(key_release); - glutMouseFunc(mouse); - glutMotionFunc(app_mouse_motion); - glutPassiveMotionFunc(app_mouse_motion); - - if(!init()) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { + fprintf(stderr, "failed to initialize SDL\n"); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); + + int defpos = SDL_WINDOWPOS_UNDEFINED; + unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; + + if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) { + // try again without sRGB capability + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0); + if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) { + fprintf(stderr, "failed to create window\n"); + SDL_Quit(); + return 1; + } + fprintf(stderr, "failed to get an sRGB framebuffer.\n"); + } + int val; + SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val); + printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get"); + + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + SDL_Quit(); + return 1; + } + SDL_GL_GetDrawableSize(win, &win_width, &win_height); + win_aspect = (float)win_width / (float)win_height; + + if(!init(argc, argv)) { + SDL_Quit(); return 1; } - atexit(app_cleanup); + app_reshape(win_width, win_height); - glutMainLoop(); + while(!quit) { + SDL_Event ev; + + time_msec = SDL_GetTicks() - start_time; + while(SDL_PollEvent(&ev)) { + process_event(&ev); + if(quit) goto break_evloop; + } + + app_display(); + } +break_evloop: + + app_cleanup(); + SDL_Quit(); return 0; } void app_swap_buffers() { - glutSwapBuffers(); + SDL_GL_SwapWindow(win); } void app_quit() { - exit(0); + quit = true; } unsigned int app_get_modifiers() @@ -61,70 +94,112 @@ unsigned int app_get_modifiers() return modkeys; } -static bool init() +void app_resize(int x, int y) { - glewInit(); - - if(!app_init()) { - return false; - } - - start_time = glutGet(GLUT_ELAPSED_TIME); - return true; + SDL_SetWindowSize(win, x, y); } -static void display() +void app_fullscreen(bool fs) { - time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time; - app_display(); + SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); + fullscreen = fs; } -static void idle() +void app_toggle_fullscreen() { - glutPostRedisplay(); + app_fullscreen(!fullscreen); } -static void reshape(int x, int y) +bool app_is_fullscreen() { - win_width = x; - win_height = y; + return fullscreen; +} - app_reshape(x, y); +void app_grab_mouse(bool grab) +{ + if(grab) { + SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2); + } + //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE); + //SDL_ShowCursor(grab ? 1 : 0); + SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE); + mouse_grabbed = grab; } -static void key_press(unsigned char key, int x, int y) +void app_toggle_grab_mouse() { - proc_modkeys(); - app_keyboard(key, true); + app_grab_mouse(!mouse_grabbed); } -static void key_release(unsigned char key, int x, int y) +bool app_is_mouse_grabbed() { - proc_modkeys(); - app_keyboard(key, false); + return mouse_grabbed; } -static void mouse(int bn, int st, int x, int y) + +static bool init(int argc, char **argv) { - int bidx = bn - GLUT_LEFT_BUTTON; - bool down = st == GLUT_DOWN; + glewInit(); - proc_modkeys(); - app_mouse_button(bidx, down, x, y); + if(!app_init(argc, argv)) { + return false; + } + + start_time = SDL_GetTicks(); + return true; } -static void proc_modkeys() +static void process_event(SDL_Event *ev) { - int glutmod = glutGetModifiers(); + switch(ev->type) { + case SDL_QUIT: + quit = true; + break; + + case SDL_KEYDOWN: + case SDL_KEYUP: + proc_modkeys(); + app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + proc_modkeys(); + app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, + ev->button.x * scale_factor, ev->button.y * scale_factor); + break; + + case SDL_MOUSEMOTION: + if(mouse_grabbed) { + // XXX xrel/yrel seems faster by default + app_mouse_delta(ev->motion.xrel * 0.5, ev->motion.yrel * 0.5); + } else { + app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor); + } + break; + + case SDL_WINDOWEVENT: + if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { + SDL_GL_GetDrawableSize(win, &win_width, &win_height); + win_aspect = (float)win_width / (float)win_height; + scale_factor = win_width / ev->window.data1; + app_reshape(win_width, win_height); + } + break; + } +} +static void proc_modkeys() +{ modkeys = 0; - if(glutmod & GLUT_ACTIVE_SHIFT) { + SDL_Keymod sdlmod = SDL_GetModState(); + if(sdlmod & KMOD_SHIFT) { modkeys |= MOD_SHIFT; } - if(glutmod & GLUT_ACTIVE_CTRL) { - modkeys |= MOD_CTRL; - } - if(glutmod & GLUT_ACTIVE_ALT) { + if(sdlmod & KMOD_ALT) { modkeys |= MOD_ALT; } + if(sdlmod & KMOD_CTRL) { + modkeys |= MOD_CTRL; + } }