Use unique_ptr

This commit is contained in:
Quang Ngô 2025-01-18 13:16:40 +07:00
parent ecbee1ff55
commit 10ea1d266c
3 changed files with 8 additions and 8 deletions

View File

@ -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> engine) {
std::scoped_lock _{m_mutex}; std::scoped_lock _{m_mutex};
delete m_engine; m_engine = std::move(engine);
m_engine = engine;
if (m_engine) { if (m_engine) {
m_engine->Init(); m_engine->Init();
} }
} }
Engine* GameController::GetEngine() { Engine* GameController::GetEngine() {
return m_engine; return m_engine.get();
} }
u32 GameController::Poll() { u32 GameController::Poll() {

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <memory>
#include <mutex> #include <mutex>
#include "common/types.h" #include "common/types.h"
#include "core/libraries/pad/pad.h" #include "core/libraries/pad/pad.h"
@ -77,7 +78,7 @@ public:
void SetLightBarRGB(u8 r, u8 g, u8 b); void SetLightBarRGB(u8 r, u8 g, u8 b);
void SetVibration(u8 smallMotor, u8 largeMotor); void SetVibration(u8 smallMotor, u8 largeMotor);
void SetTouchpadState(int touchIndex, bool touchDown, float x, float y); void SetTouchpadState(int touchIndex, bool touchDown, float x, float y);
void SetEngine(Engine*); void SetEngine(std::unique_ptr<Engine>);
Engine* GetEngine(); Engine* GetEngine();
u32 Poll(); u32 Poll();
@ -100,7 +101,7 @@ private:
std::array<State, MAX_STATES> m_states; std::array<State, MAX_STATES> m_states;
std::array<StateInternal, MAX_STATES> m_private; std::array<StateInternal, MAX_STATES> m_private;
Engine* m_engine = nullptr; std::unique_ptr<Engine> m_engine = nullptr;
}; };
} // namespace Input } // namespace Input

View File

@ -263,7 +263,7 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_
SDL_SetWindowFullscreen(window, Config::getIsFullscreen()); SDL_SetWindowFullscreen(window, Config::getIsFullscreen());
SDL_InitSubSystem(SDL_INIT_GAMEPAD); SDL_InitSubSystem(SDL_INIT_GAMEPAD);
controller->SetEngine(new Input::SDLInputEngine()); controller->SetEngine(std::make_unique<Input::SDLInputEngine>());
#if defined(SDL_PLATFORM_WIN32) #if defined(SDL_PLATFORM_WIN32)
window_info.type = WindowSystemType::Windows; window_info.type = WindowSystemType::Windows;
@ -573,7 +573,7 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) {
switch (event->type) { switch (event->type) {
case SDL_EVENT_GAMEPAD_ADDED: case SDL_EVENT_GAMEPAD_ADDED:
case SDL_EVENT_GAMEPAD_REMOVED: case SDL_EVENT_GAMEPAD_REMOVED:
controller->SetEngine(new Input::SDLInputEngine()); controller->SetEngine(std::make_unique<Input::SDLInputEngine>());
break; break;
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: