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