]> git.localhorst.tv Git - blank.git/blobdiff - src/ui/ui.cpp
also show peak in frame counter
[blank.git] / src / ui / ui.cpp
index 88190e643d57b05f6bf9dba6e9db9b617ea8bfdd..43a5f5d1ca68ef5b44d9ad3766d99e1154dd5670 100644 (file)
@@ -4,9 +4,8 @@
 #include "../app/Assets.hpp"
 #include "../app/FrameCounter.hpp"
 #include "../app/init.hpp"
-#include "../graphics/BlendedSprite.hpp"
-#include "../graphics/DirectionalLighting.hpp"
 #include "../graphics/Font.hpp"
+#include "../graphics/Viewport.hpp"
 #include "../model/shapes.hpp"
 #include "../world/World.hpp"
 
@@ -27,16 +26,8 @@ HUD::HUD(const BlockTypeRegistry &types, const Font &font)
 , block_buf()
 , block_transform(1.0f)
 , block_label()
-, label_sprite()
-, label_transform(1.0f)
-, label_color{0xFF, 0xFF, 0xFF, 0xFF}
 , block_visible(false)
-, crosshair()
-, crosshair_transform(1.0f)
-, near(100.0f)
-, far(-100.0f)
-, projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
-, view(1.0f) {
+, crosshair() {
        block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
        block_transform = glm::scale(block_transform, glm::vec3(50.0f));
        block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
@@ -51,16 +42,14 @@ HUD::HUD(const BlockTypeRegistry &types, const Font &font)
        });
        crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
        crosshair.Invalidate();
-}
-
 
-void HUD::Viewport(float width, float height) noexcept {
-       Viewport(0, 0, width, height);
-}
-
-void HUD::Viewport(float x, float y, float width, float height) noexcept {
-       projection = glm::ortho(x, width, height, y, near, far);
-       crosshair_transform = glm::translate(glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
+       block_label.Position(
+               glm::vec3(50.0f, 85.0f, 0.0f),
+               Gravity::NORTH_WEST,
+               Gravity::NORTH
+       );
+       block_label.Foreground(glm::vec4(1.0f));
+       block_label.Background(glm::vec4(0.5f));
 }
 
 
@@ -71,39 +60,30 @@ void HUD::Display(const Block &b) {
        type.FillModel(block_buf, b.Transform());
        block.Update(block_buf);
 
-       font.Render(type.label.c_str(), label_color, block_label);
-       glm::vec2 size(font.TextSize(type.label.c_str()));
-       label_sprite.LoadRect(size.x, size.y);
-       label_transform = glm::translate(glm::vec3(
-               std::max(5.0f, 50.0f - std::round(size.x * 0.5f)),
-               70.0f + size.y,
-               0.75f
-       ));
+       block_label.Set(font, type.label);
 
        block_visible = type.visible;
 }
 
 
-void HUD::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
-       world_prog.Activate();
+void HUD::Render(Viewport &viewport) noexcept {
+       viewport.ClearDepth();
+
+       DirectionalLighting &world_prog = viewport.HUDProgram();
        world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
        // disable distance fog
        world_prog.SetFogDensity(0.0f);
-       GLContext::ClearDepthBuffer();
 
-       GLContext::EnableInvertBlending();
-       world_prog.SetMVP(crosshair_transform, view, projection);
+       viewport.EnableInvertBlending();
+       viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
+       world_prog.SetM(viewport.Cursor());
        crosshair.Draw();
 
        if (block_visible) {
-               GLContext::DisableBlending();
+               viewport.DisableBlending();
                world_prog.SetM(block_transform);
                block.Draw();
-
-               sprite_prog.Activate();
-               sprite_prog.SetMVP(label_transform, view, projection);
-               sprite_prog.SetTexture(block_label);
-               label_sprite.Draw();
+               block_label.Render(viewport);
        }
 }
 
@@ -124,11 +104,9 @@ Interface::Interface(
 , aim_normal()
 , outline()
 , outline_transform(1.0f)
-, show_counter(false)
-, counter_tex()
-, counter_sprite()
-, counter_transform(1.0f)
-, counter_color{0xFF, 0xFF, 0xFF, 0xFF}
+, counter_text()
+, messages(font)
+, msg_timer(5000)
 , config(config)
 , place_timer(256)
 , remove_timer(256)
@@ -136,7 +114,13 @@ Interface::Interface(
 , selection(1)
 , fwd(0)
 , rev(0) {
-       hud.Viewport(960, 600);
+       counter_text.Hide();
+       counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
+       counter_text.Foreground(glm::vec4(1.0f));
+       counter_text.Background(glm::vec4(0.5f));
+       messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
+       messages.Foreground(glm::vec4(1.0f));
+       messages.Background(glm::vec4(0.5f));
        hud.Display(selection);
 }
 
@@ -231,61 +215,83 @@ void Interface::TurnBlock() {
 
 void Interface::ToggleCollision() {
        ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
-       std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
+       if (ctrl.Controlled().WorldCollidable()) {
+               PostMessage("collision on");
+       } else {
+               PostMessage("collision off");
+       }
 }
 
 void Interface::PrintBlockInfo() {
        std::cout << std::endl;
        if (!aim_chunk) {
-               std::cout << "not looking at any block" << std::endl;
+               PostMessage("not looking at any block");
                Ray aim = ctrl.Aim();
-               std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
+               std::stringstream s;
+               s << "aim ray: " << aim.orig << ", " << aim.dir;
+               PostMessage(s.str());
                return;
        }
-       std::cout << "looking at block " << aim_block
+       std::stringstream s;
+       s << "looking at block " << aim_block
                << " " << Chunk::ToCoords(aim_block)
                << " of chunk " << aim_chunk->Position()
-               << std::endl;
+       ;
+       PostMessage(s.str());
        Print(aim_chunk->BlockAt(aim_block));
 }
 
 void Interface::PrintChunkInfo() {
        std::cout << std::endl;
        if (!aim_chunk) {
-               std::cout << "not looking at any block" << std::endl;
+               PostMessage("not looking at any block");
                return;
        }
-       std::cout << "looking at chunk "
-               << aim_chunk->Position()
-               << std::endl;
+       std::stringstream s;
+       s << "looking at chunk " << aim_chunk->Position();
+       PostMessage(s.str());
 
-       std::cout << "  neighbors:" << std::endl;
+       PostMessage("  neighbors:");
        if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
-               std::cout << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
+               s.str("");
+               s << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
+               PostMessage(s.str());
        }
        if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
-               std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
+               s.str("");
+               s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
+               PostMessage(s.str());
        }
        if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
-               std::cout << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
+               s.str("");
+               s << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
+               PostMessage(s.str());
        }
        if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
-               std::cout << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
+               s.str("");
+               s << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
+               PostMessage(s.str());
        }
        if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
-               std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
+               s.str("");
+               s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
+               PostMessage(s.str());
        }
        if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
-               std::cout << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
+               s.str("");
+               s << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
+               PostMessage(s.str());
        }
        std::cout << std::endl;
 }
 
 void Interface::PrintLightInfo() {
-       std::cout
+       std::stringstream s;
+       s
                << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
                << " at position " << world.Player().Position()
-               << std::endl;
+       ;
+       PostMessage(s.str());
 }
 
 void Interface::PrintSelectionInfo() {
@@ -294,30 +300,28 @@ void Interface::PrintSelectionInfo() {
 }
 
 void Interface::Print(const Block &block) {
-       std::cout << "type: " << block.type
+       std::stringstream s;
+       s << "type: " << block.type
                << ", face: " << block.GetFace()
                << ", turn: " << block.GetTurn()
-               << std::endl;
+       ;
+       PostMessage(s.str());
 }
 
 void Interface::ToggleCounter() {
-       if ((show_counter = !show_counter)) {
+       counter_text.Toggle();
+       if (counter_text.Visible()) {
                UpdateCounter();
        }
 }
 
 void Interface::UpdateCounter() {
        std::stringstream s;
-       s << std::setprecision(3) << counter.AvgRunning() << "ms";
+       s << std::setprecision(3) <<
+               "avg: " << counter.Average().running << "ms, "
+               "peak: " << counter.Peak().running << "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
-       ));
+       counter_text.Set(font, text);
 }
 
 
@@ -402,10 +406,12 @@ void Interface::SelectPrevious() {
        hud.Display(selection);
 }
 
-void Interface::Handle(const SDL_WindowEvent &event) noexcept {
-       if (event.event == SDL_WINDOWEVENT_RESIZED) {
-               hud.Viewport(event.data1, event.data2);
-       }
+
+void Interface::PostMessage(const char *msg) {
+       messages.PushLine(msg);
+       msg_timer.Reset();
+       msg_timer.Start();
+       std::cout << msg << std::endl;
 }
 
 
@@ -413,12 +419,17 @@ void Interface::Update(int dt) {
        ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
        ctrl.Update(dt);
 
+       msg_timer.Update(dt);
        place_timer.Update(dt);
        remove_timer.Update(dt);
 
        aim = ctrl.Aim();
        CheckAim();
 
+       if (msg_timer.HitOnce()) {
+               msg_timer.Stop();
+       }
+
        if (remove_timer.Hit()) {
                RemoveBlock();
                CheckAim();
@@ -429,7 +440,7 @@ void Interface::Update(int dt) {
                CheckAim();
        }
 
-       if (show_counter && counter.Changed()) {
+       if (counter_text.Visible() && counter.Changed()) {
                UpdateCounter();
        }
 }
@@ -448,23 +459,24 @@ void Interface::CheckAim() {
 }
 
 
-void Interface::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
+void Interface::Render(Viewport &viewport) noexcept {
        if (config.visual_disabled) return;
 
        if (aim_chunk) {
-               world_prog.Activate();
+               DirectionalLighting &world_prog = viewport.EntityProgram();
                world_prog.SetM(outline_transform);
                outline.Draw();
        }
 
-       if (show_counter) {
-               sprite_prog.Activate();
-               sprite_prog.SetM(counter_transform);
-               sprite_prog.SetTexture(counter_tex);
-               counter_sprite.Draw();
+       if (counter_text.Visible()) {
+               counter_text.Render(viewport);
+       }
+
+       if (msg_timer.Running()) {
+               messages.Render(viewport);
        }
 
-       hud.Render(world_prog, sprite_prog);
+       hud.Render(viewport);
 }
 
 }