test/testjoystick.c
changeset 6397 efdf44f20799
parent 6256 1d905b13b102
child 6398 c294faf5fce5
     1.1 --- a/test/testjoystick.c	Sun Aug 12 23:10:16 2012 -0700
     1.2 +++ b/test/testjoystick.c	Wed Aug 15 20:53:24 2012 -0400
     1.3 @@ -12,15 +12,6 @@
     1.4  
     1.5  /* Simple program to test the SDL joystick routines */
     1.6  
     1.7 -#if 1 /* FIXME: Rework this using the 2.0 API */
     1.8 -#include <stdio.h>
     1.9 -
    1.10 -int main(int argc, char *argv[])
    1.11 -{
    1.12 -    printf("FIXME\n");
    1.13 -    return 0;
    1.14 -}
    1.15 -#else
    1.16  #include <stdio.h>
    1.17  #include <stdlib.h>
    1.18  #include <string.h>
    1.19 @@ -35,23 +26,43 @@
    1.20  #define SCREEN_HEIGHT	480
    1.21  #endif
    1.22  
    1.23 +#define MAX_NUM_AXES 6
    1.24 +#define MAX_NUM_HATS 2
    1.25 +
    1.26  void
    1.27  WatchJoystick(SDL_Joystick * joystick)
    1.28  {
    1.29 -    SDL_Surface *screen;
    1.30 +    SDL_Window *window;
    1.31 +    SDL_Renderer *screen;
    1.32      const char *name;
    1.33      int i, done;
    1.34      SDL_Event event;
    1.35 -    int x, y, draw;
    1.36 -    SDL_Rect axis_area[6][2];
    1.37 +    int x, y;
    1.38 +    SDL_Rect axis_area[MAX_NUM_AXES][2];
    1.39 +    int axis_draw[MAX_NUM_AXES];
    1.40 +    SDL_Rect hat_area[MAX_NUM_HATS][2];
    1.41 +    int hat_draw[MAX_NUM_HATS];
    1.42 +    Uint8 hat_pos;
    1.43  
    1.44 -    /* Set a video mode to display joystick axis position */
    1.45 -    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.46 -    if (screen == NULL) {
    1.47 -        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    1.48 +    /* Create a window to display joystick axis position */
    1.49 +    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    1.50 +                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.51 +                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    1.52 +    if (window == NULL) {
    1.53 +        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    1.54          return;
    1.55      }
    1.56  
    1.57 +    screen = SDL_CreateRenderer(window, -1, 0);
    1.58 +    if (screen == NULL) {
    1.59 +        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    1.60 +        SDL_DestroyWindow(window);
    1.61 +        return;
    1.62 +    }
    1.63 +
    1.64 +    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.65 +    SDL_RenderClear(screen);
    1.66 +
    1.67      /* Print info about the joystick we are watching */
    1.68      name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.69      printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.70 @@ -62,7 +73,9 @@
    1.71  
    1.72      /* Initialize drawing rectangles */
    1.73      memset(axis_area, 0, (sizeof axis_area));
    1.74 -    draw = 0;
    1.75 +    memset(axis_draw, 0, (sizeof axis_draw));
    1.76 +    memset(hat_area, 0, (sizeof hat_area));
    1.77 +    memset(hat_draw, 0, (sizeof hat_draw));
    1.78  
    1.79      /* Loop, getting joystick events! */
    1.80      done = 0;
    1.81 @@ -123,21 +136,24 @@
    1.82              area.w = 32;
    1.83              area.h = 32;
    1.84              if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    1.85 -                SDL_FillRect(screen, &area, 0xFFFF);
    1.86 +                SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
    1.87              } else {
    1.88 -                SDL_FillRect(screen, &area, 0x0000);
    1.89 +                SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.90              }
    1.91 -            SDL_UpdateRects(screen, 1, &area);
    1.92 +            SDL_RenderFillRect(screen, &area);
    1.93 +            SDL_RenderPresent(screen);
    1.94          }
    1.95  
    1.96          for (i = 0;
    1.97               i < SDL_JoystickNumAxes(joystick) / 2
    1.98               && i < SDL_arraysize(axis_area); ++i) {
    1.99 +
   1.100              /* Erase previous axes */
   1.101 -            SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
   1.102 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.103 +            SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
   1.104  
   1.105              /* Draw the X/Y axis */
   1.106 -            draw = !draw;
   1.107 +            axis_draw[i] = !axis_draw[i];
   1.108              x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.109              x *= SCREEN_WIDTH;
   1.110              x /= 65535;
   1.111 @@ -155,15 +171,55 @@
   1.112                  y = SCREEN_HEIGHT - 16;
   1.113              }
   1.114  
   1.115 -            axis_area[i][draw].x = (Sint16) x;
   1.116 -            axis_area[i][draw].y = (Sint16) y;
   1.117 -            axis_area[i][draw].w = 16;
   1.118 -            axis_area[i][draw].h = 16;
   1.119 -            SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
   1.120 +            axis_area[i][axis_draw[i]].x = (Sint16) x;
   1.121 +            axis_area[i][axis_draw[i]].y = (Sint16) y;
   1.122 +            axis_area[i][axis_draw[i]].w = 16;
   1.123 +            axis_area[i][axis_draw[i]].h = 16;
   1.124  
   1.125 -            SDL_UpdateRects(screen, 2, axis_area[i]);
   1.126 +            SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   1.127 +            SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
   1.128 +            SDL_RenderPresent(screen);
   1.129 +        }
   1.130 +
   1.131 +        for (i = 0;
   1.132 +             i < SDL_JoystickNumHats(joystick)
   1.133 +             && i < SDL_arraysize(hat_area); ++i) {
   1.134 +
   1.135 +            /* Erase previous hat position */
   1.136 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.137 +            SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
   1.138 +
   1.139 +            hat_draw[i] = !hat_draw[i];
   1.140 +
   1.141 +            /* Derive the new position */
   1.142 +            hat_pos = SDL_JoystickGetHat(joystick, i);
   1.143 +
   1.144 +            hat_area[i][hat_draw[i]].x = SCREEN_WIDTH/2;
   1.145 +            hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT/2;
   1.146 +            hat_area[i][hat_draw[i]].w = 8;
   1.147 +            hat_area[i][hat_draw[i]].h = 8;
   1.148 +
   1.149 +            if (hat_pos & SDL_HAT_UP) {
   1.150 +                hat_area[i][hat_draw[i]].y = 0;
   1.151 +            } else if (hat_pos & SDL_HAT_DOWN) {
   1.152 +                hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT-8;
   1.153 +            }
   1.154 +
   1.155 +            if (hat_pos & SDL_HAT_LEFT) {
   1.156 +                hat_area[i][hat_draw[i]].x = 0;
   1.157 +            } else if (hat_pos & SDL_HAT_RIGHT) {
   1.158 +                hat_area[i][hat_draw[i]].x = SCREEN_WIDTH-8;
   1.159 +            }
   1.160 +
   1.161 +            /* Draw it */
   1.162 +            SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   1.163 +            SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
   1.164 +            SDL_RenderPresent(screen);
   1.165          }
   1.166      }
   1.167 +
   1.168 +    SDL_DestroyRenderer(screen);
   1.169 +    SDL_DestroyWindow(window);
   1.170  }
   1.171  
   1.172  int
   1.173 @@ -211,4 +267,3 @@
   1.174  
   1.175      return (0);
   1.176  }
   1.177 -#endif