From 37a1465a83e4ac4363ed0d8e0fa1ce5055dd2db4 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 23 Jul 2015 16:25:40 +0200 Subject: [PATCH] display frame counter F3 to toggle --- running | 2 ++ src/app/app.cpp | 2 +- src/ui/Interface.hpp | 15 +++++++++++-- src/ui/ui.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/running b/running index 6764224..efc1062 100644 --- a/running +++ b/running @@ -63,3 +63,5 @@ info about the active block. L spits out the player position and light level there. C dumps info about the chunk of the pointed at block. Press N to toggle player/world collision. + +F3 toggles a display telling how long on average it takes to compute a frame. diff --git a/src/app/app.cpp b/src/app/app.cpp index 5c068a4..52a5905 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -41,7 +41,7 @@ Application::Application(const Config &config) , sprite_prog() , cam() , world(config.world) -, interface(config.interface, assets, world) +, interface(config.interface, assets, counter, world) , test_controller(MakeTestEntity(world)) , running(false) { if (config.vsync) { diff --git a/src/ui/Interface.hpp b/src/ui/Interface.hpp index 88637c6..d01fe25 100644 --- a/src/ui/Interface.hpp +++ b/src/ui/Interface.hpp @@ -15,10 +15,11 @@ namespace blank { +class Assets; class Chunk; class BlendedSprite; class DirectionalLighting; -class Assets; +class FrameCounter; class World; class Interface { @@ -34,7 +35,7 @@ public: bool visual_disabled = false; }; - Interface(const Config &, const Assets &, World &); + Interface(const Config &, const Assets &, const FrameCounter &, World &); void HandlePress(const SDL_KeyboardEvent &); void HandleRelease(const SDL_KeyboardEvent &); @@ -62,6 +63,9 @@ public: void SelectNext(); void SelectPrevious(); + void ToggleCounter(); + void UpdateCounter(); + void Update(int dt); void Render(DirectionalLighting &, BlendedSprite &) noexcept; @@ -70,6 +74,7 @@ private: void CheckAim(); private: + const FrameCounter &counter; World &world; FPSController ctrl; Font font; @@ -83,6 +88,12 @@ private: OutlineModel outline; glm::mat4 outline_transform; + bool show_counter; + Texture counter_tex; + SpriteModel counter_sprite; + glm::mat4 counter_transform; + SDL_Color counter_color; + Config config; IntervalTimer place_timer; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 4775999..88190e6 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -2,6 +2,7 @@ #include "Interface.hpp" #include "../app/Assets.hpp" +#include "../app/FrameCounter.hpp" #include "../app/init.hpp" #include "../graphics/BlendedSprite.hpp" #include "../graphics/DirectionalLighting.hpp" @@ -12,6 +13,7 @@ #include #include #include +#include #include #include @@ -106,8 +108,13 @@ void HUD::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) no } -Interface::Interface(const Config &config, const Assets &assets, World &world) -: world(world) +Interface::Interface( + const Config &config, + const Assets &assets, + const FrameCounter &counter, + World &world) +: counter(counter) +, world(world) , ctrl(world.Player()) , font(assets.LoadFont("DejaVuSans", 16)) , hud(world.BlockTypes(), font) @@ -117,6 +124,11 @@ Interface::Interface(const Config &config, const Assets &assets, World &world) , aim_normal() , outline() , outline_transform(1.0f) +, show_counter(false) +, counter_tex() +, counter_sprite() +, counter_transform(1.0f) +, counter_color{0xFF, 0xFF, 0xFF, 0xFF} , config(config) , place_timer(256) , remove_timer(256) @@ -175,6 +187,10 @@ void Interface::HandlePress(const SDL_KeyboardEvent &event) { case SDLK_p: PrintSelectionInfo(); break; + + case SDLK_F3: + ToggleCounter(); + break; } } @@ -284,6 +300,26 @@ void Interface::Print(const Block &block) { << std::endl; } +void Interface::ToggleCounter() { + if ((show_counter = !show_counter)) { + UpdateCounter(); + } +} + +void Interface::UpdateCounter() { + std::stringstream s; + s << std::setprecision(3) << counter.AvgRunning() << "ms"; + std::string text = s.str(); + font.Render(text.c_str(), counter_color, counter_tex); + glm::vec2 size(font.TextSize(text.c_str())); + counter_sprite.LoadRect(size.x, size.y); + counter_transform = glm::translate(glm::vec3( + 400.0f - size.x, + 25.0f, + 0.75f + )); +} + void Interface::Handle(const SDL_MouseMotionEvent &event) { if (config.mouse_disabled) return; @@ -392,6 +428,10 @@ void Interface::Update(int dt) { PlaceBlock(); CheckAim(); } + + if (show_counter && counter.Changed()) { + UpdateCounter(); + } } void Interface::CheckAim() { @@ -417,6 +457,13 @@ void Interface::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_pr outline.Draw(); } + if (show_counter) { + sprite_prog.Activate(); + sprite_prog.SetM(counter_transform); + sprite_prog.SetTexture(counter_tex); + counter_sprite.Draw(); + } + hud.Render(world_prog, sprite_prog); } -- 2.39.2