test/testjoystick.c
changeset 9278 8900afb78a19
parent 8762 4b7d26162a2e
child 9338 86bc1c9a090f
     1.1 --- a/test/testjoystick.c	Sat Dec 13 02:33:52 2014 -0500
     1.2 +++ b/test/testjoystick.c	Thu Dec 18 00:19:52 2014 -0500
     1.3 @@ -18,6 +18,10 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 +#ifdef __EMSCRIPTEN__
     1.8 +#include <emscripten/emscripten.h>
     1.9 +#endif
    1.10 +
    1.11  #ifndef SDL_JOYSTICK_DISABLED
    1.12  
    1.13  #ifdef __IPHONEOS__
    1.14 @@ -28,6 +32,9 @@
    1.15  #define SCREEN_HEIGHT   480
    1.16  #endif
    1.17  
    1.18 +SDL_Renderer *screen = NULL;
    1.19 +SDL_bool retval = SDL_FALSE;
    1.20 +SDL_bool done = SDL_FALSE;
    1.21  
    1.22  static void
    1.23  DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    1.24 @@ -36,50 +43,15 @@
    1.25      SDL_RenderFillRect(r, &area);
    1.26  }
    1.27  
    1.28 -static SDL_bool
    1.29 -WatchJoystick(SDL_Joystick * joystick)
    1.30 +void
    1.31 +loop(void *arg)
    1.32  {
    1.33 -    SDL_Window *window = NULL;
    1.34 -    SDL_Renderer *screen = NULL;
    1.35 -    const char *name = NULL;
    1.36 -    SDL_bool retval = SDL_FALSE;
    1.37 -    SDL_bool done = SDL_FALSE;
    1.38      SDL_Event event;
    1.39      int i;
    1.40 +    SDL_Joystick *joystick = (SDL_Joystick *)arg;
    1.41  
    1.42 -    /* Create a window to display joystick axis position */
    1.43 -    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    1.44 -                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.45 -                              SCREEN_HEIGHT, 0);
    1.46 -    if (window == NULL) {
    1.47 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
    1.48 -        return SDL_FALSE;
    1.49 -    }
    1.50 -
    1.51 -    screen = SDL_CreateRenderer(window, -1, 0);
    1.52 -    if (screen == NULL) {
    1.53 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
    1.54 -        SDL_DestroyWindow(window);
    1.55 -        return SDL_FALSE;
    1.56 -    }
    1.57 -
    1.58 -    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.59 -    SDL_RenderClear(screen);
    1.60 -    SDL_RenderPresent(screen);
    1.61 -    SDL_RaiseWindow(window);
    1.62 -
    1.63 -    /* Print info about the joystick we are watching */
    1.64 -    name = SDL_JoystickName(joystick);
    1.65 -    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    1.66 -           name ? name : "Unknown Joystick");
    1.67 -    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.68 -           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.69 -           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    1.70 -
    1.71 -    /* Loop, getting joystick events! */
    1.72 -    while (!done) {
    1.73          /* blank screen, set up for drawing this frame. */
    1.74 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.75 +    SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
    1.76          SDL_RenderClear(screen);
    1.77  
    1.78          while (SDL_PollEvent(&event)) {
    1.79 @@ -197,8 +169,53 @@
    1.80              done = SDL_TRUE;
    1.81              retval = SDL_TRUE;  /* keep going, wait for reattach. */
    1.82          }
    1.83 +}
    1.84 +
    1.85 +static SDL_bool
    1.86 +WatchJoystick(SDL_Joystick * joystick)
    1.87 +{
    1.88 +    SDL_Window *window = NULL;
    1.89 +    const char *name = NULL;
    1.90 +
    1.91 +
    1.92 +    /* Create a window to display joystick axis position */
    1.93 +    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    1.94 +                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.95 +                              SCREEN_HEIGHT, 0);
    1.96 +    if (window == NULL) {
    1.97 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
    1.98 +        return SDL_FALSE;
    1.99      }
   1.100  
   1.101 +    screen = SDL_CreateRenderer(window, -1, 0);
   1.102 +    if (screen == NULL) {
   1.103 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   1.104 +        SDL_DestroyWindow(window);
   1.105 +        return SDL_FALSE;
   1.106 +    }
   1.107 +
   1.108 +    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.109 +    SDL_RenderClear(screen);
   1.110 +    SDL_RenderPresent(screen);
   1.111 +    SDL_RaiseWindow(window);
   1.112 +
   1.113 +    /* Print info about the joystick we are watching */
   1.114 +    name = SDL_JoystickName(joystick);
   1.115 +    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   1.116 +           name ? name : "Unknown Joystick");
   1.117 +    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   1.118 +           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   1.119 +           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   1.120 +
   1.121 +    /* Loop, getting joystick events! */
   1.122 +#ifdef __EMSCRIPTEN__
   1.123 +    emscripten_set_main_loop_arg(loop, joystick, 0, 1);
   1.124 +#else
   1.125 +    while (!done) {
   1.126 +        loop(joystick);
   1.127 +    }
   1.128 +#endif
   1.129 +
   1.130      SDL_DestroyRenderer(screen);
   1.131      SDL_DestroyWindow(window);
   1.132      return retval;