1
0
Files
BasaltMeter/BasaltPresenter/Presenter/stopwatch.cpp
2026-01-09 19:29:39 +08:00

31 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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