From 12de0006eb77b8fed95d3ad74029bb2ff544f653 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 19 Mar 2013 22:25:02 -0700 Subject: [PATCH] Fixed bug 1759 - Updates to testjoystick.c ny00 A minor patch is attached, with the following few changes to testjoystick.c: - Unused constant definitions have been removed. - Output for all analog axes is drawn, even when there is an odd number of axes. (I have a controller with 5 analog axes.) - Buttons are now drawn on two rows, so there's room for more. In fact, it has been used for testing a proposed joystick patch for Android, where large button ID numbers have been involved (20 and up). For more details see http://bugzilla.libsdl.org/show_bug.cgi?id=1700. - A few adaptations have been done for the Android platform, assuming joystick support is ever applied to it. One of them is that the very first joystick (in the enumeration of all joysticks) is opened for testing, if there is any. - It is now possible to quit from the calibration by pressing on a mouse button, tapping on a touchscreen or pressing/tapping on the "Back" button of an Android device. Technically, a press on a key identified by key code SDLK_AC_BACK results in that. --- test/testjoystick.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/test/testjoystick.c b/test/testjoystick.c index af990ad9a..df94631f1 100644 --- a/test/testjoystick.c +++ b/test/testjoystick.c @@ -28,9 +28,6 @@ #define SCREEN_HEIGHT 480 #endif -#define MAX_NUM_AXES 6 -#define MAX_NUM_HATS 2 - static void DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h) @@ -121,10 +118,13 @@ WatchJoystick(SDL_Joystick * joystick) event.jbutton.which, event.jbutton.button); break; case SDL_KEYDOWN: - if (event.key.keysym.sym != SDLK_ESCAPE) { + if ((event.key.keysym.sym != SDLK_ESCAPE) && + (event.key.keysym.sym != SDLK_AC_BACK)) { break; } /* Fall through to signal quit */ + case SDL_FINGERDOWN: + case SDL_MOUSEBUTTONDOWN: case SDL_QUIT: done = SDL_TRUE; break; @@ -136,15 +136,15 @@ WatchJoystick(SDL_Joystick * joystick) SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE); for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) { if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { - DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32); + DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32); } } SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE); - for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) { + for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) { /* Draw the X/Y axis */ int x, y; - x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768); + x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768); x *= SCREEN_WIDTH; x /= 65535; if (x < 0) { @@ -152,7 +152,12 @@ WatchJoystick(SDL_Joystick * joystick) } else if (x > (SCREEN_WIDTH - 16)) { x = SCREEN_WIDTH - 16; } - y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768); + ++i; + if (i < SDL_JoystickNumAxes(joystick)) { + y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768); + } else { + y = 32768; + } y *= SCREEN_HEIGHT; y /= 65535; if (y < 0) { @@ -235,11 +240,19 @@ main(int argc, char *argv[]) } } +#ifdef ANDROID + if (SDL_NumJoysticks() > 0) { +#else if (argv[1]) { +#endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; +#ifdef ANDROID + joystick = SDL_JoystickOpen(0); +#else joystick = SDL_JoystickOpen(atoi(argv[1])); +#endif while ( keepGoing ) { if (joystick == NULL) { if ( !reportederror ) { @@ -259,7 +272,8 @@ main(int argc, char *argv[]) } while (keepGoing) { SDL_WaitEvent(&event); - if (event.type == SDL_QUIT) { + if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN) + || (event.type == SDL_MOUSEBUTTONDOWN)) { keepGoing = SDL_FALSE; } else if (event.type == SDL_JOYDEVICEADDED) { joystick = SDL_JoystickOpen(atoi(argv[1])); @@ -270,7 +284,11 @@ main(int argc, char *argv[]) } SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); +#ifdef ANDROID + exit(0); +#else return 0; +#endif } #else