test/testgamecontroller.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 7705 e70dc8b38f26
child 8043 74fcbf6987ba
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

Previously, the shaders would get compiled separately, the output of which would need to be packaged into the app. This change should make SDL's dll be the only binary needed to include SDL in a WinRT app.
     1 /*
     2   Copyright (C) 1997-2013 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 = (char *)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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   111         return;
   112     }
   113 
   114     screen = SDL_CreateRenderer(window, -1, 0);
   115     if (screen == NULL) {
   116         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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     SDL_Log("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                 SDL_Log("Controller %d axis %d ('%s') value: %d\n",
   139                        event.caxis.which,
   140                        event.caxis.axis,
   141                        ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
   142                        event.caxis.value);
   143                 break;
   144             case SDL_CONTROLLERBUTTONDOWN:
   145                 SDL_Log("Controller %d button %d ('%s') down\n",
   146                        event.cbutton.which, event.cbutton.button,
   147                        ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
   148                 break;
   149             case SDL_CONTROLLERBUTTONUP:
   150                 SDL_Log("Controller %d button %d ('%s') up\n",
   151                        event.cbutton.which, event.cbutton.button,
   152                        ControllerButtonName((SDL_GameControllerButton)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, (SDL_GameControllerButton)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, (SDL_GameControllerAxis)(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, (SDL_GameControllerAxis)(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     /* Enable standard application logging */
   221 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   222 
   223     /* Initialize SDL (Note: video is required to start event loop) */
   224     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   225         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   226         return 1;
   227     }
   228 
   229     /* Print information about the controller */
   230     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   231         const char *name;
   232         const char *description;
   233 
   234         SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
   235                                   guid, sizeof (guid));
   236 
   237         if ( SDL_IsGameController(i) )
   238         {
   239             nController++;
   240             name = SDL_GameControllerNameForIndex(i);
   241             description = "Controller";
   242         } else {
   243             name = SDL_JoystickNameForIndex(i);
   244             description = "Joystick";
   245         }
   246         SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   247     }
   248     SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   249 
   250     if (argv[1]) {
   251         int device = atoi(argv[1]);
   252         if (device >= SDL_NumJoysticks()) {
   253 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
   254             retcode = 1;
   255         } else {
   256             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   257                                       guid, sizeof (guid));
   258             SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
   259             gamecontroller = SDL_GameControllerOpen(device);
   260             if (gamecontroller == NULL) {
   261                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
   262                 retcode = 1;
   263             } else {
   264                 WatchGameController(gamecontroller);
   265                 SDL_GameControllerClose(gamecontroller);
   266             }
   267         }
   268     }
   269 
   270     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   271 
   272     return retcode;
   273 }
   274 
   275 #else
   276 
   277 int
   278 main(int argc, char *argv[])
   279 {
   280     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   281     exit(1);
   282 }
   283 
   284 #endif