From 06e42ead575675a0018032d057af122e2fbb0d67 Mon Sep 17 00:00:00 2001 From: Attila Uygun Date: Sun, 15 Oct 2023 10:02:46 +0200 Subject: [PATCH] Use imgui to show stats --- src/engine/engine.cc | 74 +++++++++----------------------------------- src/engine/engine.h | 6 +--- 2 files changed, 16 insertions(+), 64 deletions(-) diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 83ec335..f3e3664 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -13,13 +13,13 @@ #include "engine/drawable.h" #include "engine/game.h" #include "engine/game_factory.h" -#include "engine/image_quad.h" #include "engine/input_event.h" #include "engine/platform/platform.h" #include "engine/renderer/geometry.h" #include "engine/renderer/renderer.h" #include "engine/renderer/shader.h" #include "engine/renderer/texture.h" +#include "third_party/imgui/imgui.h" #include "third_party/texture_compressor/texture_compressor.h" using namespace base; @@ -43,8 +43,6 @@ Engine::Engine(Platform* platform) singleton = this; platform_->SetObserver(this); - - stats_ = std::make_unique(); } Engine::~Engine() { @@ -55,7 +53,6 @@ Engine::~Engine() { imgui_backend_.Shutdown(); game_.reset(); - stats_.reset(); textures_.clear(); shaders_.clear(); quad_.reset(); @@ -116,9 +113,6 @@ void Engine::Initialize() { system_font_ = std::make_unique(); system_font_->Load("engine/RobotoMono-Regular.ttf"); - SetImageSource("stats_tex", std::bind(&Engine::PrintStats, this)); - stats_->SetZOrder(std::numeric_limits::max()); - game_ = GameFactoryBase::CreateGame(""); CHECK(game_) << "No game found to run."; CHECK(game_->PreInitialize()) << "Failed to pre-initialize the game."; @@ -146,9 +140,6 @@ void Engine::Update(float delta_time) { fps_ = renderer_->GetAndResetFPS(); fps_seconds_ = 0; } - - if (stats_->IsVisible()) - RefreshImage("stats_tex"); } void Engine::Draw(float frame_frac) { @@ -165,6 +156,18 @@ void Engine::Draw(float frame_frac) { d->Draw(frame_frac); } + if (stats_visible_) { + ImGuiWindowFlags window_flags = + ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse | + ImGuiWindowFlags_NoNav | ImGuiWindowFlags_AlwaysAutoResize; + ImGui::Begin("Stats", nullptr, window_flags); + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); + ImGui::Text("%s", renderer_->GetDebugName()); + ImGui::Text("%d fps", fps_); + ImGui::End(); + } + imgui_backend_.Render(); renderer_->Present(); @@ -536,25 +539,17 @@ void Engine::AddInputEvent(std::unique_ptr event) { case InputEvent::kDragEnd: if (((GetScreenSize() / 2) * 0.9f - event->GetVector()).Length() <= 0.25f) { - SetStatsVisible(!stats_->IsVisible()); + stats_visible_ = !stats_visible_; // TODO: Enqueue DragCancel so we can consume this event. } break; case InputEvent::kKeyPress: if (event->GetKeyPress() == 's') { - SetStatsVisible(!stats_->IsVisible()); + stats_visible_ = !stats_visible_; // Consume event. return; } break; - case InputEvent::kDrag: - if (stats_->IsVisible()) { - if ((stats_->GetPosition() - event->GetVector()).Length() <= - stats_->GetSize().y) - stats_->SetPosition(event->GetVector()); - // TODO: Enqueue DragCancel so we can consume this event. - } - break; default: break; } @@ -700,43 +695,4 @@ void Engine::WaitForAsyncWork() { } } -void Engine::SetStatsVisible(bool visible) { - stats_->SetVisible(visible); - if (visible) - stats_->Create("stats_tex"); - else - stats_->Destroy(); -} - -std::unique_ptr Engine::PrintStats() { - if (!stats_->IsVisible()) - return nullptr; - - constexpr int width = 200; - std::vector lines; - std::string line; - line = "fps: "; - line += std::to_string(fps_); - lines.push_back(line); - lines.push_back(renderer_->GetDebugName()); - - constexpr int margin = 5; - int line_height = system_font_->GetLineHeight(); - int image_width = width + margin * 2; - int image_height = (line_height + margin) * lines.size() + margin; - - auto image = std::make_unique(); - image->Create(image_width, image_height); - image->Clear({1, 1, 1, 0.08f}); - - int y = margin; - for (auto& text : lines) { - system_font_->Print(margin, y, text.c_str(), image->GetBuffer(), - image->GetWidth()); - y += line_height + margin; - } - - return image; -} - } // namespace eng diff --git a/src/engine/engine.h b/src/engine/engine.h index 05042e7..02b4d9b 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -26,7 +26,6 @@ class Font; class Game; class Geometry; class Image; -class ImageQuad; class InputEvent; class Platform; class Renderer; @@ -195,7 +194,7 @@ class Engine : public PlatformObserver { size_t async_work_count_ = 0; - std::unique_ptr stats_; + bool stats_visible_ = false; ImguiBackend imgui_backend_; @@ -243,9 +242,6 @@ class Engine : public PlatformObserver { void WaitForAsyncWork(); - void SetStatsVisible(bool visible); - std::unique_ptr PrintStats(); - Engine(const Engine&) = delete; Engine& operator=(const Engine&) = delete; };