Skip to content

Commit

Permalink
samples: Add rumble and hotplugging to SDL gamecontroller sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 committed Apr 9, 2021
1 parent 46226e9 commit f7d93e5
Showing 1 changed file with 67 additions and 43 deletions.
110 changes: 67 additions & 43 deletions samples/sdl_gamecontroller/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
int main(void)
{
SDL_GameController *pad = NULL;
static SDL_Event e;
bool pbk_init = false, sdl_init = false;

XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
Expand All @@ -18,17 +19,6 @@ int main(void)
goto wait_then_cleanup;
}

if (SDL_NumJoysticks() < 1) {
debugPrint("Please connect gamepad\n");
goto wait_then_cleanup;
}

pad = SDL_GameControllerOpen(0);
if (pad == NULL) {
debugPrint("Failed to open gamecontroller 0\n");
goto wait_then_cleanup;
}

pbk_init = pb_init() == 0;
if (!pbk_init) {
debugPrint("pbkit init failed\n");
Expand All @@ -44,39 +34,73 @@ int main(void)
pb_fill(0, 0, 640, 480, 0);
pb_erase_text_screen();

while (SDL_PollEvent(&e)) {
if (e.type == SDL_CONTROLLERDEVICEADDED) {
SDL_GameController *new_pad = SDL_GameControllerOpen(e.cdevice.which);
if (pad == NULL) {
pad = new_pad;
}
}
else if (e.type == SDL_CONTROLLERDEVICEREMOVED) {
if (pad == SDL_GameControllerFromInstanceID(e.cdevice.which)) {
pad = NULL;
}
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(e.cdevice.which));
}
else if (e.type == SDL_CONTROLLERBUTTONDOWN) {
if (e.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
pad = (SDL_GameControllerFromInstanceID(e.cdevice.which));
}
}
}

SDL_GameControllerUpdate();
pb_print(
"Axis:\n"
"- Lstick: x=%d, y=%d\n"
"- Rstick: x=%d, y=%d\n"
"- Ltrig: %d\n"
"- Rtrig: %d\n"
"Buttons:\n"
"- A:%d B:%d X:%d Y:%d\n"
"- Back:%d Start:%d White:%d Black:%d\n"
"- Up:%d Down:%d Left:%d Right:%d\n"
"- Lstick:%d, Rstick:%d\n",
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTX),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTY),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTX),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTY),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERLEFT),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERRIGHT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_A),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_B),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_X),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_Y),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_BACK),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_START),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_LEFTSHOULDER),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_UP),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_DOWN),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_LEFT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_RIGHT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_LEFTSTICK),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_RIGHTSTICK));
if (pad == NULL) {
pb_print("Press start on a controller to test\n");
}
else {
pb_print(
"Testing Controller uid: %d.\n"
"Press Start on another controller to test\n\n"
"Axis:\n"
"- Lstick: x=%d, y=%d\n"
"- Rstick: x=%d, y=%d\n"
"- Ltrig: %d\n"
"- Rtrig: %d\n"
"Buttons:\n"
"- A:%d B:%d X:%d Y:%d\n"
"- Back:%d Start:%d White:%d Black:%d\n"
"- Up:%d Down:%d Left:%d Right:%d\n"
"- Lstick:%d, Rstick:%d\n"
"- Vendor: %04x Product: %04x\n",
SDL_GameControllerGetPlayerIndex(pad),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTX),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTY),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTX),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTY),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERLEFT),
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERRIGHT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_A),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_B),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_X),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_Y),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_BACK),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_START),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_LEFTSHOULDER),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_UP),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_DOWN),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_LEFT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_RIGHT),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_LEFTSTICK),
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_RIGHTSTICK),
SDL_GameControllerGetVendor(pad), SDL_GameControllerGetProduct(pad)
);

SDL_GameControllerRumble(pad, SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERLEFT) * 2,
SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) * 2,
100);
}
pb_draw_text_screen();
while (pb_busy());
while (pb_finished());
Expand All @@ -97,4 +121,4 @@ int main(void)
}

return 0;
}
}

0 comments on commit f7d93e5

Please sign in to comment.