test/testgamecontroller.c
changeset 8199 a060c6a7ae4d
parent 8149 681eb46b8ac4
child 8202 b84a1f55075e
equal deleted inserted replaced
8198:51935d107921 8199:a060c6a7ae4d
    22 
    22 
    23 #ifdef __IPHONEOS__
    23 #ifdef __IPHONEOS__
    24 #define SCREEN_WIDTH    320
    24 #define SCREEN_WIDTH    320
    25 #define SCREEN_HEIGHT    480
    25 #define SCREEN_HEIGHT    480
    26 #else
    26 #else
    27 #define SCREEN_WIDTH    640
    27 #define SCREEN_WIDTH    512
    28 #define SCREEN_HEIGHT    480
    28 #define SCREEN_HEIGHT   317
    29 #endif
    29 #endif
    30 
       
    31 #define MAX_NUM_AXES 6
       
    32 #define MAX_NUM_HATS 2
       
    33 
       
    34 static void
       
    35 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
       
    36 {
       
    37     const SDL_Rect area = { x, y, w, h };
       
    38     SDL_RenderFillRect(r, &area);
       
    39 }
       
    40 
    30 
    41 static const char *
    31 static const char *
    42 ControllerAxisName(const SDL_GameControllerAxis axis)
    32 ControllerAxisName(const SDL_GameControllerAxis axis)
    43 {
    33 {
    44     switch (axis)
    34     switch (axis)
    81         #undef BUTTON_CASE
    71         #undef BUTTON_CASE
    82         default: return "???";
    72         default: return "???";
    83     }
    73     }
    84 }
    74 }
    85 
    75 
       
    76 static SDL_Texture *
       
    77 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
       
    78 {
       
    79     SDL_Surface *temp = NULL;
       
    80     SDL_Texture *texture = NULL;
       
    81 
       
    82     temp = SDL_LoadBMP(file);
       
    83     if (temp == NULL) {
       
    84         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
       
    85     } else {
       
    86         /* Set transparent pixel as the pixel at (0,0) */
       
    87         if (transparent) {
       
    88             SDL_assert(!temp->format->palette);
       
    89             SDL_assert(temp->format->BitsPerPixel == 24);
       
    90             SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
       
    91         }
       
    92 
       
    93         texture = SDL_CreateTextureFromSurface(renderer, temp);
       
    94         if (!texture) {
       
    95             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
       
    96         }
       
    97     }
       
    98     if (temp) {
       
    99         SDL_FreeSurface(temp);
       
   100     }
       
   101     return texture;
       
   102 }
       
   103 
    86 SDL_bool
   104 SDL_bool
    87 WatchGameController(SDL_GameController * gamecontroller)
   105 WatchGameController(SDL_GameController * gamecontroller)
    88 {
   106 {
       
   107     /* This is indexed by SDL_GameControllerButton. */
       
   108     static const struct { int x; int y; } button_positions[] = {
       
   109         {387, 167},  /* A */
       
   110         {431, 132},  /* B */
       
   111         {342, 132},  /* X */
       
   112         {389, 101},  /* Y */
       
   113         {174, 132},  /* BACK */
       
   114         {233, 132},  /* GUIDE */
       
   115         {289, 132},  /* START */
       
   116         {75,  154},  /* LEFTSTICK */
       
   117         {305, 230},  /* RIGHTSTICK */
       
   118         {77,  40},   /* LEFTSHOULDER */
       
   119         {396, 36},   /* RIGHTSHOULDER */
       
   120         {154, 188},  /* DPAD_UP */
       
   121         {154, 249},  /* DPAD_DOWN */
       
   122         {116, 217},  /* DPAD_LEFT */
       
   123         {186, 217},  /* DPAD_RIGHT */
       
   124     };
       
   125 
       
   126     /* This is indexed by SDL_GameControllerAxis. */
       
   127     static const struct { int x; int y; double angle; } axis_positions[] = {
       
   128         {75,  154, 0.0},  /* LEFTX */
       
   129         {75,  154, 90.0},  /* LEFTY */
       
   130         {305, 230, 0.0},  /* RIGHTX */
       
   131         {305, 230, 90.0},  /* RIGHTY */
       
   132         {91, 0, 90.0},     /* TRIGGERLEFT */
       
   133         {375, 0, 90.0},    /* TRIGGERRIGHT */
       
   134     };
       
   135 
    89     const char *name = SDL_GameControllerName(gamecontroller);
   136     const char *name = SDL_GameControllerName(gamecontroller);
    90     const char *basetitle = "Game Controller Test: ";
   137     const char *basetitle = "Game Controller Test: ";
    91     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
   138     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    92     char *title = (char *)SDL_malloc(titlelen);
   139     char *title = (char *)SDL_malloc(titlelen);
       
   140     SDL_Texture *background, *button, *axis;
    93     SDL_Window *window = NULL;
   141     SDL_Window *window = NULL;
    94     SDL_Renderer *screen = NULL;
   142     SDL_Renderer *screen = NULL;
    95     SDL_bool retval = SDL_FALSE;
   143     SDL_bool retval = SDL_FALSE;
    96     SDL_bool done = SDL_FALSE;
   144     SDL_bool done = SDL_FALSE;
    97     SDL_Event event;
   145     SDL_Event event;
    99 
   147 
   100     if (title) {
   148     if (title) {
   101         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   149         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   102     }
   150     }
   103 
   151 
   104     /* Create a window to display controller axis position */
   152     /* Create a window to display controller state */
   105     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   153     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   106                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   154                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   107                               SCREEN_HEIGHT, 0);
   155                               SCREEN_HEIGHT, 0);
   108     if (window == NULL) {
   156     if (window == NULL) {
   109         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   157         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   120     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   168     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   121     SDL_RenderClear(screen);
   169     SDL_RenderClear(screen);
   122     SDL_RenderPresent(screen);
   170     SDL_RenderPresent(screen);
   123     SDL_RaiseWindow(window);
   171     SDL_RaiseWindow(window);
   124 
   172 
       
   173     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
       
   174     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
       
   175     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
       
   176 
       
   177     if (!background || !button || !axis) {
       
   178         SDL_DestroyRenderer(screen);
       
   179         SDL_DestroyWindow(window);
       
   180         return SDL_FALSE;
       
   181     }
       
   182     SDL_SetTextureColorMod(button, 10, 255, 21);
       
   183     SDL_SetTextureColorMod(axis, 10, 255, 21);
       
   184 
       
   185     /* !!! FIXME: */
       
   186     /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
       
   187 
   125     /* Print info about the controller we are watching */
   188     /* Print info about the controller we are watching */
   126     SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   189     SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   127 
   190 
   128     /* Loop, getting controller events! */
   191     /* Loop, getting controller events! */
   129     while (!done) {
   192     while (!done) {
   130         /* blank screen, set up for drawing this frame. */
   193         /* blank screen, set up for drawing this frame. */
   131         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   194         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   132         SDL_RenderClear(screen);
   195         SDL_RenderClear(screen);
       
   196         SDL_RenderCopy(screen, background, NULL, NULL);
   133 
   197 
   134         while (SDL_PollEvent(&event)) {
   198         while (SDL_PollEvent(&event)) {
   135             switch (event.type) {
   199             switch (event.type) {
   136             case SDL_CONTROLLERAXISMOTION:
       
   137                 SDL_Log("Controller %d axis %d ('%s') value: %d\n",
       
   138                        event.caxis.which,
       
   139                        event.caxis.axis,
       
   140                        ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
       
   141                        event.caxis.value);
       
   142                 break;
       
   143             case SDL_CONTROLLERBUTTONDOWN:
       
   144                 SDL_Log("Controller %d button %d ('%s') down\n",
       
   145                        event.cbutton.which, event.cbutton.button,
       
   146                        ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
       
   147                 break;
       
   148             case SDL_CONTROLLERBUTTONUP:
       
   149                 SDL_Log("Controller %d button %d ('%s') up\n",
       
   150                        event.cbutton.which, event.cbutton.button,
       
   151                        ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
       
   152                 break;
       
   153             case SDL_KEYDOWN:
   200             case SDL_KEYDOWN:
   154                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   201                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   155                     break;
   202                     break;
   156                 }
   203                 }
   157                 /* Fall through to signal quit */
   204                 /* Fall through to signal quit */
   160                 break;
   207                 break;
   161             default:
   208             default:
   162                 break;
   209                 break;
   163             }
   210             }
   164         }
   211         }
       
   212 
   165         /* Update visual controller state */
   213         /* Update visual controller state */
   166         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   214         for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
   167         for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
       
   168             if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   215             if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   169                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   216                 const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
       
   217                 SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
   170             }
   218             }
   171         }
   219         }
   172 
   220 
   173         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   221         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
   174         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   222             const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
   175             /* Draw the X/Y axis */
   223             const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
   176             int x, y;
   224             if (value < -deadzone) {
   177             x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
   225                 const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   178             x *= SCREEN_WIDTH;
   226                 const double angle = axis_positions[i].angle;
   179             x /= 65535;
   227                 SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   180             if (x < 0) {
   228             } else if (value > deadzone) {
   181                 x = 0;
   229                 const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   182             } else if (x > (SCREEN_WIDTH - 16)) {
   230                 const double angle = axis_positions[i].angle + 180.0;
   183                 x = SCREEN_WIDTH - 16;
   231                 SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   184             }
   232             }
   185             y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
   233         }
   186             y *= SCREEN_HEIGHT;
       
   187             y /= 65535;
       
   188             if (y < 0) {
       
   189                 y = 0;
       
   190             } else if (y > (SCREEN_HEIGHT - 16)) {
       
   191                 y = SCREEN_HEIGHT - 16;
       
   192             }
       
   193 
       
   194             DrawRect(screen, x, y, 16, 16);
       
   195         }
       
   196 
       
   197         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
       
   198 
   234 
   199         SDL_RenderPresent(screen);
   235         SDL_RenderPresent(screen);
   200 
   236 
   201         if (!SDL_GameControllerGetAttached(gamecontroller)) {
   237         if (!SDL_GameControllerGetAttached(gamecontroller)) {
   202             done = SDL_TRUE;
   238             done = SDL_TRUE;