]> git.localhorst.tv Git - blank.git/commitdiff
display frame counter
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 23 Jul 2015 14:25:40 +0000 (16:25 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 23 Jul 2015 15:28:41 +0000 (17:28 +0200)
F3 to toggle

running
src/app/app.cpp
src/ui/Interface.hpp
src/ui/ui.cpp

diff --git a/running b/running
index 6764224d216d40a75ac71eb7f3da998c84906cd6..efc10622250668924aa495db9f00d0d3e19e04d8 100644 (file)
--- 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.
index 5c068a45bc15419fa9be74a5c9af36c8d51607f1..52a5905bd19b065828edc708212c0021587cada0 100644 (file)
@@ -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) {
index 88637c6adc368144b9825d411c03729b46cf9ba1..d01fe254a665ed0011c214a97fae7af4b9dbb030 100644 (file)
 
 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;
index 4775999310d1bb6b2bdd44998725597a6011539e..88190e643d57b05f6bf9dba6e9db9b617ea8bfdd 100644 (file)
@@ -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 <algorithm>
 #include <cmath>
 #include <iostream>
+#include <sstream>
 #include <glm/gtc/matrix_transform.hpp>
 #include <glm/gtx/io.hpp>
 
@@ -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);
 }