summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-15 18:01:38 +0200
committerAki <please@ignore.pl>2022-05-15 18:01:38 +0200
commite49474fc0dcff041a9fae2857273938481cc9ec9 (patch)
treeeb0dc5b7eceaa2ef9079385bacddd42c7f6c257c
parent345bb237a7f682670a6107ae75afd948cc1f6ab6 (diff)
downloadderelict-e49474fc0dcff041a9fae2857273938481cc9ec9.zip
derelict-e49474fc0dcff041a9fae2857273938481cc9ec9.tar.gz
derelict-e49474fc0dcff041a9fae2857273938481cc9ec9.tar.bz2
Expanded label rendering
-rw-r--r--Label.h1
-rw-r--r--View.cpp36
-rw-r--r--View.h2
-rw-r--r--main.cpp20
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<Grid> 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<Grid> 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<Grid> grids);
+ virtual ~View();
void update(float dt);
void draw() const;
private:
@@ -20,4 +21,5 @@ private:
std::vector<Label> m_labels;
int m_grid;
float m_timer;
+ Texture2D m_texture;
};
diff --git a/main.cpp b/main.cpp
index c21aa0d..2116582 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,4 +1,4 @@
-#include <memory>
+#include <utility>
#include <raylib.h>
@@ -11,15 +11,17 @@ int
main(int argc, char* argv[])
{
InitWindow(800, 600, "Derelict");
- SetWindowState(FLAG_WINDOW_RESIZABLE);
- SetTargetFPS(60);
- DumpSource source("sample.json");
- auto grids = Reader::read(source);
- View view(grids);
- while (!WindowShouldClose())
{
- view.update(GetFrameTime());
- view.draw();
+ SetWindowState(FLAG_WINDOW_RESIZABLE);
+ SetTargetFPS(60);
+ DumpSource source("sample.json");
+ auto grids = Reader::read(source);
+ View view(std::move(grids));
+ while (!WindowShouldClose())
+ {
+ view.update(GetFrameTime());
+ view.draw();
+ }
}
CloseWindow();
}