diff options
author | Aki <please@ignore.pl> | 2022-05-03 20:47:59 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-05-03 20:47:59 +0200 |
commit | 313b7a531106c623a28883db515f245a74a5fafb (patch) | |
tree | c839eb081a45310cf30d44849bf85097797f944e /View.cpp | |
parent | c610c4b94eda867a3f0b083038502dccb8116170 (diff) | |
download | derelict-313b7a531106c623a28883db515f245a74a5fafb.zip derelict-313b7a531106c623a28883db515f245a74a5fafb.tar.gz derelict-313b7a531106c623a28883db515f245a74a5fafb.tar.bz2 |
Moved view functionality into own class
Diffstat (limited to 'View.cpp')
-rw-r--r-- | View.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/View.cpp b/View.cpp new file mode 100644 index 0000000..90bc542 --- /dev/null +++ b/View.cpp @@ -0,0 +1,74 @@ +#include "View.h" + +#include <algorithm> +#include <cmath> +#include <memory> +#include <utility> + +#include <raylib.h> + +#include "Source.h" + + +View::View(std::unique_ptr<Source> source) : + m_camera {}, + m_source {std::move(source)}, + m_projected {} +{ + m_camera.position = Vector3{10.0f, 10.0f, 10.0f}; + m_camera.target = Vector3{0.0f, 0.0f, 0.0f}; + m_camera.up = Vector3{0.0f, 1.0f, 0.0f}; + m_camera.fovy = 45; + m_camera.projection = CAMERA_PERSPECTIVE; + SetCameraMode(m_camera, CAMERA_ORBITAL); +} + + +void +View::update(const float dt) +{ + UpdateCamera(&m_camera); + const int height = GetScreenHeight(); + const int width = GetScreenWidth(); + const auto killmails = m_source->all(); + m_projected.clear(); + m_projected.reserve(killmails.size()); + for (const auto& km : killmails) { + const auto pos = GetWorldToScreen(km.position, m_camera); + const float depth = + std::sqrt( + std::pow(m_camera.position.x - km.position.x, 2) + + std::pow(m_camera.position.y - km.position.y, 2) + + std::pow(m_camera.position.z - km.position.z, 2)); + if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y) + continue; + const auto base = GetWorldToScreen({km.position.x, 0.0f, km.position.z}, m_camera); + m_projected.push_back(Projected{pos, base, depth}); + } + std::sort(m_projected.begin(), m_projected.end(), [](auto& a, auto& b){ return a.depth > b.depth; }); +} + + +void +View::draw() const +{ + BeginDrawing(); + ClearBackground(RAYWHITE); + BeginMode3D(m_camera); + DrawGrid(12, 1.0f); + EndMode3D(); + for (const auto& point : m_projected) { + DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, LIGHTGRAY); + DrawCircle(point.pos.x, point.pos.y, 3, RED); + } + DrawFPS(5, 5); + int y = 25; + const int height = GetScreenHeight(); + for (const auto& point : m_projected) { + 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; + } + EndDrawing(); +} |