Skip to content

Commit

Permalink
sdl: Add SDL_joystick sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dracc committed May 8, 2019
1 parent 8bd921d commit 558fb19
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/sdl_joystick/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEBUG = y
XBE_TITLE = sdl_joystick
GEN_XISO = $(XBE_TITLE).iso
SRCS += $(CURDIR)/main.cpp
NXDK_DIR = $(CURDIR)/../..
NXDK_SDL = y

all_local: all

include $(NXDK_DIR)/Makefile
46 changes: 46 additions & 0 deletions samples/sdl_joystick/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <xboxrt/debug.h>
#include <pbkit/pbkit.h>
#include <hal/xbox.h>
#include <SDL.h>

int main() {
int ret = pb_init();
if (ret != 0) {
XSleep(2000);
return -1;
}

pb_show_debug_screen();

SDL_Joystick *joy;
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

joy = SDL_JoystickOpen(0);

SDL_Event event;

const char* buttonNames[10] = {"A", "B", "X", "Y", "Black", "White", "Start", "Back", "LeftThumb", "RightThumb"};
const char* axisNames[6] = {"LY", "LX", "RY", "RX", "LTrigg", "RTrigg"};

debugPrint("Num joysticks : %02i\n", SDL_NumJoysticks());
debugPrint("Joystick name : %s\n", SDL_JoystickNameForIndex(0));
debugPrint("Number of axes : %02i\n", SDL_JoystickNumAxes(joy));
debugPrint("Number of buttons: %02i\n", SDL_JoystickNumButtons(joy));
debugPrint("Number of balls : %02i\n", SDL_JoystickNumBalls(joy));
debugPrint("Number of hats : %02i\n", SDL_JoystickNumHats(joy));
debugPrint("\n");

while (true) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_JOYBUTTONDOWN) {
debugPrint("Button \"%s\" pressed\n", buttonNames[event.jbutton.button]);
} else if (event.type == SDL_JOYAXISMOTION) {
debugPrint("Axis %s moved, new value: %i\n", axisNames[event.jaxis.axis], event.jaxis.value);
}
}
}

pb_kill();

return 0;
}

0 comments on commit 558fb19

Please sign in to comment.