diff --git a/src/input/controller.cpp b/src/input/controller.cpp index dc5fdf370..71f0b0c09 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -260,17 +260,16 @@ void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, f } } -void GameController::SetEngine(Engine* engine) { +void GameController::SetEngine(std::unique_ptr engine) { std::scoped_lock _{m_mutex}; - delete m_engine; - m_engine = engine; + m_engine = std::move(engine); if (m_engine) { m_engine->Init(); } } Engine* GameController::GetEngine() { - return m_engine; + return m_engine.get(); } u32 GameController::Poll() { diff --git a/src/input/controller.h b/src/input/controller.h index bb7363457..a45e71d77 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include "common/types.h" #include "core/libraries/pad/pad.h" @@ -77,7 +78,7 @@ public: void SetLightBarRGB(u8 r, u8 g, u8 b); void SetVibration(u8 smallMotor, u8 largeMotor); void SetTouchpadState(int touchIndex, bool touchDown, float x, float y); - void SetEngine(Engine*); + void SetEngine(std::unique_ptr); Engine* GetEngine(); u32 Poll(); @@ -100,7 +101,7 @@ private: std::array m_states; std::array m_private; - Engine* m_engine = nullptr; + std::unique_ptr m_engine = nullptr; }; } // namespace Input diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index 89187df69..0de0ea396 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -263,7 +263,7 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_ SDL_SetWindowFullscreen(window, Config::getIsFullscreen()); SDL_InitSubSystem(SDL_INIT_GAMEPAD); - controller->SetEngine(new Input::SDLInputEngine()); + controller->SetEngine(std::make_unique()); #if defined(SDL_PLATFORM_WIN32) window_info.type = WindowSystemType::Windows; @@ -573,7 +573,7 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) { switch (event->type) { case SDL_EVENT_GAMEPAD_ADDED: case SDL_EVENT_GAMEPAD_REMOVED: - controller->SetEngine(new Input::SDLInputEngine()); + controller->SetEngine(std::make_unique()); break; case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: