rudimentary gamepad controls
[laserbrain_demo] / src / main.cc
index 4efca60..58e01de 100644 (file)
@@ -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;
        }
@@ -55,6 +57,17 @@ int main(int argc, char **argv)
        SDL_GL_GetDrawableSize(win, &win_width, &win_height);
        win_aspect = (float)win_width / (float)win_height;
 
+       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;
@@ -186,6 +199,10 @@ static void process_event(SDL_Event *ev)
                        app_reshape(win_width, win_height);
                }
                break;
+
+       case SDL_CONTROLLERAXISMOTION:
+               app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 65535.0f);
+               break;
        }
 }