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
This commit is contained in:
Phillip Stephens 2023-01-21 19:48:26 -08:00
parent b91c2739c9
commit ca822a7679
4 changed files with 13 additions and 1 deletions

View File

@ -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 <SDL.h>
#include <SDL_filesystem.h>
#include <imgui.h>
@ -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());

View File

@ -8,6 +8,7 @@
#include <SDL_haptic.h>
#include <SDL_version.h>
#include <SDL.h>
#include <absl/container/btree_map.h>
#include <absl/container/flat_hash_map.h>
@ -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<PADButtonMapping, 12> mDefaultButtons{{

View File

@ -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

View File

@ -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");