]> git.localhorst.tv Git - blank.git/blobdiff - src/ui/ui.cpp
dedicated shader for outlines
[blank.git] / src / ui / ui.cpp
index 8a17be2b873b80ae3cddd9b855f5e69815757e62..f9836ba65a6af4035d7b95e8934c27eebcdced2c 100644 (file)
@@ -2,6 +2,7 @@
 #include "Interface.hpp"
 
 #include "../app/Assets.hpp"
+#include "../app/Environment.hpp"
 #include "../app/FrameCounter.hpp"
 #include "../app/init.hpp"
 #include "../audio/Audio.hpp"
@@ -34,15 +35,16 @@ HUD::HUD(const BlockTypeRegistry &types, const Font &font)
        block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
        block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
 
-       crosshair.vertices = std::vector<glm::vec3>({
+       OutlineModel::Buffer buf;
+       buf.vertices = std::vector<glm::vec3>({
                { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
                {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
        });
-       crosshair.indices = std::vector<OutlineModel::Index>({
+       buf.indices = std::vector<OutlineModel::Index>({
                0, 1, 2, 3
        });
-       crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
-       crosshair.Invalidate();
+       buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
+       crosshair.Update(buf);
 
        block_label.Position(
                glm::vec3(50.0f, 85.0f, 0.0f),
@@ -58,7 +60,7 @@ void HUD::Display(const Block &b) {
        const BlockType &type = types.Get(b.type);
 
        block_buf.Clear();
-       type.FillModel(block_buf, b.Transform());
+       type.FillEntityModel(block_buf, b.Transform());
        block.Update(block_buf);
 
        block_label.Set(font, type.label);
@@ -70,17 +72,18 @@ void HUD::Display(const Block &b) {
 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);
-
+       PlainColor &outline_prog = viewport.HUDOutlineProgram();
        viewport.EnableInvertBlending();
        viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
-       world_prog.SetM(viewport.Cursor());
+       outline_prog.SetM(viewport.Cursor());
        crosshair.Draw();
 
        if (block_visible) {
+               DirectionalLighting &world_prog = viewport.HUDProgram();
+               world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
+               // disable distance fog
+               world_prog.SetFogDensity(0.0f);
+
                viewport.DisableBlending();
                world_prog.SetM(block_transform);
                block.Draw();
@@ -91,16 +94,12 @@ void HUD::Render(Viewport &viewport) noexcept {
 
 Interface::Interface(
        const Config &config,
-       const Assets &assets,
-       Audio &audio,
-       const FrameCounter &counter,
+       Environment &env,
        World &world)
-: audio(audio)
-, counter(counter)
+: env(env)
 , world(world)
 , ctrl(world.Player())
-, font(assets.LoadFont("DejaVuSans", 16))
-, hud(world.BlockTypes(), font)
+, hud(world.BlockTypes(), env.assets.small_ui_font)
 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
 , aim_chunk(nullptr)
 , aim_block(0)
@@ -108,21 +107,25 @@ Interface::Interface(
 , outline()
 , outline_transform(1.0f)
 , counter_text()
-, messages(font)
+, messages(env.assets.small_ui_font)
 , msg_timer(5000)
 , config(config)
 , place_timer(256)
 , remove_timer(256)
 , remove(0)
 , selection(1)
-, place_sound(assets.LoadSound("thump"))
-, remove_sound(assets.LoadSound("plop"))
+, place_sound(env.assets.LoadSound("thump"))
+, remove_sound(env.assets.LoadSound("plop"))
 , fwd(0)
 , rev(0) {
        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));
+       position_text.Hide();
+       position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
+       position_text.Foreground(glm::vec4(1.0f));
+       position_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));
@@ -181,7 +184,7 @@ void Interface::HandlePress(const SDL_KeyboardEvent &event) {
                        ToggleVisual();
                        break;
                case SDLK_F3:
-                       ToggleCounter();
+                       ToggleDebug();
                        break;
                case SDLK_F4:
                        ToggleAudio();
@@ -337,20 +340,28 @@ void Interface::ToggleVisual() {
        }
 }
 
-void Interface::ToggleCounter() {
+void Interface::ToggleDebug() {
        counter_text.Toggle();
+       position_text.Toggle();
        if (counter_text.Visible()) {
                UpdateCounter();
+               UpdatePosition();
        }
 }
 
 void Interface::UpdateCounter() {
        std::stringstream s;
        s << std::setprecision(3) <<
-               "avg: " << counter.Average().running << "ms, "
-               "peak: " << counter.Peak().running << "ms";
+               "avg: " << env.counter.Average().running << "ms, "
+               "peak: " << env.counter.Peak().running << "ms";
        std::string text = s.str();
-       counter_text.Set(font, text);
+       counter_text.Set(env.assets.small_ui_font, text);
+}
+
+void Interface::UpdatePosition() {
+       std::stringstream s;
+       s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
+       position_text.Set(env.assets.small_ui_font, s.str());
 }
 
 
@@ -399,11 +410,10 @@ void Interface::PlaceBlock() {
                next_pos -= aim_normal * glm::vec3(Chunk::Extent());
        }
        mod_chunk->SetBlock(next_pos, selection);
-       mod_chunk->Invalidate();
 
        if (config.audio_disabled) return;
        const Entity &player = ctrl.Controlled();
-       audio.Play(
+       env.audio.Play(
                place_sound,
                mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
        );
@@ -412,11 +422,10 @@ void Interface::PlaceBlock() {
 void Interface::RemoveBlock() noexcept {
        if (!aim_chunk) return;
        aim_chunk->SetBlock(aim_block, remove);
-       aim_chunk->Invalidate();
 
        if (config.audio_disabled) return;
        const Entity &player = ctrl.Controlled();
-       audio.Play(
+       env.audio.Play(
                remove_sound,
                aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
        );
@@ -483,19 +492,29 @@ void Interface::Update(int dt) {
                CheckAim();
        }
 
-       if (counter_text.Visible() && counter.Changed()) {
+       if (counter_text.Visible() && env.counter.Changed()) {
                UpdateCounter();
        }
+       if (position_text.Visible()) {
+               UpdatePosition();
+       }
+}
+
+namespace {
+
+OutlineModel::Buffer outl_buf;
+
 }
 
 void Interface::CheckAim() {
        float dist;
        if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
-               outline.Clear();
-               aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
-               outline_transform = glm::scale(glm::vec3(1.0002f));
-               outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
+               outl_buf.Clear();
+               aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
+               outline.Update(outl_buf);
+               outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
                outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
+               outline_transform *= glm::scale(glm::vec3(1.005f));
        } else {
                aim_chunk = nullptr;
        }
@@ -506,14 +525,17 @@ void Interface::Render(Viewport &viewport) noexcept {
        if (config.visual_disabled) return;
 
        if (aim_chunk) {
-               DirectionalLighting &world_prog = viewport.EntityProgram();
-               world_prog.SetM(outline_transform);
+               PlainColor &outline_prog = viewport.WorldOutlineProgram();
+               outline_prog.SetM(outline_transform);
                outline.Draw();
        }
 
        if (counter_text.Visible()) {
                counter_text.Render(viewport);
        }
+       if (position_text.Visible()) {
+               position_text.Render(viewport);
+       }
 
        if (msg_timer.Running()) {
                messages.Render(viewport);