Skip to content

Commit

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

include $(NXDK_DIR)/Makefile
46 changes: 46 additions & 0 deletions samples/sdl_gamecontroller/main.c
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 <assert.h>
#include <SDL.h>
#include <stdbool.h>

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

pb_show_debug_screen();

SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);

SDL_GameController *gameController;
gameController = SDL_GameControllerOpen(0);


debugPrint("Num joysticks : %02i\n", SDL_NumJoysticks());
debugPrint("GameController name: %s\n", SDL_GameControllerName(gameController));
debugPrint("Is GameController : %02i\n", SDL_IsGameController(0));
debugPrint("GameController Map : %s\n", SDL_GameControllerMapping(gameController));
debugPrint("GameControllerEvent: %02i\n", SDL_GameControllerEventState(SDL_QUERY));

debugPrint("\n");
assert(SDL_IsGameController(0));

SDL_Event event;
while (true) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_CONTROLLERBUTTONDOWN) {
debugPrint("Button \"%s\" pressed\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button));
} else if (event.type == SDL_CONTROLLERAXISMOTION) {
debugPrint("Axis %s moved, new value: %i\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
}
}
}

pb_kill();

return 0;
}

0 comments on commit 8d25562

Please sign in to comment.