test/testgamecontroller.c
changeset 6914 8a9915ab24f0
parent 6771 55337ff4256f
child 6916 92e978d6dafa
equal deleted inserted replaced
6913:7e9c4d29e668 6914:8a9915ab24f0
    38 {
    38 {
    39     const SDL_Rect area = { x, y, w, h };
    39     const SDL_Rect area = { x, y, w, h };
    40     SDL_RenderFillRect(r, &area);
    40     SDL_RenderFillRect(r, &area);
    41 }
    41 }
    42 
    42 
       
    43 static const char *
       
    44 ControllerAxisName(const SDL_CONTROLLER_AXIS axis)
       
    45 {
       
    46     switch (axis)
       
    47     {
       
    48         #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
       
    49         AXIS_CASE(INVALID);
       
    50         AXIS_CASE(LEFTX);
       
    51         AXIS_CASE(LEFTY);
       
    52         AXIS_CASE(RIGHTX);
       
    53         AXIS_CASE(RIGHTY);
       
    54         AXIS_CASE(TRIGGERLEFT);
       
    55         AXIS_CASE(TRIGGERRIGHT);
       
    56         #undef AXIS_CASE
       
    57         default: return "???";
       
    58     }
       
    59 }
       
    60 
       
    61 static const char *
       
    62 ControllerButtonName(const SDL_CONTROLLER_BUTTON button)
       
    63 {
       
    64     switch (button)
       
    65     {
       
    66         #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
       
    67         BUTTON_CASE(INVALID);
       
    68         BUTTON_CASE(A);
       
    69         BUTTON_CASE(B);
       
    70         BUTTON_CASE(X);
       
    71         BUTTON_CASE(Y);
       
    72         BUTTON_CASE(BACK);
       
    73         BUTTON_CASE(GUIDE);
       
    74         BUTTON_CASE(START);
       
    75         BUTTON_CASE(LEFTSTICK);
       
    76         BUTTON_CASE(RIGHTSTICK);
       
    77         BUTTON_CASE(LEFTSHOULDER);
       
    78         BUTTON_CASE(RIGHTSHOULDER);
       
    79         BUTTON_CASE(DPAD_UP);
       
    80         BUTTON_CASE(DPAD_DOWN);
       
    81         BUTTON_CASE(DPAD_LEFT);
       
    82         BUTTON_CASE(DPAD_RIGHT);
       
    83         #undef BUTTON_CASE
       
    84         default: return "???";
       
    85     }
       
    86 }
       
    87 
    43 void
    88 void
    44 WatchGameController(SDL_GameController * gamecontroller)
    89 WatchGameController(SDL_GameController * gamecontroller)
    45 {
    90 {
       
    91     const char *controllername = gamecontroller ? SDL_GameControllerName(gamecontroller) : "???";
       
    92     const char *basetitle = "Game Controller Test: ";
       
    93     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(controllername) + 1;
       
    94     char *title = SDL_malloc(titlelen);
    46     SDL_Window *window = NULL;
    95     SDL_Window *window = NULL;
    47     SDL_Renderer *screen = NULL;
    96     SDL_Renderer *screen = NULL;
    48     const char *name = NULL;
    97     const char *name = NULL;
    49     int done = 0;
    98     int done = 0;
    50     SDL_Event event;
    99     SDL_Event event;
    51     int i;
   100     int i;
    52 
   101 
       
   102     if (title) {
       
   103         SDL_snprintf(title, titlelen, "%s%s", basetitle, controllername);
       
   104     }
       
   105 
    53     /* Create a window to display controller axis position */
   106     /* Create a window to display controller axis position */
    54     window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
   107     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
    55                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   108                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    56                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
   109                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    57     if (window == NULL) {
   110     if (window == NULL) {
    58         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   111         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    59         return;
   112         return;
    82         SDL_RenderClear(screen);
   135         SDL_RenderClear(screen);
    83 
   136 
    84         while (SDL_PollEvent(&event)) {
   137         while (SDL_PollEvent(&event)) {
    85             switch (event.type) {
   138             switch (event.type) {
    86             case SDL_CONTROLLERAXISMOTION:
   139             case SDL_CONTROLLERAXISMOTION:
    87                 printf("Controller %d axis %d value: %d\n",
   140                 printf("Controller %d axis %d ('%s') value: %d\n",
    88                        event.caxis.which,
   141                        event.caxis.which,
    89                        event.caxis.axis, event.caxis.value);
   142                        event.caxis.axis,
       
   143                        ControllerAxisName(event.caxis.axis),
       
   144                        event.caxis.value);
    90                 break;
   145                 break;
    91             case SDL_CONTROLLERBUTTONDOWN:
   146             case SDL_CONTROLLERBUTTONDOWN:
    92                 printf("Controller %d button %d down\n",
   147                 printf("Controller %d button %d ('%s') down\n",
    93                        event.cbutton.which, event.cbutton.button);
   148                        event.cbutton.which, event.cbutton.button,
       
   149                        ControllerButtonName(event.cbutton.button));
    94                 break;
   150                 break;
    95             case SDL_CONTROLLERBUTTONUP:
   151             case SDL_CONTROLLERBUTTONUP:
    96                 printf("Controller %d button %d up\n",
   152                 printf("Controller %d button %d ('%s') up\n",
    97                        event.cbutton.which, event.cbutton.button);
   153                        event.cbutton.which, event.cbutton.button,
       
   154                        ControllerButtonName(event.cbutton.button));
    98                 break;
   155                 break;
    99             case SDL_KEYDOWN:
   156             case SDL_KEYDOWN:
   100                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   157                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   101                     break;
   158                     break;
   102                 }
   159                 }
   103                 /* Fall through to signal quit */
   160                 /* Fall through to signal quit */
   104             case SDL_QUIT:
   161             case SDL_QUIT:
   105                 done = 1;
   162                 done = 1;
   106 				s_ForceQuit = SDL_TRUE;
   163                 s_ForceQuit = SDL_TRUE;
   107                 break;
   164                 break;
   108             default:
   165             default:
   109                 break;
   166                 break;
   110             }
   167             }
   111         }
   168         }