52
52
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
54
54
if (window == NULL) {
55
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
55
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
59
59
screen = SDL_CreateRenderer(window, -1, 0);
60
60
if (screen == NULL) {
61
fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
61
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
62
62
SDL_DestroyWindow(window);
71
71
/* Print info about the joystick we are watching */
72
72
name = SDL_JoystickName(joystick);
73
printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
73
SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
74
74
name ? name : "Unknown Joystick");
75
printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
75
SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
76
76
SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
77
77
SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
85
85
while (SDL_PollEvent(&event)) {
86
86
switch (event.type) {
87
87
case SDL_JOYAXISMOTION:
88
printf("Joystick %d axis %d value: %d\n",
88
SDL_Log("Joystick %d axis %d value: %d\n",
90
90
event.jaxis.axis, event.jaxis.value);
92
92
case SDL_JOYHATMOTION:
93
printf("Joystick %d hat %d value:",
93
SDL_Log("Joystick %d hat %d value:",
94
94
event.jhat.which, event.jhat.hat);
95
95
if (event.jhat.value == SDL_HAT_CENTERED)
97
97
if (event.jhat.value & SDL_HAT_UP)
99
99
if (event.jhat.value & SDL_HAT_RIGHT)
101
101
if (event.jhat.value & SDL_HAT_DOWN)
103
103
if (event.jhat.value & SDL_HAT_LEFT)
107
107
case SDL_JOYBALLMOTION:
108
printf("Joystick %d ball %d delta: (%d,%d)\n",
108
SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
109
109
event.jball.which,
110
110
event.jball.ball, event.jball.xrel, event.jball.yrel);
112
112
case SDL_JOYBUTTONDOWN:
113
printf("Joystick %d button %d down\n",
113
SDL_Log("Joystick %d button %d down\n",
114
114
event.jbutton.which, event.jbutton.button);
116
116
case SDL_JOYBUTTONUP:
117
printf("Joystick %d button %d up\n",
117
SDL_Log("Joystick %d button %d up\n",
118
118
event.jbutton.which, event.jbutton.button);
120
120
case SDL_KEYDOWN:
212
212
SDL_Joystick *joystick;
214
/* Enable standard application logging */
215
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
214
217
/* Initialize SDL (Note: video is required to start event loop) */
215
218
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
216
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
219
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
220
223
/* Print information about the joysticks */
221
printf("There are %d joysticks attached\n", SDL_NumJoysticks());
224
SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
222
225
for (i = 0; i < SDL_NumJoysticks(); ++i) {
223
226
name = SDL_JoystickNameForIndex(i);
224
printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
227
SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
225
228
joystick = SDL_JoystickOpen(i);
226
229
if (joystick == NULL) {
227
fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
230
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
231
234
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
232
235
guid, sizeof (guid));
233
printf(" axes: %d\n", SDL_JoystickNumAxes(joystick));
234
printf(" balls: %d\n", SDL_JoystickNumBalls(joystick));
235
printf(" hats: %d\n", SDL_JoystickNumHats(joystick));
236
printf(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
237
printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
238
printf(" guid: %s\n", guid);
236
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
237
SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
238
SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
239
SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
240
SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
241
SDL_Log(" guid: %s\n", guid);
239
242
SDL_JoystickClose(joystick);