X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fmain.cc;h=e3c8a739d3f35d3963ba50372f69e67b27b35a91;hp=e2e969a6806a4b4592f1de146479fbbb09b70eca;hb=215f16e2f26cc2acc79255bab06f13452ec06ae5;hpb=9802d969be55668e4dcc10fe427b0dcdeb6302be diff --git a/src/main.cc b/src/main.cc index e2e969a..e3c8a73 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,6 +8,7 @@ static bool init(int argc, char **argv); static void process_event(SDL_Event *ev); static void proc_modkeys(); +static int translate_keysym(SDL_Keycode sym); static SDL_Window *win; static SDL_GLContext ctx; @@ -31,6 +32,9 @@ int main(int argc, char **argv) SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); +#ifndef NDEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +#endif int defpos = SDL_WINDOWPOS_UNDEFINED; unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; @@ -48,6 +52,7 @@ int main(int argc, char **argv) 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"); + fb_srgb = val; if(!(ctx = SDL_GL_CreateContext(win))) { fprintf(stderr, "failed to create OpenGL context\n"); @@ -152,8 +157,6 @@ bool app_is_mouse_grabbed() static bool init(int argc, char **argv) { - glewInit(); - if(!app_init(argc, argv)) { return false; } @@ -164,6 +167,8 @@ static bool init(int argc, char **argv) static void process_event(SDL_Event *ev) { + int key; + switch(ev->type) { case SDL_QUIT: quit = true; @@ -172,7 +177,9 @@ static void process_event(SDL_Event *ev) case SDL_KEYDOWN: case SDL_KEYUP: proc_modkeys(); - app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + if((key = translate_keysym(ev->key.keysym.sym)) != -1) { + app_keyboard(key, ev->key.state == SDL_PRESSED); + } break; case SDL_MOUSEBUTTONDOWN: @@ -184,15 +191,18 @@ static void process_event(SDL_Event *ev) case SDL_MOUSEMOTION: if(mouse_grabbed) { - // XXX xrel/yrel seems faster by default - app_mouse_delta(ev->motion.xrel * 0.75, ev->motion.yrel * 0.75); + app_mouse_delta(ev->motion.xrel, ev->motion.yrel); } else { app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor); } break; + case SDL_MOUSEWHEEL: + app_mouse_wheel(ev->wheel.y); + break; + case SDL_WINDOWEVENT: - if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { + if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { SDL_GL_GetDrawableSize(win, &win_width, &win_height); win_aspect = (float)win_width / (float)win_height; scale_factor = win_width / ev->window.data1; @@ -203,6 +213,11 @@ static void process_event(SDL_Event *ev) case SDL_CONTROLLERAXISMOTION: app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f); break; + + case SDL_CONTROLLERBUTTONDOWN: + case SDL_CONTROLLERBUTTONUP: + app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN); + break; } } @@ -220,3 +235,39 @@ static void proc_modkeys() modkeys |= MOD_CTRL; } } + +static int translate_keysym(SDL_Keycode sym) +{ + switch(sym) { + case SDLK_RETURN: + return '\n'; + case SDLK_DELETE: + return KEY_DEL; + case SDLK_LEFT: + return KEY_LEFT; + case SDLK_RIGHT: + return KEY_RIGHT; + case SDLK_UP: + return KEY_UP; + case SDLK_DOWN: + return KEY_DOWN; + case SDLK_PAGEUP: + return KEY_PGUP; + case SDLK_PAGEDOWN: + return KEY_PGDOWN; + case SDLK_HOME: + return KEY_HOME; + case SDLK_END: + return KEY_END; + default: + break; + } + + if(sym < 127) { + return sym; + } + if(sym >= SDLK_F1 && sym <= SDLK_F12) { + return KEY_F1 + sym - SDLK_F1; + } + return -1; +}