SDL: Fix crashes on starting emulation

This commit is contained in:
Connor McLaughlin 2020-02-16 00:33:43 +09:00
parent 8aed270a1f
commit 961bc09979
4 changed files with 29 additions and 29 deletions

View File

@ -381,6 +381,7 @@ void D3D11HostDisplay::Render()
RenderDisplay();
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
if (!m_vsync && m_allow_tearing_supported)

View File

@ -355,6 +355,7 @@ void OpenGLHostDisplay::Render()
RenderDisplay();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(m_window);

View File

@ -27,7 +27,7 @@ Log_SetChannel(SDLHostInterface);
SDLHostInterface::SDLHostInterface()
{
m_update_settings_event_id = SDL_RegisterEvents(1);
m_run_later_event_id = SDL_RegisterEvents(1);
}
SDLHostInterface::~SDLHostInterface()
@ -198,20 +198,22 @@ void SDLHostInterface::OnControllerTypeChanged(u32 slot)
g_sdl_controller_interface.SetDefaultBindings();
}
void SDLHostInterface::SaveSettings()
{
SDLSettingsInterface si(GetSettingsFileName().c_str());
m_settings.Save(si);
}
void SDLHostInterface::QueueUpdateSettings()
void SDLHostInterface::RunLater(std::function<void()> callback)
{
SDL_Event ev = {};
ev.type = SDL_USEREVENT;
ev.user.code = m_update_settings_event_id;
ev.user.code = m_run_later_event_id;
ev.user.data1 = new std::function<void()>(std::move(callback));
SDL_PushEvent(&ev);
}
void SDLHostInterface::SaveAndUpdateSettings()
{
SDLSettingsInterface si(GetSettingsFileName().c_str());
m_settings.Save(si);
m_settings.Load(si);
}
void SDLHostInterface::UpdateFullscreen()
{
SDL_SetWindowFullscreen(m_window, m_settings.display_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
@ -309,12 +311,12 @@ void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
case SDL_USEREVENT:
{
if (static_cast<u32>(event->user.code) == m_update_settings_event_id)
if (static_cast<u32>(event->user.code) == m_run_later_event_id)
{
UpdateSettings([this]() {
SDLSettingsInterface si(GetSettingsFileName().c_str());
m_settings.Load(si);
});
std::function<void()>* callback = static_cast<std::function<void()>*>(event->user.data1);
Assert(callback);
(*callback)();
delete callback;
}
}
break;
@ -784,10 +786,7 @@ void SDLHostInterface::DrawQuickSettingsMenu()
settings_changed |= ImGui::MenuItem("Display Linear Filtering", nullptr, &m_settings.display_linear_filtering);
if (settings_changed)
{
SaveSettings();
QueueUpdateSettings();
}
RunLater(std::bind(&SDLHostInterface::SaveAndUpdateSettings, this));
}
void SDLHostInterface::DrawDebugMenu()
@ -851,7 +850,7 @@ void SDLHostInterface::DrawPoweredOffWindow()
ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Resume", button_size))
{
ResumeSystemFromMostRecentState();
RunLater([this]() { ResumeSystemFromMostRecentState(); });
ClearImGuiFocus();
}
ImGui::NewLine();
@ -859,7 +858,7 @@ void SDLHostInterface::DrawPoweredOffWindow()
ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Start Disc", button_size))
{
DoStartDisc();
RunLater([this]() { DoStartDisc(); });
ClearImGuiFocus();
}
ImGui::NewLine();
@ -867,7 +866,7 @@ void SDLHostInterface::DrawPoweredOffWindow()
ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Start BIOS", button_size))
{
BootSystemFromFile(nullptr);
RunLater([this]() { BootSystemFromFile(nullptr); });
ClearImGuiFocus();
}
ImGui::NewLine();
@ -883,7 +882,7 @@ void SDLHostInterface::DrawPoweredOffWindow()
std::snprintf(buf, sizeof(buf), "State %u", i);
if (ImGui::MenuItem(buf))
{
LoadState(true, i);
RunLater([this, i]() { LoadState(true, i); });
ClearImGuiFocus();
}
}
@ -1152,10 +1151,7 @@ void SDLHostInterface::DrawSettingsWindow()
ImGui::End();
if (settings_changed)
{
SaveSettings();
QueueUpdateSettings();
}
RunLater(std::bind(&SDLHostInterface::SaveAndUpdateSettings, this));
}
void SDLHostInterface::DrawAboutWindow()

View File

@ -83,8 +83,10 @@ private:
void DestroyDisplay();
void CreateImGuiContext();
void SaveSettings();
void QueueUpdateSettings();
/// Executes a callback later, after the UI has finished rendering. Needed to boot while rendering ImGui.
void RunLater(std::function<void()> callback);
void SaveAndUpdateSettings();
void UpdateFullscreen();
@ -115,7 +117,7 @@ private:
KeyboardControllerActionMap m_keyboard_button_mapping;
u32 m_update_settings_event_id = 0;
u32 m_run_later_event_id = 0;
bool m_quit_request = false;
bool m_frame_step_request = false;