test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Oct 2008 06:46:59 +0000
changeset 2765 f55c87ae336b
parent 2201 6280c111ee80
child 5535 96594ac5fd1a
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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