test/testjoystick.c
changeset 6586 ab57cc69dcc0
parent 6398 c294faf5fce5
child 6690 9548c8a58103
     1.1 --- a/test/testjoystick.c	Fri Oct 12 02:56:41 2012 -0700
     1.2 +++ b/test/testjoystick.c	Sun Oct 14 00:24:07 2012 -0700
     1.3 @@ -17,14 +17,10 @@
     1.4  #include <string.h>
     1.5  
     1.6  #include "SDL.h"
     1.7 +#include "common.h"
     1.8  
     1.9 -#ifdef __IPHONEOS__
    1.10 -#define SCREEN_WIDTH	320
    1.11 -#define SCREEN_HEIGHT	480
    1.12 -#else
    1.13 -#define SCREEN_WIDTH	640
    1.14 -#define SCREEN_HEIGHT	480
    1.15 -#endif
    1.16 +static CommonState *state;
    1.17 +static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    1.18  
    1.19  #define MAX_NUM_AXES 6
    1.20  #define MAX_NUM_HATS 2
    1.21 @@ -41,31 +37,13 @@
    1.22  {
    1.23      SDL_Window *window = NULL;
    1.24      SDL_Renderer *screen = NULL;
    1.25 +    SDL_Rect viewport;
    1.26 +    SDL_Event event;
    1.27 +
    1.28      const char *name = NULL;
    1.29      int done = 0;
    1.30 -    SDL_Event event;
    1.31      int i;
    1.32  
    1.33 -    /* Create a window to display joystick axis position */
    1.34 -    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    1.35 -                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.36 -                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    1.37 -    if (window == NULL) {
    1.38 -        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    1.39 -        return;
    1.40 -    }
    1.41 -
    1.42 -    screen = SDL_CreateRenderer(window, -1, 0);
    1.43 -    if (screen == NULL) {
    1.44 -        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    1.45 -        SDL_DestroyWindow(window);
    1.46 -        return;
    1.47 -    }
    1.48 -
    1.49 -    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.50 -    SDL_RenderClear(screen);
    1.51 -    SDL_RenderPresent(screen);
    1.52 -
    1.53      /* Print info about the joystick we are watching */
    1.54      name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.55      printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.56 @@ -76,10 +54,6 @@
    1.57  
    1.58      /* Loop, getting joystick events! */
    1.59      while (!done) {
    1.60 -        /* blank screen, set up for drawing this frame. */
    1.61 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.62 -        SDL_RenderClear(screen);
    1.63 -
    1.64          while (SDL_PollEvent(&event)) {
    1.65              switch (event.type) {
    1.66              case SDL_JOYAXISMOTION:
    1.67 @@ -127,80 +101,96 @@
    1.68                  break;
    1.69              }
    1.70          }
    1.71 +
    1.72          /* Update visual joystick state */
    1.73 -        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
    1.74 -        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
    1.75 -            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    1.76 -                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
    1.77 -            }
    1.78 -        }
    1.79 +        for (i = 0; i < state->num_windows; ++i) {
    1.80 +            screen = state->renderers[i];
    1.81  
    1.82 -        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.83 -        for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
    1.84 -            /* Draw the X/Y axis */
    1.85 -            int x, y;
    1.86 -            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
    1.87 -            x *= SCREEN_WIDTH;
    1.88 -            x /= 65535;
    1.89 -            if (x < 0) {
    1.90 -                x = 0;
    1.91 -            } else if (x > (SCREEN_WIDTH - 16)) {
    1.92 -                x = SCREEN_WIDTH - 16;
    1.93 -            }
    1.94 -            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
    1.95 -            y *= SCREEN_HEIGHT;
    1.96 -            y /= 65535;
    1.97 -            if (y < 0) {
    1.98 -                y = 0;
    1.99 -            } else if (y > (SCREEN_HEIGHT - 16)) {
   1.100 -                y = SCREEN_HEIGHT - 16;
   1.101 +            /* Erase previous axes */
   1.102 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.103 +            SDL_RenderClear(screen);
   1.104 +
   1.105 +            /* Query the sizes */
   1.106 +            SDL_RenderGetViewport(screen, &viewport);
   1.107 +
   1.108 +            SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   1.109 +            for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   1.110 +                if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   1.111 +                    DrawRect(screen, i * 34, viewport.h - 34, 32, 32);
   1.112 +                }
   1.113              }
   1.114  
   1.115 -            DrawRect(screen, x, y, 16, 16);
   1.116 -        }
   1.117 +            SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.118 +            for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   1.119 +                /* Draw the X/Y axis */
   1.120 +                int x, y;
   1.121 +                x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.122 +                x *= viewport.w ;
   1.123 +                x /= 65535;
   1.124 +                if (x < 0) {
   1.125 +                    x = 0;
   1.126 +                } else if (x > (viewport.w - 16)) {
   1.127 +                    x = viewport.w - 16;
   1.128 +                }
   1.129 +                y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   1.130 +                y *= viewport.h;
   1.131 +                y /= 65535;
   1.132 +                if (y < 0) {
   1.133 +                    y = 0;
   1.134 +                } else if (y > (viewport.h - 16)) {
   1.135 +                    y = viewport.h - 16;
   1.136 +                }
   1.137  
   1.138 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.139 -        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   1.140 -            /* Derive the new position */
   1.141 -            int x = SCREEN_WIDTH/2;
   1.142 -            int y = SCREEN_HEIGHT/2;
   1.143 -            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   1.144 -
   1.145 -            if (hat_pos & SDL_HAT_UP) {
   1.146 -                y = 0;
   1.147 -            } else if (hat_pos & SDL_HAT_DOWN) {
   1.148 -                y = SCREEN_HEIGHT-8;
   1.149 +                DrawRect(screen, x, y, 16, 16);
   1.150              }
   1.151  
   1.152 -            if (hat_pos & SDL_HAT_LEFT) {
   1.153 -                x = 0;
   1.154 -            } else if (hat_pos & SDL_HAT_RIGHT) {
   1.155 -                x = SCREEN_WIDTH-8;
   1.156 +            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.157 +            for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   1.158 +                /* Derive the new position */
   1.159 +                int x = viewport.w/2;
   1.160 +                int y = viewport.h/2;
   1.161 +                const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   1.162 +
   1.163 +                if (hat_pos & SDL_HAT_UP) {
   1.164 +                    y = 0;
   1.165 +                } else if (hat_pos & SDL_HAT_DOWN) {
   1.166 +                    y = viewport.h-8;
   1.167 +                }
   1.168 +
   1.169 +                if (hat_pos & SDL_HAT_LEFT) {
   1.170 +                    x = 0;
   1.171 +                } else if (hat_pos & SDL_HAT_RIGHT) {
   1.172 +                    x = viewport.w-8;
   1.173 +                }
   1.174 +
   1.175 +                DrawRect(screen, x, y, 8, 8);
   1.176              }
   1.177  
   1.178 -            DrawRect(screen, x, y, 8, 8);
   1.179 +            SDL_RenderPresent(screen);
   1.180          }
   1.181 -
   1.182 -        SDL_RenderPresent(screen);
   1.183      }
   1.184 -
   1.185 -    SDL_DestroyRenderer(screen);
   1.186 -    SDL_DestroyWindow(window);
   1.187  }
   1.188  
   1.189  int
   1.190  main(int argc, char *argv[])
   1.191  {
   1.192      const char *name;
   1.193 -    int i;
   1.194 +    int i, joy=-1;
   1.195      SDL_Joystick *joystick;
   1.196  
   1.197      /* Initialize SDL (Note: video is required to start event loop) */
   1.198 -    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.199 +    if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
   1.200          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.201          exit(1);
   1.202      }
   1.203  
   1.204 +    /* Initialize test framework */
   1.205 +    state = CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.206 +    if (!state) {
   1.207 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.208 +        return 1;
   1.209 +    }
   1.210 +
   1.211      /* Print information about the joysticks */
   1.212      printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.213      for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.214 @@ -219,17 +209,38 @@
   1.215          }
   1.216      }
   1.217  
   1.218 -    if (argv[1]) {
   1.219 -        joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.220 +    for (i = 1; i < argc;) {
   1.221 +        int consumed;
   1.222 +
   1.223 +        consumed = CommonArg(state, i);
   1.224 +        if (consumed == 0) {
   1.225 +            consumed = -1;
   1.226 +            if (SDL_isdigit(*argv[i])) {
   1.227 +                joy = SDL_atoi(argv[i]);
   1.228 +                consumed = 1;
   1.229 +            }
   1.230 +        }
   1.231 +        if (consumed < 0) {
   1.232 +            return 1;
   1.233 +        }
   1.234 +        i += consumed;
   1.235 +    }
   1.236 +    if (!CommonInit(state)) {
   1.237 +        return 2;
   1.238 +    }
   1.239 +
   1.240 +    if (joy > -1) {
   1.241 +        joystick = SDL_JoystickOpen(joy);
   1.242          if (joystick == NULL) {
   1.243 -            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   1.244 +            printf("Couldn't open joystick %d: %s\n", joy,
   1.245                     SDL_GetError());
   1.246          } else {
   1.247              WatchJoystick(joystick);
   1.248              SDL_JoystickClose(joystick);
   1.249          }
   1.250      }
   1.251 -    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.252 +    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
   1.253 +    CommonQuit(state);
   1.254  
   1.255      return (0);
   1.256  }