fixed hand tracking world position, and picking up objects in VR
[laserbrain_demo] / src / main.cc
index e2e969a..4fae92d 100644 (file)
@@ -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");
@@ -74,6 +79,8 @@ int main(int argc, char **argv)
        }
        app_reshape(win_width, win_height);
 
+       SDL_RaiseWindow(win);
+
        while(!quit) {
                SDL_Event ev;
 
@@ -152,8 +159,6 @@ bool app_is_mouse_grabbed()
 
 static bool init(int argc, char **argv)
 {
-       glewInit();
-
        if(!app_init(argc, argv)) {
                return false;
        }
@@ -164,6 +169,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 +179,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 +193,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 +215,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 +237,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;
+}