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