Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix controller issues #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ uint8_t load_scene = 0;

bool running = true;

void update_controllers() {
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
if (SDL_IsGameController(i)) {
SDL_GameControllerOpen(i);
}
}
}

int main(void) {
#ifdef _XBOX
XVideoSetMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, REFRESH_DEFAULT);
Expand All @@ -79,21 +87,8 @@ int main(void) {
// Load joystick
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");

if (SDL_NumJoysticks() < 1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't find any joysticks.\n");
printSDLErrorAndReboot();
} else {
SDL_GameController *controller = NULL;
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
if (SDL_IsGameController(i)) {
controller = SDL_GameControllerOpen(i);
if (controller == nullptr) {
printSDLErrorAndReboot();
}
}
}
}
// Update controllers at least once before the main loop
update_controllers();

if (TTF_Init() != 0) {
debugPrint("TTF_Init failed: %s", TTF_GetError());
Expand Down Expand Up @@ -141,6 +136,9 @@ int main(void) {
// Main render loop
Scene *currentScene = new Scene0();

// Keep track of frame-counter
uint32_t frame_counter = 0;

while (running) {
#ifdef _XBOX
XVideoWaitForVBlank();
Expand Down Expand Up @@ -197,6 +195,13 @@ int main(void) {
currentScene->render(gRenderer);

SDL_RenderPresent(gRenderer);

// Update controllers roughly every second
if(frame_counter % 60 == 0) {
update_controllers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm certified stupid, but wouldn't this attempt to open the same gamecontroller(s) once every second?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. This should allow controllers to be plugged in after the app is loaded. Looks to me to be pretty safe to call this often. https://github.com/XboxDev/nxdk-sdl/blob/a2a2d9ca1a5a7082c830490a0b110829939de3a7/src/joystick/SDL_gamecontroller.c#L1548

}

frame_counter++;
}

return 0;
Expand Down