test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1661 281d3f4870e5
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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