diff --git a/src/duckstation/main.cpp b/src/duckstation/main.cpp index 8da93ee4a..26c114fa3 100644 --- a/src/duckstation/main.cpp +++ b/src/duckstation/main.cpp @@ -30,15 +30,6 @@ static int Run(int argc, char* argv[]) return -1; } - // create display and host interface - std::unique_ptr host_interface = SDLInterface::Create(); - if (!host_interface) - { - Panic("Failed to create host interface"); - SDL_Quit(); - return -1; - } - // parameters const char* filename = nullptr; const char* exp1_filename = nullptr; @@ -59,21 +50,14 @@ static int Run(int argc, char* argv[]) #undef CHECK_ARG_PARAM } - // create system - const bool boot = (filename != nullptr || exp1_filename != nullptr || !state_filename.IsEmpty()); - if (boot) + // create display and host interface + std::unique_ptr host_interface = + SDLInterface::Create(filename, exp1_filename, state_filename.IsEmpty() ? nullptr : state_filename.GetCharArray()); + if (!host_interface) { - if (!host_interface->InitializeSystem(filename, exp1_filename)) - { - host_interface.reset(); - SDL_Quit(); - return -1; - } - - host_interface->ConnectDevices(); - - if (!state_filename.IsEmpty()) - host_interface->LoadState(state_filename); + Panic("Failed to create host interface"); + SDL_Quit(); + return -1; } // run diff --git a/src/duckstation/sdl_interface.cpp b/src/duckstation/sdl_interface.cpp index 7754b35e3..e8984ee3e 100644 --- a/src/duckstation/sdl_interface.cpp +++ b/src/duckstation/sdl_interface.cpp @@ -193,7 +193,30 @@ bool SDLInterface::CreateAudioStream() return true; } -std::unique_ptr SDLInterface::Create() +bool SDLInterface::InitializeSystem(const char* filename, const char* exp1_filename) +{ + if (!HostInterface::InitializeSystem(filename, exp1_filename)) + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "System initialization failed.", m_window); + return false; + } + + ConnectDevices(); + return true; +} + +void SDLInterface::ConnectDevices() +{ + m_controller = DigitalController::Create(); + m_system->SetController(0, m_controller); + + m_memory_card = MemoryCard::Create(); + m_system->SetMemoryCard(0, m_memory_card); +} + +std::unique_ptr SDLInterface::Create(const char* filename /* = nullptr */, + const char* exp1_filename /* = nullptr */, + const char* save_state_filename /* = nullptr */) { std::unique_ptr intf = std::make_unique(); if (!intf->CreateSDLWindow() || !intf->CreateGLContext() || !intf->CreateImGuiContext() || @@ -202,6 +225,16 @@ std::unique_ptr SDLInterface::Create() return nullptr; } + const bool boot = (filename != nullptr || exp1_filename != nullptr || save_state_filename != nullptr); + if (boot) + { + if (!intf->InitializeSystem(filename, exp1_filename)) + return nullptr; + + if (save_state_filename) + intf->LoadState(save_state_filename); + } + return intf; } @@ -561,7 +594,7 @@ void SDLInterface::DrawMainMenuBar() if (ImGui::BeginMenu("Save State")) { - for (u32 i = 1; i <= 8; i++) + for (u32 i = 1; i <= NUM_QUICK_SAVE_STATES; i++) { if (ImGui::MenuItem(TinyString::FromFormat("State %u", i).GetCharArray())) DoSaveState(i); @@ -842,12 +875,7 @@ void SDLInterface::DoStartDisc() AddOSDMessage(SmallString::FromFormat("Starting disc from '%s'...", path)); if (!InitializeSystem(path, nullptr)) - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "System initialization failed.", m_window); return; - } - - ConnectDevices(); } void SDLInterface::DoStartBIOS() @@ -856,16 +884,14 @@ void SDLInterface::DoStartBIOS() AddOSDMessage("Starting BIOS..."); if (!InitializeSystem(nullptr, nullptr)) - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "System initialization failed.", m_window); return; - } - - ConnectDevices(); } void SDLInterface::DoLoadState(u32 index) { + if (!HasSystem() && !InitializeSystem(nullptr, nullptr)) + return; + LoadState(GetSaveStateFilename(index)); m_last_frame_number = m_system->GetFrameNumber(); m_last_internal_frame_number = m_system->GetInternalFrameNumber(); @@ -875,18 +901,10 @@ void SDLInterface::DoLoadState(u32 index) void SDLInterface::DoSaveState(u32 index) { + Assert(m_system); SaveState(GetSaveStateFilename(index)); } -void SDLInterface::ConnectDevices() -{ - m_controller = DigitalController::Create(); - m_system->SetController(0, m_controller); - - m_memory_card = MemoryCard::Create(); - m_system->SetMemoryCard(0, m_memory_card); -} - void SDLInterface::Run() { m_audio_stream->PauseOutput(false); diff --git a/src/duckstation/sdl_interface.h b/src/duckstation/sdl_interface.h index da77908cb..f597cc23e 100644 --- a/src/duckstation/sdl_interface.h +++ b/src/duckstation/sdl_interface.h @@ -7,8 +7,8 @@ #include #include #include -#include #include +#include class System; class DigitalController; @@ -21,22 +21,24 @@ public: SDLInterface(); ~SDLInterface(); - static std::unique_ptr Create(); + static std::unique_ptr Create(const char* filename = nullptr, const char* exp1_filename = nullptr, + const char* save_state_filename = nullptr); static TinyString GetSaveStateFilename(u32 index); - void SetDisplayTexture(GL::Texture* texture, u32 offset_x, u32 offset_y, u32 width, u32 height, float aspect_ratio) override; + void SetDisplayTexture(GL::Texture* texture, u32 offset_x, u32 offset_y, u32 width, u32 height, + float aspect_ratio) override; void ReportMessage(const char* message) override; // Adds OSD messages, duration is in seconds. void AddOSDMessage(const char* message, float duration = 2.0f) override; - void ConnectDevices(); - void Run(); private: + static constexpr u32 NUM_QUICK_SAVE_STATES = 10; + struct OSDMessage { String text; @@ -44,12 +46,17 @@ private: float duration; }; + bool HasSystem() const { return static_cast(m_system); } + bool CreateSDLWindow(); bool CreateGLContext(); bool CreateImGuiContext(); bool CreateGLResources(); bool CreateAudioStream(); + bool InitializeSystem(const char* filename = nullptr, const char* exp1_filename = nullptr); + void ConnectDevices(); + // We only pass mouse input through if it's grabbed bool IsWindowFullscreen() const; void DrawImGui();