From 313b7a531106c623a28883db515f245a74a5fafb Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 3 May 2022 20:47:59 +0200 Subject: Moved view functionality into own class --- CMakeLists.txt | 1 + ExampleSource.cpp | 40 ++++++++++++++---------------- ExampleSource.h | 5 +++- Source.h | 2 +- View.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ View.h | 27 ++++++++++++++++++++ main.cpp | 66 +++---------------------------------------------- 7 files changed, 129 insertions(+), 86 deletions(-) create mode 100644 View.cpp create mode 100644 View.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b0e7a6b..b93c6f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,5 +8,6 @@ add_executable( ${PROJECT_NAME} ExampleSource.cpp main.cpp + View.cpp ) target_link_libraries(${PROJECT_NAME} raylib) diff --git a/ExampleSource.cpp b/ExampleSource.cpp index e79e7ad..fa022e9 100644 --- a/ExampleSource.cpp +++ b/ExampleSource.cpp @@ -1,5 +1,6 @@ #include "ExampleSource.h" +#include #include #include @@ -8,16 +9,13 @@ #include "Killmail.h" -constexpr int AMOUNT {1000}; constexpr float MAX {4.0f}; constexpr float MIN {-4.0f}; -constexpr float STEP {0.1f}; +constexpr float STEP {0.2f}; -static float random_position(); - - -ExampleSource::ExampleSource() +ExampleSource::ExampleSource() : + m_time {0.0f} { } @@ -26,22 +24,20 @@ std::vector ExampleSource::all() const { std::vector killmails; - killmails.reserve(AMOUNT); - for (int i = 0; i < AMOUNT; ++i) { - Killmail km; - km.position = { - random_position(), - random_position(), - random_position(), - }; - killmails.push_back(std::move(km)); + killmails.reserve(std::pow((MAX - MIN) / STEP, 2)); + m_time += GetFrameTime(); + if (m_time > 2.0f * M_PI) + m_time -= 2.0f * M_PI; + for (float z = MIN; z <= MAX; z += STEP) { + for (float x = MIN; x <= MAX; x += STEP) { + Killmail km; + km.position = { + x, + std::cos(x) + std::cos(z + m_time), + z, + }; + killmails.push_back(std::move(km)); + } } return killmails; } - - -float -random_position() -{ - return GetRandomValue(MIN / STEP, MAX / STEP) * STEP; -} diff --git a/ExampleSource.h b/ExampleSource.h index 2f45332..2086ca5 100644 --- a/ExampleSource.h +++ b/ExampleSource.h @@ -6,8 +6,11 @@ #include "Source.h" -struct ExampleSource : public Source +class ExampleSource : public Source { +public: ExampleSource(); std::vector all() const override; +private: + mutable float m_time; }; diff --git a/Source.h b/Source.h index 295a65b..547fd0e 100644 --- a/Source.h +++ b/Source.h @@ -8,5 +8,5 @@ struct Source { virtual ~Source() = default; - virtual std::vector all() const = 0; + virtual std::vector all() const = 0; // maybe an immutable view instead of a copy? }; 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 +#include +#include +#include + +#include + +#include "Source.h" + + +View::View(std::unique_ptr 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(); +} diff --git a/View.h b/View.h new file mode 100644 index 0000000..13eb881 --- /dev/null +++ b/View.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include + +#include "Source.h" + + +class View +{ +public: + explicit View(std::unique_ptr source); + void update(float dt); + void draw() const; +private: + struct Projected + { + Vector2 pos; + Vector2 base; + float depth; + }; + Camera m_camera; + std::unique_ptr m_source; + std::vector m_projected; +}; diff --git a/main.cpp b/main.cpp index 58c7347..f3c16f6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,23 +1,9 @@ -#include -#include #include -#include #include #include "ExampleSource.h" -#include "Source.h" - - -constexpr int AMOUNT {2000}; - - -struct Entry -{ - Vector2 pos; - Vector2 base; - float depth; -}; +#include "View.h" int @@ -26,55 +12,11 @@ main(int argc, char* argv[]) InitWindow(800, 600, "Derelict"); SetWindowState(FLAG_WINDOW_RESIZABLE); SetTargetFPS(60); - Camera camera {}; - camera.position = Vector3{10.0, 10.0, 10.0}; - camera.target = Vector3{0.0, 0.0, 0.0}; - camera.up = Vector3{0.0, 1.0, 0.0}; - camera.fovy = 45; - camera.projection = CAMERA_PERSPECTIVE; - SetCameraMode(camera, CAMERA_ORBITAL); - std::unique_ptr source = std::make_unique(); - auto killmails = source->all(); - std::vector projected; + View view(std::make_unique()); while (!WindowShouldClose()) { - UpdateCamera(&camera); - projected.clear(); - projected.reserve(killmails.size()); - const int height = GetScreenHeight(); - const int width = GetScreenWidth(); - for (const auto& km : killmails) { - const auto vec2 = GetWorldToScreen(km.position, camera); - const float d = - std::sqrt( - std::pow(camera.position.x - km.position.x, 2) + - std::pow(camera.position.y - km.position.y, 2) + - std::pow(camera.position.z - km.position.z, 2)); - if (0 > vec2.x || width < vec2.x || 0 > vec2.y || height < vec2.y) - continue; - projected.push_back(Entry{ - vec2, GetWorldToScreen({km.position.x, 0, km.position.z}, camera), d - }); - } - std::sort(projected.begin(), projected.end(), [](Entry& a, Entry& b){ return a.depth > b.depth; }); - BeginDrawing(); - ClearBackground(RAYWHITE); - BeginMode3D(camera); - DrawGrid(12, 1); - EndMode3D(); - DrawFPS(5, 5); - for (const auto& entry : projected) { - DrawLine(entry.base.x, entry.base.y, entry.pos.x, entry.pos.y, LIGHTGRAY); - DrawCircle(entry.pos.x, entry.pos.y, 3, RED); - } - int y = 25; - for (const auto& entry : projected) { - DrawText(TextFormat("%5.1f x %5.1f x %5.1f", entry.pos.x, entry.pos.y, entry.depth), 5, y, 10, DARKGRAY); - y += 10; - if (y > height) - break; - } - EndDrawing(); + view.update(GetFrameTime()); + view.draw(); } CloseWindow(); } -- cgit v1.1