Project Alice
Loading...
Searching...
No Matches
gui_fps_counter.hpp
Go to the documentation of this file.
1#pragma once
2
4namespace ui {
5
7private:
8 std::chrono::time_point<std::chrono::steady_clock> last_render_time{};
9 std::chrono::time_point<std::chrono::steady_clock> last_compute_time{};
10
11public:
12 void on_create(sys::state& state) noexcept override {
14 set_text(state, "0");
15 }
16
17 void render(sys::state& state, int32_t x, int32_t y) noexcept override {
18 std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
19 if(last_render_time == std::chrono::time_point<std::chrono::steady_clock>{}) {
20 last_render_time = now;
21 }
22 auto microseconds_since_last_render = std::chrono::duration_cast<std::chrono::microseconds>(now - last_render_time);
23 auto microseconds_since_last_compute = std::chrono::duration_cast<std::chrono::microseconds>(now - last_compute_time);
24 if(microseconds_since_last_render.count() > 0.f && microseconds_since_last_compute.count() > 1e5) {
25 auto frames_per_second = 1.f / float(microseconds_since_last_render.count() / 1e6);
26 set_text(state, std::to_string(int32_t(frames_per_second)));
27 last_compute_time = now;
28 }
29 last_render_time = now;
30
32 }
33};
34
35} // namespace ui
void render(sys::state &state, int32_t x, int32_t y) noexcept override
void on_create(sys::state &state) noexcept override
void render(sys::state &state, int32_t x, int32_t y) noexcept override
void on_create(sys::state &state) noexcept override
void set_text(sys::state &state, std::string const &new_text)