1
0
Files
BasaltMeter/BasaltPresenter/Presenter/stopwatch.cpp

31 lines
1.1 KiB
C++
Raw Normal View History

2026-01-09 19:29:39 +08:00
#include "stopwatch.hpp"
2026-01-09 22:50:24 +08:00
#include <spdlog/spdlog.h>
2026-01-09 19:29:39 +08:00
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;
// 输出给用户
2026-01-09 22:50:24 +08:00
spdlog::info("FPS: {:.2f} Frame Time: {:.4f} ms", fps, frame_time);
2026-01-09 19:29:39 +08:00
// 把结束时间设置为开始时间继续下一轮循环
this->start_point = stop_point;
}
}
} // namespace basalt::presenter::stopwatch