test/testjoystick.c
changeset 6690 9548c8a58103
parent 6586 ab57cc69dcc0
child 6730 36158e4061f4
     1.1 --- a/test/testjoystick.c	Mon Nov 26 14:59:07 2012 -0800
     1.2 +++ b/test/testjoystick.c	Mon Nov 26 16:37:54 2012 -0800
     1.3 @@ -17,10 +17,14 @@
     1.4  #include <string.h>
     1.5  
     1.6  #include "SDL.h"
     1.7 -#include "common.h"
     1.8  
     1.9 -static CommonState *state;
    1.10 -static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    1.11 +#ifdef __IPHONEOS__
    1.12 +#define SCREEN_WIDTH	320
    1.13 +#define SCREEN_HEIGHT	480
    1.14 +#else
    1.15 +#define SCREEN_WIDTH	640
    1.16 +#define SCREEN_HEIGHT	480
    1.17 +#endif
    1.18  
    1.19  #define MAX_NUM_AXES 6
    1.20  #define MAX_NUM_HATS 2
    1.21 @@ -37,16 +41,35 @@
    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 +	SDL_RaiseWindow(window);
    1.53 +
    1.54      /* Print info about the joystick we are watching */
    1.55 -    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.56 -    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.57 +    name = SDL_JoystickName(joystick);
    1.58 +    printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    1.59             name ? name : "Unknown Joystick");
    1.60      printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.61             SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.62 @@ -54,6 +77,10 @@
    1.63  
    1.64      /* Loop, getting joystick events! */
    1.65      while (!done) {
    1.66 +        /* blank screen, set up for drawing this frame. */
    1.67 +        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.68 +        SDL_RenderClear(screen);
    1.69 +
    1.70          while (SDL_PollEvent(&event)) {
    1.71              switch (event.type) {
    1.72              case SDL_JOYAXISMOTION:
    1.73 @@ -101,100 +128,86 @@
    1.74                  break;
    1.75              }
    1.76          }
    1.77 +        /* Update visual joystick state */
    1.78 +        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
    1.79 +        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
    1.80 +            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    1.81 +                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
    1.82 +            }
    1.83 +        }
    1.84  
    1.85 -        /* Update visual joystick state */
    1.86 -        for (i = 0; i < state->num_windows; ++i) {
    1.87 -            screen = state->renderers[i];
    1.88 -
    1.89 -            /* Erase previous axes */
    1.90 -            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.91 -            SDL_RenderClear(screen);
    1.92 -
    1.93 -            /* Query the sizes */
    1.94 -            SDL_RenderGetViewport(screen, &viewport);
    1.95 -
    1.96 -            SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
    1.97 -            for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
    1.98 -                if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    1.99 -                    DrawRect(screen, i * 34, viewport.h - 34, 32, 32);
   1.100 -                }
   1.101 +        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.102 +        for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   1.103 +            /* Draw the X/Y axis */
   1.104 +            int x, y;
   1.105 +            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.106 +            x *= SCREEN_WIDTH;
   1.107 +            x /= 65535;
   1.108 +            if (x < 0) {
   1.109 +                x = 0;
   1.110 +            } else if (x > (SCREEN_WIDTH - 16)) {
   1.111 +                x = SCREEN_WIDTH - 16;
   1.112 +            }
   1.113 +            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   1.114 +            y *= SCREEN_HEIGHT;
   1.115 +            y /= 65535;
   1.116 +            if (y < 0) {
   1.117 +                y = 0;
   1.118 +            } else if (y > (SCREEN_HEIGHT - 16)) {
   1.119 +                y = SCREEN_HEIGHT - 16;
   1.120              }
   1.121  
   1.122 -            SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.123 -            for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   1.124 -                /* Draw the X/Y axis */
   1.125 -                int x, y;
   1.126 -                x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.127 -                x *= viewport.w ;
   1.128 -                x /= 65535;
   1.129 -                if (x < 0) {
   1.130 -                    x = 0;
   1.131 -                } else if (x > (viewport.w - 16)) {
   1.132 -                    x = viewport.w - 16;
   1.133 -                }
   1.134 -                y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   1.135 -                y *= viewport.h;
   1.136 -                y /= 65535;
   1.137 -                if (y < 0) {
   1.138 -                    y = 0;
   1.139 -                } else if (y > (viewport.h - 16)) {
   1.140 -                    y = viewport.h - 16;
   1.141 -                }
   1.142 +            DrawRect(screen, x, y, 16, 16);
   1.143 +        }
   1.144  
   1.145 -                DrawRect(screen, x, y, 16, 16);
   1.146 +        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.147 +        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   1.148 +            /* Derive the new position */
   1.149 +            int x = SCREEN_WIDTH/2;
   1.150 +            int y = SCREEN_HEIGHT/2;
   1.151 +            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   1.152 +
   1.153 +            if (hat_pos & SDL_HAT_UP) {
   1.154 +                y = 0;
   1.155 +            } else if (hat_pos & SDL_HAT_DOWN) {
   1.156 +                y = SCREEN_HEIGHT-8;
   1.157              }
   1.158  
   1.159 -            SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.160 -            for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   1.161 -                /* Derive the new position */
   1.162 -                int x = viewport.w/2;
   1.163 -                int y = viewport.h/2;
   1.164 -                const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   1.165 -
   1.166 -                if (hat_pos & SDL_HAT_UP) {
   1.167 -                    y = 0;
   1.168 -                } else if (hat_pos & SDL_HAT_DOWN) {
   1.169 -                    y = viewport.h-8;
   1.170 -                }
   1.171 -
   1.172 -                if (hat_pos & SDL_HAT_LEFT) {
   1.173 -                    x = 0;
   1.174 -                } else if (hat_pos & SDL_HAT_RIGHT) {
   1.175 -                    x = viewport.w-8;
   1.176 -                }
   1.177 -
   1.178 -                DrawRect(screen, x, y, 8, 8);
   1.179 +            if (hat_pos & SDL_HAT_LEFT) {
   1.180 +                x = 0;
   1.181 +            } else if (hat_pos & SDL_HAT_RIGHT) {
   1.182 +                x = SCREEN_WIDTH-8;
   1.183              }
   1.184  
   1.185 -            SDL_RenderPresent(screen);
   1.186 +            DrawRect(screen, x, y, 8, 8);
   1.187          }
   1.188 +
   1.189 +        SDL_RenderPresent(screen);
   1.190 +		
   1.191 +		done = SDL_JoystickGetAttached( joystick ) == 0;
   1.192      }
   1.193 +
   1.194 +    SDL_DestroyRenderer(screen);
   1.195 +    SDL_DestroyWindow(window);
   1.196  }
   1.197  
   1.198  int
   1.199  main(int argc, char *argv[])
   1.200  {
   1.201      const char *name;
   1.202 -    int i, joy=-1;
   1.203 +    int i;
   1.204      SDL_Joystick *joystick;
   1.205  
   1.206      /* Initialize SDL (Note: video is required to start event loop) */
   1.207 -    if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
   1.208 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.209          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.210          exit(1);
   1.211      }
   1.212  
   1.213 -    /* Initialize test framework */
   1.214 -    state = CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.215 -    if (!state) {
   1.216 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.217 -        return 1;
   1.218 -    }
   1.219 -
   1.220      /* Print information about the joysticks */
   1.221      printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.222      for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.223 -        name = SDL_JoystickName(i);
   1.224 +        name = SDL_JoystickNameForIndex(i);
   1.225          printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   1.226          joystick = SDL_JoystickOpen(i);
   1.227          if (joystick == NULL) {
   1.228 @@ -205,42 +218,34 @@
   1.229              printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   1.230              printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   1.231              printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   1.232 +            printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   1.233              SDL_JoystickClose(joystick);
   1.234          }
   1.235      }
   1.236  
   1.237 -    for (i = 1; i < argc;) {
   1.238 -        int consumed;
   1.239 -
   1.240 -        consumed = CommonArg(state, i);
   1.241 -        if (consumed == 0) {
   1.242 -            consumed = -1;
   1.243 -            if (SDL_isdigit(*argv[i])) {
   1.244 -                joy = SDL_atoi(argv[i]);
   1.245 -                consumed = 1;
   1.246 -            }
   1.247 -        }
   1.248 -        if (consumed < 0) {
   1.249 -            return 1;
   1.250 -        }
   1.251 -        i += consumed;
   1.252 -    }
   1.253 -    if (!CommonInit(state)) {
   1.254 -        return 2;
   1.255 -    }
   1.256 -
   1.257 -    if (joy > -1) {
   1.258 -        joystick = SDL_JoystickOpen(joy);
   1.259 -        if (joystick == NULL) {
   1.260 -            printf("Couldn't open joystick %d: %s\n", joy,
   1.261 -                   SDL_GetError());
   1.262 -        } else {
   1.263 -            WatchJoystick(joystick);
   1.264 -            SDL_JoystickClose(joystick);
   1.265 -        }
   1.266 -    }
   1.267 -    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
   1.268 -    CommonQuit(state);
   1.269 +    if (argv[1]) {
   1.270 +		int nreportederror = 0;
   1.271 +		SDL_Event event;
   1.272 +		joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.273 +		while ( 1 ) {
   1.274 +			if (joystick == NULL) {
   1.275 +				if ( nreportederror == 0 ) {
   1.276 +					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   1.277 +					nreportederror = 1;
   1.278 +				}
   1.279 +			} else {
   1.280 +				nreportederror = 0;
   1.281 +				WatchJoystick(joystick);
   1.282 +				SDL_JoystickClose(joystick);
   1.283 +			}
   1.284 +			
   1.285 +			joystick = NULL;
   1.286 +			SDL_WaitEvent( &event );
   1.287 +			if ( event.type == SDL_JOYDEVICEADDED )
   1.288 +				joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.289 +		}
   1.290 +	}
   1.291 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.292  
   1.293      return (0);
   1.294  }