1
0

add stopwatch

This commit is contained in:
2026-01-09 19:29:39 +08:00
parent ff34754274
commit fba5d0627c
4 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
#include "stopwatch.hpp"
#include <print>
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<std::chrono::milliseconds>(stop_point - this->start_point);
auto frame_time = static_cast<double>(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