test/testjoystick.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 19:23:13 +0000
branchgsoc2008_iphone
changeset 2377 e9a0bad0ea07
parent 2201 6280c111ee80
child 2433 450a9bf50c37
child 2765 f55c87ae336b
permissions -rw-r--r--
Just renamed it...
slouken@0
     1
slouken@0
     2
/* Simple program to test the SDL joystick routines */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
slouken@0
    10
#define SCREEN_WIDTH	640
slouken@0
    11
#define SCREEN_HEIGHT	480
slouken@0
    12
slouken@1895
    13
void
slouken@1895
    14
WatchJoystick(SDL_Joystick * joystick)
slouken@0
    15
{
slouken@1895
    16
    SDL_Surface *screen;
slouken@1895
    17
    const char *name;
slouken@1895
    18
    int i, done;
slouken@1895
    19
    SDL_Event event;
slouken@1895
    20
    int x, y, draw;
slouken@1895
    21
    SDL_Rect axis_area[6][2];
slouken@0
    22
slouken@1895
    23
    /* Set a video mode to display joystick axis position */
slouken@1895
    24
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
slouken@1895
    25
    if (screen == NULL) {
slouken@1895
    26
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
slouken@1895
    27
        return;
slouken@1895
    28
    }
slouken@0
    29
slouken@1895
    30
    /* Print info about the joystick we are watching */
slouken@1895
    31
    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@1895
    32
    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@1895
    33
           name ? name : "Unknown Joystick");
slouken@1895
    34
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@1895
    35
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@1895
    36
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
slouken@0
    37
slouken@1895
    38
    /* Initialize drawing rectangles */
slouken@1895
    39
    memset(axis_area, 0, (sizeof axis_area));
slouken@1895
    40
    draw = 0;
slouken@0
    41
slouken@1895
    42
    /* Loop, getting joystick events! */
slouken@1895
    43
    done = 0;
slouken@1895
    44
    while (!done) {
slouken@1895
    45
        while (SDL_PollEvent(&event)) {
slouken@1895
    46
            switch (event.type) {
slouken@1895
    47
            case SDL_JOYAXISMOTION:
slouken@1895
    48
                printf("Joystick %d axis %d value: %d\n",
slouken@1895
    49
                       event.jaxis.which,
slouken@1895
    50
                       event.jaxis.axis, event.jaxis.value);
slouken@1895
    51
                break;
slouken@1895
    52
            case SDL_JOYHATMOTION:
slouken@1895
    53
                printf("Joystick %d hat %d value:",
slouken@1895
    54
                       event.jhat.which, event.jhat.hat);
slouken@1895
    55
                if (event.jhat.value == SDL_HAT_CENTERED)
slouken@1895
    56
                    printf(" centered");
slouken@1895
    57
                if (event.jhat.value & SDL_HAT_UP)
slouken@1895
    58
                    printf(" up");
slouken@1895
    59
                if (event.jhat.value & SDL_HAT_RIGHT)
slouken@1895
    60
                    printf(" right");
slouken@1895
    61
                if (event.jhat.value & SDL_HAT_DOWN)
slouken@1895
    62
                    printf(" down");
slouken@1895
    63
                if (event.jhat.value & SDL_HAT_LEFT)
slouken@1895
    64
                    printf(" left");
slouken@1895
    65
                printf("\n");
slouken@1895
    66
                break;
slouken@1895
    67
            case SDL_JOYBALLMOTION:
slouken@1895
    68
                printf("Joystick %d ball %d delta: (%d,%d)\n",
slouken@1895
    69
                       event.jball.which,
slouken@1895
    70
                       event.jball.ball, event.jball.xrel, event.jball.yrel);
slouken@1895
    71
                break;
slouken@1895
    72
            case SDL_JOYBUTTONDOWN:
slouken@1895
    73
                printf("Joystick %d button %d down\n",
slouken@1895
    74
                       event.jbutton.which, event.jbutton.button);
slouken@1895
    75
                break;
slouken@1895
    76
            case SDL_JOYBUTTONUP:
slouken@1895
    77
                printf("Joystick %d button %d up\n",
slouken@1895
    78
                       event.jbutton.which, event.jbutton.button);
slouken@1895
    79
                break;
slouken@1895
    80
            case SDL_KEYDOWN:
slouken@1895
    81
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@1895
    82
                    break;
slouken@1895
    83
                }
slouken@1895
    84
                /* Fall through to signal quit */
slouken@1895
    85
            case SDL_QUIT:
slouken@1895
    86
                done = 1;
slouken@1895
    87
                break;
slouken@1895
    88
            default:
slouken@1895
    89
                break;
slouken@1895
    90
            }
slouken@1895
    91
        }
slouken@1895
    92
        /* Update visual joystick state */
slouken@1895
    93
        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
slouken@1895
    94
            SDL_Rect area;
slouken@0
    95
slouken@1895
    96
            area.x = i * 34;
slouken@1895
    97
            area.y = SCREEN_HEIGHT - 34;
slouken@1895
    98
            area.w = 32;
slouken@1895
    99
            area.h = 32;
slouken@1895
   100
            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
slouken@1895
   101
                SDL_FillRect(screen, &area, 0xFFFF);
slouken@1895
   102
            } else {
slouken@1895
   103
                SDL_FillRect(screen, &area, 0x0000);
slouken@1895
   104
            }
slouken@1895
   105
            SDL_UpdateRects(screen, 1, &area);
slouken@1895
   106
        }
slouken@0
   107
slouken@1895
   108
        for (i = 0;
slouken@1895
   109
             i < SDL_JoystickNumAxes(joystick) / 2
slouken@1895
   110
             && i < SDL_arraysize(axis_area); ++i) {
slouken@1895
   111
            /* Erase previous axes */
slouken@1895
   112
            SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
slouken@0
   113
slouken@1895
   114
            /* Draw the X/Y axis */
slouken@1895
   115
            draw = !draw;
slouken@1895
   116
            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
slouken@1895
   117
            x *= SCREEN_WIDTH;
slouken@1895
   118
            x /= 65535;
slouken@1895
   119
            if (x < 0) {
slouken@1895
   120
                x = 0;
slouken@1895
   121
            } else if (x > (SCREEN_WIDTH - 16)) {
slouken@1895
   122
                x = SCREEN_WIDTH - 16;
slouken@1895
   123
            }
slouken@1895
   124
            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
slouken@1895
   125
            y *= SCREEN_HEIGHT;
slouken@1895
   126
            y /= 65535;
slouken@1895
   127
            if (y < 0) {
slouken@1895
   128
                y = 0;
slouken@1895
   129
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@1895
   130
                y = SCREEN_HEIGHT - 16;
slouken@1895
   131
            }
slouken@1895
   132
            axis_area[i][draw].x = (Sint16) x;
slouken@1895
   133
            axis_area[i][draw].y = (Sint16) y;
slouken@1895
   134
            axis_area[i][draw].w = 16;
slouken@1895
   135
            axis_area[i][draw].h = 16;
slouken@1895
   136
            SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
slouken@1854
   137
slouken@1895
   138
            SDL_UpdateRects(screen, 2, axis_area[i]);
slouken@1895
   139
        }
slouken@1895
   140
    }
slouken@0
   141
}
slouken@0
   142
slouken@1895
   143
int
slouken@1895
   144
main(int argc, char *argv[])
slouken@0
   145
{
slouken@1895
   146
    const char *name;
slouken@1895
   147
    int i;
slouken@1895
   148
    SDL_Joystick *joystick;
slouken@0
   149
slouken@1895
   150
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@1895
   151
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
slouken@1895
   152
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   153
        exit(1);
slouken@1895
   154
    }
slouken@0
   155
slouken@1895
   156
    /* Print information about the joysticks */
slouken@1895
   157
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@1895
   158
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@1895
   159
        name = SDL_JoystickName(i);
slouken@2201
   160
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
icculus@2200
   161
        joystick = SDL_JoystickOpen(i);
icculus@2200
   162
        if (joystick == NULL) {
slouken@2201
   163
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
slouken@2201
   164
                    SDL_GetError());
icculus@2200
   165
        } else {
icculus@2200
   166
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
icculus@2200
   167
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
icculus@2200
   168
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
icculus@2200
   169
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
icculus@2200
   170
            SDL_JoystickClose(joystick);
icculus@2200
   171
        }
slouken@1895
   172
    }
slouken@0
   173
slouken@1895
   174
    if (argv[1]) {
slouken@1895
   175
        joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@1895
   176
        if (joystick == NULL) {
slouken@1895
   177
            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@1895
   178
                   SDL_GetError());
slouken@1895
   179
        } else {
slouken@1895
   180
            WatchJoystick(joystick);
slouken@1895
   181
            SDL_JoystickClose(joystick);
slouken@1895
   182
        }
slouken@1895
   183
    }
slouken@1895
   184
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
slouken@0
   185
slouken@1895
   186
    return (0);
slouken@0
   187
}