From d6120f05bcc63a40d803272a43a289754c907af9 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Sat, 2 Jul 2022 16:10:58 +1000 Subject: [PATCH] Only call fmt::format_to when the FPS value updates Don't even need to initialise the buffer since the last update time is set when DiabloInit is called, then as long as the loading screen takes more than a second the threshold gets hit to call format_to on the first frame of gameplay. --- Source/engine/render/scrollrt.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index a87a5408f..a1a36ce56 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -209,9 +209,7 @@ uint32_t sgdwCursHgtOld; */ Bitset2d dRendered; -int frameend; -int framerate; -int framestart; +int lastFpsUpdateInMs; const char *const PlayerModeNames[] = { "standing", @@ -1322,23 +1320,24 @@ void DrawView(const Surface &out, Point startPosition) */ void DrawFPS(const Surface &out) { - char buf[12]; + static int framesSinceLastUpdate = 0; + static fmt::memory_buffer buf {}; if (!frameflag || !gbActive) { return; } - frameend++; - uint32_t tc = SDL_GetTicks(); - uint32_t frames = tc - framestart; - if (tc - framestart >= 1000) { - framestart = tc; - framerate = 1000 * frameend / frames; - frameend = 0; - } - const char *end = fmt::format_to_n(buf, sizeof(buf), FMT_COMPILE("{} FPS"), framerate).out; - string_view str { buf, static_cast(end - buf) }; - DrawString(out, str, Point { 8, 68 }, UiFlags::ColorRed); + framesSinceLastUpdate++; + uint32_t runtimeInMs = SDL_GetTicks(); + uint32_t msSinceLastUpdate = runtimeInMs - lastFpsUpdateInMs; + if (msSinceLastUpdate >= 1000) { + lastFpsUpdateInMs = runtimeInMs; + int framerate = 1000 * framesSinceLastUpdate / msSinceLastUpdate; + framesSinceLastUpdate = 0; + buf.clear(); + fmt::format_to(std::back_inserter(buf), FMT_COMPILE("{} FPS"), framerate); + }; + DrawString(out, { buf.data(), buf.size() }, Point { 8, 68 }, UiFlags::ColorRed); } /** @@ -1654,7 +1653,7 @@ void ScrollView() void EnableFrameCount() { frameflag = true; - framestart = SDL_GetTicks(); + lastFpsUpdateInMs = SDL_GetTicks(); } void scrollrt_draw_game_screen()