test/testjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 15 Aug 2012 20:53:24 -0400
changeset 6397 efdf44f20799
parent 6256 1d905b13b102
child 6398 c294faf5fce5
permissions -rw-r--r--
Updated testjoystick.c for SDL2 API and draw more information.

Fixes Bugzilla #1570.

Thanks to Ondra Hosek for the patch!
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple program to test the SDL joystick routines */
slouken@0
    14
slouken@0
    15
#include <stdio.h>
slouken@0
    16
#include <stdlib.h>
slouken@0
    17
#include <string.h>
slouken@0
    18
slouken@0
    19
#include "SDL.h"
slouken@0
    20
slouken@2765
    21
#ifdef __IPHONEOS__
slouken@2765
    22
#define SCREEN_WIDTH	320
slouken@2765
    23
#define SCREEN_HEIGHT	480
slouken@2765
    24
#else
slouken@0
    25
#define SCREEN_WIDTH	640
slouken@0
    26
#define SCREEN_HEIGHT	480
slouken@2765
    27
#endif
slouken@0
    28
icculus@6397
    29
#define MAX_NUM_AXES 6
icculus@6397
    30
#define MAX_NUM_HATS 2
icculus@6397
    31
slouken@1895
    32
void
slouken@1895
    33
WatchJoystick(SDL_Joystick * joystick)
slouken@0
    34
{
icculus@6397
    35
    SDL_Window *window;
icculus@6397
    36
    SDL_Renderer *screen;
slouken@1895
    37
    const char *name;
slouken@1895
    38
    int i, done;
slouken@1895
    39
    SDL_Event event;
icculus@6397
    40
    int x, y;
icculus@6397
    41
    SDL_Rect axis_area[MAX_NUM_AXES][2];
icculus@6397
    42
    int axis_draw[MAX_NUM_AXES];
icculus@6397
    43
    SDL_Rect hat_area[MAX_NUM_HATS][2];
icculus@6397
    44
    int hat_draw[MAX_NUM_HATS];
icculus@6397
    45
    Uint8 hat_pos;
slouken@0
    46
icculus@6397
    47
    /* Create a window to display joystick axis position */
icculus@6397
    48
    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
icculus@6397
    49
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
icculus@6397
    50
                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
icculus@6397
    51
    if (window == NULL) {
icculus@6397
    52
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
slouken@1895
    53
        return;
slouken@1895
    54
    }
slouken@0
    55
icculus@6397
    56
    screen = SDL_CreateRenderer(window, -1, 0);
icculus@6397
    57
    if (screen == NULL) {
icculus@6397
    58
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
icculus@6397
    59
        SDL_DestroyWindow(window);
icculus@6397
    60
        return;
icculus@6397
    61
    }
icculus@6397
    62
icculus@6397
    63
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6397
    64
    SDL_RenderClear(screen);
icculus@6397
    65
slouken@1895
    66
    /* Print info about the joystick we are watching */
slouken@1895
    67
    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@1895
    68
    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@1895
    69
           name ? name : "Unknown Joystick");
slouken@1895
    70
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@1895
    71
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@1895
    72
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
slouken@0
    73
slouken@1895
    74
    /* Initialize drawing rectangles */
slouken@1895
    75
    memset(axis_area, 0, (sizeof axis_area));
icculus@6397
    76
    memset(axis_draw, 0, (sizeof axis_draw));
icculus@6397
    77
    memset(hat_area, 0, (sizeof hat_area));
icculus@6397
    78
    memset(hat_draw, 0, (sizeof hat_draw));
slouken@0
    79
slouken@1895
    80
    /* Loop, getting joystick events! */
slouken@1895
    81
    done = 0;
slouken@1895
    82
    while (!done) {
slouken@1895
    83
        while (SDL_PollEvent(&event)) {
slouken@1895
    84
            switch (event.type) {
slouken@1895
    85
            case SDL_JOYAXISMOTION:
slouken@1895
    86
                printf("Joystick %d axis %d value: %d\n",
slouken@1895
    87
                       event.jaxis.which,
slouken@1895
    88
                       event.jaxis.axis, event.jaxis.value);
slouken@1895
    89
                break;
slouken@1895
    90
            case SDL_JOYHATMOTION:
slouken@1895
    91
                printf("Joystick %d hat %d value:",
slouken@1895
    92
                       event.jhat.which, event.jhat.hat);
slouken@1895
    93
                if (event.jhat.value == SDL_HAT_CENTERED)
slouken@1895
    94
                    printf(" centered");
slouken@1895
    95
                if (event.jhat.value & SDL_HAT_UP)
slouken@1895
    96
                    printf(" up");
slouken@1895
    97
                if (event.jhat.value & SDL_HAT_RIGHT)
slouken@1895
    98
                    printf(" right");
slouken@1895
    99
                if (event.jhat.value & SDL_HAT_DOWN)
slouken@1895
   100
                    printf(" down");
slouken@1895
   101
                if (event.jhat.value & SDL_HAT_LEFT)
slouken@1895
   102
                    printf(" left");
slouken@1895
   103
                printf("\n");
slouken@1895
   104
                break;
slouken@1895
   105
            case SDL_JOYBALLMOTION:
slouken@1895
   106
                printf("Joystick %d ball %d delta: (%d,%d)\n",
slouken@1895
   107
                       event.jball.which,
slouken@1895
   108
                       event.jball.ball, event.jball.xrel, event.jball.yrel);
slouken@1895
   109
                break;
slouken@1895
   110
            case SDL_JOYBUTTONDOWN:
slouken@1895
   111
                printf("Joystick %d button %d down\n",
slouken@1895
   112
                       event.jbutton.which, event.jbutton.button);
slouken@1895
   113
                break;
slouken@1895
   114
            case SDL_JOYBUTTONUP:
slouken@1895
   115
                printf("Joystick %d button %d up\n",
slouken@1895
   116
                       event.jbutton.which, event.jbutton.button);
slouken@1895
   117
                break;
slouken@1895
   118
            case SDL_KEYDOWN:
slouken@1895
   119
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@1895
   120
                    break;
slouken@1895
   121
                }
slouken@1895
   122
                /* Fall through to signal quit */
slouken@1895
   123
            case SDL_QUIT:
slouken@1895
   124
                done = 1;
slouken@1895
   125
                break;
slouken@1895
   126
            default:
slouken@1895
   127
                break;
slouken@1895
   128
            }
slouken@1895
   129
        }
slouken@1895
   130
        /* Update visual joystick state */
slouken@1895
   131
        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
slouken@1895
   132
            SDL_Rect area;
slouken@0
   133
slouken@1895
   134
            area.x = i * 34;
slouken@1895
   135
            area.y = SCREEN_HEIGHT - 34;
slouken@1895
   136
            area.w = 32;
slouken@1895
   137
            area.h = 32;
slouken@1895
   138
            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
icculus@6397
   139
                SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
slouken@1895
   140
            } else {
icculus@6397
   141
                SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@1895
   142
            }
icculus@6397
   143
            SDL_RenderFillRect(screen, &area);
icculus@6397
   144
            SDL_RenderPresent(screen);
slouken@1895
   145
        }
slouken@0
   146
slouken@1895
   147
        for (i = 0;
slouken@1895
   148
             i < SDL_JoystickNumAxes(joystick) / 2
slouken@1895
   149
             && i < SDL_arraysize(axis_area); ++i) {
icculus@6397
   150
slouken@1895
   151
            /* Erase previous axes */
icculus@6397
   152
            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6397
   153
            SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
slouken@0
   154
slouken@1895
   155
            /* Draw the X/Y axis */
icculus@6397
   156
            axis_draw[i] = !axis_draw[i];
slouken@1895
   157
            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
slouken@1895
   158
            x *= SCREEN_WIDTH;
slouken@1895
   159
            x /= 65535;
slouken@1895
   160
            if (x < 0) {
slouken@1895
   161
                x = 0;
slouken@1895
   162
            } else if (x > (SCREEN_WIDTH - 16)) {
slouken@1895
   163
                x = SCREEN_WIDTH - 16;
slouken@1895
   164
            }
slouken@1895
   165
            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
slouken@1895
   166
            y *= SCREEN_HEIGHT;
slouken@1895
   167
            y /= 65535;
slouken@1895
   168
            if (y < 0) {
slouken@1895
   169
                y = 0;
slouken@1895
   170
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@1895
   171
                y = SCREEN_HEIGHT - 16;
slouken@1895
   172
            }
slouken@2765
   173
icculus@6397
   174
            axis_area[i][axis_draw[i]].x = (Sint16) x;
icculus@6397
   175
            axis_area[i][axis_draw[i]].y = (Sint16) y;
icculus@6397
   176
            axis_area[i][axis_draw[i]].w = 16;
icculus@6397
   177
            axis_area[i][axis_draw[i]].h = 16;
slouken@1854
   178
icculus@6397
   179
            SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
icculus@6397
   180
            SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
icculus@6397
   181
            SDL_RenderPresent(screen);
icculus@6397
   182
        }
icculus@6397
   183
icculus@6397
   184
        for (i = 0;
icculus@6397
   185
             i < SDL_JoystickNumHats(joystick)
icculus@6397
   186
             && i < SDL_arraysize(hat_area); ++i) {
icculus@6397
   187
icculus@6397
   188
            /* Erase previous hat position */
icculus@6397
   189
            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6397
   190
            SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
icculus@6397
   191
icculus@6397
   192
            hat_draw[i] = !hat_draw[i];
icculus@6397
   193
icculus@6397
   194
            /* Derive the new position */
icculus@6397
   195
            hat_pos = SDL_JoystickGetHat(joystick, i);
icculus@6397
   196
icculus@6397
   197
            hat_area[i][hat_draw[i]].x = SCREEN_WIDTH/2;
icculus@6397
   198
            hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT/2;
icculus@6397
   199
            hat_area[i][hat_draw[i]].w = 8;
icculus@6397
   200
            hat_area[i][hat_draw[i]].h = 8;
icculus@6397
   201
icculus@6397
   202
            if (hat_pos & SDL_HAT_UP) {
icculus@6397
   203
                hat_area[i][hat_draw[i]].y = 0;
icculus@6397
   204
            } else if (hat_pos & SDL_HAT_DOWN) {
icculus@6397
   205
                hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT-8;
icculus@6397
   206
            }
icculus@6397
   207
icculus@6397
   208
            if (hat_pos & SDL_HAT_LEFT) {
icculus@6397
   209
                hat_area[i][hat_draw[i]].x = 0;
icculus@6397
   210
            } else if (hat_pos & SDL_HAT_RIGHT) {
icculus@6397
   211
                hat_area[i][hat_draw[i]].x = SCREEN_WIDTH-8;
icculus@6397
   212
            }
icculus@6397
   213
icculus@6397
   214
            /* Draw it */
icculus@6397
   215
            SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
icculus@6397
   216
            SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
icculus@6397
   217
            SDL_RenderPresent(screen);
slouken@1895
   218
        }
slouken@1895
   219
    }
icculus@6397
   220
icculus@6397
   221
    SDL_DestroyRenderer(screen);
icculus@6397
   222
    SDL_DestroyWindow(window);
slouken@0
   223
}
slouken@0
   224
slouken@1895
   225
int
slouken@1895
   226
main(int argc, char *argv[])
slouken@0
   227
{
slouken@1895
   228
    const char *name;
slouken@1895
   229
    int i;
slouken@1895
   230
    SDL_Joystick *joystick;
slouken@0
   231
slouken@1895
   232
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@1895
   233
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
slouken@1895
   234
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   235
        exit(1);
slouken@1895
   236
    }
slouken@0
   237
slouken@1895
   238
    /* Print information about the joysticks */
slouken@1895
   239
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@1895
   240
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@1895
   241
        name = SDL_JoystickName(i);
slouken@2201
   242
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
icculus@2200
   243
        joystick = SDL_JoystickOpen(i);
icculus@2200
   244
        if (joystick == NULL) {
slouken@2201
   245
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
slouken@2201
   246
                    SDL_GetError());
icculus@2200
   247
        } else {
icculus@2200
   248
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
icculus@2200
   249
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
icculus@2200
   250
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
icculus@2200
   251
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
icculus@2200
   252
            SDL_JoystickClose(joystick);
icculus@2200
   253
        }
slouken@1895
   254
    }
slouken@0
   255
slouken@1895
   256
    if (argv[1]) {
slouken@1895
   257
        joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@1895
   258
        if (joystick == NULL) {
slouken@1895
   259
            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@1895
   260
                   SDL_GetError());
slouken@1895
   261
        } else {
slouken@1895
   262
            WatchJoystick(joystick);
slouken@1895
   263
            SDL_JoystickClose(joystick);
slouken@1895
   264
        }
slouken@1895
   265
    }
slouken@1895
   266
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
slouken@0
   267
slouken@1895
   268
    return (0);
slouken@0
   269
}