From c274ac05690b34ba05b41d6e7864e6a663c73c1a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Mar 2021 00:53:08 +0000 Subject: Push RailLinks functionality into new base classes Network and NetworkOf<> --- game/network/network.cpp | 15 +++++++++++++++ game/network/network.h | 38 ++++++++++++++++++++++++++++++++++++++ game/network/network.impl.h | 12 ++++++++++++ game/network/rail.cpp | 28 ++++++---------------------- game/network/rail.h | 11 ++--------- 5 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 game/network/network.cpp create mode 100644 game/network/network.h create mode 100644 game/network/network.impl.h (limited to 'game') diff --git a/game/network/network.cpp b/game/network/network.cpp new file mode 100644 index 0000000..adda8fc --- /dev/null +++ b/game/network/network.cpp @@ -0,0 +1,15 @@ +#include "network.h" +#include +#include +#include + +Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } + +NodePtr +Network::findNodeAt(glm::vec3 pos) const +{ + if (const auto n = nodes.find(std::make_shared(pos)); n != nodes.end()) { + return *n; + } + return {}; +} diff --git a/game/network/network.h b/game/network/network.h new file mode 100644 index 0000000..2c24916 --- /dev/null +++ b/game/network/network.h @@ -0,0 +1,38 @@ +#ifndef NETWORK_H +#define NETWORK_H + +#include "link.h" +#include +#include +#include +#include +#include +#include +#include + +class Texture; +class Shader; + +class Network { +public: + explicit Network(const std::string & textureName); + + [[nodiscard]] NodePtr findNodeAt(glm::vec3) const; + +protected: + using Nodes = std::set>; + Nodes nodes; + std::shared_ptr texture; +}; + +template class NetworkOf : public Network, public Renderable { +protected: + using Network::Network; + + Collection links; + +public: + void render(const Shader &) const override; +}; + +#endif diff --git a/game/network/network.impl.h b/game/network/network.impl.h new file mode 100644 index 0000000..ddb1ae5 --- /dev/null +++ b/game/network/network.impl.h @@ -0,0 +1,12 @@ +#include "network.h" +#include +#include + +template +void +NetworkOf::render(const Shader & shader) const +{ + shader.setModel(Location {}, Shader::Program::StaticPos); + texture->Bind(); + links.apply(&T::render, shader); +} diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 8ec6893..133b80c 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -1,11 +1,10 @@ #include "rail.h" -#include "game/network/link.h" +#include "network.h" #include #include -#include #include -#include -#include +#include +#include // IWYU pragma: keep #include #include #include @@ -14,10 +13,12 @@ #include #include +template class NetworkOf; + constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; constexpr glm::vec3 RAIL_HEIGHT {0, .25F, 0}; -RailLinks::RailLinks() : texture {Texture::cachedTexture.get("rails.jpg")} { } +RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } void RailLinks::tick(TickDuration) { } void @@ -37,15 +38,6 @@ RailLinks::joinLinks(const LinkPtr & l) const } } -NodePtr -RailLinks::findNodeAt(glm::vec3 pos) const -{ - if (const auto n = nodes.find(std::make_shared(pos)); n != nodes.end()) { - return *n; - } - return {}; -} - std::shared_ptr RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { @@ -110,14 +102,6 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) return addLink(start, end, centre.first); } -void -RailLinks::render(const Shader & shader) const -{ - shader.setModel(Location {}, Shader::Program::StaticPos); - texture->Bind(); - links.apply(&RailLink::render, shader); -} - void RailLink::defaultMesh() { diff --git a/game/network/rail.h b/game/network/rail.h index 4460fcd..1d0ef68 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -7,17 +7,16 @@ #include "gfx/models/vertex.hpp" #include "gfx/renderable.h" #include "link.h" +#include "network.h" #include #include #include #include #include -#include #include #include class Shader; -class Texture; // A piece of rail track class RailLink : public Link, public Renderable { @@ -57,7 +56,7 @@ private: template concept RailLinkConcept = std::is_base_of_v; -class RailLinks : public Renderable, public WorldObject { +class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); template @@ -72,16 +71,10 @@ public: } std::shared_ptr addLinksBetween(glm::vec3 start, glm::vec3 end); - [[nodiscard]] NodePtr findNodeAt(glm::vec3) const; private: - using Nodes = std::set>; - Collection links; - Nodes nodes; - void render(const Shader &) const override; void tick(TickDuration elapsed) override; void joinLinks(const LinkPtr &) const; - std::shared_ptr texture; }; #endif -- cgit v1.2.3