#include "stopwatch.hpp" #include namespace basalt::presenter::stopwatch { Stopwatch::Stopwatch(size_t interval) : start_point(std::chrono::steady_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::steady_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; // 输出给用户 spdlog::info("FPS: {:.2f} Frame Time: {:.4f} ms", fps, frame_time); // 把结束时间设置为开始时间继续下一轮循环 this->start_point = stop_point; } } } // namespace basalt::presenter::stopwatch