From 162a0df9221f3d89d6396f006b490c1ea6c96ff5 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 4 Oct 2019 20:48:29 +1000 Subject: [PATCH] Frontend: Display emulation speed --- src/core/system.cpp | 14 ++++++++++++-- src/core/system.h | 2 ++ src/duckstation/sdl_interface.cpp | 17 ++++++++++++++++- src/duckstation/sdl_interface.h | 2 ++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index 7727378f7..d61b1b9f8 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -81,6 +81,13 @@ bool System::Initialize() bool System::DoState(StateWrapper& sw) { + if (!sw.DoMarker("System")) + return false; + + sw.Do(&m_frame_number); + sw.Do(&m_internal_frame_number); + sw.Do(&m_global_tick_counter); + if (!sw.DoMarker("CPU") || !m_cpu->DoState(sw)) return false; @@ -127,6 +134,8 @@ void System::Reset() m_spu->Reset(); m_mdec->Reset(); m_frame_number = 1; + m_internal_frame_number = 0; + m_global_tick_counter = 0; } bool System::LoadState(ByteStream* state) @@ -268,10 +277,11 @@ bool System::SetExpansionROM(const char* filename) void System::Synchronize() { - m_cpu->ResetDowncount(); - const TickCount pending_ticks = m_cpu->GetPendingTicks(); m_cpu->ResetPendingTicks(); + m_cpu->ResetDowncount(); + + m_global_tick_counter += static_cast(pending_ticks); m_gpu->Execute(pending_ticks); m_timers->AddSystemTicks(pending_ticks); diff --git a/src/core/system.h b/src/core/system.h index 399257410..d81c8aec9 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -36,6 +36,7 @@ public: u32 GetFrameNumber() const { return m_frame_number; } u32 GetInternalFrameNumber() const { return m_internal_frame_number; } + u32 GetGlobalTickCounter() const { return m_global_tick_counter; } void IncrementFrameNumber() { m_frame_number++; } void IncrementInternalFrameNumber() { m_internal_frame_number++; } @@ -79,6 +80,7 @@ private: std::unique_ptr m_mdec; u32 m_frame_number = 1; u32 m_internal_frame_number = 1; + u32 m_global_tick_counter = 0; Settings m_settings; }; diff --git a/src/duckstation/sdl_interface.cpp b/src/duckstation/sdl_interface.cpp index 284367af1..604406dad 100644 --- a/src/duckstation/sdl_interface.cpp +++ b/src/duckstation/sdl_interface.cpp @@ -562,7 +562,17 @@ void SDLInterface::RenderMainMenuBar() ImGui::EndMenu(); } - ImGui::SetCursorPosX(ImGui::GetIO().DisplaySize.x - 170.0f); + ImGui::SetCursorPosX(ImGui::GetIO().DisplaySize.x - 205.0f); + + const u32 rounded_speed = static_cast(std::round(m_speed)); + if (m_speed < 90.0f) + ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "%u%%", rounded_speed); + else if (m_speed < 110.0f) + ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), "%u%%", rounded_speed); + else + ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "%u%%", rounded_speed); + + ImGui::SetCursorPosX(ImGui::GetIO().DisplaySize.x - 165.0f); ImGui::Text("FPS: %.2f", m_fps); ImGui::SetCursorPosX(ImGui::GetIO().DisplaySize.x - 80.0f); @@ -680,6 +690,11 @@ void SDLInterface::Run() m_fps = static_cast( static_cast(m_system->GetInternalFrameNumber() - m_last_internal_frame_number) / time); m_last_internal_frame_number = m_system->GetInternalFrameNumber(); + m_speed = + static_cast(static_cast(m_system->GetGlobalTickCounter() - m_last_global_tick_counter) / + (static_cast(MASTER_CLOCK) * time)) * + 100.0f; + m_last_global_tick_counter = m_system->GetGlobalTickCounter(); m_fps_timer.Reset(); } } diff --git a/src/duckstation/sdl_interface.h b/src/duckstation/sdl_interface.h index aebdc0a3f..c5eb8166f 100644 --- a/src/duckstation/sdl_interface.h +++ b/src/duckstation/sdl_interface.h @@ -84,8 +84,10 @@ private: float m_vps = 0.0f; float m_fps = 0.0f; + float m_speed = 1.0f; u32 m_last_frame_number = 0; u32 m_last_internal_frame_number = 0; + u32 m_last_global_tick_counter = 0; Timer m_fps_timer; // UI options