X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fui%2Fui.cpp;h=198defb2850f5a628247784c2ebc1bddea41fed5;hb=5998b18978bd8e7a0c9deb516474634e1d3521c9;hp=e3ddf1020a26bbde6cde0a2499c8c9c60008e810;hpb=50f35affb16c78bd3d0b420f5ba37d74fcac391f;p=blank.git diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index e3ddf10..198defb 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -4,6 +4,7 @@ #include "../app/Assets.hpp" #include "../app/FrameCounter.hpp" #include "../app/init.hpp" +#include "../audio/Audio.hpp" #include "../graphics/Font.hpp" #include "../graphics/Viewport.hpp" #include "../model/shapes.hpp" @@ -33,15 +34,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({ + OutlineModel::Buffer buf; + buf.vertices = std::vector({ { -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({ + buf.indices = std::vector({ 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), @@ -57,7 +59,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); @@ -91,9 +93,11 @@ void HUD::Render(Viewport &viewport) noexcept { Interface::Interface( const Config &config, const Assets &assets, + Audio &audio, const FrameCounter &counter, World &world) -: counter(counter) +: audio(audio) +, counter(counter) , world(world) , ctrl(world.Player()) , font(assets.LoadFont("DejaVuSans", 16)) @@ -105,17 +109,24 @@ Interface::Interface( , outline() , outline_transform(1.0f) , counter_text() +, messages(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")) , 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)); + 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); } @@ -167,9 +178,15 @@ void Interface::HandlePress(const SDL_KeyboardEvent &event) { PrintSelectionInfo(); break; + case SDLK_F1: + ToggleVisual(); + break; case SDLK_F3: ToggleCounter(); break; + case SDLK_F4: + ToggleAudio(); + break; } } @@ -210,61 +227,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() { @@ -273,10 +312,30 @@ 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::ToggleAudio() { + config.audio_disabled = !config.audio_disabled; + if (config.audio_disabled) { + PostMessage("audio off"); + } else { + PostMessage("audio on"); + } +} + +void Interface::ToggleVisual() { + config.visual_disabled = !config.visual_disabled; + if (config.visual_disabled) { + PostMessage("visual off"); + } else { + PostMessage("visual on"); + } } void Interface::ToggleCounter() { @@ -288,7 +347,9 @@ void Interface::ToggleCounter() { 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(); counter_text.Set(font, text); } @@ -340,12 +401,26 @@ void Interface::PlaceBlock() { } mod_chunk->SetBlock(next_pos, selection); mod_chunk->Invalidate(); + + if (config.audio_disabled) return; + const Entity &player = ctrl.Controlled(); + audio.Play( + place_sound, + mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos) + ); } 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( + remove_sound, + aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block)) + ); } @@ -376,16 +451,29 @@ void Interface::SelectPrevious() { } +void Interface::PostMessage(const char *msg) { + messages.PushLine(msg); + msg_timer.Reset(); + msg_timer.Start(); + std::cout << msg << std::endl; +} + + 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(); @@ -401,11 +489,18 @@ void Interface::Update(int dt) { } } +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); + outl_buf.Clear(); + aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf); + outline.Update(outl_buf); outline_transform = glm::scale(glm::vec3(1.0002f)); outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords()); outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block); @@ -428,6 +523,10 @@ void Interface::Render(Viewport &viewport) noexcept { counter_text.Render(viewport); } + if (msg_timer.Running()) { + messages.Render(viewport); + } + hud.Render(viewport); }