summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-21 11:55:08 +0200
committerAki <please@ignore.pl>2022-05-21 11:55:08 +0200
commit7863e4caa1d07aef348a437fe0a662db68fd7ac3 (patch)
treebf52dfa7a7c45be1d1a696f5bf7c0c3f90beba68
parentde8d7a812c4da9f0375dc47626c0e836ba728207 (diff)
downloadderelict-7863e4caa1d07aef348a437fe0a662db68fd7ac3.zip
derelict-7863e4caa1d07aef348a437fe0a662db68fd7ac3.tar.gz
derelict-7863e4caa1d07aef348a437fe0a662db68fd7ac3.tar.bz2
Added naive Timeline
-rw-r--r--CMakeLists.txt1
-rw-r--r--DumpSource.cpp4
-rw-r--r--Killmail.h2
-rw-r--r--Label.h5
-rw-r--r--Reader.cpp57
-rw-r--r--Reader.h4
-rw-r--r--Timeline.cpp40
-rw-r--r--Timeline.h18
-rw-r--r--View.cpp25
-rw-r--r--View.h4
-rw-r--r--Wreck.h1
-rw-r--r--main.cpp4
12 files changed, 107 insertions, 58 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e300660..2f8bb00 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(
DumpSource.cpp
main.cpp
Reader.cpp
+ Timeline.cpp
View.cpp
)
target_link_libraries(
diff --git a/DumpSource.cpp b/DumpSource.cpp
index b80be2f..b9c86be 100644
--- a/DumpSource.cpp
+++ b/DumpSource.cpp
@@ -3,7 +3,6 @@
#include <ctime>
#include <sstream>
#include <string>
-#include <utility>
#include <vector>
#include <nlohmann/json.hpp>
@@ -60,5 +59,6 @@ from_json(const json& j, Killmail& km)
j.at("victim").at("corporation_id").get_to(km.owner.corporation);
j.at("victim").at("alliance_id").get_to(km.owner.alliance);
j.at("victim").at("faction_id").get_to(km.owner.faction);
- j.at("killmail_time").get_to(km.time);
+ auto calendar = j.at("killmail_time").get<tm>();
+ km.time = std::mktime(&calendar);
}
diff --git a/Killmail.h b/Killmail.h
index 047eb68..59e2c54 100644
--- a/Killmail.h
+++ b/Killmail.h
@@ -12,5 +12,5 @@ struct Killmail
long int ship;
LongVector3 position;
Owner owner;
- std::tm time;
+ std::time_t time;
};
diff --git a/Label.h b/Label.h
index 6372a6f..cfd5fc7 100644
--- a/Label.h
+++ b/Label.h
@@ -1,7 +1,11 @@
#pragma once
+#include <functional>
+
#include <raylib.h>
+#include "Wreck.h"
+
struct Label
{
@@ -10,4 +14,5 @@ struct Label
float depth;
float length;
bool hover;
+ std::reference_wrapper<const Wreck> wreck;
};
diff --git a/Reader.cpp b/Reader.cpp
index f14b837..165e049 100644
--- a/Reader.cpp
+++ b/Reader.cpp
@@ -1,12 +1,14 @@
#include "Reader.h"
#include <ctime>
-#include <vector>
#include <limits>
+#include <tuple>
+#include <vector>
#include "Grid.h"
#include "LongVector3.h"
#include "Source.h"
+#include "Timeline.h"
#include "Utils.h"
#include "Wreck.h"
@@ -16,21 +18,19 @@ static constexpr long double EXTENT {1000 * 50000};
static Grid& find_grid_for(std::vector<Grid>& grids, const LongVector3& position);
-static std::tm end_of_time();
-static bool earlier(const std::tm& lhs, const std::tm& rhs);
-std::vector<Grid>
+std::tuple<std::vector<Grid>, Timeline>
Reader::read(Source& source)
{
std::vector<Grid> grids;
- std::tm start = end_of_time();
- std::tm end = {};
+ std::time_t start {std::numeric_limits<std::time_t>::max()};
+ std::time_t end {0};
for (auto& km : source.killmails()) {
- if (earlier(km.time, start)) start = km.time;
- if (earlier(end, km.time)) end = km.time;
+ if (km.time < start) start = km.time;
+ if (end < km.time) end = km.time;
auto& grid = find_grid_for(grids, km.position);
- grid.wrecks.push_back(Wreck{Vector3{0, 0, 0}, km});
+ grid.wrecks.push_back(Wreck{Vector3{0, 0, 0}, km, 0});
}
for (auto& grid : grids) {
LongVector3 average {0, 0, 0};
@@ -49,12 +49,12 @@ Reader::read(Source& source)
static_cast<float>((wreck.killmail.position.y - average.y) * SCALE),
static_cast<float>((wreck.killmail.position.z - average.z) * SCALE),
};
+ wreck.time = static_cast<double>(wreck.killmail.time - start);
}
grid.origin = average;
}
- (void) start;
- (void) end;
- return grids;
+ Timeline timeline(start, end);
+ return {grids, timeline};
}
@@ -68,36 +68,3 @@ find_grid_for(std::vector<Grid>& grids, const LongVector3& position)
grids.push_back(Grid{});
return grids.back();
}
-
-
-std::tm
-end_of_time()
-{
- std::tm time {};
- time.tm_sec = 60;
- time.tm_min = 59;
- time.tm_hour = 23;
- time.tm_mday = 31;
- time.tm_mon = 11;
- time.tm_year = std::numeric_limits<decltype(time.tm_year)>::max();
- return time;
-}
-
-
-bool
-earlier(const std::tm& lhs, const std::tm& rhs)
-{
- if (lhs.tm_year > rhs.tm_year) return false;
- if (lhs.tm_year < rhs.tm_year) return true;
- if (lhs.tm_mon > rhs.tm_mon) return false;
- if (lhs.tm_mon < rhs.tm_mon) return true;
- if (lhs.tm_mday > rhs.tm_mday) return false;
- if (lhs.tm_mday < rhs.tm_mday) return true;
- if (lhs.tm_hour > rhs.tm_hour) return false;
- if (lhs.tm_hour < rhs.tm_hour) return true;
- if (lhs.tm_min > rhs.tm_min) return false;
- if (lhs.tm_min < rhs.tm_min) return true;
- if (lhs.tm_sec > rhs.tm_sec) return false;
- if (lhs.tm_sec < rhs.tm_sec) return true;
- return false;
-}
diff --git a/Reader.h b/Reader.h
index 6149958..92f2e2c 100644
--- a/Reader.h
+++ b/Reader.h
@@ -1,12 +1,14 @@
#pragma once
+#include <tuple>
#include <vector>
#include "Grid.h"
#include "Source.h"
+#include "Timeline.h"
struct Reader
{
- static std::vector<Grid> read(Source& source);
+ static std::tuple<std::vector<Grid>, Timeline> read(Source& source);
};
diff --git a/Timeline.cpp b/Timeline.cpp
new file mode 100644
index 0000000..b6003e5
--- /dev/null
+++ b/Timeline.cpp
@@ -0,0 +1,40 @@
+#include "Timeline.h"
+
+#include <algorithm>
+#include <ctime>
+
+
+Timeline::Timeline(const std::time_t start, const std::time_t end) :
+ m_start {start},
+ m_end {static_cast<double>(end - start)},
+ m_current {0.0}
+{
+}
+
+
+std::time_t
+Timeline::timestamp() const
+{
+ return m_start + m_current;
+}
+
+
+double
+Timeline::current() const
+{
+ return m_current;
+}
+
+
+double
+Timeline::progress() const
+{
+ return m_current / m_end;
+}
+
+
+double
+Timeline::move(const double dt)
+{
+ return m_current = std::clamp(m_current + dt, 0.0, m_end);
+}
diff --git a/Timeline.h b/Timeline.h
new file mode 100644
index 0000000..515d2d3
--- /dev/null
+++ b/Timeline.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <ctime>
+
+
+class Timeline
+{
+public:
+ Timeline(std::time_t start, std::time_t end);
+ std::time_t timestamp() const;
+ double current() const;
+ double progress() const;
+ double move(double dt);
+private:
+ const std::time_t m_start;
+ const double m_end;
+ double m_current;
+};
diff --git a/View.cpp b/View.cpp
index 719456e..1b15a17 100644
--- a/View.cpp
+++ b/View.cpp
@@ -1,21 +1,26 @@
#include "View.h"
+#include <iomanip>
#include <algorithm>
+#include <functional>
+#include <sstream>
#include <raylib.h>
#include "Grid.h"
#include "Label.h"
+#include "Timeline.h"
#include "Utils.h"
-View::View(std::vector<Grid> grids) :
+View::View(std::vector<Grid> grids, Timeline timeline) :
m_camera {},
m_grids {grids},
m_labels {},
m_grid {0},
m_texture {LoadTexture("resources/wreck.png")},
- m_active {nullptr}
+ m_active {nullptr},
+ m_timeline {timeline}
{
m_camera.position = Vector3{10.0f, 10.0f, 10.0f};
m_camera.target = Vector3{0.0f, 0.0f, 0.0f};
@@ -33,13 +38,14 @@ View::~View()
void
-View::update(const float)
+View::update(const float dt)
{
if (IsKeyPressed(KEY_SPACE)) {
m_grid++;
if (m_grid >= m_grids.size())
m_grid = 0;
}
+ m_timeline.move(dt * 60);
UpdateCamera(&m_camera);
const int height = GetScreenHeight();
const int width = GetScreenWidth();
@@ -56,7 +62,7 @@ View::update(const float)
const auto depth = dist(m_camera.position, wreck.position);
const auto length = dist(pos, base);
const bool hover = 8.0f > dist(mouse, pos);
- m_labels.push_back(Label{pos, base, depth, length, hover});
+ m_labels.push_back(Label{pos, base, depth, length, hover, std::ref(wreck)});
}
std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });
m_active = nullptr;
@@ -85,11 +91,18 @@ View::draw() const
DrawGrid(12, 1.0f);
EndMode3D();
for (const auto& point : m_labels) {
+ Color line = point.hover ? ORANGE : GRAY;
+ Color icon = point.hover ? ORANGE : WHITE;
+ if (!point.hover && point.wreck.get().time > m_timeline.current()) {
+ line.a = 20;
+ icon.a = 20;
+ }
if (point.length > 8)
- DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, point.hover ? ORANGE : DARKGRAY);
- DrawTexture(m_texture, point.pos.x - 8, point.pos.y - 8, point.hover ? ORANGE : WHITE);
+ DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, line);
+ DrawTexture(m_texture, point.pos.x - 8, point.pos.y - 8, icon);
}
DrawFPS(5, 5);
DrawText(TextFormat("%d", m_labels.size()), 5, 25, 20, DARKGRAY);
+ DrawRectangle(2, GetScreenHeight() - 10, (GetScreenWidth() - 4) * m_timeline.progress(), 8, RED);
EndDrawing();
}
diff --git a/View.h b/View.h
index e700bb0..48c5491 100644
--- a/View.h
+++ b/View.h
@@ -6,12 +6,13 @@
#include "Grid.h"
#include "Label.h"
+#include "Timeline.h"
class View
{
public:
- explicit View(std::vector<Grid> grids);
+ View(std::vector<Grid> grids, Timeline timeline);
virtual ~View();
void update(float dt);
void draw() const;
@@ -23,4 +24,5 @@ private:
float m_timer;
Texture2D m_texture;
Label* m_active;
+ Timeline m_timeline;
};
diff --git a/Wreck.h b/Wreck.h
index f7b3d1b..3fa4aa5 100644
--- a/Wreck.h
+++ b/Wreck.h
@@ -9,4 +9,5 @@ struct Wreck
{
Vector3 position;
Killmail killmail;
+ double time;
};
diff --git a/main.cpp b/main.cpp
index 96f5e23..b419c2e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -15,8 +15,8 @@ main(int, char*[])
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
DumpSource source("sample.json");
- auto grids = Reader::read(source);
- View view(std::move(grids));
+ auto [grids, timeline] = Reader::read(source);
+ View view(std::move(grids), std::move(timeline));
while (!WindowShouldClose())
{
view.update(GetFrameTime());