From ca822a7679bd0589ce8243f0138054085e3cabd6 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Sat, 21 Jan 2023 19:48:26 -0800 Subject: [PATCH] Input: Fix bad assumption about init If we try and initialize anything controller related before the event subsystem is initialized we lose all controllers detected on startup. To solve this we simply wait until the first frame is about to be rendered *then* initialize the controller subsystems --- lib/aurora.cpp | 4 ++++ lib/input.cpp | 6 ++++++ lib/input.hpp | 1 + lib/window.cpp | 3 ++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/aurora.cpp b/lib/aurora.cpp index d345f3f..d50176d 100644 --- a/lib/aurora.cpp +++ b/lib/aurora.cpp @@ -2,10 +2,12 @@ #include "gfx/common.hpp" #include "imgui.hpp" +#include "input.hpp" #include "internal.hpp" #include "webgpu/gpu.hpp" #include "window.hpp" +#include #include #include @@ -95,6 +97,7 @@ static AuroraInfo initialize(int argc, char* argv[], const AuroraConfig& config) } window::show_window(); + gfx::initialize(); imgui::create_context(); @@ -135,6 +138,7 @@ static const AuroraEvent* update() noexcept { if (g_initialFrame) { aurora_end_frame(); g_initialFrame = false; + input::initialize(); } const auto* events = window::poll_events(); imgui::new_frame(window::get_window_size()); diff --git a/lib/input.cpp b/lib/input.cpp index 3cc6352..286b074 100644 --- a/lib/input.cpp +++ b/lib/input.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -226,6 +227,11 @@ void controller_rumble(uint32_t instance, uint16_t low_freq_intensity, uint16_t uint32_t controller_count() noexcept { return g_GameControllers.size(); } +void initialize() noexcept { + /* Make sure we initialize everything input related now, this will automatically add all of the connected controllers + * as expected */ + SDL_Init(SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER); +} } // namespace aurora::input static const std::array mDefaultButtons{{ diff --git a/lib/input.hpp b/lib/input.hpp index 3d8fa19..5e92faf 100644 --- a/lib/input.hpp +++ b/lib/input.hpp @@ -19,4 +19,5 @@ bool controller_has_rumble(Uint32 instance) noexcept; void controller_rumble(uint32_t instance, uint16_t low_freq_intensity, uint16_t high_freq_intensity, uint16_t duration_ms) noexcept; uint32_t controller_count() noexcept; +void initialize() noexcept; } // namespace aurora::input diff --git a/lib/window.cpp b/lib/window.cpp index 1a39279..1292742 100644 --- a/lib/window.cpp +++ b/lib/window.cpp @@ -195,7 +195,8 @@ void show_window() { } bool initialize() { - ASSERT(SDL_Init(SDL_INIT_EVERYTHING & ~SDL_INIT_HAPTIC) == 0, "Error initializing SDL: {}", SDL_GetError()); + /* We don't want to initialize anything input related here, otherwise the add events will get lost to the void */ + ASSERT(SDL_Init(SDL_INIT_EVERYTHING & ~(SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)) == 0, "Error initializing SDL: {}", SDL_GetError()); #if !defined(_WIN32) && !defined(__APPLE__) SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");