test/testgamecontroller.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 20 Jul 2013 21:47:16 +0200
changeset 7478 0d02f6a323f5
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Removed not needed SDL_WINDOW_SHOWN from test programs.
See bug #1667.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program to test the SDL game controller routines */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 
    19 #include "SDL.h"
    20 
    21 #ifndef SDL_JOYSTICK_DISABLED
    22 
    23 #ifdef __IPHONEOS__
    24 #define SCREEN_WIDTH    320
    25 #define SCREEN_HEIGHT    480
    26 #else
    27 #define SCREEN_WIDTH    640
    28 #define SCREEN_HEIGHT    480
    29 #endif
    30 
    31 #define MAX_NUM_AXES 6
    32 #define MAX_NUM_HATS 2
    33 
    34 static SDL_bool s_ForceQuit = SDL_FALSE;
    35 
    36 static void
    37 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    38 {
    39     const SDL_Rect area = { x, y, w, h };
    40     SDL_RenderFillRect(r, &area);
    41 }
    42 
    43 static const char *
    44 ControllerAxisName(const SDL_GameControllerAxis 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_GameControllerButton 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 
    88 void
    89 WatchGameController(SDL_GameController * gamecontroller)
    90 {
    91     const char *name = SDL_GameControllerName(gamecontroller);
    92     const char *basetitle = "Game Controller Test: ";
    93     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    94     char *title = SDL_malloc(titlelen);
    95     SDL_Window *window = NULL;
    96     SDL_Renderer *screen = NULL;
    97     int done = 0;
    98     SDL_Event event;
    99     int i;
   100 
   101     if (title) {
   102         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   103     }
   104 
   105     /* Create a window to display controller axis position */
   106     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   107                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   108                               SCREEN_HEIGHT, 0);
   109     if (window == NULL) {
   110         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
   111         return;
   112     }
   113 
   114     screen = SDL_CreateRenderer(window, -1, 0);
   115     if (screen == NULL) {
   116         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   117         SDL_DestroyWindow(window);
   118         return;
   119     }
   120 
   121     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   122     SDL_RenderClear(screen);
   123     SDL_RenderPresent(screen);
   124     SDL_RaiseWindow(window);
   125 
   126     /* Print info about the controller we are watching */
   127     printf("Watching controller %s\n",  name ? name : "Unknown Controller");
   128 
   129     /* Loop, getting controller events! */
   130     while (!done) {
   131         /* blank screen, set up for drawing this frame. */
   132         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   133         SDL_RenderClear(screen);
   134 
   135         while (SDL_PollEvent(&event)) {
   136             switch (event.type) {
   137             case SDL_CONTROLLERAXISMOTION:
   138                 printf("Controller %d axis %d ('%s') value: %d\n",
   139                        event.caxis.which,
   140                        event.caxis.axis,
   141                        ControllerAxisName(event.caxis.axis),
   142                        event.caxis.value);
   143                 break;
   144             case SDL_CONTROLLERBUTTONDOWN:
   145                 printf("Controller %d button %d ('%s') down\n",
   146                        event.cbutton.which, event.cbutton.button,
   147                        ControllerButtonName(event.cbutton.button));
   148                 break;
   149             case SDL_CONTROLLERBUTTONUP:
   150                 printf("Controller %d button %d ('%s') up\n",
   151                        event.cbutton.which, event.cbutton.button,
   152                        ControllerButtonName(event.cbutton.button));
   153                 break;
   154             case SDL_KEYDOWN:
   155                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   156                     break;
   157                 }
   158                 /* Fall through to signal quit */
   159             case SDL_QUIT:
   160                 done = 1;
   161                 s_ForceQuit = SDL_TRUE;
   162                 break;
   163             default:
   164                 break;
   165             }
   166         }
   167         /* Update visual controller state */
   168         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   169         for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   170             if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
   171                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   172             }
   173         }
   174 
   175         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   176         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   177             /* Draw the X/Y axis */
   178             int x, y;
   179             x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
   180             x *= SCREEN_WIDTH;
   181             x /= 65535;
   182             if (x < 0) {
   183                 x = 0;
   184             } else if (x > (SCREEN_WIDTH - 16)) {
   185                 x = SCREEN_WIDTH - 16;
   186             }
   187             y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
   188             y *= SCREEN_HEIGHT;
   189             y /= 65535;
   190             if (y < 0) {
   191                 y = 0;
   192             } else if (y > (SCREEN_HEIGHT - 16)) {
   193                 y = SCREEN_HEIGHT - 16;
   194             }
   195 
   196             DrawRect(screen, x, y, 16, 16);
   197         }
   198 
   199         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   200 
   201         SDL_RenderPresent(screen);
   202 
   203         if ( !done )
   204             done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   205     }
   206 
   207     SDL_DestroyRenderer(screen);
   208     SDL_DestroyWindow(window);
   209 }
   210 
   211 int
   212 main(int argc, char *argv[])
   213 {
   214     int i;
   215     int nController = 0;
   216     int retcode = 0;
   217     char guid[64];
   218     SDL_GameController *gamecontroller;
   219 
   220     /* Initialize SDL (Note: video is required to start event loop) */
   221     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   222         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   223         return 1;
   224     }
   225 
   226     /* Print information about the controller */
   227     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   228         const char *name;
   229         const char *description = "Joystick (not recognized as game controller)";
   230 
   231         SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
   232                                   guid, sizeof (guid));
   233 
   234         if ( SDL_IsGameController(i) )
   235         {
   236             nController++;
   237             name = SDL_GameControllerNameForIndex(i);
   238         } else {
   239             name = SDL_JoystickNameForIndex(i);
   240         }
   241         printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   242     }
   243     printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   244 
   245     if (argv[1]) {
   246         int device = atoi(argv[1]);
   247         if (device >= SDL_NumJoysticks()) {
   248             printf("%i is an invalid joystick index.\n", device);
   249             retcode = 1;
   250         } else {
   251             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   252                                       guid, sizeof (guid));
   253             printf("Attempting to open device %i, guid %s\n", device, guid);
   254             gamecontroller = SDL_GameControllerOpen(device);
   255             if (gamecontroller == NULL) {
   256                 printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   257                 retcode = 1;
   258             } else {
   259                 WatchGameController(gamecontroller);
   260                 SDL_GameControllerClose(gamecontroller);
   261             }
   262         }
   263     }
   264 
   265     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   266 
   267     return retcode;
   268 }
   269 
   270 #else
   271 
   272 int
   273 main(int argc, char *argv[])
   274 {
   275     fprintf(stderr, "SDL compiled without Joystick support.\n");
   276     exit(1);
   277 }
   278 
   279 #endif