test/testgamecontroller.c
changeset 8199 a060c6a7ae4d
parent 8149 681eb46b8ac4
child 8202 b84a1f55075e
     1.1 --- a/test/testgamecontroller.c	Sun Feb 09 15:20:41 2014 -0500
     1.2 +++ b/test/testgamecontroller.c	Mon Feb 10 01:41:58 2014 -0500
     1.3 @@ -24,20 +24,10 @@
     1.4  #define SCREEN_WIDTH    320
     1.5  #define SCREEN_HEIGHT    480
     1.6  #else
     1.7 -#define SCREEN_WIDTH    640
     1.8 -#define SCREEN_HEIGHT    480
     1.9 +#define SCREEN_WIDTH    512
    1.10 +#define SCREEN_HEIGHT   317
    1.11  #endif
    1.12  
    1.13 -#define MAX_NUM_AXES 6
    1.14 -#define MAX_NUM_HATS 2
    1.15 -
    1.16 -static void
    1.17 -DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    1.18 -{
    1.19 -    const SDL_Rect area = { x, y, w, h };
    1.20 -    SDL_RenderFillRect(r, &area);
    1.21 -}
    1.22 -
    1.23  static const char *
    1.24  ControllerAxisName(const SDL_GameControllerAxis axis)
    1.25  {
    1.26 @@ -83,13 +73,71 @@
    1.27      }
    1.28  }
    1.29  
    1.30 +static SDL_Texture *
    1.31 +LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    1.32 +{
    1.33 +    SDL_Surface *temp = NULL;
    1.34 +    SDL_Texture *texture = NULL;
    1.35 +
    1.36 +    temp = SDL_LoadBMP(file);
    1.37 +    if (temp == NULL) {
    1.38 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
    1.39 +    } else {
    1.40 +        /* Set transparent pixel as the pixel at (0,0) */
    1.41 +        if (transparent) {
    1.42 +            SDL_assert(!temp->format->palette);
    1.43 +            SDL_assert(temp->format->BitsPerPixel == 24);
    1.44 +            SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    1.45 +        }
    1.46 +
    1.47 +        texture = SDL_CreateTextureFromSurface(renderer, temp);
    1.48 +        if (!texture) {
    1.49 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    1.50 +        }
    1.51 +    }
    1.52 +    if (temp) {
    1.53 +        SDL_FreeSurface(temp);
    1.54 +    }
    1.55 +    return texture;
    1.56 +}
    1.57 +
    1.58  SDL_bool
    1.59  WatchGameController(SDL_GameController * gamecontroller)
    1.60  {
    1.61 +    /* This is indexed by SDL_GameControllerButton. */
    1.62 +    static const struct { int x; int y; } button_positions[] = {
    1.63 +        {387, 167},  /* A */
    1.64 +        {431, 132},  /* B */
    1.65 +        {342, 132},  /* X */
    1.66 +        {389, 101},  /* Y */
    1.67 +        {174, 132},  /* BACK */
    1.68 +        {233, 132},  /* GUIDE */
    1.69 +        {289, 132},  /* START */
    1.70 +        {75,  154},  /* LEFTSTICK */
    1.71 +        {305, 230},  /* RIGHTSTICK */
    1.72 +        {77,  40},   /* LEFTSHOULDER */
    1.73 +        {396, 36},   /* RIGHTSHOULDER */
    1.74 +        {154, 188},  /* DPAD_UP */
    1.75 +        {154, 249},  /* DPAD_DOWN */
    1.76 +        {116, 217},  /* DPAD_LEFT */
    1.77 +        {186, 217},  /* DPAD_RIGHT */
    1.78 +    };
    1.79 +
    1.80 +    /* This is indexed by SDL_GameControllerAxis. */
    1.81 +    static const struct { int x; int y; double angle; } axis_positions[] = {
    1.82 +        {75,  154, 0.0},  /* LEFTX */
    1.83 +        {75,  154, 90.0},  /* LEFTY */
    1.84 +        {305, 230, 0.0},  /* RIGHTX */
    1.85 +        {305, 230, 90.0},  /* RIGHTY */
    1.86 +        {91, 0, 90.0},     /* TRIGGERLEFT */
    1.87 +        {375, 0, 90.0},    /* TRIGGERRIGHT */
    1.88 +    };
    1.89 +
    1.90      const char *name = SDL_GameControllerName(gamecontroller);
    1.91      const char *basetitle = "Game Controller Test: ";
    1.92      const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    1.93      char *title = (char *)SDL_malloc(titlelen);
    1.94 +    SDL_Texture *background, *button, *axis;
    1.95      SDL_Window *window = NULL;
    1.96      SDL_Renderer *screen = NULL;
    1.97      SDL_bool retval = SDL_FALSE;
    1.98 @@ -101,7 +149,7 @@
    1.99          SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   1.100      }
   1.101  
   1.102 -    /* Create a window to display controller axis position */
   1.103 +    /* Create a window to display controller state */
   1.104      window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   1.105                                SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   1.106                                SCREEN_HEIGHT, 0);
   1.107 @@ -122,6 +170,21 @@
   1.108      SDL_RenderPresent(screen);
   1.109      SDL_RaiseWindow(window);
   1.110  
   1.111 +    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   1.112 +    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   1.113 +    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   1.114 +
   1.115 +    if (!background || !button || !axis) {
   1.116 +        SDL_DestroyRenderer(screen);
   1.117 +        SDL_DestroyWindow(window);
   1.118 +        return SDL_FALSE;
   1.119 +    }
   1.120 +    SDL_SetTextureColorMod(button, 10, 255, 21);
   1.121 +    SDL_SetTextureColorMod(axis, 10, 255, 21);
   1.122 +
   1.123 +    /* !!! FIXME: */
   1.124 +    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
   1.125 +
   1.126      /* Print info about the controller we are watching */
   1.127      SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   1.128  
   1.129 @@ -130,26 +193,10 @@
   1.130          /* blank screen, set up for drawing this frame. */
   1.131          SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.132          SDL_RenderClear(screen);
   1.133 +        SDL_RenderCopy(screen, background, NULL, NULL);
   1.134  
   1.135          while (SDL_PollEvent(&event)) {
   1.136              switch (event.type) {
   1.137 -            case SDL_CONTROLLERAXISMOTION:
   1.138 -                SDL_Log("Controller %d axis %d ('%s') value: %d\n",
   1.139 -                       event.caxis.which,
   1.140 -                       event.caxis.axis,
   1.141 -                       ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
   1.142 -                       event.caxis.value);
   1.143 -                break;
   1.144 -            case SDL_CONTROLLERBUTTONDOWN:
   1.145 -                SDL_Log("Controller %d button %d ('%s') down\n",
   1.146 -                       event.cbutton.which, event.cbutton.button,
   1.147 -                       ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
   1.148 -                break;
   1.149 -            case SDL_CONTROLLERBUTTONUP:
   1.150 -                SDL_Log("Controller %d button %d ('%s') up\n",
   1.151 -                       event.cbutton.which, event.cbutton.button,
   1.152 -                       ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
   1.153 -                break;
   1.154              case SDL_KEYDOWN:
   1.155                  if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.156                      break;
   1.157 @@ -162,40 +209,29 @@
   1.158                  break;
   1.159              }
   1.160          }
   1.161 +
   1.162          /* Update visual controller state */
   1.163 -        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   1.164 -        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   1.165 +        for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
   1.166              if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   1.167 -                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   1.168 +                const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
   1.169 +                SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
   1.170              }
   1.171          }
   1.172  
   1.173 -        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.174 -        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   1.175 -            /* Draw the X/Y axis */
   1.176 -            int x, y;
   1.177 -            x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
   1.178 -            x *= SCREEN_WIDTH;
   1.179 -            x /= 65535;
   1.180 -            if (x < 0) {
   1.181 -                x = 0;
   1.182 -            } else if (x > (SCREEN_WIDTH - 16)) {
   1.183 -                x = SCREEN_WIDTH - 16;
   1.184 +        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
   1.185 +            const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
   1.186 +            const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
   1.187 +            if (value < -deadzone) {
   1.188 +                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   1.189 +                const double angle = axis_positions[i].angle;
   1.190 +                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.191 +            } else if (value > deadzone) {
   1.192 +                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   1.193 +                const double angle = axis_positions[i].angle + 180.0;
   1.194 +                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   1.195              }
   1.196 -            y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
   1.197 -            y *= SCREEN_HEIGHT;
   1.198 -            y /= 65535;
   1.199 -            if (y < 0) {
   1.200 -                y = 0;
   1.201 -            } else if (y > (SCREEN_HEIGHT - 16)) {
   1.202 -                y = SCREEN_HEIGHT - 16;
   1.203 -            }
   1.204 -
   1.205 -            DrawRect(screen, x, y, 16, 16);
   1.206          }
   1.207  
   1.208 -        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.209 -
   1.210          SDL_RenderPresent(screen);
   1.211  
   1.212          if (!SDL_GameControllerGetAttached(gamecontroller)) {