test/testjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 15 Aug 2012 21:00:33 -0400
changeset 6398 c294faf5fce5
parent 6397 efdf44f20799
child 6586 ab57cc69dcc0
permissions -rw-r--r--
Cleaned up testjoystick.c, improved usage of renderer API, added colors!
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
icculus@6398
    32
static void
icculus@6398
    33
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
icculus@6398
    34
{
icculus@6398
    35
    const SDL_Rect area = { x, y, w, h };
icculus@6398
    36
    SDL_RenderFillRect(r, &area);
icculus@6398
    37
}
icculus@6398
    38
slouken@1895
    39
void
slouken@1895
    40
WatchJoystick(SDL_Joystick * joystick)
slouken@0
    41
{
icculus@6398
    42
    SDL_Window *window = NULL;
icculus@6398
    43
    SDL_Renderer *screen = NULL;
icculus@6398
    44
    const char *name = NULL;
icculus@6398
    45
    int done = 0;
slouken@1895
    46
    SDL_Event event;
icculus@6398
    47
    int i;
slouken@0
    48
icculus@6397
    49
    /* Create a window to display joystick axis position */
icculus@6397
    50
    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
icculus@6397
    51
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
icculus@6397
    52
                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
icculus@6397
    53
    if (window == NULL) {
icculus@6397
    54
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
slouken@1895
    55
        return;
slouken@1895
    56
    }
slouken@0
    57
icculus@6397
    58
    screen = SDL_CreateRenderer(window, -1, 0);
icculus@6397
    59
    if (screen == NULL) {
icculus@6397
    60
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
icculus@6397
    61
        SDL_DestroyWindow(window);
icculus@6397
    62
        return;
icculus@6397
    63
    }
icculus@6397
    64
icculus@6397
    65
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6397
    66
    SDL_RenderClear(screen);
icculus@6398
    67
    SDL_RenderPresent(screen);
icculus@6397
    68
slouken@1895
    69
    /* Print info about the joystick we are watching */
slouken@1895
    70
    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@1895
    71
    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@1895
    72
           name ? name : "Unknown Joystick");
slouken@1895
    73
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@1895
    74
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@1895
    75
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
slouken@0
    76
icculus@6398
    77
    /* Loop, getting joystick events! */
icculus@6398
    78
    while (!done) {
icculus@6398
    79
        /* blank screen, set up for drawing this frame. */
icculus@6398
    80
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6398
    81
        SDL_RenderClear(screen);
slouken@0
    82
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 */
icculus@6398
   131
        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
slouken@1895
   132
        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
slouken@1895
   133
            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
icculus@6398
   134
                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
slouken@1895
   135
            }
slouken@1895
   136
        }
slouken@0
   137
icculus@6398
   138
        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
icculus@6398
   139
        for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
slouken@1895
   140
            /* Draw the X/Y axis */
icculus@6398
   141
            int x, y;
slouken@1895
   142
            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
slouken@1895
   143
            x *= SCREEN_WIDTH;
slouken@1895
   144
            x /= 65535;
slouken@1895
   145
            if (x < 0) {
slouken@1895
   146
                x = 0;
slouken@1895
   147
            } else if (x > (SCREEN_WIDTH - 16)) {
slouken@1895
   148
                x = SCREEN_WIDTH - 16;
slouken@1895
   149
            }
slouken@1895
   150
            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
slouken@1895
   151
            y *= SCREEN_HEIGHT;
slouken@1895
   152
            y /= 65535;
slouken@1895
   153
            if (y < 0) {
slouken@1895
   154
                y = 0;
slouken@1895
   155
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@1895
   156
                y = SCREEN_HEIGHT - 16;
slouken@1895
   157
            }
slouken@2765
   158
icculus@6398
   159
            DrawRect(screen, x, y, 16, 16);
icculus@6397
   160
        }
icculus@6397
   161
icculus@6398
   162
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
icculus@6398
   163
        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
icculus@6397
   164
            /* Derive the new position */
icculus@6398
   165
            int x = SCREEN_WIDTH/2;
icculus@6398
   166
            int y = SCREEN_HEIGHT/2;
icculus@6398
   167
            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
icculus@6397
   168
icculus@6397
   169
            if (hat_pos & SDL_HAT_UP) {
icculus@6398
   170
                y = 0;
icculus@6397
   171
            } else if (hat_pos & SDL_HAT_DOWN) {
icculus@6398
   172
                y = SCREEN_HEIGHT-8;
icculus@6397
   173
            }
icculus@6397
   174
icculus@6397
   175
            if (hat_pos & SDL_HAT_LEFT) {
icculus@6398
   176
                x = 0;
icculus@6397
   177
            } else if (hat_pos & SDL_HAT_RIGHT) {
icculus@6398
   178
                x = SCREEN_WIDTH-8;
icculus@6397
   179
            }
icculus@6397
   180
icculus@6398
   181
            DrawRect(screen, x, y, 8, 8);
slouken@1895
   182
        }
icculus@6398
   183
icculus@6398
   184
        SDL_RenderPresent(screen);
slouken@1895
   185
    }
icculus@6397
   186
icculus@6397
   187
    SDL_DestroyRenderer(screen);
icculus@6397
   188
    SDL_DestroyWindow(window);
slouken@0
   189
}
slouken@0
   190
slouken@1895
   191
int
slouken@1895
   192
main(int argc, char *argv[])
slouken@0
   193
{
slouken@1895
   194
    const char *name;
slouken@1895
   195
    int i;
slouken@1895
   196
    SDL_Joystick *joystick;
slouken@0
   197
slouken@1895
   198
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@1895
   199
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
slouken@1895
   200
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   201
        exit(1);
slouken@1895
   202
    }
slouken@0
   203
slouken@1895
   204
    /* Print information about the joysticks */
slouken@1895
   205
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@1895
   206
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@1895
   207
        name = SDL_JoystickName(i);
slouken@2201
   208
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
icculus@2200
   209
        joystick = SDL_JoystickOpen(i);
icculus@2200
   210
        if (joystick == NULL) {
slouken@2201
   211
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
slouken@2201
   212
                    SDL_GetError());
icculus@2200
   213
        } else {
icculus@2200
   214
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
icculus@2200
   215
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
icculus@2200
   216
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
icculus@2200
   217
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
icculus@2200
   218
            SDL_JoystickClose(joystick);
icculus@2200
   219
        }
slouken@1895
   220
    }
slouken@0
   221
slouken@1895
   222
    if (argv[1]) {
slouken@1895
   223
        joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@1895
   224
        if (joystick == NULL) {
slouken@1895
   225
            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@1895
   226
                   SDL_GetError());
slouken@1895
   227
        } else {
slouken@1895
   228
            WatchJoystick(joystick);
slouken@1895
   229
            SDL_JoystickClose(joystick);
slouken@1895
   230
        }
slouken@1895
   231
    }
slouken@1895
   232
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
slouken@0
   233
slouken@1895
   234
    return (0);
slouken@0
   235
}