test/testjoystick.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
     1.1 --- a/test/testjoystick.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/test/testjoystick.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -11,7 +11,7 @@
     1.4  #define SCREEN_HEIGHT	480
     1.5  
     1.6  void
     1.7 -WatchJoystick (SDL_Joystick * joystick)
     1.8 +WatchJoystick(SDL_Joystick * joystick)
     1.9  {
    1.10      SDL_Surface *screen;
    1.11      const char *name;
    1.12 @@ -21,62 +21,61 @@
    1.13      SDL_Rect axis_area[6][2];
    1.14  
    1.15      /* Set a video mode to display joystick axis position */
    1.16 -    screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.17 +    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.18      if (screen == NULL) {
    1.19 -        fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
    1.20 +        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    1.21          return;
    1.22      }
    1.23  
    1.24      /* Print info about the joystick we are watching */
    1.25 -    name = SDL_JoystickName (SDL_JoystickIndex (joystick));
    1.26 -    printf ("Watching joystick %d: (%s)\n", SDL_JoystickIndex (joystick),
    1.27 -            name ? name : "Unknown Joystick");
    1.28 -    printf ("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.29 -            SDL_JoystickNumAxes (joystick), SDL_JoystickNumHats (joystick),
    1.30 -            SDL_JoystickNumBalls (joystick),
    1.31 -            SDL_JoystickNumButtons (joystick));
    1.32 +    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.33 +    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.34 +           name ? name : "Unknown Joystick");
    1.35 +    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.36 +           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.37 +           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    1.38  
    1.39      /* Initialize drawing rectangles */
    1.40 -    memset (axis_area, 0, (sizeof axis_area));
    1.41 +    memset(axis_area, 0, (sizeof axis_area));
    1.42      draw = 0;
    1.43  
    1.44      /* Loop, getting joystick events! */
    1.45      done = 0;
    1.46      while (!done) {
    1.47 -        while (SDL_PollEvent (&event)) {
    1.48 +        while (SDL_PollEvent(&event)) {
    1.49              switch (event.type) {
    1.50              case SDL_JOYAXISMOTION:
    1.51 -                printf ("Joystick %d axis %d value: %d\n",
    1.52 -                        event.jaxis.which,
    1.53 -                        event.jaxis.axis, event.jaxis.value);
    1.54 +                printf("Joystick %d axis %d value: %d\n",
    1.55 +                       event.jaxis.which,
    1.56 +                       event.jaxis.axis, event.jaxis.value);
    1.57                  break;
    1.58              case SDL_JOYHATMOTION:
    1.59 -                printf ("Joystick %d hat %d value:",
    1.60 -                        event.jhat.which, event.jhat.hat);
    1.61 +                printf("Joystick %d hat %d value:",
    1.62 +                       event.jhat.which, event.jhat.hat);
    1.63                  if (event.jhat.value == SDL_HAT_CENTERED)
    1.64 -                    printf (" centered");
    1.65 +                    printf(" centered");
    1.66                  if (event.jhat.value & SDL_HAT_UP)
    1.67 -                    printf (" up");
    1.68 +                    printf(" up");
    1.69                  if (event.jhat.value & SDL_HAT_RIGHT)
    1.70 -                    printf (" right");
    1.71 +                    printf(" right");
    1.72                  if (event.jhat.value & SDL_HAT_DOWN)
    1.73 -                    printf (" down");
    1.74 +                    printf(" down");
    1.75                  if (event.jhat.value & SDL_HAT_LEFT)
    1.76 -                    printf (" left");
    1.77 -                printf ("\n");
    1.78 +                    printf(" left");
    1.79 +                printf("\n");
    1.80                  break;
    1.81              case SDL_JOYBALLMOTION:
    1.82 -                printf ("Joystick %d ball %d delta: (%d,%d)\n",
    1.83 -                        event.jball.which,
    1.84 -                        event.jball.ball, event.jball.xrel, event.jball.yrel);
    1.85 +                printf("Joystick %d ball %d delta: (%d,%d)\n",
    1.86 +                       event.jball.which,
    1.87 +                       event.jball.ball, event.jball.xrel, event.jball.yrel);
    1.88                  break;
    1.89              case SDL_JOYBUTTONDOWN:
    1.90 -                printf ("Joystick %d button %d down\n",
    1.91 -                        event.jbutton.which, event.jbutton.button);
    1.92 +                printf("Joystick %d button %d down\n",
    1.93 +                       event.jbutton.which, event.jbutton.button);
    1.94                  break;
    1.95              case SDL_JOYBUTTONUP:
    1.96 -                printf ("Joystick %d button %d up\n",
    1.97 -                        event.jbutton.which, event.jbutton.button);
    1.98 +                printf("Joystick %d button %d up\n",
    1.99 +                       event.jbutton.which, event.jbutton.button);
   1.100                  break;
   1.101              case SDL_KEYDOWN:
   1.102                  if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.103 @@ -91,30 +90,30 @@
   1.104              }
   1.105          }
   1.106          /* Update visual joystick state */
   1.107 -        for (i = 0; i < SDL_JoystickNumButtons (joystick); ++i) {
   1.108 +        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   1.109              SDL_Rect area;
   1.110  
   1.111              area.x = i * 34;
   1.112              area.y = SCREEN_HEIGHT - 34;
   1.113              area.w = 32;
   1.114              area.h = 32;
   1.115 -            if (SDL_JoystickGetButton (joystick, i) == SDL_PRESSED) {
   1.116 -                SDL_FillRect (screen, &area, 0xFFFF);
   1.117 +            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   1.118 +                SDL_FillRect(screen, &area, 0xFFFF);
   1.119              } else {
   1.120 -                SDL_FillRect (screen, &area, 0x0000);
   1.121 +                SDL_FillRect(screen, &area, 0x0000);
   1.122              }
   1.123 -            SDL_UpdateRects (screen, 1, &area);
   1.124 +            SDL_UpdateRects(screen, 1, &area);
   1.125          }
   1.126  
   1.127          for (i = 0;
   1.128 -             i < SDL_JoystickNumAxes (joystick) / 2
   1.129 -             && i < SDL_arraysize (axis_area); ++i) {
   1.130 +             i < SDL_JoystickNumAxes(joystick) / 2
   1.131 +             && i < SDL_arraysize(axis_area); ++i) {
   1.132              /* Erase previous axes */
   1.133 -            SDL_FillRect (screen, &axis_area[i][draw], 0x0000);
   1.134 +            SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
   1.135  
   1.136              /* Draw the X/Y axis */
   1.137              draw = !draw;
   1.138 -            x = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 0)) + 32768);
   1.139 +            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.140              x *= SCREEN_WIDTH;
   1.141              x /= 65535;
   1.142              if (x < 0) {
   1.143 @@ -122,7 +121,7 @@
   1.144              } else if (x > (SCREEN_WIDTH - 16)) {
   1.145                  x = SCREEN_WIDTH - 16;
   1.146              }
   1.147 -            y = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 1)) + 32768);
   1.148 +            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   1.149              y *= SCREEN_HEIGHT;
   1.150              y /= 65535;
   1.151              if (y < 0) {
   1.152 @@ -134,44 +133,44 @@
   1.153              axis_area[i][draw].y = (Sint16) y;
   1.154              axis_area[i][draw].w = 16;
   1.155              axis_area[i][draw].h = 16;
   1.156 -            SDL_FillRect (screen, &axis_area[i][draw], 0xFFFF);
   1.157 +            SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
   1.158  
   1.159 -            SDL_UpdateRects (screen, 2, axis_area[i]);
   1.160 +            SDL_UpdateRects(screen, 2, axis_area[i]);
   1.161          }
   1.162      }
   1.163  }
   1.164  
   1.165  int
   1.166 -main (int argc, char *argv[])
   1.167 +main(int argc, char *argv[])
   1.168  {
   1.169      const char *name;
   1.170      int i;
   1.171      SDL_Joystick *joystick;
   1.172  
   1.173      /* Initialize SDL (Note: video is required to start event loop) */
   1.174 -    if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.175 -        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.176 -        exit (1);
   1.177 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.178 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.179 +        exit(1);
   1.180      }
   1.181  
   1.182      /* Print information about the joysticks */
   1.183 -    printf ("There are %d joysticks attached\n", SDL_NumJoysticks ());
   1.184 -    for (i = 0; i < SDL_NumJoysticks (); ++i) {
   1.185 -        name = SDL_JoystickName (i);
   1.186 -        printf ("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   1.187 +    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.188 +    for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.189 +        name = SDL_JoystickName(i);
   1.190 +        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   1.191      }
   1.192  
   1.193      if (argv[1]) {
   1.194 -        joystick = SDL_JoystickOpen (atoi (argv[1]));
   1.195 +        joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.196          if (joystick == NULL) {
   1.197 -            printf ("Couldn't open joystick %d: %s\n", atoi (argv[1]),
   1.198 -                    SDL_GetError ());
   1.199 +            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   1.200 +                   SDL_GetError());
   1.201          } else {
   1.202 -            WatchJoystick (joystick);
   1.203 -            SDL_JoystickClose (joystick);
   1.204 +            WatchJoystick(joystick);
   1.205 +            SDL_JoystickClose(joystick);
   1.206          }
   1.207      }
   1.208 -    SDL_QuitSubSystem (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.209 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.210  
   1.211      return (0);
   1.212  }