test/testgamecontroller.c
changeset 9278 8900afb78a19
parent 9102 a22458d5eed9
child 9290 fe975b2313fa
     1.1 --- a/test/testgamecontroller.c	Sat Dec 13 02:33:52 2014 -0500
     1.2 +++ b/test/testgamecontroller.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,40 @@
    1.15  #define SCREEN_HEIGHT   317
    1.16  #endif
    1.17  
    1.18 +/* This is indexed by SDL_GameControllerButton. */
    1.19 +static const struct { int x; int y; } button_positions[] = {
    1.20 +    {387, 167},  /* A */
    1.21 +    {431, 132},  /* B */
    1.22 +    {342, 132},  /* X */
    1.23 +    {389, 101},  /* Y */
    1.24 +    {174, 132},  /* BACK */
    1.25 +    {233, 132},  /* GUIDE */
    1.26 +    {289, 132},  /* START */
    1.27 +    {75,  154},  /* LEFTSTICK */
    1.28 +    {305, 230},  /* RIGHTSTICK */
    1.29 +    {77,  40},   /* LEFTSHOULDER */
    1.30 +    {396, 36},   /* RIGHTSHOULDER */
    1.31 +    {154, 188},  /* DPAD_UP */
    1.32 +    {154, 249},  /* DPAD_DOWN */
    1.33 +    {116, 217},  /* DPAD_LEFT */
    1.34 +    {186, 217},  /* DPAD_RIGHT */
    1.35 +};
    1.36 +
    1.37 +/* This is indexed by SDL_GameControllerAxis. */
    1.38 +static const struct { int x; int y; double angle; } axis_positions[] = {
    1.39 +    {75,  154, 0.0},  /* LEFTX */
    1.40 +    {75,  154, 90.0},  /* LEFTY */
    1.41 +    {305, 230, 0.0},  /* RIGHTX */
    1.42 +    {305, 230, 90.0},  /* RIGHTY */
    1.43 +    {91, 0, 90.0},     /* TRIGGERLEFT */
    1.44 +    {375, 0, 90.0},    /* TRIGGERRIGHT */
    1.45 +};
    1.46 +
    1.47 +SDL_Renderer *screen = NULL;
    1.48 +SDL_bool retval = SDL_FALSE;
    1.49 +SDL_bool done = SDL_FALSE;
    1.50 +SDL_Texture *background, *button, *axis;
    1.51 +
    1.52  static SDL_Texture *
    1.53  LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    1.54  {
    1.55 @@ -60,50 +98,71 @@
    1.56      return texture;
    1.57  }
    1.58  
    1.59 +void
    1.60 +loop(void *arg)
    1.61 +{
    1.62 +    SDL_Event event;
    1.63 +    int i;
    1.64 +    SDL_GameController *gamecontroller = (SDL_GameController *)arg;
    1.65 +
    1.66 +    /* blank screen, set up for drawing this frame. */
    1.67 +    SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
    1.68 +    SDL_RenderClear(screen);
    1.69 +    SDL_RenderCopy(screen, background, NULL, NULL);
    1.70 +
    1.71 +    while (SDL_PollEvent(&event)) {
    1.72 +        switch (event.type) {
    1.73 +        case SDL_KEYDOWN:
    1.74 +            if (event.key.keysym.sym != SDLK_ESCAPE) {
    1.75 +                break;
    1.76 +            }
    1.77 +            /* Fall through to signal quit */
    1.78 +        case SDL_QUIT:
    1.79 +            done = SDL_TRUE;
    1.80 +            break;
    1.81 +        default:
    1.82 +            break;
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    /* Update visual controller state */
    1.87 +    for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
    1.88 +        if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
    1.89 +            const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
    1.90 +            SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
    1.95 +        const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
    1.96 +        const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
    1.97 +        if (value < -deadzone) {
    1.98 +            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
    1.99 +            const double angle = axis_positions[i].angle;
   1.100 +            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.101 +        } else if (value > deadzone) {
   1.102 +            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   1.103 +            const double angle = axis_positions[i].angle + 180.0;
   1.104 +            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    SDL_RenderPresent(screen);
   1.109 +
   1.110 +    if (!SDL_GameControllerGetAttached(gamecontroller)) {
   1.111 +        done = SDL_TRUE;
   1.112 +        retval = SDL_TRUE;  /* keep going, wait for reattach. */
   1.113 +    }
   1.114 +}
   1.115 +
   1.116  SDL_bool
   1.117  WatchGameController(SDL_GameController * gamecontroller)
   1.118  {
   1.119 -    /* This is indexed by SDL_GameControllerButton. */
   1.120 -    static const struct { int x; int y; } button_positions[] = {
   1.121 -        {387, 167},  /* A */
   1.122 -        {431, 132},  /* B */
   1.123 -        {342, 132},  /* X */
   1.124 -        {389, 101},  /* Y */
   1.125 -        {174, 132},  /* BACK */
   1.126 -        {233, 132},  /* GUIDE */
   1.127 -        {289, 132},  /* START */
   1.128 -        {75,  154},  /* LEFTSTICK */
   1.129 -        {305, 230},  /* RIGHTSTICK */
   1.130 -        {77,  40},   /* LEFTSHOULDER */
   1.131 -        {396, 36},   /* RIGHTSHOULDER */
   1.132 -        {154, 188},  /* DPAD_UP */
   1.133 -        {154, 249},  /* DPAD_DOWN */
   1.134 -        {116, 217},  /* DPAD_LEFT */
   1.135 -        {186, 217},  /* DPAD_RIGHT */
   1.136 -    };
   1.137 -
   1.138 -    /* This is indexed by SDL_GameControllerAxis. */
   1.139 -    static const struct { int x; int y; double angle; } axis_positions[] = {
   1.140 -        {75,  154, 0.0},  /* LEFTX */
   1.141 -        {75,  154, 90.0},  /* LEFTY */
   1.142 -        {305, 230, 0.0},  /* RIGHTX */
   1.143 -        {305, 230, 90.0},  /* RIGHTY */
   1.144 -        {91, 0, 90.0},     /* TRIGGERLEFT */
   1.145 -        {375, 0, 90.0},    /* TRIGGERRIGHT */
   1.146 -    };
   1.147 -
   1.148      const char *name = SDL_GameControllerName(gamecontroller);
   1.149      const char *basetitle = "Game Controller Test: ";
   1.150      const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
   1.151      char *title = (char *)SDL_malloc(titlelen);
   1.152 -    SDL_Texture *background, *button, *axis;
   1.153      SDL_Window *window = NULL;
   1.154 -    SDL_Renderer *screen = NULL;
   1.155 -    SDL_bool retval = SDL_FALSE;
   1.156 -    SDL_bool done = SDL_FALSE;
   1.157 -    SDL_Event event;
   1.158 -    int i;
   1.159 -
   1.160      if (title) {
   1.161          SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   1.162      }
   1.163 @@ -151,56 +210,13 @@
   1.164      SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   1.165  
   1.166      /* Loop, getting controller events! */
   1.167 +#ifdef __EMSCRIPTEN__
   1.168 +    emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
   1.169 +#else
   1.170      while (!done) {
   1.171 -        /* blank screen, set up for drawing this frame. */
   1.172 -        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   1.173 -        SDL_RenderClear(screen);
   1.174 -        SDL_RenderCopy(screen, background, NULL, NULL);
   1.175 -
   1.176 -        while (SDL_PollEvent(&event)) {
   1.177 -            switch (event.type) {
   1.178 -            case SDL_KEYDOWN:
   1.179 -                if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.180 -                    break;
   1.181 -                }
   1.182 -                /* Fall through to signal quit */
   1.183 -            case SDL_QUIT:
   1.184 -                done = SDL_TRUE;
   1.185 -                break;
   1.186 -            default:
   1.187 -                break;
   1.188 -            }
   1.189 -        }
   1.190 -
   1.191 -        /* Update visual controller state */
   1.192 -        for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
   1.193 -            if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   1.194 -                const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
   1.195 -                SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
   1.196 -            }
   1.197 -        }
   1.198 -
   1.199 -        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
   1.200 -            const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
   1.201 -            const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
   1.202 -            if (value < -deadzone) {
   1.203 -                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   1.204 -                const double angle = axis_positions[i].angle;
   1.205 -                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.206 -            } else if (value > deadzone) {
   1.207 -                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   1.208 -                const double angle = axis_positions[i].angle + 180.0;
   1.209 -                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.210 -            }
   1.211 -        }
   1.212 -
   1.213 -        SDL_RenderPresent(screen);
   1.214 -
   1.215 -        if (!SDL_GameControllerGetAttached(gamecontroller)) {
   1.216 -            done = SDL_TRUE;
   1.217 -            retval = SDL_TRUE;  /* keep going, wait for reattach. */
   1.218 -        }
   1.219 +        loop(gamecontroler);
   1.220      }
   1.221 +#endif
   1.222  
   1.223      SDL_DestroyRenderer(screen);
   1.224      SDL_DestroyWindow(window);