SDL  2.0
testgamecontroller.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for testgamecontroller.c:

Go to the source code of this file.

Macros

#define SCREEN_WIDTH   512
 
#define SCREEN_HEIGHT   320
 

Functions

static SDL_TextureLoadTexture (SDL_Renderer *renderer, const char *file, SDL_bool transparent)
 
void loop (void *arg)
 
SDL_bool WatchGameController (SDL_GameController *gamecontroller)
 
int main (int argc, char *argv[])
 

Variables

struct {
   int   x
 
   int   y
 
button_positions []
 
struct {
   int   x
 
   int   y
 
   double   angle
 
axis_positions []
 
SDL_Rendererscreen = NULL
 
SDL_bool retval = SDL_FALSE
 
SDL_bool done = SDL_FALSE
 
SDL_Texturebackground
 
SDL_Texturebutton
 
SDL_Textureaxis
 

Macro Definition Documentation

◆ SCREEN_HEIGHT

#define SCREEN_HEIGHT   320

Definition at line 32 of file testgamecontroller.c.

Referenced by WatchGameController().

◆ SCREEN_WIDTH

#define SCREEN_WIDTH   512

Definition at line 31 of file testgamecontroller.c.

Referenced by WatchGameController().

Function Documentation

◆ LoadTexture()

static SDL_Texture* LoadTexture ( SDL_Renderer renderer,
const char *  file,
SDL_bool  transparent 
)
static

Definition at line 70 of file testgamecontroller.c.

References SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, NULL, SDL_Surface::pixels, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, and SDL_TRUE.

Referenced by WatchGameController().

71 {
72  SDL_Surface *temp = NULL;
74 
75  temp = SDL_LoadBMP(file);
76  if (temp == NULL) {
77  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
78  } else {
79  /* Set transparent pixel as the pixel at (0,0) */
80  if (transparent) {
81  if (temp->format->BytesPerPixel == 1) {
82  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
83  }
84  }
85 
86  texture = SDL_CreateTextureFromSurface(renderer, temp);
87  if (!texture) {
88  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
89  }
90  }
91  if (temp) {
92  SDL_FreeSurface(temp);
93  }
94  return texture;
95 }
#define SDL_GetError
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_LogError
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_SetColorKey
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72

◆ loop()

void loop ( void arg)

Definition at line 98 of file testgamecontroller.c.

References SDL_ControllerAxisEvent::axis, axis_positions, SDL_ControllerButtonEvent::button, button_positions, SDL_Event::caxis, SDL_Event::cbutton, done, i, SDL_Event::key, SDL_KeyboardEvent::keysym, NULL, retval, SDL_ALPHA_OPAQUE, SDL_CONTROLLER_AXIS_MAX, SDL_CONTROLLER_BUTTON_MAX, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_FLIP_NONE, SDL_GameControllerGetAttached, SDL_GameControllerGetAxis, SDL_GameControllerGetButton, SDL_GameControllerGetStringForAxis, SDL_GameControllerGetStringForButton, SDL_KEYDOWN, SDL_Log, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_RenderClear, SDL_RenderCopy, SDL_RenderCopyEx, SDL_RenderPresent, SDL_SetRenderDrawColor, SDL_TRUE, SDLK_ESCAPE, SDL_ControllerButtonEvent::state, SDL_Keysym::sym, SDL_Event::type, and SDL_ControllerAxisEvent::value.

Referenced by WatchGameController().

99 {
101  int i;
102  SDL_GameController *gamecontroller = (SDL_GameController *)arg;
103 
104  /* blank screen, set up for drawing this frame. */
105  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
108 
109  while (SDL_PollEvent(&event)) {
110  switch (event.type) {
112  SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
113  break;
116  SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
117  break;
118  case SDL_KEYDOWN:
119  if (event.key.keysym.sym != SDLK_ESCAPE) {
120  break;
121  }
122  /* Fall through to signal quit */
123  case SDL_QUIT:
124  done = SDL_TRUE;
125  break;
126  default:
127  break;
128  }
129  }
130 
131  /* Update visual controller state */
132  for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
134  const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
136  }
137  }
138 
139  for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
140  const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
141  const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
142  if (value < -deadzone) {
143  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
144  const double angle = axis_positions[i].angle;
146  } else if (value > deadzone) {
147  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
148  const double angle = axis_positions[i].angle + 180.0;
150  }
151  }
152 
154 
155  if (!SDL_GameControllerGetAttached(gamecontroller)) {
156  done = SDL_TRUE;
157  retval = SDL_TRUE; /* keep going, wait for reattach. */
158  }
159 
160 #ifdef __EMSCRIPTEN__
161  if (done) {
162  emscripten_cancel_main_loop();
163  }
164 #endif
165 }
SDL_bool done
#define SDL_PollEvent
SDL_ControllerAxisEvent caxis
Definition: SDL_events.h:541
SDL_Texture * button
GLenum GLenum dst
#define SDL_GameControllerGetAttached
#define SDL_GameControllerGetStringForButton
SDL_Texture * axis
SDL_Texture * background
SDL_GameControllerButton
#define SDL_RenderCopy
SDL_bool retval
#define SDL_Log
#define SDL_GameControllerGetStringForAxis
struct _cl_event * event
static const struct @55 axis_positions[]
GLsizei const GLfloat * value
static const struct @54 button_positions[]
SDL_Keysym keysym
Definition: SDL_events.h:199
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_GameControllerGetButton
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:530
SDL_ControllerButtonEvent cbutton
Definition: SDL_events.h:542
#define SDL_RenderCopyEx
SDL_Keycode sym
Definition: SDL_keyboard.h:50
GLfloat angle
General event structure.
Definition: SDL_events.h:525
#define SDL_SetRenderDrawColor
#define SDL_GameControllerGetAxis
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_Renderer * screen
SDL_GameControllerAxis
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
int16_t Sint16
Definition: SDL_stdinc.h:163
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:527

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 246 of file testgamecontroller.c.

References SDL_Event::cdevice, device, i, NULL, SDL_assert, SDL_CONTROLLERDEVICEADDED, SDL_FALSE, SDL_FINGERDOWN, SDL_free, SDL_GameControllerAddMappingsFromFile, SDL_GameControllerClose, SDL_GameControllerFromInstanceID, SDL_GameControllerGetJoystick, SDL_GameControllerMappingForIndex, SDL_GameControllerNameForIndex, SDL_GameControllerNumMappings, SDL_GameControllerOpen, SDL_GetError, SDL_Init, SDL_INIT_GAMECONTROLLER, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_IsGameController, SDL_JoystickGetDeviceGUID, SDL_JoystickGetDeviceProduct, SDL_JoystickGetDeviceVendor, SDL_JoystickGetGUIDString, SDL_JoystickInstanceID, SDL_JoystickNameForIndex, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_MOUSEBUTTONDOWN, SDL_NumJoysticks, SDL_QUIT, SDL_QuitSubSystem, SDL_TRUE, SDL_WaitEvent, SDL_Event::type, WatchGameController(), and SDL_ControllerDeviceEvent::which.

247 {
248  int i;
249  int nController = 0;
250  int retcode = 0;
251  char guid[64];
252  SDL_GameController *gamecontroller;
253 
254  /* Enable standard application logging */
256 
257  /* Initialize SDL (Note: video is required to start event loop) */
259  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
260  return 1;
261  }
262 
263  SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
264 
265  /* Print information about the mappings */
266  if (!argv[1]) {
267  SDL_Log("Supported mappings:\n");
268  for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
270  if (mapping) {
271  SDL_Log("\t%s\n", mapping);
272  SDL_free(mapping);
273  }
274  }
275  SDL_Log("\n");
276  }
277 
278  /* Print information about the controller */
279  for (i = 0; i < SDL_NumJoysticks(); ++i) {
280  const char *name;
281  const char *description;
282 
284  guid, sizeof (guid));
285 
286  if ( SDL_IsGameController(i) )
287  {
288  nController++;
290  description = "Controller";
291  } else {
292  name = SDL_JoystickNameForIndex(i);
293  description = "Joystick";
294  }
295  SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x)\n",
296  description, i, name ? name : "Unknown", guid,
298  }
299  SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
300 
301  if (argv[1]) {
302  SDL_bool reportederror = SDL_FALSE;
303  SDL_bool keepGoing = SDL_TRUE;
305  int device = atoi(argv[1]);
306  if (device >= SDL_NumJoysticks()) {
307  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
308  retcode = 1;
309  } else {
311  guid, sizeof (guid));
312  SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
313  gamecontroller = SDL_GameControllerOpen(device);
314 
315  if (gamecontroller != NULL) {
317  }
318 
319  while (keepGoing) {
320  if (gamecontroller == NULL) {
321  if (!reportederror) {
322  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
323  retcode = 1;
324  keepGoing = SDL_FALSE;
325  reportederror = SDL_TRUE;
326  }
327  } else {
328  reportederror = SDL_FALSE;
329  keepGoing = WatchGameController(gamecontroller);
330  SDL_GameControllerClose(gamecontroller);
331  }
332 
333  gamecontroller = NULL;
334  if (keepGoing) {
335  SDL_Log("Waiting for attach\n");
336  }
337  while (keepGoing) {
338  SDL_WaitEvent(&event);
339  if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
340  || (event.type == SDL_MOUSEBUTTONDOWN)) {
341  keepGoing = SDL_FALSE;
342  } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
343  gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
344  if (gamecontroller != NULL) {
346  }
347  break;
348  }
349  }
350  }
351  }
352  }
353 
355 
356  return retcode;
357 }
#define SDL_GetError
SDL_bool WatchGameController(SDL_GameController *gamecontroller)
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:543
#define SDL_INIT_JOYSTICK
Definition: SDL.h:79
#define SDL_IsGameController
#define SDL_GameControllerOpen
#define SDL_QuitSubSystem
#define SDL_JoystickNameForIndex
#define SDL_GameControllerNumMappings
#define SDL_GameControllerGetJoystick
#define SDL_NumJoysticks
#define SDL_JoystickGetGUIDString
#define SDL_JoystickInstanceID
GLuint const GLchar * name
#define SDL_JoystickGetDeviceGUID
#define SDL_LogError
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_Log
#define SDL_GameControllerFromInstanceID
#define SDL_free
struct _cl_event * event
#define SDL_GameControllerClose
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
#define SDL_GameControllerMappingForIndex
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_WaitEvent
#define SDL_GameControllerNameForIndex
#define SDL_Init
GLenum GLenum GLenum GLenum mapping
#define SDL_INIT_GAMECONTROLLER
Definition: SDL.h:81
General event structure.
Definition: SDL_events.h:525
#define SDL_GameControllerAddMappingsFromFile(file)
#define SDL_JoystickGetDeviceVendor
#define SDL_INIT_VIDEO
Definition: SDL.h:78
Uint32 type
Definition: SDL_events.h:527
#define SDL_JoystickGetDeviceProduct

◆ WatchGameController()

SDL_bool WatchGameController ( SDL_GameController *  gamecontroller)

Definition at line 168 of file testgamecontroller.c.

References done, LoadTexture(), loop(), NULL, retval, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_CreateRenderer, SDL_CreateWindow, SDL_DestroyRenderer, SDL_DestroyWindow, SDL_FALSE, SDL_free, SDL_GameControllerName, SDL_GetError, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_malloc, SDL_RaiseWindow, SDL_RenderClear, SDL_RenderPresent, SDL_RenderSetLogicalSize, SDL_SetRenderDrawColor, SDL_SetTextureColorMod, SDL_snprintf, SDL_strlen, SDL_TRUE, and SDL_WINDOWPOS_CENTERED.

Referenced by main().

169 {
170  const char *name = SDL_GameControllerName(gamecontroller);
171  const char *basetitle = "Game Controller Test: ";
172  const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
173  char *title = (char *)SDL_malloc(titlelen);
175 
176  retval = SDL_FALSE;
177  done = SDL_FALSE;
178 
179  if (title) {
180  SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
181  }
182 
183  /* Create a window to display controller state */
186  SCREEN_HEIGHT, 0);
187  SDL_free(title);
188  title = NULL;
189  if (window == NULL) {
190  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
191  return SDL_FALSE;
192  }
193 
194  screen = SDL_CreateRenderer(window, -1, 0);
195  if (screen == NULL) {
196  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
197  SDL_DestroyWindow(window);
198  return SDL_FALSE;
199  }
200 
201  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
204  SDL_RaiseWindow(window);
205 
206  /* scale for platforms that don't give you the window size you asked for. */
208 
209  background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
210  button = LoadTexture(screen, "button.bmp", SDL_TRUE);
211  axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
212 
213  if (!background || !button || !axis) {
215  SDL_DestroyWindow(window);
216  return SDL_FALSE;
217  }
218  SDL_SetTextureColorMod(button, 10, 255, 21);
219  SDL_SetTextureColorMod(axis, 10, 255, 21);
220 
221  /* !!! FIXME: */
222  /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
223 
224  /* Print info about the controller we are watching */
225  SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
226 
227  /* Loop, getting controller events! */
228 #ifdef __EMSCRIPTEN__
229  emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
230 #else
231  while (!done) {
232  loop(gamecontroller);
233  }
234 #endif
235 
237  screen = NULL;
238  background = NULL;
239  button = NULL;
240  axis = NULL;
241  SDL_DestroyWindow(window);
242  return retval;
243 }
SDL_bool done
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
#define SDL_GetError
SDL_Texture * button
static SDL_Texture * LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
#define SCREEN_HEIGHT
#define SDL_GameControllerName
#define SDL_CreateWindow
SDL_Texture * axis
SDL_Texture * background
void loop(void *arg)
GLuint const GLchar * name
#define SDL_LogError
#define SCREEN_WIDTH
SDL_bool retval
#define SDL_Log
#define SDL_free
#define SDL_RenderSetLogicalSize
#define SDL_RaiseWindow
#define SDL_SetTextureColorMod
#define NULL
Definition: begin_code.h:164
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_strlen
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_snprintf
#define SDL_malloc
#define SDL_SetRenderDrawColor
#define SDL_DestroyRenderer
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_Renderer * screen
#define SDL_DestroyWindow
#define SDL_CreateRenderer
#define SDL_RenderPresent

Variable Documentation

◆ angle

double angle

Definition at line 55 of file testgamecontroller.c.

◆ axis

◆ axis_positions

const { ... } axis_positions[]
Initial value:
= {
{74, 153, 270.0},
{74, 153, 0.0},
{306, 231, 270.0},
{306, 231, 0.0},
{91, -20, 0.0},
{375, -20, 0.0},
}

Referenced by loop().

◆ background

SDL_Texture* background

Definition at line 67 of file testgamecontroller.c.

Referenced by WatchJoystick().

◆ button

◆ button_positions

const { ... } button_positions[]
Initial value:
= {
{387, 167},
{431, 132},
{342, 132},
{389, 101},
{174, 132},
{233, 132},
{289, 132},
{75, 154},
{305, 230},
{77, 40},
{396, 36},
{154, 188},
{154, 249},
{116, 217},
{186, 217},
}

Referenced by loop().

◆ done

Definition at line 66 of file testgamecontroller.c.

Referenced by loop(), and WatchGameController().

◆ retval

◆ screen

SDL_Renderer* screen = NULL

Definition at line 64 of file testgamecontroller.c.

Referenced by WatchJoystick().

◆ x

int x

Definition at line 36 of file testgamecontroller.c.

◆ y

int y

Definition at line 36 of file testgamecontroller.c.