test/testgamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Feb 2014 10:02:42 -0800
changeset 8206 6e59aa7b53af
parent 8202 b84a1f55075e
child 8687 1d2ca48523a7
permissions -rw-r--r--
Exposed the font character size and SDLTest_DrawCharacter() to make it easier to do custom debug text layout
slouken@6690
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@6690
     3
slouken@6690
     4
  This software is provided 'as-is', without any express or implied
slouken@6690
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@6690
     6
  arising from the use of this software.
slouken@6690
     7
slouken@6690
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@6690
     9
  including commercial applications, and to alter it and redistribute it
slouken@6690
    10
  freely.
slouken@6690
    11
*/
slouken@6690
    12
slouken@6690
    13
/* Simple program to test the SDL game controller routines */
slouken@6690
    14
slouken@6690
    15
#include <stdio.h>
slouken@6690
    16
#include <stdlib.h>
slouken@6690
    17
#include <string.h>
slouken@6690
    18
slouken@6690
    19
#include "SDL.h"
slouken@6690
    20
aschiffler@6771
    21
#ifndef SDL_JOYSTICK_DISABLED
aschiffler@6771
    22
slouken@6690
    23
#ifdef __IPHONEOS__
jorgen@7056
    24
#define SCREEN_WIDTH    320
jorgen@7056
    25
#define SCREEN_HEIGHT    480
slouken@6690
    26
#else
icculus@8199
    27
#define SCREEN_WIDTH    512
icculus@8199
    28
#define SCREEN_HEIGHT   317
slouken@6690
    29
#endif
slouken@6690
    30
icculus@6914
    31
static const char *
icculus@6917
    32
ControllerAxisName(const SDL_GameControllerAxis axis)
icculus@6914
    33
{
icculus@6914
    34
    switch (axis)
icculus@6914
    35
    {
icculus@6914
    36
        #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
icculus@6914
    37
        AXIS_CASE(INVALID);
icculus@6914
    38
        AXIS_CASE(LEFTX);
icculus@6914
    39
        AXIS_CASE(LEFTY);
icculus@6914
    40
        AXIS_CASE(RIGHTX);
icculus@6914
    41
        AXIS_CASE(RIGHTY);
icculus@6914
    42
        AXIS_CASE(TRIGGERLEFT);
icculus@6914
    43
        AXIS_CASE(TRIGGERRIGHT);
icculus@6914
    44
        #undef AXIS_CASE
icculus@6914
    45
        default: return "???";
icculus@6914
    46
    }
icculus@6914
    47
}
icculus@6914
    48
icculus@6914
    49
static const char *
icculus@6917
    50
ControllerButtonName(const SDL_GameControllerButton button)
icculus@6914
    51
{
icculus@6914
    52
    switch (button)
icculus@6914
    53
    {
icculus@6914
    54
        #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
icculus@6914
    55
        BUTTON_CASE(INVALID);
icculus@6914
    56
        BUTTON_CASE(A);
icculus@6914
    57
        BUTTON_CASE(B);
icculus@6914
    58
        BUTTON_CASE(X);
icculus@6914
    59
        BUTTON_CASE(Y);
icculus@6914
    60
        BUTTON_CASE(BACK);
icculus@6914
    61
        BUTTON_CASE(GUIDE);
icculus@6914
    62
        BUTTON_CASE(START);
icculus@6914
    63
        BUTTON_CASE(LEFTSTICK);
icculus@6914
    64
        BUTTON_CASE(RIGHTSTICK);
icculus@6914
    65
        BUTTON_CASE(LEFTSHOULDER);
icculus@6914
    66
        BUTTON_CASE(RIGHTSHOULDER);
icculus@6914
    67
        BUTTON_CASE(DPAD_UP);
icculus@6914
    68
        BUTTON_CASE(DPAD_DOWN);
icculus@6914
    69
        BUTTON_CASE(DPAD_LEFT);
icculus@6914
    70
        BUTTON_CASE(DPAD_RIGHT);
icculus@6914
    71
        #undef BUTTON_CASE
icculus@6914
    72
        default: return "???";
icculus@6914
    73
    }
icculus@6914
    74
}
icculus@6914
    75
icculus@8199
    76
static SDL_Texture *
icculus@8199
    77
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
icculus@8199
    78
{
icculus@8199
    79
    SDL_Surface *temp = NULL;
icculus@8199
    80
    SDL_Texture *texture = NULL;
icculus@8199
    81
icculus@8199
    82
    temp = SDL_LoadBMP(file);
icculus@8199
    83
    if (temp == NULL) {
icculus@8199
    84
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
icculus@8199
    85
    } else {
icculus@8199
    86
        /* Set transparent pixel as the pixel at (0,0) */
icculus@8199
    87
        if (transparent) {
icculus@8199
    88
            SDL_assert(!temp->format->palette);
icculus@8199
    89
            SDL_assert(temp->format->BitsPerPixel == 24);
icculus@8199
    90
            SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
icculus@8199
    91
        }
icculus@8199
    92
icculus@8199
    93
        texture = SDL_CreateTextureFromSurface(renderer, temp);
icculus@8199
    94
        if (!texture) {
icculus@8199
    95
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
icculus@8199
    96
        }
icculus@8199
    97
    }
icculus@8199
    98
    if (temp) {
icculus@8199
    99
        SDL_FreeSurface(temp);
icculus@8199
   100
    }
icculus@8199
   101
    return texture;
icculus@8199
   102
}
icculus@8199
   103
slouken@8068
   104
SDL_bool
slouken@6690
   105
WatchGameController(SDL_GameController * gamecontroller)
slouken@6690
   106
{
icculus@8199
   107
    /* This is indexed by SDL_GameControllerButton. */
icculus@8199
   108
    static const struct { int x; int y; } button_positions[] = {
icculus@8199
   109
        {387, 167},  /* A */
icculus@8199
   110
        {431, 132},  /* B */
icculus@8199
   111
        {342, 132},  /* X */
icculus@8199
   112
        {389, 101},  /* Y */
icculus@8199
   113
        {174, 132},  /* BACK */
icculus@8199
   114
        {233, 132},  /* GUIDE */
icculus@8199
   115
        {289, 132},  /* START */
icculus@8199
   116
        {75,  154},  /* LEFTSTICK */
icculus@8199
   117
        {305, 230},  /* RIGHTSTICK */
icculus@8199
   118
        {77,  40},   /* LEFTSHOULDER */
icculus@8199
   119
        {396, 36},   /* RIGHTSHOULDER */
icculus@8199
   120
        {154, 188},  /* DPAD_UP */
icculus@8199
   121
        {154, 249},  /* DPAD_DOWN */
icculus@8199
   122
        {116, 217},  /* DPAD_LEFT */
icculus@8199
   123
        {186, 217},  /* DPAD_RIGHT */
icculus@8199
   124
    };
icculus@8199
   125
icculus@8199
   126
    /* This is indexed by SDL_GameControllerAxis. */
icculus@8199
   127
    static const struct { int x; int y; double angle; } axis_positions[] = {
icculus@8199
   128
        {75,  154, 0.0},  /* LEFTX */
icculus@8199
   129
        {75,  154, 90.0},  /* LEFTY */
icculus@8199
   130
        {305, 230, 0.0},  /* RIGHTX */
icculus@8199
   131
        {305, 230, 90.0},  /* RIGHTY */
icculus@8199
   132
        {91, 0, 90.0},     /* TRIGGERLEFT */
icculus@8199
   133
        {375, 0, 90.0},    /* TRIGGERRIGHT */
icculus@8199
   134
    };
icculus@8199
   135
icculus@6916
   136
    const char *name = SDL_GameControllerName(gamecontroller);
icculus@6914
   137
    const char *basetitle = "Game Controller Test: ";
icculus@6916
   138
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
aschiffler@7639
   139
    char *title = (char *)SDL_malloc(titlelen);
icculus@8199
   140
    SDL_Texture *background, *button, *axis;
slouken@6690
   141
    SDL_Window *window = NULL;
slouken@6690
   142
    SDL_Renderer *screen = NULL;
slouken@8068
   143
    SDL_bool retval = SDL_FALSE;
slouken@8068
   144
    SDL_bool done = SDL_FALSE;
slouken@6690
   145
    SDL_Event event;
slouken@6690
   146
    int i;
slouken@6690
   147
icculus@6914
   148
    if (title) {
icculus@6916
   149
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
icculus@6914
   150
    }
icculus@6914
   151
icculus@8199
   152
    /* Create a window to display controller state */
icculus@6914
   153
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
slouken@6690
   154
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
philipp@7478
   155
                              SCREEN_HEIGHT, 0);
slouken@6690
   156
    if (window == NULL) {
aschiffler@7639
   157
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
slouken@8068
   158
        return SDL_FALSE;
slouken@6690
   159
    }
slouken@6690
   160
slouken@6690
   161
    screen = SDL_CreateRenderer(window, -1, 0);
slouken@6690
   162
    if (screen == NULL) {
aschiffler@7639
   163
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@6690
   164
        SDL_DestroyWindow(window);
slouken@8068
   165
        return SDL_FALSE;
slouken@6690
   166
    }
slouken@6690
   167
slouken@6690
   168
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   169
    SDL_RenderClear(screen);
slouken@6690
   170
    SDL_RenderPresent(screen);
jorgen@7056
   171
    SDL_RaiseWindow(window);
slouken@6690
   172
icculus@8202
   173
    /* scale for platforms that don't give you the window size you asked for. */
icculus@8202
   174
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
icculus@8202
   175
icculus@8199
   176
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
icculus@8199
   177
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
icculus@8199
   178
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
icculus@8199
   179
icculus@8199
   180
    if (!background || !button || !axis) {
icculus@8199
   181
        SDL_DestroyRenderer(screen);
icculus@8199
   182
        SDL_DestroyWindow(window);
icculus@8199
   183
        return SDL_FALSE;
icculus@8199
   184
    }
icculus@8199
   185
    SDL_SetTextureColorMod(button, 10, 255, 21);
icculus@8199
   186
    SDL_SetTextureColorMod(axis, 10, 255, 21);
icculus@8199
   187
icculus@8199
   188
    /* !!! FIXME: */
icculus@8199
   189
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
icculus@8199
   190
slouken@6690
   191
    /* Print info about the controller we are watching */
aschiffler@7639
   192
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
slouken@7191
   193
slouken@6690
   194
    /* Loop, getting controller events! */
slouken@6690
   195
    while (!done) {
slouken@6690
   196
        /* blank screen, set up for drawing this frame. */
icculus@8202
   197
        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
slouken@6690
   198
        SDL_RenderClear(screen);
icculus@8199
   199
        SDL_RenderCopy(screen, background, NULL, NULL);
slouken@6690
   200
slouken@6690
   201
        while (SDL_PollEvent(&event)) {
slouken@6690
   202
            switch (event.type) {
slouken@6690
   203
            case SDL_KEYDOWN:
slouken@6690
   204
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@6690
   205
                    break;
slouken@6690
   206
                }
slouken@6690
   207
                /* Fall through to signal quit */
slouken@6690
   208
            case SDL_QUIT:
slouken@8068
   209
                done = SDL_TRUE;
slouken@6690
   210
                break;
slouken@6690
   211
            default:
slouken@6690
   212
                break;
slouken@6690
   213
            }
slouken@6690
   214
        }
icculus@8199
   215
slouken@6690
   216
        /* Update visual controller state */
icculus@8199
   217
        for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
aschiffler@7639
   218
            if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
icculus@8199
   219
                const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
icculus@8199
   220
                SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
slouken@6690
   221
            }
slouken@6690
   222
        }
slouken@6690
   223
icculus@8199
   224
        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
icculus@8199
   225
            const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
icculus@8199
   226
            const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
icculus@8199
   227
            if (value < -deadzone) {
icculus@8199
   228
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@8199
   229
                const double angle = axis_positions[i].angle;
icculus@8199
   230
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
icculus@8199
   231
            } else if (value > deadzone) {
icculus@8199
   232
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@8199
   233
                const double angle = axis_positions[i].angle + 180.0;
icculus@8199
   234
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
slouken@6690
   235
            }
slouken@6690
   236
        }
slouken@6690
   237
slouken@6690
   238
        SDL_RenderPresent(screen);
slouken@7191
   239
slouken@8068
   240
        if (!SDL_GameControllerGetAttached(gamecontroller)) {
slouken@8068
   241
            done = SDL_TRUE;
slouken@8068
   242
            retval = SDL_TRUE;  /* keep going, wait for reattach. */
slouken@8068
   243
        }
slouken@6690
   244
    }
slouken@6690
   245
slouken@6690
   246
    SDL_DestroyRenderer(screen);
slouken@6690
   247
    SDL_DestroyWindow(window);
slouken@8068
   248
    return retval;
slouken@6690
   249
}
slouken@6690
   250
slouken@6690
   251
int
slouken@6690
   252
main(int argc, char *argv[])
slouken@6690
   253
{
slouken@6690
   254
    int i;
jorgen@7056
   255
    int nController = 0;
jorgen@7056
   256
    int retcode = 0;
jorgen@7056
   257
    char guid[64];
slouken@6690
   258
    SDL_GameController *gamecontroller;
slouken@6690
   259
aschiffler@7639
   260
    /* Enable standard application logging */
aschiffler@7639
   261
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   262
slouken@6690
   263
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@6690
   264
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
aschiffler@7639
   265
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
jorgen@7056
   266
        return 1;
slouken@6690
   267
    }
gabomdq@8043
   268
    
gabomdq@8043
   269
    SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
slouken@6690
   270
slouken@6690
   271
    /* Print information about the controller */
slouken@6690
   272
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
jorgen@7056
   273
        const char *name;
icculus@7705
   274
        const char *description;
jorgen@7056
   275
jorgen@7056
   276
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
jorgen@7056
   277
                                  guid, sizeof (guid));
jorgen@7056
   278
jorgen@7056
   279
        if ( SDL_IsGameController(i) )
jorgen@7056
   280
        {
jorgen@7056
   281
            nController++;
jorgen@7056
   282
            name = SDL_GameControllerNameForIndex(i);
icculus@7705
   283
            description = "Controller";
jorgen@7056
   284
        } else {
jorgen@7056
   285
            name = SDL_JoystickNameForIndex(i);
icculus@7705
   286
            description = "Joystick";
jorgen@7056
   287
        }
aschiffler@7639
   288
        SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
slouken@6690
   289
    }
aschiffler@7639
   290
    SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
slouken@6690
   291
slouken@6690
   292
    if (argv[1]) {
slouken@8068
   293
        SDL_bool reportederror = SDL_FALSE;
slouken@8068
   294
        SDL_bool keepGoing = SDL_TRUE;
slouken@8068
   295
        SDL_Event event;
jorgen@7056
   296
        int device = atoi(argv[1]);
jorgen@7056
   297
        if (device >= SDL_NumJoysticks()) {
aschiffler@7639
   298
			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
jorgen@7056
   299
            retcode = 1;
jorgen@7056
   300
        } else {
jorgen@7056
   301
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
jorgen@7056
   302
                                      guid, sizeof (guid));
aschiffler@7639
   303
            SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
jorgen@7056
   304
            gamecontroller = SDL_GameControllerOpen(device);
slouken@8068
   305
            while (keepGoing) {
slouken@8068
   306
                if (gamecontroller == NULL) {
slouken@8068
   307
                    if (!reportederror) {
slouken@8068
   308
                        if (gamecontroller == NULL) {
slouken@8068
   309
                            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
slouken@8068
   310
                            retcode = 1;
slouken@8068
   311
                        }
slouken@8068
   312
                        keepGoing = SDL_FALSE;
slouken@8068
   313
                        reportederror = SDL_TRUE;
slouken@8068
   314
                    }
slouken@8068
   315
                } else {
slouken@8068
   316
                    reportederror = SDL_FALSE;
slouken@8068
   317
                    keepGoing = WatchGameController(gamecontroller);
slouken@8068
   318
                    SDL_GameControllerClose(gamecontroller);
slouken@8068
   319
                }
slouken@8068
   320
slouken@8068
   321
                gamecontroller = NULL;
slouken@8068
   322
                if (keepGoing) {
slouken@8068
   323
                    SDL_Log("Waiting for attach\n");
slouken@8068
   324
                }
slouken@8068
   325
                while (keepGoing) {
slouken@8068
   326
                    SDL_WaitEvent(&event);
slouken@8068
   327
                    if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
slouken@8068
   328
                        || (event.type == SDL_MOUSEBUTTONDOWN)) {
slouken@8068
   329
                        keepGoing = SDL_FALSE;
slouken@8068
   330
                    } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
slouken@8068
   331
                        gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
slouken@8068
   332
                        break;
slouken@8068
   333
                    }
slouken@8068
   334
                }
jorgen@7056
   335
            }
jorgen@7056
   336
        }
jorgen@7056
   337
    }
slouken@6690
   338
jorgen@7056
   339
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
jorgen@7056
   340
jorgen@7056
   341
    return retcode;
slouken@6690
   342
}
aschiffler@6771
   343
aschiffler@6771
   344
#else
aschiffler@6771
   345
aschiffler@6771
   346
int
aschiffler@6771
   347
main(int argc, char *argv[])
aschiffler@6771
   348
{
aschiffler@7639
   349
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
aschiffler@6771
   350
    exit(1);
aschiffler@6771
   351
}
aschiffler@6771
   352
aschiffler@6771
   353
#endif