The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
tracked_drawable.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2025 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
16
19
20#include <algorithm>
21#include <numeric>
22#include <utility>
23
24namespace gui2
25{
26using namespace std::chrono_literals;
27
29 : frametimes_(50)
30 , render_count_(0)
31 , render_counter_(0)
32 , last_lap_()
33 , last_render_(clock::time_point::max())
34{
35}
36
38{
39 // Unconditionally close the fps report, since there currently can only be one
40 // report open at a time. If we need to support multiple reports, we should add
41 // a way to track its associated drawable and close its companion report here.
43}
44
46{
47 if(prefs::get().show_fps()) {
49 } else {
51 }
52}
53
55{
56 if(frametimes_.empty()) {
57 return utils::nullopt;
58 }
59
60 using std::chrono::milliseconds;
61 const auto [min_time, avg_time, max_time] = get_times();
62
63 return frame_info {
64 std::chrono::duration_cast<milliseconds>(min_time),
65 std::chrono::duration_cast<milliseconds>(avg_time),
66 std::chrono::duration_cast<milliseconds>(max_time),
67
68 // NOTE: max fps corresponds to the *shortest* time between frames, and vice-versa
69 static_cast<unsigned>(1s / max_time), // min
70 static_cast<unsigned>(1s / avg_time), // avg
71 static_cast<unsigned>(1s / min_time), // max
73 };
74}
75
77{
78 const auto [min_time, max_time]
79 = std::minmax_element(frametimes_.begin(), frametimes_.end());
80
81 const auto total_time
82 = std::accumulate(frametimes_.begin(), frametimes_.end(), clock::duration{0});
83
84 return { *min_time, total_time / frametimes_.size(), *max_time };
85}
86
88{
89 auto now = clock::now();
90 auto elapsed = now - last_render_;
91
92 if(elapsed > clock::duration{0}) {
93 frametimes_.push_back(elapsed);
94 }
95
96 last_render_ = now;
98
99 if(now - last_lap_ >= 1s) {
100 last_lap_ = now;
101 render_count_ = std::exchange(render_counter_, 0);
102 }
103}
104
105} // namespace gui2
void process() override
Inherited from events::pump_monitor.
clock::time_point last_render_
std::tuple< clock::duration, clock::duration, clock::duration > times
auto get_times() const -> times
Get min, average, and max frametimes in steady_clock resolution.
boost::circular_buffer< clock::duration > frametimes_
clock::time_point last_lap_
auto get_info() const -> utils::optional< frame_info >
Returns the current frame time and info, or nullopt if no times have been recorded.
void update_count()
Records time since last invocation.
std::chrono::steady_clock clock
static prefs & get()
void show(const gui2::tracked_drawable &target)
Displays the fps report popup for the given tracked_drawable.
void hide()
Hides the fps report popup.
Generic file dialog.
static map_location::direction s