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