Replace testgamecontroller visualization mode with something more useful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 440721d..e7c1e05 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -24,20 +24,10 @@
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480
#else
-#define SCREEN_WIDTH 640
-#define SCREEN_HEIGHT 480
+#define SCREEN_WIDTH 512
+#define SCREEN_HEIGHT 317
#endif
-#define MAX_NUM_AXES 6
-#define MAX_NUM_HATS 2
-
-static void
-DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
-{
- const SDL_Rect area = { x, y, w, h };
- SDL_RenderFillRect(r, &area);
-}
-
static const char *
ControllerAxisName(const SDL_GameControllerAxis axis)
{
@@ -83,13 +73,71 @@ ControllerButtonName(const SDL_GameControllerButton button)
}
}
+static SDL_Texture *
+LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
+{
+ SDL_Surface *temp = NULL;
+ SDL_Texture *texture = NULL;
+
+ temp = SDL_LoadBMP(file);
+ if (temp == NULL) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
+ } else {
+ /* Set transparent pixel as the pixel at (0,0) */
+ if (transparent) {
+ SDL_assert(!temp->format->palette);
+ SDL_assert(temp->format->BitsPerPixel == 24);
+ SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
+ }
+
+ texture = SDL_CreateTextureFromSurface(renderer, temp);
+ if (!texture) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
+ }
+ }
+ if (temp) {
+ SDL_FreeSurface(temp);
+ }
+ return texture;
+}
+
SDL_bool
WatchGameController(SDL_GameController * gamecontroller)
{
+ /* This is indexed by SDL_GameControllerButton. */
+ static const struct { int x; int y; } button_positions[] = {
+ {387, 167}, /* A */
+ {431, 132}, /* B */
+ {342, 132}, /* X */
+ {389, 101}, /* Y */
+ {174, 132}, /* BACK */
+ {233, 132}, /* GUIDE */
+ {289, 132}, /* START */
+ {75, 154}, /* LEFTSTICK */
+ {305, 230}, /* RIGHTSTICK */
+ {77, 40}, /* LEFTSHOULDER */
+ {396, 36}, /* RIGHTSHOULDER */
+ {154, 188}, /* DPAD_UP */
+ {154, 249}, /* DPAD_DOWN */
+ {116, 217}, /* DPAD_LEFT */
+ {186, 217}, /* DPAD_RIGHT */
+ };
+
+ /* This is indexed by SDL_GameControllerAxis. */
+ static const struct { int x; int y; double angle; } axis_positions[] = {
+ {75, 154, 0.0}, /* LEFTX */
+ {75, 154, 90.0}, /* LEFTY */
+ {305, 230, 0.0}, /* RIGHTX */
+ {305, 230, 90.0}, /* RIGHTY */
+ {91, 0, 90.0}, /* TRIGGERLEFT */
+ {375, 0, 90.0}, /* TRIGGERRIGHT */
+ };
+
const char *name = SDL_GameControllerName(gamecontroller);
const char *basetitle = "Game Controller Test: ";
const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
char *title = (char *)SDL_malloc(titlelen);
+ SDL_Texture *background, *button, *axis;
SDL_Window *window = NULL;
SDL_Renderer *screen = NULL;
SDL_bool retval = SDL_FALSE;
@@ -101,7 +149,7 @@ WatchGameController(SDL_GameController * gamecontroller)
SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
}
- /* Create a window to display controller axis position */
+ /* Create a window to display controller state */
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
@@ -122,6 +170,21 @@ WatchGameController(SDL_GameController * gamecontroller)
SDL_RenderPresent(screen);
SDL_RaiseWindow(window);
+ background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
+ button = LoadTexture(screen, "button.bmp", SDL_TRUE);
+ axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
+
+ if (!background || !button || !axis) {
+ SDL_DestroyRenderer(screen);
+ SDL_DestroyWindow(window);
+ return SDL_FALSE;
+ }
+ SDL_SetTextureColorMod(button, 10, 255, 21);
+ SDL_SetTextureColorMod(axis, 10, 255, 21);
+
+ /* !!! FIXME: */
+ /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
+
/* Print info about the controller we are watching */
SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
@@ -130,26 +193,10 @@ WatchGameController(SDL_GameController * gamecontroller)
/* blank screen, set up for drawing this frame. */
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
SDL_RenderClear(screen);
+ SDL_RenderCopy(screen, background, NULL, NULL);
while (SDL_PollEvent(&event)) {
switch (event.type) {
- case SDL_CONTROLLERAXISMOTION:
- SDL_Log("Controller %d axis %d ('%s') value: %d\n",
- event.caxis.which,
- event.caxis.axis,
- ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
- event.caxis.value);
- break;
- case SDL_CONTROLLERBUTTONDOWN:
- SDL_Log("Controller %d button %d ('%s') down\n",
- event.cbutton.which, event.cbutton.button,
- ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
- break;
- case SDL_CONTROLLERBUTTONUP:
- SDL_Log("Controller %d button %d ('%s') up\n",
- event.cbutton.which, event.cbutton.button,
- ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
- break;
case SDL_KEYDOWN:
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
@@ -162,40 +209,29 @@ WatchGameController(SDL_GameController * gamecontroller)
break;
}
}
+
/* Update visual controller state */
- SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
- for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
+ for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
- DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
+ const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
+ SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
}
}
- SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
- for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
- /* Draw the X/Y axis */
- int x, y;
- x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
- x *= SCREEN_WIDTH;
- x /= 65535;
- if (x < 0) {
- x = 0;
- } else if (x > (SCREEN_WIDTH - 16)) {
- x = SCREEN_WIDTH - 16;
+ for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
+ const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
+ const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
+ if (value < -deadzone) {
+ const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
+ const double angle = axis_positions[i].angle;
+ SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
+ } else if (value > deadzone) {
+ const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
+ const double angle = axis_positions[i].angle + 180.0;
+ SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
}
- y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
- y *= SCREEN_HEIGHT;
- y /= 65535;
- if (y < 0) {
- y = 0;
- } else if (y > (SCREEN_HEIGHT - 16)) {
- y = SCREEN_HEIGHT - 16;
- }
-
- DrawRect(screen, x, y, 16, 16);
}
- SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
-
SDL_RenderPresent(screen);
if (!SDL_GameControllerGetAttached(gamecontroller)) {