Made testgamecontroller minimally more useful.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 24 Feb 2013 22:57:25 -0500
changeset 69148a9915ab24f0
parent 6913 7e9c4d29e668
child 6915 d58bf9cfd709
Made testgamecontroller minimally more useful.
test/testgamecontroller.c
     1.1 --- a/test/testgamecontroller.c	Sun Feb 24 22:56:35 2013 -0500
     1.2 +++ b/test/testgamecontroller.c	Sun Feb 24 22:57:25 2013 -0500
     1.3 @@ -40,9 +40,58 @@
     1.4      SDL_RenderFillRect(r, &area);
     1.5  }
     1.6  
     1.7 +static const char *
     1.8 +ControllerAxisName(const SDL_CONTROLLER_AXIS axis)
     1.9 +{
    1.10 +    switch (axis)
    1.11 +    {
    1.12 +        #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
    1.13 +        AXIS_CASE(INVALID);
    1.14 +        AXIS_CASE(LEFTX);
    1.15 +        AXIS_CASE(LEFTY);
    1.16 +        AXIS_CASE(RIGHTX);
    1.17 +        AXIS_CASE(RIGHTY);
    1.18 +        AXIS_CASE(TRIGGERLEFT);
    1.19 +        AXIS_CASE(TRIGGERRIGHT);
    1.20 +        #undef AXIS_CASE
    1.21 +        default: return "???";
    1.22 +    }
    1.23 +}
    1.24 +
    1.25 +static const char *
    1.26 +ControllerButtonName(const SDL_CONTROLLER_BUTTON button)
    1.27 +{
    1.28 +    switch (button)
    1.29 +    {
    1.30 +        #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
    1.31 +        BUTTON_CASE(INVALID);
    1.32 +        BUTTON_CASE(A);
    1.33 +        BUTTON_CASE(B);
    1.34 +        BUTTON_CASE(X);
    1.35 +        BUTTON_CASE(Y);
    1.36 +        BUTTON_CASE(BACK);
    1.37 +        BUTTON_CASE(GUIDE);
    1.38 +        BUTTON_CASE(START);
    1.39 +        BUTTON_CASE(LEFTSTICK);
    1.40 +        BUTTON_CASE(RIGHTSTICK);
    1.41 +        BUTTON_CASE(LEFTSHOULDER);
    1.42 +        BUTTON_CASE(RIGHTSHOULDER);
    1.43 +        BUTTON_CASE(DPAD_UP);
    1.44 +        BUTTON_CASE(DPAD_DOWN);
    1.45 +        BUTTON_CASE(DPAD_LEFT);
    1.46 +        BUTTON_CASE(DPAD_RIGHT);
    1.47 +        #undef BUTTON_CASE
    1.48 +        default: return "???";
    1.49 +    }
    1.50 +}
    1.51 +
    1.52  void
    1.53  WatchGameController(SDL_GameController * gamecontroller)
    1.54  {
    1.55 +    const char *controllername = gamecontroller ? SDL_GameControllerName(gamecontroller) : "???";
    1.56 +    const char *basetitle = "Game Controller Test: ";
    1.57 +    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(controllername) + 1;
    1.58 +    char *title = SDL_malloc(titlelen);
    1.59      SDL_Window *window = NULL;
    1.60      SDL_Renderer *screen = NULL;
    1.61      const char *name = NULL;
    1.62 @@ -50,8 +99,12 @@
    1.63      SDL_Event event;
    1.64      int i;
    1.65  
    1.66 +    if (title) {
    1.67 +        SDL_snprintf(title, titlelen, "%s%s", basetitle, controllername);
    1.68 +    }
    1.69 +
    1.70      /* Create a window to display controller axis position */
    1.71 -    window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
    1.72 +    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
    1.73                                SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.74                                SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    1.75      if (window == NULL) {
    1.76 @@ -84,17 +137,21 @@
    1.77          while (SDL_PollEvent(&event)) {
    1.78              switch (event.type) {
    1.79              case SDL_CONTROLLERAXISMOTION:
    1.80 -                printf("Controller %d axis %d value: %d\n",
    1.81 +                printf("Controller %d axis %d ('%s') value: %d\n",
    1.82                         event.caxis.which,
    1.83 -                       event.caxis.axis, event.caxis.value);
    1.84 +                       event.caxis.axis,
    1.85 +                       ControllerAxisName(event.caxis.axis),
    1.86 +                       event.caxis.value);
    1.87                  break;
    1.88              case SDL_CONTROLLERBUTTONDOWN:
    1.89 -                printf("Controller %d button %d down\n",
    1.90 -                       event.cbutton.which, event.cbutton.button);
    1.91 +                printf("Controller %d button %d ('%s') down\n",
    1.92 +                       event.cbutton.which, event.cbutton.button,
    1.93 +                       ControllerButtonName(event.cbutton.button));
    1.94                  break;
    1.95              case SDL_CONTROLLERBUTTONUP:
    1.96 -                printf("Controller %d button %d up\n",
    1.97 -                       event.cbutton.which, event.cbutton.button);
    1.98 +                printf("Controller %d button %d ('%s') up\n",
    1.99 +                       event.cbutton.which, event.cbutton.button,
   1.100 +                       ControllerButtonName(event.cbutton.button));
   1.101                  break;
   1.102              case SDL_KEYDOWN:
   1.103                  if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.104 @@ -103,7 +160,7 @@
   1.105                  /* Fall through to signal quit */
   1.106              case SDL_QUIT:
   1.107                  done = 1;
   1.108 -				s_ForceQuit = SDL_TRUE;
   1.109 +                s_ForceQuit = SDL_TRUE;
   1.110                  break;
   1.111              default:
   1.112                  break;