- only enable sRGB if we got an sRGB framebuffer.
[laserbrain_demo] / src / main.cc
index f21c406..d24428c 100644 (file)
@@ -5,7 +5,7 @@
 #include <SDL2/SDL.h>
 #include "app.h"
 
-static bool init();
+static bool init(int argc, char **argv);
 static void process_event(SDL_Event *ev);
 static void proc_modkeys();
 
@@ -17,11 +17,13 @@ static bool quit;
 static unsigned int start_time;
 static unsigned int modkeys;
 
+SDL_GameController *gamepad;
+
 static int scale_factor = 1;
 
 int main(int argc, char **argv)
 {
-       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
+       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
                fprintf(stderr, "failed to initialize SDL\n");
                return 1;
        }
@@ -41,7 +43,12 @@ int main(int argc, char **argv)
                        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");
+       fb_srgb = val;
 
        if(!(ctx = SDL_GL_CreateContext(win))) {
                fprintf(stderr, "failed to create OpenGL context\n");
@@ -51,7 +58,18 @@ int main(int argc, char **argv)
        SDL_GL_GetDrawableSize(win, &win_width, &win_height);
        win_aspect = (float)win_width / (float)win_height;
 
-       if(!init()) {
+       printf("detected %d joysticks\n", SDL_NumJoysticks());
+       for(int i=0; i<SDL_NumJoysticks(); i++) {
+               if(SDL_IsGameController(i)) {
+                       if(!(gamepad = SDL_GameControllerOpen(i))) {
+                               fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
+                               continue;
+                       }
+                       printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
+               }
+       }
+
+       if(!init(argc, argv)) {
                SDL_Quit();
                return 1;
        }
@@ -113,9 +131,11 @@ bool app_is_fullscreen()
 
 void app_grab_mouse(bool grab)
 {
-       /*SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
-       SDL_ShowCursor(grab ? 1 : 0);
-       */
+       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;
 }
@@ -131,11 +151,11 @@ bool app_is_mouse_grabbed()
 }
 
 
-static bool init()
+static bool init(int argc, char **argv)
 {
        glewInit();
 
-       if(!app_init()) {
+       if(!app_init(argc, argv)) {
                return false;
        }
 
@@ -172,13 +192,22 @@ static void process_event(SDL_Event *ev)
                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;
                        app_reshape(win_width, win_height);
                }
                break;
+
+       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;
        }
 }