Updated testjoystick for SDL 2.0 API - patch from simon
authorSam Lantinga <slouken@libsdl.org>
Sun, 14 Oct 2012 00:24:07 -0700
changeset 6586ab57cc69dcc0
parent 6581 008057dcc079
child 6587 4ef7c70295c9
Updated testjoystick for SDL 2.0 API - patch from simon
test/Makefile.in
test/testjoystick.c
     1.1 --- a/test/Makefile.in	Fri Oct 12 02:56:41 2012 -0700
     1.2 +++ b/test/Makefile.in	Sun Oct 14 00:24:07 2012 -0700
     1.3 @@ -110,7 +110,7 @@
     1.4  testime$(EXE): $(srcdir)/testime.c $(srcdir)/common.c
     1.5  	$(CC) -o $@ $(srcdir)/testime.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
     1.6  
     1.7 -testjoystick$(EXE): $(srcdir)/testjoystick.c
     1.8 +testjoystick$(EXE): $(srcdir)/testjoystick.c $(srcdir)/common.c
     1.9  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.10  
    1.11  testkeys$(EXE): $(srcdir)/testkeys.c
     2.1 --- a/test/testjoystick.c	Fri Oct 12 02:56:41 2012 -0700
     2.2 +++ b/test/testjoystick.c	Sun Oct 14 00:24:07 2012 -0700
     2.3 @@ -17,14 +17,10 @@
     2.4  #include <string.h>
     2.5  
     2.6  #include "SDL.h"
     2.7 +#include "common.h"
     2.8  
     2.9 -#ifdef __IPHONEOS__
    2.10 -#define SCREEN_WIDTH	320
    2.11 -#define SCREEN_HEIGHT	480
    2.12 -#else
    2.13 -#define SCREEN_WIDTH	640
    2.14 -#define SCREEN_HEIGHT	480
    2.15 -#endif
    2.16 +static CommonState *state;
    2.17 +static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    2.18  
    2.19  #define MAX_NUM_AXES 6
    2.20  #define MAX_NUM_HATS 2
    2.21 @@ -41,31 +37,13 @@
    2.22  {
    2.23      SDL_Window *window = NULL;
    2.24      SDL_Renderer *screen = NULL;
    2.25 +    SDL_Rect viewport;
    2.26 +    SDL_Event event;
    2.27 +
    2.28      const char *name = NULL;
    2.29      int done = 0;
    2.30 -    SDL_Event event;
    2.31      int i;
    2.32  
    2.33 -    /* Create a window to display joystick axis position */
    2.34 -    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    2.35 -                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    2.36 -                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    2.37 -    if (window == NULL) {
    2.38 -        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    2.39 -        return;
    2.40 -    }
    2.41 -
    2.42 -    screen = SDL_CreateRenderer(window, -1, 0);
    2.43 -    if (screen == NULL) {
    2.44 -        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    2.45 -        SDL_DestroyWindow(window);
    2.46 -        return;
    2.47 -    }
    2.48 -
    2.49 -    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    2.50 -    SDL_RenderClear(screen);
    2.51 -    SDL_RenderPresent(screen);
    2.52 -
    2.53      /* Print info about the joystick we are watching */
    2.54      name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    2.55      printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    2.56 @@ -76,10 +54,6 @@
    2.57  
    2.58      /* Loop, getting joystick events! */
    2.59      while (!done) {
    2.60 -        /* blank screen, set up for drawing this frame. */
    2.61 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    2.62 -        SDL_RenderClear(screen);
    2.63 -
    2.64          while (SDL_PollEvent(&event)) {
    2.65              switch (event.type) {
    2.66              case SDL_JOYAXISMOTION:
    2.67 @@ -127,80 +101,96 @@
    2.68                  break;
    2.69              }
    2.70          }
    2.71 +
    2.72          /* Update visual joystick state */
    2.73 -        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
    2.74 -        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
    2.75 -            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    2.76 -                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
    2.77 -            }
    2.78 -        }
    2.79 +        for (i = 0; i < state->num_windows; ++i) {
    2.80 +            screen = state->renderers[i];
    2.81  
    2.82 -        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    2.83 -        for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
    2.84 -            /* Draw the X/Y axis */
    2.85 -            int x, y;
    2.86 -            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
    2.87 -            x *= SCREEN_WIDTH;
    2.88 -            x /= 65535;
    2.89 -            if (x < 0) {
    2.90 -                x = 0;
    2.91 -            } else if (x > (SCREEN_WIDTH - 16)) {
    2.92 -                x = SCREEN_WIDTH - 16;
    2.93 -            }
    2.94 -            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
    2.95 -            y *= SCREEN_HEIGHT;
    2.96 -            y /= 65535;
    2.97 -            if (y < 0) {
    2.98 -                y = 0;
    2.99 -            } else if (y > (SCREEN_HEIGHT - 16)) {
   2.100 -                y = SCREEN_HEIGHT - 16;
   2.101 +            /* Erase previous axes */
   2.102 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   2.103 +            SDL_RenderClear(screen);
   2.104 +
   2.105 +            /* Query the sizes */
   2.106 +            SDL_RenderGetViewport(screen, &viewport);
   2.107 +
   2.108 +            SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   2.109 +            for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   2.110 +                if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   2.111 +                    DrawRect(screen, i * 34, viewport.h - 34, 32, 32);
   2.112 +                }
   2.113              }
   2.114  
   2.115 -            DrawRect(screen, x, y, 16, 16);
   2.116 -        }
   2.117 +            SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   2.118 +            for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   2.119 +                /* Draw the X/Y axis */
   2.120 +                int x, y;
   2.121 +                x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   2.122 +                x *= viewport.w ;
   2.123 +                x /= 65535;
   2.124 +                if (x < 0) {
   2.125 +                    x = 0;
   2.126 +                } else if (x > (viewport.w - 16)) {
   2.127 +                    x = viewport.w - 16;
   2.128 +                }
   2.129 +                y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   2.130 +                y *= viewport.h;
   2.131 +                y /= 65535;
   2.132 +                if (y < 0) {
   2.133 +                    y = 0;
   2.134 +                } else if (y > (viewport.h - 16)) {
   2.135 +                    y = viewport.h - 16;
   2.136 +                }
   2.137  
   2.138 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   2.139 -        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   2.140 -            /* Derive the new position */
   2.141 -            int x = SCREEN_WIDTH/2;
   2.142 -            int y = SCREEN_HEIGHT/2;
   2.143 -            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   2.144 -
   2.145 -            if (hat_pos & SDL_HAT_UP) {
   2.146 -                y = 0;
   2.147 -            } else if (hat_pos & SDL_HAT_DOWN) {
   2.148 -                y = SCREEN_HEIGHT-8;
   2.149 +                DrawRect(screen, x, y, 16, 16);
   2.150              }
   2.151  
   2.152 -            if (hat_pos & SDL_HAT_LEFT) {
   2.153 -                x = 0;
   2.154 -            } else if (hat_pos & SDL_HAT_RIGHT) {
   2.155 -                x = SCREEN_WIDTH-8;
   2.156 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   2.157 +            for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   2.158 +                /* Derive the new position */
   2.159 +                int x = viewport.w/2;
   2.160 +                int y = viewport.h/2;
   2.161 +                const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   2.162 +
   2.163 +                if (hat_pos & SDL_HAT_UP) {
   2.164 +                    y = 0;
   2.165 +                } else if (hat_pos & SDL_HAT_DOWN) {
   2.166 +                    y = viewport.h-8;
   2.167 +                }
   2.168 +
   2.169 +                if (hat_pos & SDL_HAT_LEFT) {
   2.170 +                    x = 0;
   2.171 +                } else if (hat_pos & SDL_HAT_RIGHT) {
   2.172 +                    x = viewport.w-8;
   2.173 +                }
   2.174 +
   2.175 +                DrawRect(screen, x, y, 8, 8);
   2.176              }
   2.177  
   2.178 -            DrawRect(screen, x, y, 8, 8);
   2.179 +            SDL_RenderPresent(screen);
   2.180          }
   2.181 -
   2.182 -        SDL_RenderPresent(screen);
   2.183      }
   2.184 -
   2.185 -    SDL_DestroyRenderer(screen);
   2.186 -    SDL_DestroyWindow(window);
   2.187  }
   2.188  
   2.189  int
   2.190  main(int argc, char *argv[])
   2.191  {
   2.192      const char *name;
   2.193 -    int i;
   2.194 +    int i, joy=-1;
   2.195      SDL_Joystick *joystick;
   2.196  
   2.197      /* Initialize SDL (Note: video is required to start event loop) */
   2.198 -    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   2.199 +    if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
   2.200          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   2.201          exit(1);
   2.202      }
   2.203  
   2.204 +    /* Initialize test framework */
   2.205 +    state = CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   2.206 +    if (!state) {
   2.207 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   2.208 +        return 1;
   2.209 +    }
   2.210 +
   2.211      /* Print information about the joysticks */
   2.212      printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   2.213      for (i = 0; i < SDL_NumJoysticks(); ++i) {
   2.214 @@ -219,17 +209,38 @@
   2.215          }
   2.216      }
   2.217  
   2.218 -    if (argv[1]) {
   2.219 -        joystick = SDL_JoystickOpen(atoi(argv[1]));
   2.220 +    for (i = 1; i < argc;) {
   2.221 +        int consumed;
   2.222 +
   2.223 +        consumed = CommonArg(state, i);
   2.224 +        if (consumed == 0) {
   2.225 +            consumed = -1;
   2.226 +            if (SDL_isdigit(*argv[i])) {
   2.227 +                joy = SDL_atoi(argv[i]);
   2.228 +                consumed = 1;
   2.229 +            }
   2.230 +        }
   2.231 +        if (consumed < 0) {
   2.232 +            return 1;
   2.233 +        }
   2.234 +        i += consumed;
   2.235 +    }
   2.236 +    if (!CommonInit(state)) {
   2.237 +        return 2;
   2.238 +    }
   2.239 +
   2.240 +    if (joy > -1) {
   2.241 +        joystick = SDL_JoystickOpen(joy);
   2.242          if (joystick == NULL) {
   2.243 -            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   2.244 +            printf("Couldn't open joystick %d: %s\n", joy,
   2.245                     SDL_GetError());
   2.246          } else {
   2.247              WatchJoystick(joystick);
   2.248              SDL_JoystickClose(joystick);
   2.249          }
   2.250      }
   2.251 -    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   2.252 +    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
   2.253 +    CommonQuit(state);
   2.254  
   2.255      return (0);
   2.256  }