Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
metrics_overlay.hpp
Go to the documentation of this file.
1
4#pragma once
5
8#include <aegis2d/render/export.hpp>
10
11#include <array>
12#include <chrono>
13#include <cstddef>
14#include <cstdint>
15#include <optional>
16#include <span>
17#include <string>
18
19namespace aegis2d::render {
20
24class AEGIS2D_RENDER_EXPORT FrameMetrics {
25public:
27 using Clock = std::chrono::steady_clock;
28
30 static constexpr std::size_t capacity = 120;
31
35 void tick(Clock::time_point now) noexcept;
36
39 void record(double seconds) noexcept;
40
42 void clear() noexcept;
43
46 [[nodiscard]] std::size_t size() const noexcept;
47
52 [[nodiscard]] double frame_ms(std::size_t index) const;
53
56 [[nodiscard]] double last_ms() const noexcept;
57
60 [[nodiscard]] double average_ms() const noexcept;
61
64 [[nodiscard]] double worst_ms() const noexcept;
65
69 [[nodiscard]] double fps() const noexcept;
70
71private:
72 std::array<double, capacity> frames_{};
73 std::size_t next_ = 0;
74 std::size_t size_ = 0;
75 std::optional<Clock::time_point> last_tick_;
76};
77
81 Vec2 position{8.0F, 8.0F};
83 float text_scale = 2.0F;
85 float graph_height = 40.0F;
87 float bar_width = 2.0F;
90 double budget_ms = 1000.0 / 60.0;
93 std::int32_t layer = 1'000'000;
95 Color text = rgb(0xFFFFFF);
97 Color fast = rgb(0x56B4E9);
99 Color slow = rgb(0xD55E00);
101 Color panel = rgb(0x000000, 0.7F);
102};
103
113[[nodiscard]] AEGIS2D_RENDER_EXPORT std::string
114metrics_text(const FrameMetrics& metrics, FrameStats stats, std::optional<std::size_t> entities,
115 std::optional<std::size_t> memory = std::nullopt,
116 std::span<const std::string> lines = {});
117
136class AEGIS2D_RENDER_EXPORT MetricsOverlay {
137public:
140 explicit MetricsOverlay(const MetricsOverlayStyle& style = {}) noexcept;
141
144 [[nodiscard]] bool visible() const noexcept;
145
148 void set_visible(bool visible) noexcept;
149
151 void toggle() noexcept;
152
155 [[nodiscard]] const MetricsOverlayStyle& style() const noexcept;
156
160 [[nodiscard]] FrameStats game_stats(const IRenderer& renderer) const noexcept;
161
170 void draw(IRenderer& renderer, const BitmapFont& font, const FrameMetrics& metrics,
171 std::optional<std::size_t> entities = std::nullopt,
172 std::span<const std::string> lines = {});
173
179 [[nodiscard]] std::optional<std::size_t> memory() const noexcept;
180
181private:
182 MetricsOverlayStyle style_;
183 bool visible_ = false;
184 std::size_t drawn_sprites_ = 0;
185 std::size_t drawn_batches_ = 0;
186 std::optional<std::size_t> memory_;
187 std::optional<FrameMetrics::Clock::time_point> memory_read_;
188};
189
190} // namespace aegis2d::render
Text drawn with a bitmap font, through the sprite batcher.
Monospaced font whose glyphs are cells of a texture, drawn as sprites.
Definition bitmap_font.hpp:68
Durations of the last frames, and the frame rate they make.
Definition metrics_overlay.hpp:24
void tick(Clock::time_point now) noexcept
Records the frame that ends at now: the time since the previous tick().
void record(double seconds) noexcept
Records a frame of a given duration; the oldest one is dropped when full.
std::chrono::steady_clock Clock
Clock of tick().
Definition metrics_overlay.hpp:27
void clear() noexcept
Forgets every frame and the last tick().
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
Overlay of the frame metrics, for developers: frames per second, frame times, entities,...
Definition metrics_overlay.hpp:136
MetricsOverlay(const MetricsOverlayStyle &style={}) noexcept
Hidden overlay.
bool visible() const noexcept
Tells whether the overlay is shown.
std::optional< std::size_t > memory() const noexcept
Memory of the process the overlay shows.
RGBA color used by the renderer.
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25
std::string metrics_text(const FrameMetrics &metrics, FrameStats stats, std::optional< std::size_t > entities, std::optional< std::size_t > memory=std::nullopt, std::span< const std::string > lines={})
Text of the metrics overlay.
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
Counters of the last finished frame.
Definition renderer.hpp:64
Where and how the metrics overlay is drawn.
Definition metrics_overlay.hpp:79
float bar_width
Width of a frame's bar, in logical pixels.
Definition metrics_overlay.hpp:87
Color fast
Color of the frames within the budget.
Definition metrics_overlay.hpp:97
double budget_ms
Frame time budget in milliseconds: a line marks it, half way up the graph, and longer frames are draw...
Definition metrics_overlay.hpp:90
float text_scale
Scale of the text (TextStyle::scale).
Definition metrics_overlay.hpp:83
float graph_height
Height of the frame time graph, in logical pixels.
Definition metrics_overlay.hpp:85
Vec2 position
Top-left corner of the panel, in logical pixels.
Definition metrics_overlay.hpp:81
std::int32_t layer
Layer of the panel and the bars; the text is drawn one layer above. Keep both layers for the overlay ...
Definition metrics_overlay.hpp:93
Color slow
Color of the frames over the budget, told from fast with any color vision.
Definition metrics_overlay.hpp:99
Color text
Color of the text.
Definition metrics_overlay.hpp:95
Color panel
Color of the panel behind.
Definition metrics_overlay.hpp:101