From fba5d0627cc27c93d9ac13092db5db1cacb5981c Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Fri, 9 Jan 2026 19:29:39 +0800 Subject: [PATCH] add stopwatch --- BasaltPresenter/Presenter/CMakeLists.txt | 2 ++ BasaltPresenter/Presenter/main.cpp | 4 ++++ BasaltPresenter/Presenter/stopwatch.cpp | 30 ++++++++++++++++++++++++ BasaltPresenter/Presenter/stopwatch.hpp | 21 +++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 BasaltPresenter/Presenter/stopwatch.cpp create mode 100644 BasaltPresenter/Presenter/stopwatch.hpp diff --git a/BasaltPresenter/Presenter/CMakeLists.txt b/BasaltPresenter/Presenter/CMakeLists.txt index bf97147..6345742 100644 --- a/BasaltPresenter/Presenter/CMakeLists.txt +++ b/BasaltPresenter/Presenter/CMakeLists.txt @@ -4,6 +4,7 @@ PRIVATE main.cpp dll_loader.cpp cmd_client.cpp + stopwatch.cpp ) target_sources(BasaltPresenter PUBLIC @@ -11,6 +12,7 @@ FILE_SET HEADERS FILES dll_loader.hpp cmd_client.hpp + stopwatch.hpp ) target_include_directories(BasaltPresenter PUBLIC diff --git a/BasaltPresenter/Presenter/main.cpp b/BasaltPresenter/Presenter/main.cpp index c67becd..bfad3eb 100644 --- a/BasaltPresenter/Presenter/main.cpp +++ b/BasaltPresenter/Presenter/main.cpp @@ -1,5 +1,6 @@ #include "dll_loader.hpp" #include "cmd_client.hpp" +#include "stopwatch.hpp" #include #include #include @@ -16,6 +17,7 @@ using dll_loader::DllKind; using dll_loader::DllLoader; using ::basalt::presenter::cmd_client::CmdClient; +using ::basalt::presenter::stopwatch::Stopwatch; int main(int argc, char* argv[]) { auto engine_dll = DllLoader(DllKind::Engine, BSTEXT("BasaltDirectX11Engine")); @@ -30,8 +32,10 @@ int main(int argc, char* argv[]) { EngineConfig engine_config{.headless = false, .width = payload.width, .height = payload.height}; engine->startup(std::move(engine_config)); + Stopwatch stopwatch(120); while (true) { auto req_stop = engine->tick(); + stopwatch.tick(); auto can_stop = client.tick(req_stop); if (can_stop) break; } diff --git a/BasaltPresenter/Presenter/stopwatch.cpp b/BasaltPresenter/Presenter/stopwatch.cpp new file mode 100644 index 0000000..95fc597 --- /dev/null +++ b/BasaltPresenter/Presenter/stopwatch.cpp @@ -0,0 +1,30 @@ +#include "stopwatch.hpp" +#include + +namespace basalt::presenter::stopwatch { + + Stopwatch::Stopwatch(size_t interval) : start_point(std::chrono::high_resolution_clock::now()), interval(interval), counter(0) {} + + Stopwatch::~Stopwatch() {} + + void Stopwatch::tick() { + // 自增计时器 + ++this->counter; + if (this->counter >= this->interval) { + // 计数器归零 + this->counter = 0; + + // 记下当前时刻,计算帧时间和FPS + auto stop_point = std::chrono::high_resolution_clock::now(); + auto time_diff = std::chrono::duration_cast(stop_point - this->start_point); + auto frame_time = static_cast(time_diff.count()) / this->interval; + auto fps = 1000.0 / frame_time; + // 输出给用户 + std::print("FPS: {:.2f} Frame Time: {:.4f} ms\n", fps, frame_time); + + // 把结束时间设置为开始时间继续下一轮循环 + this->start_point = stop_point; + } + } + +} // namespace basalt::presenter::stopwatch diff --git a/BasaltPresenter/Presenter/stopwatch.hpp b/BasaltPresenter/Presenter/stopwatch.hpp new file mode 100644 index 0000000..20c150a --- /dev/null +++ b/BasaltPresenter/Presenter/stopwatch.hpp @@ -0,0 +1,21 @@ +#pragma once +#include + +namespace basalt::presenter::stopwatch { + + /** + * @brief 一个简单的FPS计算器,每隔指定次数统计一次FPS并输出 + */ + class Stopwatch { + public: + Stopwatch(size_t interval); + ~Stopwatch(); + + void tick(); + + private: + std::chrono::steady_clock::time_point start_point; + size_t interval, counter; + }; + +} // namespace basalt::presenter::stopwatch