test/testgamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Apr 2013 00:26:17 -0700
changeset 7110 2a98852fd58d
parent 7056 a5699778d80f
child 7191 75360622e65f
permissions -rw-r--r--
Fixed bug 1582 - Allow disabling SDL_VIDEO_DRIVER_WINDOWS

Marcus von Appen

Trying to build SDL 2.x (HG) on Win32 platforms with either VS.NET or MinGW requires one to have the video subsystem and SDL_VIDEO_DRIVER_WINDOWS flag enabled due to the helper window creation routines.

The attached patch changes the helper window creation behaviour, so that one can build SDL2 without the video subsystem or Windows video drivers on Win32 platforms.
slouken@6690
     1
/*
slouken@6690
     2
  Copyright (C) 1997-2011 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
jorgen@7056
    27
#define SCREEN_WIDTH    640
jorgen@7056
    28
#define SCREEN_HEIGHT    480
slouken@6690
    29
#endif
slouken@6690
    30
slouken@6690
    31
#define MAX_NUM_AXES 6
slouken@6690
    32
#define MAX_NUM_HATS 2
slouken@6690
    33
slouken@6690
    34
static SDL_bool s_ForceQuit = SDL_FALSE;
slouken@6690
    35
slouken@6690
    36
static void
slouken@6690
    37
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
slouken@6690
    38
{
slouken@6690
    39
    const SDL_Rect area = { x, y, w, h };
slouken@6690
    40
    SDL_RenderFillRect(r, &area);
slouken@6690
    41
}
slouken@6690
    42
icculus@6914
    43
static const char *
icculus@6917
    44
ControllerAxisName(const SDL_GameControllerAxis axis)
icculus@6914
    45
{
icculus@6914
    46
    switch (axis)
icculus@6914
    47
    {
icculus@6914
    48
        #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
icculus@6914
    49
        AXIS_CASE(INVALID);
icculus@6914
    50
        AXIS_CASE(LEFTX);
icculus@6914
    51
        AXIS_CASE(LEFTY);
icculus@6914
    52
        AXIS_CASE(RIGHTX);
icculus@6914
    53
        AXIS_CASE(RIGHTY);
icculus@6914
    54
        AXIS_CASE(TRIGGERLEFT);
icculus@6914
    55
        AXIS_CASE(TRIGGERRIGHT);
icculus@6914
    56
        #undef AXIS_CASE
icculus@6914
    57
        default: return "???";
icculus@6914
    58
    }
icculus@6914
    59
}
icculus@6914
    60
icculus@6914
    61
static const char *
icculus@6917
    62
ControllerButtonName(const SDL_GameControllerButton button)
icculus@6914
    63
{
icculus@6914
    64
    switch (button)
icculus@6914
    65
    {
icculus@6914
    66
        #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
icculus@6914
    67
        BUTTON_CASE(INVALID);
icculus@6914
    68
        BUTTON_CASE(A);
icculus@6914
    69
        BUTTON_CASE(B);
icculus@6914
    70
        BUTTON_CASE(X);
icculus@6914
    71
        BUTTON_CASE(Y);
icculus@6914
    72
        BUTTON_CASE(BACK);
icculus@6914
    73
        BUTTON_CASE(GUIDE);
icculus@6914
    74
        BUTTON_CASE(START);
icculus@6914
    75
        BUTTON_CASE(LEFTSTICK);
icculus@6914
    76
        BUTTON_CASE(RIGHTSTICK);
icculus@6914
    77
        BUTTON_CASE(LEFTSHOULDER);
icculus@6914
    78
        BUTTON_CASE(RIGHTSHOULDER);
icculus@6914
    79
        BUTTON_CASE(DPAD_UP);
icculus@6914
    80
        BUTTON_CASE(DPAD_DOWN);
icculus@6914
    81
        BUTTON_CASE(DPAD_LEFT);
icculus@6914
    82
        BUTTON_CASE(DPAD_RIGHT);
icculus@6914
    83
        #undef BUTTON_CASE
icculus@6914
    84
        default: return "???";
icculus@6914
    85
    }
icculus@6914
    86
}
icculus@6914
    87
slouken@6690
    88
void
slouken@6690
    89
WatchGameController(SDL_GameController * gamecontroller)
slouken@6690
    90
{
icculus@6916
    91
    const char *name = SDL_GameControllerName(gamecontroller);
icculus@6914
    92
    const char *basetitle = "Game Controller Test: ";
icculus@6916
    93
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
icculus@6914
    94
    char *title = SDL_malloc(titlelen);
slouken@6690
    95
    SDL_Window *window = NULL;
slouken@6690
    96
    SDL_Renderer *screen = NULL;
slouken@6690
    97
    int done = 0;
slouken@6690
    98
    SDL_Event event;
slouken@6690
    99
    int i;
slouken@6690
   100
icculus@6914
   101
    if (title) {
icculus@6916
   102
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
icculus@6914
   103
    }
icculus@6914
   104
slouken@6690
   105
    /* Create a window to display controller axis position */
icculus@6914
   106
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
slouken@6690
   107
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
slouken@6690
   108
                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
slouken@6690
   109
    if (window == NULL) {
slouken@6690
   110
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
slouken@6690
   111
        return;
slouken@6690
   112
    }
slouken@6690
   113
slouken@6690
   114
    screen = SDL_CreateRenderer(window, -1, 0);
slouken@6690
   115
    if (screen == NULL) {
slouken@6690
   116
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@6690
   117
        SDL_DestroyWindow(window);
slouken@6690
   118
        return;
slouken@6690
   119
    }
slouken@6690
   120
slouken@6690
   121
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   122
    SDL_RenderClear(screen);
slouken@6690
   123
    SDL_RenderPresent(screen);
jorgen@7056
   124
    SDL_RaiseWindow(window);
slouken@6690
   125
slouken@6690
   126
    /* Print info about the controller we are watching */
slouken@6690
   127
    printf("Watching controller %s\n",  name ? name : "Unknown Controller");
slouken@6690
   128
    
slouken@6690
   129
    /* Loop, getting controller events! */
slouken@6690
   130
    while (!done) {
slouken@6690
   131
        /* blank screen, set up for drawing this frame. */
slouken@6690
   132
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   133
        SDL_RenderClear(screen);
slouken@6690
   134
slouken@6690
   135
        while (SDL_PollEvent(&event)) {
slouken@6690
   136
            switch (event.type) {
slouken@6690
   137
            case SDL_CONTROLLERAXISMOTION:
icculus@6914
   138
                printf("Controller %d axis %d ('%s') value: %d\n",
slouken@6690
   139
                       event.caxis.which,
icculus@6914
   140
                       event.caxis.axis,
icculus@6914
   141
                       ControllerAxisName(event.caxis.axis),
icculus@6914
   142
                       event.caxis.value);
slouken@6690
   143
                break;
slouken@6690
   144
            case SDL_CONTROLLERBUTTONDOWN:
icculus@6914
   145
                printf("Controller %d button %d ('%s') down\n",
icculus@6914
   146
                       event.cbutton.which, event.cbutton.button,
icculus@6914
   147
                       ControllerButtonName(event.cbutton.button));
slouken@6690
   148
                break;
slouken@6690
   149
            case SDL_CONTROLLERBUTTONUP:
icculus@6914
   150
                printf("Controller %d button %d ('%s') up\n",
icculus@6914
   151
                       event.cbutton.which, event.cbutton.button,
icculus@6914
   152
                       ControllerButtonName(event.cbutton.button));
slouken@6690
   153
                break;
slouken@6690
   154
            case SDL_KEYDOWN:
slouken@6690
   155
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@6690
   156
                    break;
slouken@6690
   157
                }
slouken@6690
   158
                /* Fall through to signal quit */
slouken@6690
   159
            case SDL_QUIT:
slouken@6690
   160
                done = 1;
icculus@6914
   161
                s_ForceQuit = SDL_TRUE;
slouken@6690
   162
                break;
slouken@6690
   163
            default:
slouken@6690
   164
                break;
slouken@6690
   165
            }
slouken@6690
   166
        }
slouken@6690
   167
        /* Update visual controller state */
slouken@6690
   168
        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   169
        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
slouken@6690
   170
            if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
slouken@6690
   171
                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
slouken@6690
   172
            }
slouken@6690
   173
        }
slouken@6690
   174
slouken@6690
   175
        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   176
        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
slouken@6690
   177
            /* Draw the X/Y axis */
slouken@6690
   178
            int x, y;
slouken@6690
   179
            x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
slouken@6690
   180
            x *= SCREEN_WIDTH;
slouken@6690
   181
            x /= 65535;
slouken@6690
   182
            if (x < 0) {
slouken@6690
   183
                x = 0;
slouken@6690
   184
            } else if (x > (SCREEN_WIDTH - 16)) {
slouken@6690
   185
                x = SCREEN_WIDTH - 16;
slouken@6690
   186
            }
slouken@6690
   187
            y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
slouken@6690
   188
            y *= SCREEN_HEIGHT;
slouken@6690
   189
            y /= 65535;
slouken@6690
   190
            if (y < 0) {
slouken@6690
   191
                y = 0;
slouken@6690
   192
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@6690
   193
                y = SCREEN_HEIGHT - 16;
slouken@6690
   194
            }
slouken@6690
   195
slouken@6690
   196
            DrawRect(screen, x, y, 16, 16);
slouken@6690
   197
        }
slouken@6690
   198
slouken@6690
   199
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
slouken@6690
   200
slouken@6690
   201
        SDL_RenderPresent(screen);
jorgen@7056
   202
        
jorgen@7056
   203
        if ( !done )
jorgen@7056
   204
            done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
slouken@6690
   205
    }
slouken@6690
   206
slouken@6690
   207
    SDL_DestroyRenderer(screen);
slouken@6690
   208
    SDL_DestroyWindow(window);
slouken@6690
   209
}
slouken@6690
   210
slouken@6690
   211
int
slouken@6690
   212
main(int argc, char *argv[])
slouken@6690
   213
{
slouken@6690
   214
    int i;
jorgen@7056
   215
    int nController = 0;
jorgen@7056
   216
    int retcode = 0;
jorgen@7056
   217
    char guid[64];
slouken@6690
   218
    SDL_GameController *gamecontroller;
slouken@6690
   219
slouken@6690
   220
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@6690
   221
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
slouken@6690
   222
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
jorgen@7056
   223
        return 1;
slouken@6690
   224
    }
slouken@6690
   225
slouken@6690
   226
    /* Print information about the controller */
slouken@6690
   227
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
jorgen@7056
   228
        const char *name;
jorgen@7056
   229
        const char *description = "Joystick (not recognized as game controller)";
jorgen@7056
   230
jorgen@7056
   231
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
jorgen@7056
   232
                                  guid, sizeof (guid));
jorgen@7056
   233
jorgen@7056
   234
        if ( SDL_IsGameController(i) )
jorgen@7056
   235
        {
jorgen@7056
   236
            nController++;
jorgen@7056
   237
            name = SDL_GameControllerNameForIndex(i);
jorgen@7056
   238
        } else {
jorgen@7056
   239
            name = SDL_JoystickNameForIndex(i);
jorgen@7056
   240
        }
jorgen@7056
   241
        printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
slouken@6690
   242
    }
jorgen@7056
   243
    printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
slouken@6690
   244
slouken@6690
   245
    if (argv[1]) {
jorgen@7056
   246
        int device = atoi(argv[1]);
jorgen@7056
   247
        if (device >= SDL_NumJoysticks()) {
jorgen@7056
   248
            printf("%i is an invalid joystick index.\n", device);
jorgen@7056
   249
            retcode = 1;
jorgen@7056
   250
        } else {
jorgen@7056
   251
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
jorgen@7056
   252
                                      guid, sizeof (guid));
jorgen@7056
   253
            printf("Attempting to open device %i, guid %s\n", device, guid);
jorgen@7056
   254
            gamecontroller = SDL_GameControllerOpen(device);
jorgen@7056
   255
            if (gamecontroller == NULL) {
jorgen@7056
   256
                printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
jorgen@7056
   257
                retcode = 1;
jorgen@7056
   258
            } else {
jorgen@7056
   259
                WatchGameController(gamecontroller);
jorgen@7056
   260
                SDL_GameControllerClose(gamecontroller);
jorgen@7056
   261
            }
jorgen@7056
   262
        }
jorgen@7056
   263
    }
slouken@6690
   264
jorgen@7056
   265
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
jorgen@7056
   266
jorgen@7056
   267
    return retcode;
slouken@6690
   268
}
aschiffler@6771
   269
aschiffler@6771
   270
#else
aschiffler@6771
   271
aschiffler@6771
   272
int
aschiffler@6771
   273
main(int argc, char *argv[])
aschiffler@6771
   274
{
aschiffler@6771
   275
    fprintf(stderr, "SDL compiled without Joystick support.\n");
aschiffler@6771
   276
    exit(1);
aschiffler@6771
   277
}
aschiffler@6771
   278
aschiffler@6771
   279
#endif