Skip to content

Commit

Permalink
input: Use SDL GameController API
Browse files Browse the repository at this point in the history
  • Loading branch information
dracc committed Jun 12, 2019
1 parent 1760466 commit fc3ec41
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Includes/subsystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int init_systems() {
}
pb_show_debug_screen();
#endif
if (SDL_VideoInit(NULL) != 0) {
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_GAMECONTROLLER) != 0) {
outputLine("Init error: %s", SDL_GetError());
return 2;
}
Expand Down
113 changes: 71 additions & 42 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "xpadinput.h"

#include <threads.h>
#include <SDL.h>

void goToMainMenu(menuItem *mI, Renderer *r, Font &f,
int &listSize, int &currItem, int &prevItem, int &mMS) {
Expand All @@ -34,6 +35,17 @@ int main(void) {
vector<xbeMenuItem> gamesList;
if (init == 0) {
bool running = true;

// Open our GameController
SDL_GameController *sgc = SDL_GameControllerOpen(0);
if (sgc == nullptr) {
outputLine("Joystick Error: %s", SDL_GetError());
XSleep(2000);
}

// Set a hint that we want to use our gamecontroller always
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");

// Create the worker thread for populating the games list
xbeFinderArg xfa;
xfa.list = &gamesList;
Expand Down Expand Up @@ -71,25 +83,70 @@ int main(void) {
r.flip();
int currItem = 0, prevItem = 0, listSize = mainMenu.size();

SDL_Event event;

while (running) {
// FIXME: Abstract the input- and menu navigation process
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
if (thread_status == 1) {
thrd_join(thr, &thread_status);
}
running = false;
break;
} else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) {
prevItem = currItem;
if (currItem == 0) {
currItem = listSize - 1;
} else {
--currItem;
}
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) {
prevItem = currItem;
currItem = (currItem + 1) % listSize;
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
switch (mainMenuSelection) {
case 0:
mainMenuSelection = currItem + 1;
currItem = 0;
prevItem = 1;
break;
case 1:
if (currItem != (gamesList.size() - 1)) {
#ifdef NXDK
XInput_GetEvents();
for (size_t i = 0; i < XInputGetPadCount(); ++i) {
if (getDigitalKeyDown(&g_Pads[i], XPAD_DPAD_UP)) {
prevItem = currItem;
if (currItem == 0) {
currItem = listSize - 1;
} else {
--currItem;
XLaunchXBE(const_cast<char*>(gamesList[currItem].getXBEPath()));
#endif
}
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
// case 2:
// break;
case 3:
#ifdef NXDK
XLaunchXBE(const_cast<char*>("D:\\default.xbe"));
#endif
mainMenuSelection = 0;
break;
// case 4:
// break;
// case 5:
// break;
default:
break;
}
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
switch (mainMenuSelection) {
case 1:
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
default:
break;
}
}
}
if (getDigitalKeyDown(&g_Pads[i], XPAD_DPAD_DOWN)) {
prevItem = currItem;
currItem = (currItem + 1) % listSize;
}
}
#endif
// FIXME: Loads of repetitions ahead - break out into functions
switch (mainMenuSelection) {
case 0:
Expand All @@ -103,13 +160,6 @@ int main(void) {
r.drawMenuTexture(menuListTexture);
r.flip();
}
#ifdef NXDK
if (getAnalogKeyDown(&g_Pads[0], XPAD_A)) {
mainMenuSelection = currItem + 1;
currItem = 0;
prevItem = 1;
}
#endif
break;
case 1:
if (thread_status == 1) {
Expand Down Expand Up @@ -145,33 +195,12 @@ int main(void) {
r.drawMenuTexture(menuListTexture);
r.flip();
}
#ifdef NXDK
if (getAnalogKeyDown(&g_Pads[0], XPAD_A)) {
if (currItem != (gamesList.size() - 1)) {
XLaunchXBE(const_cast<char*>(gamesList[currItem].getXBEPath()));
}
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
}
if (getAnalogKeyDown(&g_Pads[0], XPAD_B)) {
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
}
#endif
break;
case 2:
// FIXME: Essentially the same deal as case 1;
// ought to be trivial to create a function for this mess.
mainMenuSelection = 0;
break;
case 3:
#ifdef NXDK
XLaunchXBE(const_cast<char*>("D:\\default.xbe"));
#endif
mainMenuSelection = 0;
break;
case 4:
// Settings menu. Not sure what we want/need here.
// "it's a problem for the future".
Expand Down

0 comments on commit fc3ec41

Please sign in to comment.