91
91
const char *name = SDL_GameControllerName(gamecontroller);
92
92
const char *basetitle = "Game Controller Test: ";
93
93
const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
94
char *title = SDL_malloc(titlelen);
94
char *title = (char *)SDL_malloc(titlelen);
95
95
SDL_Window *window = NULL;
96
96
SDL_Renderer *screen = NULL;
107
107
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
108
108
SCREEN_HEIGHT, 0);
109
109
if (window == NULL) {
110
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
110
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
114
114
screen = SDL_CreateRenderer(window, -1, 0);
115
115
if (screen == NULL) {
116
fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
116
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
117
117
SDL_DestroyWindow(window);
124
124
SDL_RaiseWindow(window);
126
126
/* Print info about the controller we are watching */
127
printf("Watching controller %s\n", name ? name : "Unknown Controller");
127
SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
129
129
/* Loop, getting controller events! */
135
135
while (SDL_PollEvent(&event)) {
136
136
switch (event.type) {
137
137
case SDL_CONTROLLERAXISMOTION:
138
printf("Controller %d axis %d ('%s') value: %d\n",
138
SDL_Log("Controller %d axis %d ('%s') value: %d\n",
139
139
event.caxis.which,
140
140
event.caxis.axis,
141
ControllerAxisName(event.caxis.axis),
141
ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
142
142
event.caxis.value);
144
144
case SDL_CONTROLLERBUTTONDOWN:
145
printf("Controller %d button %d ('%s') down\n",
145
SDL_Log("Controller %d button %d ('%s') down\n",
146
146
event.cbutton.which, event.cbutton.button,
147
ControllerButtonName(event.cbutton.button));
147
ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
149
149
case SDL_CONTROLLERBUTTONUP:
150
printf("Controller %d button %d ('%s') up\n",
150
SDL_Log("Controller %d button %d ('%s') up\n",
151
151
event.cbutton.which, event.cbutton.button,
152
ControllerButtonName(event.cbutton.button));
152
ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
154
154
case SDL_KEYDOWN:
155
155
if (event.key.keysym.sym != SDLK_ESCAPE) {
167
167
/* Update visual controller state */
168
168
SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
169
169
for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
170
if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
170
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
171
171
DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
176
176
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
177
177
/* Draw the X/Y axis */
179
x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
179
x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
180
180
x *= SCREEN_WIDTH;
184
184
} else if (x > (SCREEN_WIDTH - 16)) {
185
185
x = SCREEN_WIDTH - 16;
187
y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
187
y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
188
188
y *= SCREEN_HEIGHT;
218
218
SDL_GameController *gamecontroller;
220
/* Enable standard application logging */
221
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
220
223
/* Initialize SDL (Note: video is required to start event loop) */
221
224
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
222
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
225
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
226
229
/* Print information about the controller */
227
230
for (i = 0; i < SDL_NumJoysticks(); ++i) {
228
231
const char *name;
229
const char *description = "Joystick (not recognized as game controller)";
232
const char *description;
231
234
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
232
235
guid, sizeof (guid));
237
240
name = SDL_GameControllerNameForIndex(i);
241
description = "Controller";
239
243
name = SDL_JoystickNameForIndex(i);
244
description = "Joystick";
241
printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
246
SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
243
printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
248
SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
246
251
int device = atoi(argv[1]);
247
252
if (device >= SDL_NumJoysticks()) {
248
printf("%i is an invalid joystick index.\n", device);
253
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
251
256
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
252
257
guid, sizeof (guid));
253
printf("Attempting to open device %i, guid %s\n", device, guid);
258
SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
254
259
gamecontroller = SDL_GameControllerOpen(device);
255
260
if (gamecontroller == NULL) {
256
printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
261
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
259
264
WatchGameController(gamecontroller);
273
278
main(int argc, char *argv[])
275
fprintf(stderr, "SDL compiled without Joystick support.\n");
280
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");