diff --git a/client/src/debug/debug_overlay.cpp b/client/src/debug/debug_overlay.cpp new file mode 100644 index 0000000..52a1094 --- /dev/null +++ b/client/src/debug/debug_overlay.cpp @@ -0,0 +1,52 @@ +#include + +#include "debug_overlay.h" +#include "ui/ui_renderer.h" +#include "gfx/types.h" + +DebugOverlay::DebugOverlay(void) : + _fps(0.0f), + _frame_time(0.0f), + _draw_calls(0), + _shape_vertices(0), + _text_vertices(0), + _update_timer(0.0f) {} + +void DebugOverlay::update(float dt, int draw_calls, int shape_verts, int txt_verts) { + _update_timer += dt; + /* Only update a few times per sec to keep them readable. */ + if(_update_timer > 0.2f) { + _update_timer = 0.0f; + _frame_time = dt * 1000.0f; + _fps = 1.0f / dt; + _draw_calls = draw_calls; + _shape_vertices = shape_verts; + _text_vertices = txt_verts; + } +} + +void DebugOverlay::render(UIRenderer* ui_renderer) { + const Color text_color = { 0.0f, 1.0f, 0.0f }; /* Bright green. */ + const float line_height = 18.0f; + const float padding = 5.0f; + + char buffer[256]; + + /* Manages its own rendering batch. */ + ui_renderer->begin_shapes(); + ui_renderer->begin_text(); + + snprintf(buffer, sizeof(buffer), "FPS: %.0f (%.2f ms)", _fps, _frame_time); + ui_renderer->render_text(buffer, padding, padding+line_height*1, text_color); + + snprintf(buffer, sizeof(buffer), "Draw Calls: %d", _draw_calls); + ui_renderer->render_text(buffer, padding, padding+line_height*2, text_color); + + snprintf(buffer, sizeof(buffer), "Vertices: %d (Shape %d, Text: %d)", + _shape_vertices + _text_vertices, _shape_vertices, _text_vertices); + ui_renderer->render_text(buffer, padding, padding+line_height*3, text_color); + + ui_renderer->flush_shapes(); + ui_renderer->flush_text(); +} + diff --git a/client/src/debug/debug_overlay.h b/client/src/debug/debug_overlay.h new file mode 100644 index 0000000..89397fb --- /dev/null +++ b/client/src/debug/debug_overlay.h @@ -0,0 +1,24 @@ +#pragma once + +class UIRenderer; + +/** + * Renders real-time performance metrics on screen. + */ +class DebugOverlay { +public: + DebugOverlay(void); + + void update(float dt, int draw_calls, int shape_verts, int txt_verts); + void render(UIRenderer* ui_renderer); + +private: + float _fps; + float _frame_time; + int _draw_calls; + int _shape_vertices; + int _text_vertices; + + /* Oh? You want to be able to read the stats?! */ + float _update_timer; +}; diff --git a/client/src/debug/debug_stats.cpp b/client/src/debug/debug_stats.cpp new file mode 100644 index 0000000..091616f --- /dev/null +++ b/client/src/debug/debug_stats.cpp @@ -0,0 +1,5 @@ +#include "debug_stats.h" + +int DebugStats::draw_calls = 0; +int DebugStats::shape_vertices = 0; +int DebugStats::text_vertices = 0; diff --git a/client/src/debug/debug_stats.h b/client/src/debug/debug_stats.h new file mode 100644 index 0000000..6216f7a --- /dev/null +++ b/client/src/debug/debug_stats.h @@ -0,0 +1,14 @@ +#pragma once + +struct DebugStats { + static int draw_calls; + static int shape_vertices; + static int text_vertices; + + static void reset(void) { + draw_calls = 0; + shape_vertices = 0; + text_vertices = 0; + } +}; +