Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
2 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program to test the SDL game controller routines */
21 #ifndef SDL_JOYSTICK_DISABLED
24 #define SCREEN_WIDTH 320
25 #define SCREEN_HEIGHT 480
27 #define SCREEN_WIDTH 640
28 #define SCREEN_HEIGHT 480
31 #define MAX_NUM_AXES 6
32 #define MAX_NUM_HATS 2
34 static SDL_bool s_ForceQuit = SDL_FALSE;
37 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
39 const SDL_Rect area = { x, y, w, h };
40 SDL_RenderFillRect(r, &area);
44 ControllerAxisName(const SDL_GameControllerAxis axis)
48 #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
54 AXIS_CASE(TRIGGERLEFT);
55 AXIS_CASE(TRIGGERRIGHT);
57 default: return "???";
62 ControllerButtonName(const SDL_GameControllerButton button)
66 #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
75 BUTTON_CASE(LEFTSTICK);
76 BUTTON_CASE(RIGHTSTICK);
77 BUTTON_CASE(LEFTSHOULDER);
78 BUTTON_CASE(RIGHTSHOULDER);
80 BUTTON_CASE(DPAD_DOWN);
81 BUTTON_CASE(DPAD_LEFT);
82 BUTTON_CASE(DPAD_RIGHT);
84 default: return "???";
89 WatchGameController(SDL_GameController * gamecontroller)
91 const char *name = SDL_GameControllerName(gamecontroller);
92 const char *basetitle = "Game Controller Test: ";
93 const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
94 char *title = (char *)SDL_malloc(titlelen);
95 SDL_Window *window = NULL;
96 SDL_Renderer *screen = NULL;
102 SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
105 /* Create a window to display controller axis position */
106 window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
107 SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
109 if (window == NULL) {
110 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
114 screen = SDL_CreateRenderer(window, -1, 0);
115 if (screen == NULL) {
116 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
117 SDL_DestroyWindow(window);
121 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
122 SDL_RenderClear(screen);
123 SDL_RenderPresent(screen);
124 SDL_RaiseWindow(window);
126 /* Print info about the controller we are watching */
127 SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
129 /* Loop, getting controller events! */
131 /* blank screen, set up for drawing this frame. */
132 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
133 SDL_RenderClear(screen);
135 while (SDL_PollEvent(&event)) {
136 switch (event.type) {
137 case SDL_CONTROLLERAXISMOTION:
138 SDL_Log("Controller %d axis %d ('%s') value: %d\n",
141 ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
144 case SDL_CONTROLLERBUTTONDOWN:
145 SDL_Log("Controller %d button %d ('%s') down\n",
146 event.cbutton.which, event.cbutton.button,
147 ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
149 case SDL_CONTROLLERBUTTONUP:
150 SDL_Log("Controller %d button %d ('%s') up\n",
151 event.cbutton.which, event.cbutton.button,
152 ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
155 if (event.key.keysym.sym != SDLK_ESCAPE) {
158 /* Fall through to signal quit */
161 s_ForceQuit = SDL_TRUE;
167 /* Update visual controller state */
168 SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
169 for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
170 if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
171 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
175 SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
176 for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
177 /* Draw the X/Y axis */
179 x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
184 } else if (x > (SCREEN_WIDTH - 16)) {
185 x = SCREEN_WIDTH - 16;
187 y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
192 } else if (y > (SCREEN_HEIGHT - 16)) {
193 y = SCREEN_HEIGHT - 16;
196 DrawRect(screen, x, y, 16, 16);
199 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
201 SDL_RenderPresent(screen);
204 done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
207 SDL_DestroyRenderer(screen);
208 SDL_DestroyWindow(window);
212 main(int argc, char *argv[])
218 SDL_GameController *gamecontroller;
220 /* Enable standard application logging */
221 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
223 /* Initialize SDL (Note: video is required to start event loop) */
224 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
225 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
229 /* Print information about the controller */
230 for (i = 0; i < SDL_NumJoysticks(); ++i) {
232 const char *description = "Joystick (not recognized as game controller)";
234 SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
235 guid, sizeof (guid));
237 if ( SDL_IsGameController(i) )
240 name = SDL_GameControllerNameForIndex(i);
242 name = SDL_JoystickNameForIndex(i);
244 SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
246 SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
249 int device = atoi(argv[1]);
250 if (device >= SDL_NumJoysticks()) {
251 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
254 SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
255 guid, sizeof (guid));
256 SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
257 gamecontroller = SDL_GameControllerOpen(device);
258 if (gamecontroller == NULL) {
259 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
262 WatchGameController(gamecontroller);
263 SDL_GameControllerClose(gamecontroller);
268 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
276 main(int argc, char *argv[])
278 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");