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   317
 

Functions

static SDL_TextureLoadTexture (SDL_Renderer *renderer, 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

#define SCREEN_HEIGHT   317

Definition at line 32 of file testgamecontroller.c.

Referenced by WatchGameController().

#define SCREEN_WIDTH   512

Definition at line 31 of file testgamecontroller.c.

Referenced by WatchGameController().

Function Documentation

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

Definition at line 70 of file testgamecontroller.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, SDL_assert, 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  } else {
84  SDL_assert(!temp->format->palette);
85  SDL_assert(temp->format->BitsPerPixel == 24);
86  SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *)temp->pixels) & 0x00FFFFFF);
87  }
88  }
89 
90  texture = SDL_CreateTextureFromSurface(renderer, temp);
91  if (!texture) {
92  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
93  }
94  }
95  if (temp) {
96  SDL_FreeSurface(temp);
97  }
98  return texture;
99 }
#define SDL_GetError
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:182
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
GLenum GLenum GLuint texture
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_LogError
#define SDL_CreateTextureFromSurface
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
Uint8 BitsPerPixel
Definition: SDL_pixels.h:303
#define SDL_SetColorKey
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:302
void loop ( void arg)

Definition at line 102 of file testgamecontroller.c.

References axis_positions, button_positions, done, i, SDL_Event::key, SDL_KeyboardEvent::keysym, NULL, retval, SDL_ALPHA_OPAQUE, SDL_CONTROLLER_AXIS_MAX, SDL_CONTROLLER_BUTTON_MAX, SDL_GameControllerGetAttached, SDL_GameControllerGetAxis, SDL_GameControllerGetButton, SDL_KEYDOWN, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_RenderClear, SDL_RenderCopy, SDL_RenderCopyEx, SDL_RenderPresent, SDL_SetRenderDrawColor, SDL_TRUE, SDLK_ESCAPE, SDL_Keysym::sym, and SDL_Event::type.

Referenced by WatchGameController().

103 {
105  int i;
106  SDL_GameController *gamecontroller = (SDL_GameController *)arg;
107 
108  /* blank screen, set up for drawing this frame. */
109  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
112 
113  while (SDL_PollEvent(&event)) {
114  switch (event.type) {
115  case SDL_KEYDOWN:
116  if (event.key.keysym.sym != SDLK_ESCAPE) {
117  break;
118  }
119  /* Fall through to signal quit */
120  case SDL_QUIT:
121  done = SDL_TRUE;
122  break;
123  default:
124  break;
125  }
126  }
127 
128  /* Update visual controller state */
129  for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
131  const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
132  SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
133  }
134  }
135 
136  for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
137  const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
138  const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
139  if (value < -deadzone) {
140  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
141  const double angle = axis_positions[i].angle;
142  SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
143  } else if (value > deadzone) {
144  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
145  const double angle = axis_positions[i].angle + 180.0;
146  SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
147  }
148  }
149 
151 
152  if (!SDL_GameControllerGetAttached(gamecontroller)) {
153  done = SDL_TRUE;
154  retval = SDL_TRUE; /* keep going, wait for reattach. */
155  }
156 
157 #ifdef __EMSCRIPTEN__
158  if (done) {
159  emscripten_cancel_main_loop();
160  }
161 #endif
162 }
SDL_bool done
GLenum GLenum dst
#define SDL_PollEvent
SDL_Texture * button
#define SDL_GameControllerGetAttached
SDL_Texture * axis
SDL_Texture * background
SDL_GameControllerButton
#define SDL_RenderCopy
SDL_bool retval
GLsizei const GLfloat * value
struct _cl_event * event
static const struct @40 button_positions[]
SDL_Keysym keysym
Definition: SDL_events.h:196
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:42
#define NULL
Definition: begin_code.h:143
#define SDL_GameControllerGetButton
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:526
#define SDL_RenderCopyEx
SDL_Keycode sym
Definition: SDL_keyboard.h:50
GLfloat angle
General event structure.
Definition: SDL_events.h:521
#define SDL_SetRenderDrawColor
#define SDL_GameControllerGetAxis
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:45
SDL_Renderer * screen
SDL_GameControllerAxis
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
static const struct @41 axis_positions[]
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:523
int main ( int  argc,
char *  argv[] 
)

Definition at line 241 of file testgamecontroller.c.

References SDL_Event::cdevice, i, NULL, SDL_assert, SDL_CONTROLLERDEVICEADDED, SDL_FALSE, SDL_FINGERDOWN, SDL_GameControllerAddMappingsFromFile, SDL_GameControllerClose, SDL_GameControllerFromInstanceID, SDL_GameControllerGetJoystick, SDL_GameControllerNameForIndex, SDL_GameControllerOpen, SDL_GetError, SDL_Init, SDL_INIT_GAMECONTROLLER, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_IsGameController, SDL_JoystickGetDeviceGUID, 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.

242 {
243  int i;
244  int nController = 0;
245  int retcode = 0;
246  char guid[64];
247  SDL_GameController *gamecontroller;
248 
249  /* Enable standard application logging */
251 
252  /* Initialize SDL (Note: video is required to start event loop) */
254  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
255  return 1;
256  }
257 
258  SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
259 
260  /* Print information about the controller */
261  for (i = 0; i < SDL_NumJoysticks(); ++i) {
262  const char *name;
263  const char *description;
264 
266  guid, sizeof (guid));
267 
268  if ( SDL_IsGameController(i) )
269  {
270  nController++;
272  description = "Controller";
273  } else {
274  name = SDL_JoystickNameForIndex(i);
275  description = "Joystick";
276  }
277  SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
278  }
279  SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
280 
281  if (argv[1]) {
282  SDL_bool reportederror = SDL_FALSE;
283  SDL_bool keepGoing = SDL_TRUE;
285  int device = atoi(argv[1]);
286  if (device >= SDL_NumJoysticks()) {
287  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
288  retcode = 1;
289  } else {
291  guid, sizeof (guid));
292  SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
293  gamecontroller = SDL_GameControllerOpen(device);
294 
295  if (gamecontroller != NULL) {
297  }
298 
299  while (keepGoing) {
300  if (gamecontroller == NULL) {
301  if (!reportederror) {
302  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
303  retcode = 1;
304  keepGoing = SDL_FALSE;
305  reportederror = SDL_TRUE;
306  }
307  } else {
308  reportederror = SDL_FALSE;
309  keepGoing = WatchGameController(gamecontroller);
310  SDL_GameControllerClose(gamecontroller);
311  }
312 
313  gamecontroller = NULL;
314  if (keepGoing) {
315  SDL_Log("Waiting for attach\n");
316  }
317  while (keepGoing) {
318  SDL_WaitEvent(&event);
319  if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
320  || (event.type == SDL_MOUSEBUTTONDOWN)) {
321  keepGoing = SDL_FALSE;
322  } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
323  gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
324  if (gamecontroller != NULL) {
326  }
327  break;
328  }
329  }
330  }
331  }
332  }
333 
335 
336  return retcode;
337 }
#define SDL_GetError
SDL_bool WatchGameController(SDL_GameController *gamecontroller)
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:539
#define SDL_INIT_JOYSTICK
Definition: SDL.h:78
#define SDL_IsGameController
#define SDL_GameControllerOpen
#define SDL_QuitSubSystem
#define SDL_JoystickNameForIndex
#define SDL_GameControllerGetJoystick
#define SDL_NumJoysticks
GLuint const GLchar * name
#define SDL_JoystickGetGUIDString
#define SDL_JoystickInstanceID
#define SDL_JoystickGetDeviceGUID
#define SDL_LogError
SDL_bool
Definition: SDL_stdinc.h:126
#define SDL_Log
#define SDL_GameControllerFromInstanceID
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:42
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define SDL_WaitEvent
#define SDL_GameControllerNameForIndex
#define SDL_Init
#define SDL_INIT_GAMECONTROLLER
Definition: SDL.h:80
General event structure.
Definition: SDL_events.h:521
#define SDL_GameControllerAddMappingsFromFile(file)
#define SDL_INIT_VIDEO
Definition: SDL.h:77
Uint32 type
Definition: SDL_events.h:523
SDL_bool WatchGameController ( SDL_GameController *  gamecontroller)

Definition at line 165 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_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, SDL_WINDOWPOS_CENTERED, and window.

Referenced by main().

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

Variable Documentation

double angle

Definition at line 55 of file testgamecontroller.c.

const { ... } axis_positions[]
Initial value:
= {
{75, 154, 0.0},
{75, 154, 90.0},
{305, 230, 0.0},
{305, 230, 90.0},
{91, 0, 90.0},
{375, 0, 90.0},
}

Referenced by loop().

SDL_Texture* background

Definition at line 67 of file testgamecontroller.c.

Referenced by WatchJoystick().

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().

Definition at line 66 of file testgamecontroller.c.

Referenced by loop(), and WatchGameController().

SDL_Renderer* screen = NULL

Definition at line 64 of file testgamecontroller.c.

Referenced by WatchJoystick().

int x

Definition at line 36 of file testgamecontroller.c.

int y

Definition at line 36 of file testgamecontroller.c.