From 7863e4caa1d07aef348a437fe0a662db68fd7ac3 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 May 2022 11:55:08 +0200 Subject: Added naive Timeline --- CMakeLists.txt | 1 + DumpSource.cpp | 4 ++-- Killmail.h | 2 +- Label.h | 5 +++++ Reader.cpp | 57 ++++++++++++--------------------------------------------- Reader.h | 4 +++- Timeline.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ Timeline.h | 18 ++++++++++++++++++ View.cpp | 25 +++++++++++++++++++------ View.h | 4 +++- Wreck.h | 1 + main.cpp | 4 ++-- 12 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 Timeline.cpp create mode 100644 Timeline.h 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 #include #include -#include #include #include @@ -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(); + 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 + #include +#include "Wreck.h" + struct Label { @@ -10,4 +14,5 @@ struct Label float depth; float length; bool hover; + std::reference_wrapper 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 -#include #include +#include +#include #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& grids, const LongVector3& position); -static std::tm end_of_time(); -static bool earlier(const std::tm& lhs, const std::tm& rhs); -std::vector +std::tuple, Timeline> Reader::read(Source& source) { std::vector grids; - std::tm start = end_of_time(); - std::tm end = {}; + std::time_t start {std::numeric_limits::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((wreck.killmail.position.y - average.y) * SCALE), static_cast((wreck.killmail.position.z - average.z) * SCALE), }; + wreck.time = static_cast(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& 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::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 #include #include "Grid.h" #include "Source.h" +#include "Timeline.h" struct Reader { - static std::vector read(Source& source); + static std::tuple, 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 +#include + + +Timeline::Timeline(const std::time_t start, const std::time_t end) : + m_start {start}, + m_end {static_cast(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 + + +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 #include +#include +#include #include #include "Grid.h" #include "Label.h" +#include "Timeline.h" #include "Utils.h" -View::View(std::vector grids) : +View::View(std::vector 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 grids); + View(std::vector 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()); -- cgit v1.1