overlay: fps window clean up (#506)

## Link to GitHub Issue, if one exists
n/a

## Description of change
Use fmt library for formatting time

Round down sub-second units so that the FPS counter only updates once
per second (instead of Time and Uptime ticking independently)

clean up usage of `localtime` in `logging.cpp`
This commit is contained in:
bicarus
2026-01-08 21:52:13 -08:00
committed by GitHub
parent 5d2a9fa1cf
commit e3d63c65c1
2 changed files with 16 additions and 13 deletions
+13 -11
View File
@@ -1,5 +1,6 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include "external/fmt/include/fmt/chrono.h"
#include "fps.h" #include "fps.h"
namespace overlay::windows { namespace overlay::windows {
@@ -15,7 +16,8 @@ namespace overlay::windows {
| ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavInputs
| ImGuiWindowFlags_NoDocking; | ImGuiWindowFlags_NoDocking;
this->bg_alpha = 0.5f; this->bg_alpha = 0.5f;
this->start_time = std::chrono::system_clock::now(); this->start_time =
std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
} }
void FPS::calculate_initial_window() { void FPS::calculate_initial_window() {
@@ -34,23 +36,23 @@ namespace overlay::windows {
ImGui::Text("FPS: %.1f", io.Framerate); ImGui::Text("FPS: %.1f", io.Framerate);
// ImGui::Text("FT: %.2fms", 1000 / io.Framerate); // ImGui::Text("FT: %.2fms", 1000 / io.Framerate);
auto now = std::chrono::system_clock::now(); const auto now = std::chrono::system_clock::now();
const auto now_s = std::chrono::floor<std::chrono::seconds>(now);
// current time // current time
{ {
auto now_t = std::chrono::system_clock::to_time_t(now); const std::time_t tt = std::chrono::system_clock::to_time_t(now_s);
static CHAR buf[48]; std::tm local_tm{};
std::strftime(buf, sizeof(buf), "Time: %H:%M:%S", std::localtime(&now_t)); localtime_s(&local_tm, &tt);
ImGui::Text(buf); ImGui::TextUnformatted(fmt::format("Time: {:%H:%M:%S}", local_tm).c_str());
} }
// elapsed time // elapsed time
{ {
auto d = now - this->start_time; const auto uptime = now_s - this->start_time;
const auto h = std::chrono::duration_cast<std::chrono::hours>(d); ImGui::TextUnformatted(
const auto m = std::chrono::duration_cast<std::chrono::minutes>(d - h); fmt::format("Up: {:%H:%M:%S}",
const auto s = std::chrono::duration_cast<std::chrono::seconds>(d - h - m); std::chrono::floor<std::chrono::seconds>(uptime)).c_str());
ImGui::Text("Up: %02d:%02d:%02d", (int)h.count(), (int)m.count(), (int)s.count());
} }
} }
} }
+3 -2
View File
@@ -13,8 +13,9 @@ std::string_view log_get_datetime() {
std::string_view log_get_datetime(std::time_t now) { std::string_view log_get_datetime(std::time_t now) {
static thread_local char buf[64]; static thread_local char buf[64];
// `localtime` on Windows is thread-safe std::tm local_tm{};
strftime(buf, sizeof(buf), "[%Y/%m/%d %X]", localtime(&now)); localtime_s(&local_tm, &now);
strftime(buf, sizeof(buf), "[%Y/%m/%d %X]", &local_tm);
return buf; return buf;
} }