From e49474fc0dcff041a9fae2857273938481cc9ec9 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 May 2022 18:01:38 +0200 Subject: Expanded label rendering --- Label.h | 1 + View.cpp | 36 +++++++++++++++++++++--------------- View.h | 2 ++ main.cpp | 20 +++++++++++--------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Label.h b/Label.h index 25d9d4f..688dead 100644 --- a/Label.h +++ b/Label.h @@ -8,4 +8,5 @@ struct Label Vector2 pos; Vector2 base; float depth; + float length; }; diff --git a/View.cpp b/View.cpp index 77ab271..debf10c 100644 --- a/View.cpp +++ b/View.cpp @@ -13,7 +13,8 @@ View::View(std::vector grids) : m_camera {}, m_grids {grids}, m_labels {}, - m_grid {0} + m_grid {0}, + m_texture {LoadTexture("resources/wreck.png")} { m_camera.position = Vector3{10.0f, 10.0f, 10.0f}; m_camera.target = Vector3{0.0f, 0.0f, 0.0f}; @@ -24,6 +25,12 @@ View::View(std::vector grids) : } +View::~View() +{ + UnloadTexture(m_texture); +} + + void View::update(const float dt) { @@ -40,15 +47,20 @@ View::update(const float dt) m_labels.reserve(wrecks.size()); for (const auto& wreck : wrecks) { const auto pos = GetWorldToScreen(wreck.position, m_camera); + const auto base = GetWorldToScreen({wreck.position.x, 0.0f, wreck.position.z}, m_camera); + if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y) + if (0 > base.x || width < base.x || 0 > base.y || height < base.y) + continue; const float depth = std::sqrt( std::pow(m_camera.position.x - wreck.position.x, 2) + std::pow(m_camera.position.y - wreck.position.y, 2) + std::pow(m_camera.position.z - wreck.position.z, 2)); - if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y) - continue; - const auto base = GetWorldToScreen({wreck.position.x, 0.0f, wreck.position.z}, m_camera); - m_labels.push_back(Label{pos, base, depth}); + const float length = + std::sqrt( + std::pow(pos.x - base.x, 2) + + std::pow(pos.y - base.y, 2)); + m_labels.push_back(Label{pos, base, depth, length}); } std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; }); } @@ -63,17 +75,11 @@ View::draw() const DrawGrid(12, 1.0f); EndMode3D(); for (const auto& point : m_labels) { - DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, LIGHTGRAY); - DrawCircle(point.pos.x, point.pos.y, 3, RED); + if (point.length > 8) + DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, DARKGRAY); + DrawTexture(m_texture, point.pos.x - 8, point.pos.y - 8, WHITE); } DrawFPS(5, 5); - int y = 25; - const int height = GetScreenHeight(); - for (const auto& point : m_labels) { - DrawText(TextFormat("%5.1f x %5.1f x %5.1f", point.pos.x, point.pos.y, point.depth), 5, y, 10, DARKGRAY); - y += 10; - if (y > height) - break; - } + DrawText(TextFormat("%d", m_labels.size()), 5, 25, 20, DARKGRAY); EndDrawing(); } diff --git a/View.h b/View.h index 1928726..e259132 100644 --- a/View.h +++ b/View.h @@ -12,6 +12,7 @@ class View { public: explicit View(std::vector grids); + virtual ~View(); void update(float dt); void draw() const; private: @@ -20,4 +21,5 @@ private: std::vector