add stopwatch
This commit is contained in:
@@ -4,6 +4,7 @@ PRIVATE
|
|||||||
main.cpp
|
main.cpp
|
||||||
dll_loader.cpp
|
dll_loader.cpp
|
||||||
cmd_client.cpp
|
cmd_client.cpp
|
||||||
|
stopwatch.cpp
|
||||||
)
|
)
|
||||||
target_sources(BasaltPresenter
|
target_sources(BasaltPresenter
|
||||||
PUBLIC
|
PUBLIC
|
||||||
@@ -11,6 +12,7 @@ FILE_SET HEADERS
|
|||||||
FILES
|
FILES
|
||||||
dll_loader.hpp
|
dll_loader.hpp
|
||||||
cmd_client.hpp
|
cmd_client.hpp
|
||||||
|
stopwatch.hpp
|
||||||
)
|
)
|
||||||
target_include_directories(BasaltPresenter
|
target_include_directories(BasaltPresenter
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "dll_loader.hpp"
|
#include "dll_loader.hpp"
|
||||||
#include "cmd_client.hpp"
|
#include "cmd_client.hpp"
|
||||||
|
#include "stopwatch.hpp"
|
||||||
#include <basalt/char_types.hpp>
|
#include <basalt/char_types.hpp>
|
||||||
#include <basalt/engine.hpp>
|
#include <basalt/engine.hpp>
|
||||||
#include <basalt/deliver.hpp>
|
#include <basalt/deliver.hpp>
|
||||||
@@ -16,6 +17,7 @@ using dll_loader::DllKind;
|
|||||||
using dll_loader::DllLoader;
|
using dll_loader::DllLoader;
|
||||||
|
|
||||||
using ::basalt::presenter::cmd_client::CmdClient;
|
using ::basalt::presenter::cmd_client::CmdClient;
|
||||||
|
using ::basalt::presenter::stopwatch::Stopwatch;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
auto engine_dll = DllLoader(DllKind::Engine, BSTEXT("BasaltDirectX11Engine"));
|
auto engine_dll = DllLoader(DllKind::Engine, BSTEXT("BasaltDirectX11Engine"));
|
||||||
@@ -30,8 +32,10 @@ int main(int argc, char* argv[]) {
|
|||||||
EngineConfig engine_config{.headless = false, .width = payload.width, .height = payload.height};
|
EngineConfig engine_config{.headless = false, .width = payload.width, .height = payload.height};
|
||||||
engine->startup(std::move(engine_config));
|
engine->startup(std::move(engine_config));
|
||||||
|
|
||||||
|
Stopwatch stopwatch(120);
|
||||||
while (true) {
|
while (true) {
|
||||||
auto req_stop = engine->tick();
|
auto req_stop = engine->tick();
|
||||||
|
stopwatch.tick();
|
||||||
auto can_stop = client.tick(req_stop);
|
auto can_stop = client.tick(req_stop);
|
||||||
if (can_stop) break;
|
if (can_stop) break;
|
||||||
}
|
}
|
||||||
|
|||||||
30
BasaltPresenter/Presenter/stopwatch.cpp
Normal file
30
BasaltPresenter/Presenter/stopwatch.cpp
Normal 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
|
||||||
21
BasaltPresenter/Presenter/stopwatch.hpp
Normal file
21
BasaltPresenter/Presenter/stopwatch.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace basalt::presenter::stopwatch {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 一个简单的FPS计算器,每隔指定次数统计一次FPS并输出
|
||||||
|
*/
|
||||||
|
class Stopwatch {
|
||||||
|
public:
|
||||||
|
Stopwatch(size_t interval);
|
||||||
|
~Stopwatch();
|
||||||
|
|
||||||
|
void tick();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::chrono::steady_clock::time_point start_point;
|
||||||
|
size_t interval, counter;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace basalt::presenter::stopwatch
|
||||||
Reference in New Issue
Block a user