From 672fe8c21f3517b27b0610677a426cf317cadfb7 Mon Sep 17 00:00:00 2001 From: Dustin Holden Date: Wed, 30 Dec 2020 12:33:19 -0500 Subject: [PATCH] Rewrite controller initialization --- source/main.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 02ffab4..efaf56e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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); @@ -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()); @@ -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(); @@ -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(); + } + + frame_counter++; } return 0;