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

#define SCREEN_HEIGHT   320

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,
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.

{
SDL_Surface *temp = 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) {
if (temp->format->BytesPerPixel == 1) {
SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
}
}
texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
}
}
if (temp) {
}
return texture;
}
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_A, SDL_CONTROLLER_BUTTON_MAX, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_FLIP_NONE, SDL_GameControllerGetAttached, SDL_GameControllerGetAxis, SDL_GameControllerGetButton, SDL_GameControllerGetStringForAxis, SDL_GameControllerGetStringForButton, SDL_GameControllerRumble, 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.

{
int i;
SDL_GameController *gamecontroller = (SDL_GameController *)arg;
/* blank screen, set up for drawing this frame. */
while (SDL_PollEvent(&event)) {
switch (event.type) {
SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
break;
SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
/* First button triggers a 0.5 second full strength rumble */
SDL_GameControllerRumble(gamecontroller, 0xFFFF, 0xFFFF, 500);
}
break;
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
}
/* Fall through to signal quit */
case SDL_QUIT:
break;
default:
break;
}
}
/* Update visual controller state */
for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
}
}
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
if (value < -deadzone) {
const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
const double angle = axis_positions[i].angle;
} 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;
}
}
if (!SDL_GameControllerGetAttached(gamecontroller)) {
retval = SDL_TRUE; /* keep going, wait for reattach. */
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 251 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.

{
int i;
int nController = 0;
int retcode = 0;
char guid[64];
SDL_GameController *gamecontroller;
/* Enable standard application logging */
/* Initialize SDL (Note: video is required to start event loop) */
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
/* Print information about the mappings */
if (!argv[1]) {
SDL_Log("Supported mappings:\n");
for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
if (mapping) {
SDL_Log("\t%s\n", mapping);
SDL_free(mapping);
}
}
SDL_Log("\n");
}
/* Print information about the controller */
for (i = 0; i < SDL_NumJoysticks(); ++i) {
const char *name;
const char *description;
guid, sizeof (guid));
{
nController++;
description = "Controller";
} else {
description = "Joystick";
}
SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x)\n",
description, i, name ? name : "Unknown", guid,
}
SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
if (argv[1]) {
SDL_bool reportederror = SDL_FALSE;
SDL_bool keepGoing = SDL_TRUE;
int device = atoi(argv[1]);
if (device >= SDL_NumJoysticks()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
retcode = 1;
} else {
guid, sizeof (guid));
SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
gamecontroller = SDL_GameControllerOpen(device);
if (gamecontroller != NULL) {
}
while (keepGoing) {
if (gamecontroller == NULL) {
if (!reportederror) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
retcode = 1;
keepGoing = SDL_FALSE;
reportederror = SDL_TRUE;
}
} else {
reportederror = SDL_FALSE;
keepGoing = WatchGameController(gamecontroller);
SDL_GameControllerClose(gamecontroller);
}
gamecontroller = NULL;
if (keepGoing) {
SDL_Log("Waiting for attach\n");
}
while (keepGoing) {
SDL_WaitEvent(&event);
if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
|| (event.type == SDL_MOUSEBUTTONDOWN)) {
keepGoing = SDL_FALSE;
} else if (event.type == SDL_CONTROLLERDEVICEADDED) {
gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
if (gamecontroller != NULL) {
}
break;
}
}
}
}
}
return retcode;
}
SDL_bool WatchGameController ( SDL_GameController *  gamecontroller)

Definition at line 173 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().

{
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);
if (title) {
SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
}
/* Create a window to display controller state */
SDL_free(title);
title = NULL;
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
return SDL_FALSE;
}
screen = SDL_CreateRenderer(window, -1, 0);
if (screen == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
return SDL_FALSE;
}
SDL_RaiseWindow(window);
/* scale for platforms that don't give you the window size you asked for. */
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) {
return SDL_FALSE;
}
/* !!! 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");
/* Loop, getting controller events! */
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
#else
while (!done) {
loop(gamecontroller);
}
#endif
return retval;
}

Variable Documentation

double angle

Definition at line 55 of file testgamecontroller.c.

struct { ... } axis_positions[]

Referenced by loop().

SDL_Texture* background

Definition at line 67 of file testgamecontroller.c.

Referenced by WatchJoystick().

struct { ... } button_positions[]

Referenced by loop().

Definition at line 66 of file testgamecontroller.c.

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.