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