Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1759 - Updates to testjoystick.c
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Mar 20, 2013
1 parent 055a771 commit 12de000
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions test/testjoystick.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -136,23 +136,28 @@ 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) {
x = 0;
} 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) {
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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]));
Expand All @@ -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
Expand Down

0 comments on commit 12de000

Please sign in to comment.