X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fui%2Fui.cpp;h=e3ddf1020a26bbde6cde0a2499c8c9c60008e810;hb=50f35affb16c78bd3d0b420f5ba37d74fcac391f;hp=ea9786b0382952c4aea69a5ac29b1cd0e7d8ed00;hpb=955fbb45dedb570520fc45d2ce69f420bed2ad08;p=blank.git diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index ea9786b..e3ddf10 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -1,30 +1,33 @@ #include "HUD.hpp" #include "Interface.hpp" +#include "../app/Assets.hpp" +#include "../app/FrameCounter.hpp" #include "../app/init.hpp" -#include "../graphics/DirectionalLighting.hpp" +#include "../graphics/Font.hpp" +#include "../graphics/Viewport.hpp" #include "../model/shapes.hpp" #include "../world/World.hpp" +#include +#include #include +#include #include #include namespace blank { -HUD::HUD(const BlockTypeRegistry &types) +HUD::HUD(const BlockTypeRegistry &types, const Font &font) : types(types) +, font(font) , block() , block_buf() , block_transform(1.0f) +, block_label() , 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(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) { +, 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)); @@ -39,16 +42,14 @@ HUD::HUD(const BlockTypeRegistry &types) }); 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::mat4(1.0f), 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)); } @@ -58,34 +59,52 @@ void HUD::Display(const Block &b) { block_buf.Clear(); type.FillModel(block_buf, b.Transform()); block.Update(block_buf); + + block_label.Set(font, type.label); + block_visible = type.visible; } -void HUD::Render(DirectionalLighting &program) noexcept { +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); + + viewport.EnableInvertBlending(); + viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER); + world_prog.SetM(viewport.Cursor()); + crosshair.Draw(); + if (block_visible) { - program.SetLightDirection({ 1.0f, 3.0f, 5.0f }); - // disable distance fog - program.SetFogDensity(0.0f); - GLContext::ClearDepthBuffer(); - program.SetMVP(block_transform, view, projection); + viewport.DisableBlending(); + world_prog.SetM(block_transform); block.Draw(); - program.SetM(crosshair_transform); - crosshair.Draw(); + block_label.Render(viewport); } } -Interface::Interface(const Config &config, World &world) -: world(world) +Interface::Interface( + const Config &config, + const Assets &assets, + const FrameCounter &counter, + World &world) +: counter(counter) +, world(world) , ctrl(world.Player()) -, hud(world.BlockTypes()) +, font(assets.LoadFont("DejaVuSans", 16)) +, hud(world.BlockTypes(), font) , aim{{ 0, 0, 0 }, { 0, 0, -1 }} , aim_chunk(nullptr) , aim_block(0) , aim_normal() , outline() , outline_transform(1.0f) +, counter_text() , config(config) , place_timer(256) , remove_timer(256) @@ -93,7 +112,10 @@ Interface::Interface(const Config &config, World &world) , 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)); hud.Display(selection); } @@ -144,6 +166,10 @@ void Interface::HandlePress(const SDL_KeyboardEvent &event) { case SDLK_p: PrintSelectionInfo(); break; + + case SDLK_F3: + ToggleCounter(); + break; } } @@ -253,6 +279,20 @@ void Interface::Print(const Block &block) { << std::endl; } +void Interface::ToggleCounter() { + counter_text.Toggle(); + if (counter_text.Visible()) { + UpdateCounter(); + } +} + +void Interface::UpdateCounter() { + std::stringstream s; + s << std::setprecision(3) << counter.AvgRunning() << "ms"; + std::string text = s.str(); + counter_text.Set(font, text); +} + void Interface::Handle(const SDL_MouseMotionEvent &event) { if (config.mouse_disabled) return; @@ -335,12 +375,6 @@ 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::Update(int dt) { ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity); @@ -361,6 +395,10 @@ void Interface::Update(int dt) { PlaceBlock(); CheckAim(); } + + if (counter_text.Visible() && counter.Changed()) { + UpdateCounter(); + } } void Interface::CheckAim() { @@ -377,15 +415,20 @@ void Interface::CheckAim() { } -void Interface::Render(DirectionalLighting &program) noexcept { +void Interface::Render(Viewport &viewport) noexcept { if (config.visual_disabled) return; if (aim_chunk) { - program.SetM(outline_transform); + DirectionalLighting &world_prog = viewport.EntityProgram(); + world_prog.SetM(outline_transform); outline.Draw(); } - hud.Render(program); + if (counter_text.Visible()) { + counter_text.Render(viewport); + } + + hud.Render(viewport); } }