ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
scene_system.hpp
Go to the documentation of this file.
1
10
16
17#ifndef TOYMAKERENGINE_SCENESYSTEM_H
18#define TOYMAKERENGINE_SCENESYSTEM_H
19
20#include <vector>
21#include <memory>
22#include <unordered_map>
23#include <type_traits>
24
25#include <glm/glm.hpp>
26#include <glm/gtc/quaternion.hpp>
27
29#include "core/ecs_world.hpp"
31#include "scene_components.hpp"
32#include "render_system.hpp"
33#include "texture.hpp"
35
36namespace ToyMaker {
37
38 class SceneNodeCore;
39 class SceneNode;
40 class ViewportNode;
41 class SceneSystem;
42
52 enum class RelativeTo : uint8_t {
53 PARENT=0, //< Compute relative to/on top of this node's parent's transform.
54 // WORLD=1,
55 // CAMERA=2,
56 };
57
59 NLOHMANN_JSON_SERIALIZE_ENUM(RelativeTo, {
60 {RelativeTo::PARENT, "parent"},
61 });
62
70 ENTITY_NULL = kMaxEntities,
71 };
72
78 extern const std::string kSceneRootName;
79
85 class SceneNodeCore: public std::enable_shared_from_this<SceneNodeCore> {
86 public:
94 static void SceneNodeCore_del_(SceneNodeCore* sceneNode);
95
102 virtual ~SceneNodeCore()=default;
103
111 template <typename TComponent>
112 void addComponent(const TComponent& component, const bool bypassSceneActivityCheck=false);
113
120 void addComponent(const nlohmann::json& jsonComponent, const bool bypassSceneActivityCheck=false);
121
130 template <typename TComponent>
131 TComponent getComponent(const float simulationProgress=1.f) const;
132
140 template <typename TComponent>
141 bool hasComponent() const;
142
150 bool hasComponent(const std::string& type) const;
151
158 template <typename TComponent>
159 void updateComponent(const TComponent& component);
160
166 void updateComponent(const nlohmann::json& component);
167
175 template <typename TComponent>
176 void addOrUpdateComponent(const TComponent& component, const bool bypassSceneActivityCheck=false);
177
184 void addOrUpdateComponent(const nlohmann::json& component, const bool bypassSceneActivityCheck=false);
185
191 template <typename TComponent>
192 void removeComponent();
193
202 template <typename TSystem>
203 void setEnabled(bool state);
204
212 template <typename TSystem>
213 bool getEnabled() const;
214
220 EntityID getEntityID() const;
221
227 WorldID getWorldID() const;
228
235
241 std::weak_ptr<ECSWorld> getWorld() const;
242
251 bool inScene() const;
252
263 bool isActive() const;
264
272 bool isAncestorOf(std::shared_ptr<const SceneNodeCore> sceneNode) const;
273
282 bool hasNode(const std::string& pathToChild) const;
283
290 void addNode(std::shared_ptr<SceneNodeCore> node, const std::string& where);
291
297 std::vector<std::shared_ptr<SceneNodeCore>> getChildren();
298
304 std::vector<std::shared_ptr<const SceneNodeCore>> getChildren() const;
305
311 std::vector<std::shared_ptr<SceneNodeCore>> getDescendants();
312
320 template <typename TObject=std::shared_ptr<SceneNode>>
321 TObject getByPath(const std::string& where);
322
330 template <typename TSceneNode=SceneNode>
331 std::shared_ptr<TSceneNode> getNodeByID(EntityID entityID);
332
339 std::string getPathFromAncestor(std::shared_ptr<const SceneNodeCore> ancestor) const;
340
346 virtual std::shared_ptr<ViewportNode> getLocalViewport();
347
353 virtual std::shared_ptr<const ViewportNode> getLocalViewport() const;
354
361 std::shared_ptr<SceneNodeCore> getNode(const std::string& where);
362
368 std::shared_ptr<SceneNodeCore> getParentNode();
369
375 std::shared_ptr<const SceneNodeCore> getParentNode() const;
376
383 std::shared_ptr<SceneNodeCore> removeNode(const std::string& where);
384
390 std::vector<std::shared_ptr<SceneNodeCore>> removeChildren();
391
397 std::string getName() const;
398
404 void setName(const std::string& name);
405
413 std::string getViewportLocalPath() const;
414
424 inline void setPrototype_(std::shared_ptr<SceneNodeCore> prototype) { mPrototype=prototype; }
425
426 protected:
427
433 virtual void joinWorld(ECSWorld& world);
434
442 static std::shared_ptr<SceneNodeCore> copy(const std::shared_ptr<const SceneNodeCore> other);
443
452 template<typename ...TComponents>
453 SceneNodeCore(const Placement& placement, const std::string& name, TComponents...components);
454
461 SceneNodeCore(const nlohmann::json& jsonSceneNode);
462
468 SceneNodeCore(const SceneNodeCore& sceneObject);
469
470 // /**
471 // * @brief Copy assignment operator.
472 // *
473 // * @todo Sit down and figure out whether this operator will ever actually need to be used.
474 // */
475 // BaseSceneNode& operator=(const BaseSceneNode& sceneObject);
476
481 virtual void onCreated();
482
487 virtual void onActivated();
488
493 virtual void onDeactivated();
494
501 virtual void onDestroyed();
502
510 static void validateName(const std::string& nodeName);
511
512 private:
517 struct Key {};
518
527 template<typename ...TComponents>
528 SceneNodeCore(const Key&, const Placement& placement, const std::string& name, TComponents...components);
529
534 enum StateFlags: uint8_t {
535 ENABLED=0x1, //< This node is intended to be made active as soon as its added to the SceneSystem's scene tree.
536 ACTIVE=0x2, //< This node is presently active on the SceneSystem's scene tree.
537 };
538
545 template <typename TObject, typename Enable=void>
547 static TObject get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where);
548 static constexpr bool s_valid { false };
549 };
550
556 virtual std::shared_ptr<SceneNodeCore> clone() const;
557
564 void copyDescendants(const SceneNodeCore& other);
565
574 static void setParentViewport(std::shared_ptr<SceneNodeCore> node, std::shared_ptr<ViewportNode> newViewport);
575
582 static std::shared_ptr<SceneNodeCore> disconnectNode(std::shared_ptr<SceneNodeCore> node);
583
591 static bool detectCycle(std::shared_ptr<SceneNodeCore> node);
592
602 static std::tuple<std::string, std::string> nextInPath(const std::string& where);
603
609 void copyAndReplaceAttributes(const SceneNodeCore& other);
610
616
621 std::string mName {};
622
629 uint8_t mStateFlags { 0x00 | StateFlags::ENABLED };
630
635 RelativeTo mRelativeTo{ RelativeTo::PARENT };
636
641 std::shared_ptr<Entity> mEntity { nullptr };
642
647 std::weak_ptr<SceneNodeCore> mParent {};
648
653 std::weak_ptr<ViewportNode> mParentViewport {};
654
661 std::unordered_map<std::string, std::size_t> mChildNameToNode {};
662
667 std::vector<std::shared_ptr<SceneNodeCore>> mChildren {};
668
673 std::shared_ptr<SceneNodeCore> mPrototype { nullptr };
674
682
683 friend class SceneSystem;
684 template<typename TSceneNode>
685 friend class BaseSceneNode;
686 friend class ViewportNode;
687 };
688
689
696 template <typename TSceneNode>
697 class BaseSceneNode: public SceneNodeCore {
698 public:
699
700 protected:
712 template <typename ...TComponents>
713 static std::shared_ptr<TSceneNode> create(const Key&, const Placement& placement, const std::string& name, TComponents...components);
714
726 template <typename ...TComponents>
727 static std::shared_ptr<TSceneNode> create(const Placement& placement, const std::string& name, TComponents...components);
728
737 static std::shared_ptr<TSceneNode> create(const nlohmann::json& sceneNodeDescription);
738
739
749 static std::shared_ptr<TSceneNode> copy(const std::shared_ptr<const TSceneNode> sceneNode);
750 template <typename...TComponents>
751
760 BaseSceneNode(const Key& key, const Placement& placement, const std::string& name, TComponents...components):
761 SceneNodeCore{ key, placement, name, components... }
762 {}
763
772 template<typename ...TComponents>
773 BaseSceneNode(const Placement& placement, const std::string& name, TComponents...components):
774 SceneNodeCore{ placement, name, components... }
775 {}
776
782 BaseSceneNode(const nlohmann::json& nodeDescription) : SceneNodeCore { nodeDescription } {}
783
789 BaseSceneNode(const SceneNodeCore& other): SceneNodeCore{ other } {}
790
791 friend class SceneNodeCore;
792 };
793
801 class SceneNode: public BaseSceneNode<SceneNode>, public Resource<SceneNode> {
802 public:
803 template <typename ...TComponents>
804 static std::shared_ptr<SceneNode> create(const Placement& placement, const std::string& name, TComponents...components);
805 static std::shared_ptr<SceneNode> create(const nlohmann::json& sceneNodeDescription);
806 static std::shared_ptr<SceneNode> copy(const std::shared_ptr<const SceneNode> other);
807
808 static inline std::string getResourceTypeName() { return "SceneNode"; }
809
810 protected:
811 template<typename ...TComponents>
812 SceneNode(const Placement& placement, const std::string& name, TComponents...components):
813 BaseSceneNode<SceneNode>{placement, name, components...},
815 {}
816
817 SceneNode(const nlohmann::json& jsonSceneNode):
818 BaseSceneNode<SceneNode>{jsonSceneNode},
820 {}
821
822 SceneNode(const SceneNode& sceneObject):
823 BaseSceneNode<SceneNode>{sceneObject},
825 {}
826 friend class BaseSceneNode<SceneNode>;
827 };
828
842 class ViewportNode: public BaseSceneNode<ViewportNode>, public Resource<ViewportNode> {
843 public:
853 enum class ResizeType: uint8_t {
854 OFF=0, //< No resize, render texture is rendered as is with no scaling.
855 VIEWPORT_DIMENSIONS, //< Viewport transform configured per stretch mode and requested dimensions
856 TEXTURE_DIMENSIONS, //< Texture result rendered in base dimensions, and then warped to fit request dimensions
857 };
858
863 enum class ResizeMode: uint8_t {
864 FIXED_ASPECT=0, //< both, while retaining aspect ratio.
865 EXPAND_VERTICALLY, //< Expand vertically if permitted by target dimensions, otherwise constrain to aspect.
866 EXPAND_HORIZONTALLY, //< Expand horiontally if possible by target dimensions, otherwise constrain to aspect.
867 EXPAND_FILL, //< No constraint in either dimension, expand to fill target dimensions always.
868 };
869
874 enum class UpdateMode: uint8_t {
875 NEVER=0, //< No rerender takes place even when render is called for this viewport.
876 ONCE, //< Update on next render frame, then set to UpdateMode::NEVER
877 ON_FETCH, //< Update whenever a request for the texture is made, where frequency is entirely dependent on caller.
878 ON_FETCH_CAP_FPS, //< Update on request, but ignore requests exceeding FPS cap. Final frequency is constrained to the FPS cap configured for this viewport, but may be lower than it.
879 ON_RENDER, //< Update every render call with no constraint, so frequency depends entirely on the render frequency of the application loop.
880 ON_RENDER_CAP_FPS, //< Update on render call when fps cap isn't exceeded. Final FPS may be lower than specified as a cap.
881 };
882
888
893 ResizeType mResizeType { ResizeType::VIEWPORT_DIMENSIONS };
898 ResizeMode mResizeMode { ResizeMode::EXPAND_HORIZONTALLY };
899
904 RenderType mRenderType { RenderType::BASIC_3D };
905
910 glm::u16vec2 mBaseDimensions { 800, 600 };
911
916 glm::u16vec2 mComputedDimensions { 800, 600 };
917
922 glm::u16vec2 mRequestedDimensions { 800, 600 };
923
934 float mRenderScale { 1.f };
935
940 UpdateMode mUpdateMode { UpdateMode::ON_RENDER_CAP_FPS };
941
947 float mFPSCap { 60.f };
948 };
949
960 static std::shared_ptr<ViewportNode> create(const std::string& name, bool inheritsWorld, bool allowActionFlowThrough, const RenderConfiguration& renderConfiguration, std::shared_ptr<Texture> skybox);
961
973 static std::shared_ptr<ViewportNode> create(const nlohmann::json& sceneNodeDescription);
974
981 static std::shared_ptr<ViewportNode> copy(const std::shared_ptr<const ViewportNode> other);
982
988 static inline std::string getResourceTypeName() { return "ViewportNode"; }
989
996
1004 void updateExposure(float newExposure);
1005
1014 void updateGamma(float newGamma);
1015
1021 float getExposure();
1022
1028 float getGamma();
1029
1035 std::shared_ptr<ViewportNode> getLocalViewport() override;
1036
1042 virtual std::shared_ptr<const ViewportNode> getLocalViewport() const override;
1043
1050 std::shared_ptr<Texture> fetchRenderResult(float simulationProgress);
1051
1057 void setActiveCamera(const std::string& cameraPath);
1058
1064 void setActiveCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1065
1071 std::shared_ptr<const SceneNodeCore> getActiveCamera() const;
1072
1078 RenderConfiguration getRenderConfiguration() const;
1079
1085 void setRenderConfiguration(const RenderConfiguration& renderConfiguration);
1086
1096 void setSkybox(std::shared_ptr<Texture> skybox);
1097
1104
1111
1117 void setRenderScale(float renderScale);
1118
1125
1131 void setFPSCap(float fpsCap);
1132
1140 void requestDimensions(glm::u16vec2 requestedDimensions);
1141
1148
1156 bool handleAction(std::pair<ActionDefinition, ActionData> pendingAction);
1157
1165
1170 ~ViewportNode() override;
1171
1179 inline uint32_t getViewportLoadOrdinal() const { return mViewportLoadOrdinal; }
1180
1181 protected:
1182 ViewportNode(const Placement& placement, const std::string& name):
1183 BaseSceneNode<ViewportNode>{placement, name},
1184 Resource<ViewportNode>{0},
1185 mViewportLoadOrdinal { SDL_GetTicks() }
1186 {}
1187 ViewportNode(const nlohmann::json& jsonSceneNode):
1188 BaseSceneNode<ViewportNode>{jsonSceneNode},
1190 mViewportLoadOrdinal { SDL_GetTicks() }
1191 {}
1192 ViewportNode(const ViewportNode& sceneObject):
1193 BaseSceneNode<ViewportNode>{sceneObject},
1195 mViewportLoadOrdinal { SDL_GetTicks() }
1196 {}
1197
1202 std::shared_ptr<ECSWorld> mOwnWorld { nullptr };
1203
1208 void onActivated() override;
1209
1214 void onDeactivated() override;
1215
1221 void joinWorld(ECSWorld& world) override;
1222
1223 private:
1234 static std::shared_ptr<ViewportNode> create(const Key& key, const std::string& name, bool inheritsWorld, const RenderConfiguration& renderConfiguration, std::shared_ptr<Texture> skybox);
1235
1243 ViewportNode(const Key& key, const Placement& placement, const std::string& name):
1244 BaseSceneNode<ViewportNode>{key, Placement{}, name},
1246 {(void)placement; /*prevent unused parameter warnings*/}
1247
1253 std::shared_ptr<SceneNodeCore> clone() const override;
1254
1259 void createAndJoinWorld();
1260
1266 void registerDomainCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1267
1273 void unregisterDomainCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1274 std::shared_ptr<SceneNodeCore> findFallbackCamera();
1275
1281 std::vector<std::shared_ptr<ViewportNode>> getActiveDescendantViewports();
1282
1288 std::vector<std::weak_ptr<ECSWorld>> getActiveDescendantWorlds();
1289
1299 uint32_t render(float simulationProgress, uint32_t variableStep);
1300
1307 void render_(float simulationProgress);
1308
1315 inline SDL_Rect getCenteredViewportCoordinates() const {
1316 return {
1317 mRenderConfiguration.mRequestedDimensions.x/2 - mRenderConfiguration.mComputedDimensions.x/2, mRenderConfiguration.mRequestedDimensions.y/2 - mRenderConfiguration.mComputedDimensions.y/2,
1318 mRenderConfiguration.mComputedDimensions.x, mRenderConfiguration.mComputedDimensions.y
1319 };
1320 }
1321
1327 bool operator() (const std::shared_ptr<ViewportNode>& one, const std::shared_ptr<ViewportNode>& two) const {
1328 return one->getViewportLoadOrdinal() < two->getViewportLoadOrdinal();
1329 }
1330 };
1331
1336 uint64_t mViewportLoadOrdinal { std::numeric_limits<uint64_t>::max() };
1337
1343
1349
1354 bool mActionFlowthrough { false };
1355
1361
1366 std::set<std::shared_ptr<ViewportNode>, ViewportChildComp_> mChildViewports {};
1367
1372 std::shared_ptr<SceneNodeCore> mActiveCamera { nullptr };
1373
1378 std::set<std::shared_ptr<SceneNodeCore>, std::owner_less<std::shared_ptr<SceneNodeCore>>> mDomainCameras {};
1379
1384 RenderSetID mRenderSet;
1385
1392 std::shared_ptr<Texture> mTextureResult { nullptr };
1393
1399
1404 uint32_t mTimeSinceLastRender { static_cast<uint32_t>(1000/mRenderConfiguration.mFPSCap) };
1405
1406 friend class BaseSceneNode<ViewportNode>;
1407 friend class SceneNodeCore;
1408 friend class SceneSystem;
1409 };
1410
1418 class SceneSystem: public System<SceneSystem, std::tuple<>, std::tuple<Placement, SceneHierarchyData, Transform>> {
1419 public:
1425 explicit SceneSystem(std::weak_ptr<ECSWorld> world):
1426 System<SceneSystem, std::tuple<>, std::tuple<Placement, SceneHierarchyData, Transform>> { world }
1427 {}
1428
1434 static std::string getSystemTypeName() { return "SceneSystem"; }
1435
1444 bool isSingleton() const override { return true; }
1445
1453 template<typename TObject=std::shared_ptr<SceneNode>>
1454 TObject getByPath(const std::string& where);
1455
1463 template <typename TSceneNode>
1464 std::shared_ptr<TSceneNode> getNodeByID(const UniversalEntityID& universalEntityID);
1465
1472 std::vector<std::shared_ptr<SceneNodeCore>> getNodesByID(const std::vector<UniversalEntityID>& universalEntityIDs);
1473
1480 std::shared_ptr<SceneNodeCore> getNode(const std::string& where);
1481
1488 std::shared_ptr<SceneNodeCore> removeNode(const std::string& where);
1489
1496 void addNode(std::shared_ptr<SceneNodeCore> node, const std::string& where);
1497
1503 std::weak_ptr<ECSWorld> getRootWorld() const;
1504
1511
1517 void onApplicationInitialize(const ViewportNode::RenderConfiguration& rootViewportRenderConfiguration);
1518
1523 void onApplicationStart();
1524
1529 void onApplicationEnd();
1530
1538 void simulationStep(uint32_t simStepMillis, std::vector<std::pair<ActionDefinition, ActionData>> triggeredActions={});
1539
1551 void variableStep(float simulationProgress, uint32_t simulationLagMillis, uint32_t variableStepMillis, std::vector<std::pair<ActionDefinition, ActionData>> triggeredActions={});
1552
1557 void transformStep(uint32_t timestepMillis);
1558
1564
1572 uint32_t render(float simulationProgress, uint32_t variableStep);
1573
1574 private:
1584 class PlacementUpdateReporter: public System<PlacementUpdateReporter, std::tuple<Placement>, std::tuple<Transform, SceneHierarchyData>> {
1585 public:
1586 explicit PlacementUpdateReporter(std::weak_ptr<ECSWorld> world):
1587 System<PlacementUpdateReporter, std::tuple<Placement>, std::tuple<Transform, SceneHierarchyData>> { world }
1588 {}
1589 static std::string getSystemTypeName() { return "SceneSystem::PlacementUpdateReporter"; }
1590 private:
1591
1596 std::set<EntityID> mReportedEntities {};
1597
1602 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
1603
1609 inline void clearReportList() { mReportedEntities.clear(); }
1610
1611 friend class SceneSystem;
1612 };
1613
1614
1624 class TransformUpdateReporter: public System<TransformUpdateReporter, std::tuple<Transform>, std::tuple<Placement, SceneHierarchyData>> {
1625 public:
1626 explicit TransformUpdateReporter(std::weak_ptr<ECSWorld> world):
1627 System<TransformUpdateReporter, std::tuple<Transform>, std::tuple<Placement, SceneHierarchyData>> { world }
1628 {}
1629 static std::string getSystemTypeName() { return "SceneSystem::TransformUpdateReporter"; }
1630 private:
1635 std::set<EntityID> mReportedEntities {};
1636
1637
1642 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
1643
1649 inline void clearReportList() { mReportedEntities.clear(); }
1650
1651 friend class SceneSystem;
1652 };
1653
1662 template <typename TSceneNode, typename Enable=void>
1664 static std::shared_ptr<TSceneNode> get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem);
1665 };
1666
1674 bool isActive(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1675
1684
1692 bool inScene(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1693
1701 bool inScene(UniversalEntityID universalEntityID) const;
1702
1708 void markDirtyTransform(UniversalEntityID universalEntityID);
1709
1716 void markDirtyPlacement(UniversalEntityID universalEntityID);
1717
1723 std::vector<std::shared_ptr<ViewportNode>> getActiveViewports();
1724
1730 std::vector<std::weak_ptr<ECSWorld>> getActiveWorlds();
1731
1738 std::weak_ptr<ECSWorld> getWorld(WorldID world);
1739
1746 Transform getLocalTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1747
1754 Transform getCachedWorldTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1755
1769 Transform getInheritedTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1770
1776 void updateHierarchyDataInsertion(std::shared_ptr<SceneNodeCore> insertedNode);
1777
1783 void updateHierarchyDataRemoval(std::shared_ptr<SceneNodeCore> removedNode);
1784
1790 void nodeAdded(std::shared_ptr<SceneNodeCore> sceneNode);
1791
1797 void nodeRemoved(std::shared_ptr<SceneNodeCore> sceneNode);
1798
1805 void nodeActivationChanged(std::shared_ptr<SceneNodeCore> sceneNode, bool state);
1806
1812 void activateSubtree(std::shared_ptr<SceneNodeCore> sceneNode);
1813
1819 void deactivateSubtree(std::shared_ptr<SceneNodeCore> sceneNode);
1820
1827
1835 void onWorldTransformUpdate(UniversalEntityID universalEntityID);
1836
1843 std::shared_ptr<SceneNodeCore> getNodeByID(const UniversalEntityID& universalEntityID);
1844
1849 std::shared_ptr<ViewportNode> mRootNode{ nullptr };
1850
1855 std::map<UniversalEntityID, std::weak_ptr<SceneNodeCore>, std::less<UniversalEntityID>> mEntityToNode {};
1856
1861 std::set<UniversalEntityID, std::less<UniversalEntityID>> mActiveEntities {};
1862
1867 std::set<UniversalEntityID, std::less<UniversalEntityID>> mComputeTransformQueue {};
1868
1875 std::set<UniversalEntityID, std::less<UniversalEntityID>> mComputePlacementQueue {};
1876
1877 friend class SceneNodeCore;
1878 };
1879
1880
1881 template <typename TSceneNode>
1882 template <typename ...TComponents>
1883 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const Placement& placement, const std::string& name, TComponents...components) {
1884 std::shared_ptr<SceneNodeCore> newNode ( new TSceneNode(placement, name, components...), &SceneNodeCore_del_);
1885 newNode->onCreated();
1886 return std::static_pointer_cast<TSceneNode>(newNode);
1887 }
1888
1889 template <typename TSceneNode>
1890 template <typename ...TComponents>
1891 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const Key& key, const Placement& placement, const std::string& name, TComponents...components) {
1892 std::shared_ptr<SceneNodeCore> newNode( new TSceneNode(key, placement, name, components...), &SceneNodeCore_del_);
1893 newNode->onCreated();
1894 return std::static_pointer_cast<TSceneNode>(newNode);
1895 }
1896
1897 template <typename TSceneNode>
1898 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const nlohmann::json& sceneNodeDescription) {
1899 std::shared_ptr<SceneNodeCore> newNode{ new TSceneNode{ sceneNodeDescription }, &SceneNodeCore_del_};
1900 newNode->onCreated();
1901 return std::static_pointer_cast<TSceneNode>(newNode);
1902 }
1903
1904 template <typename TSceneNode>
1905 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::copy(const std::shared_ptr<const TSceneNode> sceneNode) {
1906 std::shared_ptr<SceneNodeCore> newNode { SceneNodeCore::copy(sceneNode) };
1907 newNode->onCreated();
1908 return std::static_pointer_cast<TSceneNode>(newNode);
1909 }
1910
1911 template<typename ...TComponents>
1912 std::shared_ptr<SceneNode> SceneNode::create(const Placement& placement, const std::string& name, TComponents...components) {
1913 return BaseSceneNode<SceneNode>::create<TComponents...>(placement, name, components...);
1914 }
1915
1916 template <typename TObject>
1917 TObject SceneNodeCore::getByPath(const std::string& where) {
1918 return getByPath_Helper<TObject>::get(shared_from_this(), where);
1919 }
1920
1921 template <typename TObject>
1922 TObject SceneSystem::getByPath(const std::string& where) {
1923 return mRootNode->getByPath<TObject>(where);
1924 }
1925
1926 template <typename TSceneNode>
1927 inline std::shared_ptr<TSceneNode> SceneSystem::getNodeByID(const UniversalEntityID& universalEntityID) {
1928 return SceneSystem::getNodeByID_Helper<TSceneNode>(universalEntityID, *this);
1929 }
1930
1931 // Fail retrieval in cases where no explicitly defined object by path
1932 // method exists
1933 template <typename TObject, typename Enable>
1934 TObject SceneNodeCore::getByPath_Helper<TObject, Enable>::get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where) {
1935 static_assert(false && "No Object-by-Path method for this type exists");
1936 return TObject{}; // this is just to shut the compiler up about no returned value
1937 }
1938
1939 template <typename TSceneNode, typename Enable>
1940 std::shared_ptr<TSceneNode> SceneSystem::getNodeByID_Helper<TSceneNode, Enable>::get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem) {
1941 static_assert(false && "No scene node of this type exists");
1942 return std::shared_ptr<TSceneNode>{};
1943 }
1944
1945 template <typename TSceneNode>
1946 struct SceneSystem::getNodeByID_Helper<TSceneNode, typename std::enable_if_t<std::is_base_of<SceneNodeCore, TSceneNode>::value>> {
1947 std::shared_ptr<TSceneNode> get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem) {
1948 return std::static_pointer_cast<TSceneNode>(sceneSystem.getNodeByID(universalEntityID));
1949 }
1950 };
1951
1952 template <typename TObject>
1953 struct SceneNodeCore::getByPath_Helper<std::shared_ptr<TObject>, typename std::enable_if_t<std::is_base_of<SceneNodeCore, TObject>::value>> {
1954 static std::shared_ptr<TObject> get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where) {
1955 return std::static_pointer_cast<TObject>(rootNode->getNode(where));
1956 }
1957 static constexpr bool s_valid { true };
1958 };
1959
1960 template <typename ...TComponents>
1961 SceneNodeCore::SceneNodeCore(const Placement& placement, const std::string& name, TComponents...components) {
1962 validateName(name);
1963 mName = name;
1964 mEntity = std::make_shared<Entity>(
1966 placement,
1968 Transform{
1969 .mModelMatrix { glm::mat4{1.f} },
1970 .mInheritedComponents { placement.mInheritedComponents },
1971 .mInheritMode { placement.mInheritMode },
1972 },
1974 components...
1975 )
1976 );
1979 }
1980 }
1981
1982 template <typename ...TComponents>
1983 SceneNodeCore::SceneNodeCore(const Key&, const Placement& placement, const std::string& name, TComponents...components) {
1984 mName = name;
1985 mEntity = std::make_shared<Entity>(
1987 placement,
1989 Transform{
1990 .mModelMatrix { glm::mat4{1.f} },
1991 .mInheritedComponents { placement.mInheritedComponents },
1992 .mInheritMode { placement.mInheritMode },
1993 },
1995 components...
1996 )
1997 );
2000 }
2001 }
2002
2003 template <typename TComponent>
2004 void SceneNodeCore::addComponent(const TComponent& component, bool bypassSceneActivityCheck) {
2005 mEntity->addComponent<TComponent>(component);
2006
2007 // NOTE: required because even though this node's entity's signature changes, it
2008 // is disabled by default on any systems it is eligible for. We need to activate
2009 // the node according to its system mask
2010 if(!bypassSceneActivityCheck && isActive()) {
2011 mEntity->enableSystems(mSystemMask);
2012 }
2013 // NOTE: no removeComponent() equivalent required, as systems that depend on the removed
2014 // component will automatically have this entity removed from their list, and hence
2015 // be disabled
2016 }
2017
2018 template <typename TComponent>
2019 TComponent SceneNodeCore::getComponent(const float simulationProgress) const {
2020 return mEntity->getComponent<TComponent>(simulationProgress);
2021 }
2022
2023 template <typename TComponent>
2025 return mEntity->hasComponent<TComponent>();
2026 }
2027
2028 template <typename TComponent>
2029 void SceneNodeCore::updateComponent(const TComponent& component) {
2030 mEntity->updateComponent<TComponent>(component);
2031 }
2032
2033 template <typename TComponent>
2034 void SceneNodeCore::addOrUpdateComponent(const TComponent& component, const bool bypassSceneActivityCheck) {
2036 addComponent<TComponent>(component, bypassSceneActivityCheck);
2037 return;
2038 }
2039 updateComponent<TComponent>(component);
2040 }
2041
2042 template <typename TComponent>
2044 mEntity->removeComponent<TComponent>();
2045 }
2046
2047 template <typename TSystem>
2049 return mEntity->isEnabled<TSystem>();
2050 }
2051
2052 template <typename TSystem>
2053 void SceneNodeCore::setEnabled(bool state) {
2054 const SystemType systemType { mEntity->getWorld().lock()->getSystemType<TSystem>() };
2055 mSystemMask.set(systemType, state);
2056
2057 // since the system mask has been changed, we'll want the scene
2058 // to talk to ECS and make this node visible to the system that
2059 // was enabled, if eligible
2060 if(state == true && isActive()){
2061 mEntity->enableSystems(mSystemMask);
2062 }
2063 }
2064
2065 // Specialization for when the scene system itself is marked
2066 // enabled or disabled
2067 template <>
2068 inline void SceneNodeCore::setEnabled<SceneSystem>(bool state) {
2069 const SystemType systemType { mEntity->getWorld().lock()->getSystemType<SceneSystem>() };
2070 // TODO: enabled entities are tracked in both SceneSystem's
2071 // mActiveNodes and ECS getEnabledEntities, which is
2072 // redundant and may eventually cause errors
2073 mSystemMask.set(systemType, state);
2074 //TODO: More redundancy. Why?
2075 mStateFlags = state? (mStateFlags | SceneNodeCore::StateFlags::ENABLED): (mStateFlags & ~SceneNodeCore::StateFlags::ENABLED);
2076 mEntity->getWorld().lock()->getSystem<SceneSystem>()->nodeActivationChanged(
2077 shared_from_this(),
2078 state
2079 );
2080 }
2081
2082 // Prevent removal of components essential to a scene node
2083 template <>
2085 assert(false && "Cannot remove a scene node's Placement component");
2086 }
2087
2088 template <>
2090 assert(false && "Cannot remove a scene node's Transform component");
2091 }
2092
2093 template <typename TSceneNode>
2094 std::shared_ptr<TSceneNode> SceneNodeCore::getNodeByID(EntityID entityID) {
2095 return getWorld().lock()->getSystem<SceneSystem>()->getNodeByID<TSceneNode>({getWorldID(), entityID});
2096 }
2097
2099 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::ResizeType, {
2100 {ViewportNode::RenderConfiguration::ResizeType::OFF, "off"},
2101 {ViewportNode::RenderConfiguration::ResizeType::VIEWPORT_DIMENSIONS, "viewport-dimensions"},
2102 {ViewportNode::RenderConfiguration::ResizeType::TEXTURE_DIMENSIONS, "texture-dimensions"},
2103 });
2104
2106 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::ResizeMode, {
2107 {ViewportNode::RenderConfiguration::ResizeMode::FIXED_ASPECT,"fixed-aspect"},
2108 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_VERTICALLY, "expand-vertically"},
2109 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_HORIZONTALLY, "expand-horizontally"},
2110 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_FILL, "expand-fill"},
2111 });
2112
2114 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::UpdateMode, {
2115 {ViewportNode::RenderConfiguration::UpdateMode::NEVER, "never"},
2116 {ViewportNode::RenderConfiguration::UpdateMode::ONCE, "once"},
2117 {ViewportNode::RenderConfiguration::UpdateMode::ON_FETCH, "on-fetch"},
2118 {ViewportNode::RenderConfiguration::UpdateMode::ON_RENDER, "on-render"},
2119 {ViewportNode::RenderConfiguration::UpdateMode::ON_RENDER_CAP_FPS, "on-render-cap-fps"},
2120 });
2121
2123 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::RenderType, {
2124 {ViewportNode::RenderConfiguration::RenderType::BASIC_3D, "basic-3d"},
2125 {ViewportNode::RenderConfiguration::RenderType::ADDITION, "addition"},
2126 });
2127
2129 inline void to_json(nlohmann::json& json, const ViewportNode::RenderConfiguration& renderConfiguration) {
2130 json = {
2131 {"base_dimensions", nlohmann::json::array({renderConfiguration.mBaseDimensions.x, renderConfiguration.mBaseDimensions.y})},
2132 {"update_mode", renderConfiguration.mUpdateMode},
2133 {"resize_type", renderConfiguration.mResizeType},
2134 {"resize_mode", renderConfiguration.mResizeMode},
2135 {"render_scale", renderConfiguration.mRenderScale},
2136 {"render_type", renderConfiguration.mRenderType},
2137 {"fps_cap", renderConfiguration.mFPSCap},
2138 };
2139 }
2140
2142 inline void from_json(const nlohmann::json& json, ViewportNode::RenderConfiguration& renderConfiguration) {
2143 assert(json.find("base_dimensions") != json.end() && "Viewport descriptions must contain the \"base_dimensions\" size 2 array of Numbers attribute");
2144 json.at("base_dimensions")[0].get_to(renderConfiguration.mBaseDimensions.x);
2145 json.at("base_dimensions")[1].get_to(renderConfiguration.mBaseDimensions.y);
2146 json.at("render_type").get_to(renderConfiguration.mRenderType);
2147 renderConfiguration.mRequestedDimensions = renderConfiguration.mBaseDimensions;
2148 renderConfiguration.mComputedDimensions = renderConfiguration.mBaseDimensions;
2149 assert(renderConfiguration.mBaseDimensions.x > 0 && renderConfiguration.mBaseDimensions.y > 0 && "Base dimensions cannot include a 0 in either dimension");
2150
2151 assert(json.find("update_mode") != json.end() && "Viewport render configuration must include the \"update_mode\" enum attribute");
2152 json.at("update_mode").get_to(renderConfiguration.mUpdateMode);
2153
2154 assert(json.find("resize_type") != json.end() && "Viewport render configuration must include the \"resize_type\" enum attribute");
2155 json.at("resize_type").get_to(renderConfiguration.mResizeType);
2156
2157 assert(json.find("resize_mode") != json.end() && "Viewport render configuration must include the \"resize_mode\" enum attribute");
2158 json.at("resize_mode").get_to(renderConfiguration.mResizeMode);
2159
2160 assert(json.find("render_scale") != json.end() && "Viewport render configuration must include the \"render_scale\" float attribute");
2161 json.at("render_scale").get_to(renderConfiguration.mRenderScale);
2162 assert(renderConfiguration.mRenderScale > 0.f && "Render scale must be a positive non-zero decimal number");
2163
2164 assert(json.find("fps_cap") != json.end() && "Viewport must include the \"fps_cap\" float attribute");
2165 json.at("fps_cap").get_to(renderConfiguration.mFPSCap);
2166 assert(renderConfiguration.mFPSCap > 0.f && "FPS cap must be a positive non-zero decimal number");
2167 }
2168
2169}
2170
2171#endif
An object responsible for tracking action listeners for a given project.
Definition input_system.hpp:586
An object containing a coarse simplified representation, AABB, of spatially queryable objects.
Definition types.hpp:1322
A CRTP template for all the scene node types present in the project.
Definition scene_system.hpp:697
BaseSceneNode(const Key &key, const Placement &placement, const std::string &name, TComponents...components)
Constructor for a single node of a subclass.
Definition scene_system.hpp:760
static std::shared_ptr< TSceneNode > copy(const std::shared_ptr< const TSceneNode > sceneNode)
Creates a scene node of a specific type based on another node of that type.
Definition scene_system.hpp:1905
BaseSceneNode(const SceneNodeCore &other)
Constructs a new node of a certain type as a copy of another node.
Definition scene_system.hpp:789
BaseSceneNode(const nlohmann::json &nodeDescription)
Constructs a new node of a certain type based on its json description.
Definition scene_system.hpp:782
static std::shared_ptr< TSceneNode > create(const Key &, const Placement &placement, const std::string &name, TComponents...components)
A (private) method for the creation of a new scene node for a particular type.
Definition scene_system.hpp:1891
BaseSceneNode(const Placement &placement, const std::string &name, TComponents...components)
General constructor for a single node of a subclass.
Definition scene_system.hpp:773
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1464
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2807
The base class for any type whose creation and storage should be managed by the ResourceDatabase.
Definition resource_database.hpp:369
Resource(int explicitlyInitializeMe)
Definition resource_database.hpp:386
The core of a node in the SceneSystem, a set of components and methods overridable or usable by all t...
Definition scene_system.hpp:85
virtual void onCreated()
Scene node lifecycle hook for when a node is created.
Definition scene_system.cpp:21
std::shared_ptr< SceneNodeCore > removeNode(const std::string &where)
Removes a node from the tree present at the path specified.
Definition scene_system.cpp:350
virtual std::shared_ptr< SceneNodeCore > clone() const
Virtual method which each type of scene node with special members should implement (in lieu of copy c...
Definition scene_system.cpp:42
std::unordered_map< std::string, std::size_t > mChildNameToNode
A mapping of names of this node's child nodes to the indices of the nodes themselves.
Definition scene_system.hpp:661
void addComponent(const TComponent &component, const bool bypassSceneActivityCheck=false)
Adds a component of type TComponent to the node.
Definition scene_system.hpp:2004
std::shared_ptr< TSceneNode > getNodeByID(EntityID entityID)
Gets a pointer to a node by its EntityID, assuming that node and this one belong to the same ECSWorld...
Definition scene_system.hpp:2094
bool isAncestorOf(std::shared_ptr< const SceneNodeCore > sceneNode) const
Tests whether a particular scene node is the ancestor of this one.
Definition scene_system.cpp:169
RelativeTo mRelativeTo
A marker indicating how this node's transform component should be computed.
Definition scene_system.hpp:635
std::weak_ptr< ECSWorld > getWorld() const
Gets a reference to the ECSWorld this node belongs to.
Definition scene_system.cpp:407
virtual std::shared_ptr< ViewportNode > getLocalViewport()
Returns the viewport node which is in the same ECSWorld as and is the closest ancestor of (or the sam...
Definition scene_system.cpp:311
SceneNodeCore(const Placement &placement, const std::string &name, TComponents...components)
Constructs a scene node object from essential and extra components.
Definition scene_system.hpp:1961
static std::shared_ptr< SceneNodeCore > disconnectNode(std::shared_ptr< SceneNodeCore > node)
Disconnects this node from its parent node if it has one.
Definition scene_system.cpp:334
void recomputeChildNameIndexMapping()
Utility function for updating SceneHierarchyData components belonging to a single ECSWorld in the Sce...
Definition scene_system.cpp:82
void setPrototype_(std::shared_ptr< SceneNodeCore > prototype)
A reference to the node which was used in order to construct this one.
Definition scene_system.hpp:424
std::string mName
The name of this scene node.
Definition scene_system.hpp:621
std::string getPathFromAncestor(std::shared_ptr< const SceneNodeCore > ancestor) const
Gets the path from a node (assumed to be an ancestor) to this node.
Definition scene_system.cpp:385
std::vector< std::shared_ptr< SceneNodeCore > > getChildren()
Returns a list of all of this node's immediate children scene nodes.
Definition scene_system.cpp:248
virtual void onDeactivated()
Scene node lifecycle hook for when a node is deactivated on the SceneSystem.
Definition scene_system.cpp:23
virtual ~SceneNodeCore()=default
Destroys SceneNodeCore.
static std::tuple< std::string, std::string > nextInPath(const std::string &where)
Strips the root-most part of the path to a node.
Definition scene_system.cpp:192
std::vector< std::shared_ptr< SceneNodeCore > > mChildren
A list of this node's child nodes.
Definition scene_system.hpp:667
virtual void joinWorld(ECSWorld &world)
Removes this node's entity from its current ECSWorld and adds it to a new ECSWorld.
Definition scene_system.cpp:410
static void setParentViewport(std::shared_ptr< SceneNodeCore > node, std::shared_ptr< ViewportNode > newViewport)
Sets a node as the parent viewport of another one.
Definition scene_system.cpp:273
std::shared_ptr< SceneNodeCore > getNode(const std::string &where)
Gets a reference to a scene node (of any valid type) based on its path relative to this node.
Definition scene_system.cpp:259
void addOrUpdateComponent(const TComponent &component, const bool bypassSceneActivityCheck=false)
A method for adding a component or updating a component if that component is already present on this ...
Definition scene_system.hpp:2034
std::string getName() const
Returns the name string for this node.
Definition scene_system.cpp:184
std::string getViewportLocalPath() const
Gets the path of this node relative to its local viewport node.
Definition scene_system.cpp:188
static void validateName(const std::string &nodeName)
Tests whether a given name is actually valid, throwing an error when it is not.
Definition scene_system.cpp:425
bool isActive() const
Returns whether this node is present as part of the SceneSystem's scene tree, AND is active there as ...
Definition scene_system.cpp:165
static void SceneNodeCore_del_(SceneNodeCore *sceneNode)
Deleter for a managed pointer to a scene node which ensures its onDestroyed virtual function gets cal...
Definition scene_system.cpp:15
void copyDescendants(const SceneNodeCore &other)
Copies descendant nodes belonging to another node, attaches the copies to this node.
Definition scene_system.cpp:105
virtual void onActivated()
Scene node lifecycle hook for when a node is made an active part of the SceneSystem.
Definition scene_system.cpp:22
void updateComponent(const TComponent &component)
Updates the value of a component of this node (to what it should be at the start of the next simulati...
Definition scene_system.hpp:2029
std::shared_ptr< Entity > mEntity
The ECSWorld entity which this node is a wrapper over.
Definition scene_system.hpp:641
TComponent getComponent(const float simulationProgress=1.f) const
Retrieves a component belonging to this node.
Definition scene_system.hpp:2019
std::shared_ptr< SceneNodeCore > getParentNode()
Gets the parent node of this node, if one is present.
Definition scene_system.cpp:318
void copyAndReplaceAttributes(const SceneNodeCore &other)
Copies component values from another node and replaces the values on node's components with them.
Definition scene_system.cpp:89
bool hasComponent() const
Tests whether this node has a component of a specific type.
Definition scene_system.hpp:2024
std::vector< std::shared_ptr< SceneNodeCore > > getDescendants()
Gets all of the descendant nodes belonging to this scene node.
Definition scene_system.cpp:375
virtual void onDestroyed()
Scene node lifecycle hook for when a node (and possibly its descendants) are about to be destroyed.
Definition scene_system.cpp:24
void addNode(std::shared_ptr< SceneNodeCore > node, const std::string &where)
Adds a node (or a tree of them) as a child of the node specified by the path in the argument.
Definition scene_system.cpp:225
uint8_t mStateFlags
Flags indicating the state of this scene node in the scene system.
Definition scene_system.hpp:629
Signature mSystemMask
A bitset, each position of which indicates whether a system should influence this node when it is par...
Definition scene_system.hpp:681
void setName(const std::string &name)
Sets the name of this node.
Definition scene_system.cpp:179
std::vector< std::shared_ptr< SceneNodeCore > > removeChildren()
Disconnects and removes all the child nodes attached to this node.
Definition scene_system.cpp:365
std::weak_ptr< ViewportNode > mParentViewport
A reference to this node's parent viewport (whose meaning changes depending on whether this node is a...
Definition scene_system.hpp:653
UniversalEntityID getUniversalEntityID() const
Gets the UniversalEntityID aka the world-entity-id pair associated with this node.
Definition scene_system.cpp:404
TObject getByPath(const std::string &where)
Gets a reference to a node or related object by its path.
Definition scene_system.hpp:1917
std::shared_ptr< SceneNodeCore > mPrototype
Allows a prototype scene node to be retained as a resource so long as this node is present in memory ...
Definition scene_system.hpp:673
bool hasNode(const std::string &pathToChild) const
Tests whether a node specified by some path relative to this node is a real descendant of this node.
Definition scene_system.cpp:208
void setEnabled(bool state)
Sets whether or not a given system should be able to influence this scene object.
Definition scene_system.hpp:2053
std::weak_ptr< SceneNodeCore > mParent
A reference to this node's parent scene node.
Definition scene_system.hpp:647
static std::shared_ptr< SceneNodeCore > copy(const std::shared_ptr< const SceneNodeCore > other)
Creates a new scene tree by copying another scene node and its descendants.
Definition scene_system.cpp:26
EntityID getEntityID() const
Returns the entity id associated with these scene node.
Definition scene_system.cpp:398
static bool detectCycle(std::shared_ptr< SceneNodeCore > node)
Tests whether there are any cycles in the path up to the node's oldest ancestor.
Definition scene_system.cpp:147
WorldID getWorldID() const
Returns the ID of the ECSWorld this node belongs to.
Definition scene_system.cpp:401
void removeComponent()
Removes a component present on this node.
Definition scene_system.hpp:2043
StateFlags
Flags that indicate whether this node is enabled and active for the SceneSystem.
Definition scene_system.hpp:534
bool inScene() const
Returns whether this node is present as part of the SceneSystem's scene tree.
Definition scene_system.cpp:161
bool getEnabled() const
Returns whether a particular system has been enabled for this node.
Definition scene_system.hpp:2048
The most basic vanilla flavour of scene node comprised of no more than a name and some components.
Definition scene_system.hpp:801
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Notifies scene system that a placement component in its world has been updated.
Definition scene_system.cpp:1522
void clearReportList()
Called at the end of the transform update to clear report list in preparation for the next time step'...
Definition scene_system.hpp:1609
std::set< EntityID > mReportedEntities
Entities whose placement updates this system has reported.
Definition scene_system.hpp:1596
void clearReportList()
Called at the end of the placement update to clear report list in preparation for the next time step'...
Definition scene_system.hpp:1649
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Notifies the scene system that an entity in its world has had its transform updated.
Definition scene_system.cpp:1533
std::set< EntityID > mReportedEntities
Entities whose transform updates this system has reported.
Definition scene_system.hpp:1635
The SceneSystem, a singleton System, responsible for tracking all objects in the scene,...
Definition scene_system.hpp:1418
bool isSingleton() const override
Informs this System's ECSWorld that the SceneSystem is a singleton, i.e., there should not be more th...
Definition scene_system.hpp:1444
bool inScene(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns whether a particular scene node is in the SceneSystem's scene tree, even if it is inactive.
Definition scene_system.cpp:1110
std::set< UniversalEntityID, std::less< UniversalEntityID > > mComputePlacementQueue
Nodes whose transforms were updated this variable or simulation step should have their placements rec...
Definition scene_system.hpp:1875
std::vector< std::weak_ptr< ECSWorld > > getActiveWorlds()
Returns the ECSWorlds owned by ViewportNodes active in the scene tree.
Definition scene_system.cpp:1142
std::weak_ptr< ECSWorld > getRootWorld() const
Gets the world associated with the root ViewportNode of the scene system.
Definition scene_system.cpp:1134
ViewportNode & getRootViewport() const
Gets a reference to the root viewport of the SceneSystem, usually used to adjust its rendering config...
Definition scene_system.cpp:1155
std::vector< std::shared_ptr< SceneNodeCore > > getNodesByID(const std::vector< UniversalEntityID > &universalEntityIDs)
Gets nodes by their world-entity ID pair.
Definition scene_system.cpp:1550
std::set< UniversalEntityID, std::less< UniversalEntityID > > mActiveEntities
A list of world-entity IDs associated with all the active nodes known by the SceneSystem.
Definition scene_system.hpp:1861
void markDirtyPlacement(UniversalEntityID universalEntityID)
Marks a node as in need of a placement update based on its universal entity id.
Definition scene_system.cpp:1495
uint32_t render(float simulationProgress, uint32_t variableStep)
Runs the render step for the SceneSystem's root viewport and its descendants.
Definition scene_system.cpp:1086
void deactivateSubtree(std::shared_ptr< SceneNodeCore > sceneNode)
Deactivates this node and its descendants.
Definition scene_system.cpp:1327
void activateSubtree(std::shared_ptr< SceneNodeCore > sceneNode)
Activates this node and its descendants.
Definition scene_system.cpp:1311
std::set< UniversalEntityID, std::less< UniversalEntityID > > mComputeTransformQueue
Nodes which were updated during this variable or simulation step scheduled for a Transform update.
Definition scene_system.hpp:1867
void simulationStep(uint32_t simStepMillis, std::vector< std::pair< ActionDefinition, ActionData > > triggeredActions={})
Runs a single step for the root viewport and its descendants, propagating any actions generated by th...
Definition scene_system.cpp:990
void nodeRemoved(std::shared_ptr< SceneNodeCore > sceneNode)
Plays any side effects related to a node being removed from the SceneSystem's scene tree.
Definition scene_system.cpp:1279
std::shared_ptr< TSceneNode > getNodeByID(const UniversalEntityID &universalEntityID)
Gets a scene node by its world-entity ID pair.
Definition scene_system.hpp:1927
void updateHierarchyDataInsertion(std::shared_ptr< SceneNodeCore > insertedNode)
Updates a node's scene hierarchy data per its location in the scene tree.
Definition scene_system.cpp:1163
void onApplicationInitialize(const ViewportNode::RenderConfiguration &rootViewportRenderConfiguration)
A method intended to be used at the start of the application to configure the SceneSystem's root view...
Definition scene_system.cpp:1508
std::shared_ptr< SceneNodeCore > removeNode(const std::string &where)
Removes a node present at the path specified in the call.
Definition scene_system.cpp:1129
Transform getCachedWorldTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns the Transform of a node based on both its local Placement and its hierarchical transforms.
Definition scene_system.cpp:1434
void onApplicationEnd()
Clean up tasks the SceneSystem should perform before the application is terminated.
Definition scene_system.cpp:1105
void variableStep(float simulationProgress, uint32_t simulationLagMillis, uint32_t variableStepMillis, std::vector< std::pair< ActionDefinition, ActionData > > triggeredActions={})
Runs the variable step for the SceneSystem's root viewport and its descendants.
Definition scene_system.cpp:1045
static std::string getSystemTypeName()
The system type string associated with the SceneSystem.
Definition scene_system.hpp:1434
void nodeAdded(std::shared_ptr< SceneNodeCore > sceneNode)
Plays any side effects associated with a node being added to the SceneSystem's scene tree.
Definition scene_system.cpp:1240
void onWorldPlacementUpdate(UniversalEntityID UniversalEntityID)
A callback used by this system's subsystem to notify the SceneSystem that an entity's placement has b...
Definition scene_system.cpp:1500
std::weak_ptr< ECSWorld > getWorld(WorldID world)
Returns a reference to the world with a particular ID.
Definition scene_system.cpp:1146
void updateHierarchyDataRemoval(std::shared_ptr< SceneNodeCore > removedNode)
Removes a node's scene hierarchy data before it's removed from the hierarchy.
Definition scene_system.cpp:1204
void updateTransformsPlacements()
Updates transforms of objects in the scene per changes in those object's Placement component.
Definition scene_system.cpp:1343
SceneSystem(std::weak_ptr< ECSWorld > world)
Constructs a new SceneSystem object.
Definition scene_system.hpp:1425
std::vector< std::shared_ptr< ViewportNode > > getActiveViewports()
Returns a list of active viewports including the root viewport of the scene tree.
Definition scene_system.cpp:1138
void nodeActivationChanged(std::shared_ptr< SceneNodeCore > sceneNode, bool state)
"Activates" or deactivates a node and its descendants on various ECS systems, per the node's system m...
Definition scene_system.cpp:1295
void transformStep(uint32_t timestepMillis)
Updates transforms and placements as needed after a system update step.
Definition scene_system.cpp:1066
void onWorldTransformUpdate(UniversalEntityID universalEntityID)
A callback used by this system's subsystem to notify it that an entity's Transform has been updated.
Definition scene_system.cpp:1504
void addNode(std::shared_ptr< SceneNodeCore > node, const std::string &where)
Adds a node to the SceneSystem's scene tree as a child of the node specified by its path.
Definition scene_system.cpp:1159
Transform getInheritedTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Gets the transform premultiplied with this object's local transform based on its transform settings.
Definition scene_system.cpp:1440
Transform getLocalTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Gets the transform of a node solely based on its Placement component and independent of its position ...
Definition scene_system.cpp:1421
std::shared_ptr< SceneNodeCore > getNode(const std::string &where)
Gets a node by its scene node path.
Definition scene_system.cpp:1124
TObject getByPath(const std::string &where)
Gets an object of a specific type based on a valid path to that object belonging to the scene system ...
Definition scene_system.hpp:1922
void markDirtyTransform(UniversalEntityID universalEntityID)
Marks a node as in need of a transform update based on its universal entity id.
Definition scene_system.cpp:1489
void onApplicationStart()
Method to be called by main to initialize the SceneSystem as a whole.
Definition scene_system.cpp:1517
std::shared_ptr< ViewportNode > mRootNode
The root node of the SceneSystem, alive and active throughout the lifetime of the application.
Definition scene_system.hpp:1849
std::map< UniversalEntityID, std::weak_ptr< SceneNodeCore >, std::less< UniversalEntityID > > mEntityToNode
A mapping from the world-entity IDs of active nodes in the SceneSystem to the nodes themselves.
Definition scene_system.hpp:1855
bool isActive(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns whether a particular scene node is an active member of the SceneSystem's scene tree.
Definition scene_system.cpp:1117
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1087
A type of node capable of and responsible for interacting sensibly with the engine's RenderSystem and...
Definition scene_system.hpp:842
std::set< std::shared_ptr< SceneNodeCore >, std::owner_less< std::shared_ptr< SceneNodeCore > > > mDomainCameras
The set of all active cameras that belong to the domain owned by this viewport.
Definition scene_system.hpp:1378
ActionDispatch & getActionDispatch()
Gets the action dispatch object for this viewport, which is the central location from which all actio...
Definition scene_system.cpp:949
bool mActionFlowthrough
Whether or not handled actions are propagated to this viewport's descendant viewports.
Definition scene_system.hpp:1354
void unregisterDomainCamera(std::shared_ptr< SceneNodeCore > cameraNode)
Removes a camera from this viewport's domain.
Definition scene_system.cpp:777
std::shared_ptr< Texture > fetchRenderResult(float simulationProgress)
Fetches the render result for the most recently computed render frame.
Definition scene_system.cpp:797
void requestDimensions(glm::u16vec2 requestedDimensions)
Target dimensions that another part of the program specifies for this viewport.
Definition scene_system.cpp:644
void updateExposure(float newExposure)
Updates the exposure of this viewport's RenderSet.
Definition scene_system.cpp:596
uint64_t mViewportLoadOrdinal
Number dictating when this viewport should be computed relative to other viewports,...
Definition scene_system.hpp:1336
std::shared_ptr< const SceneNodeCore > getActiveCamera() const
Gets the active camera for this viewport.
Definition scene_system.cpp:640
void createAndJoinWorld()
Creates and joins its own ECSWorld.
Definition scene_system.cpp:559
uint32_t mNLifetimeChildrenAdded
A number that is incremented whenever a child viewport is added to this viewport, guaranteeing unique...
Definition scene_system.hpp:1342
std::shared_ptr< ViewportNode > getLocalViewport() override
Returns this viewport instead of base class return value.
Definition scene_system.cpp:953
static std::shared_ptr< ViewportNode > copy(const std::shared_ptr< const ViewportNode > other)
Copies the properties and components of another viewport and uses them to construct a new one.
Definition scene_system.cpp:530
std::set< std::shared_ptr< ViewportNode >, ViewportChildComp_ > mChildViewports
This viewports children viewport, in the order they were added to this viewport.
Definition scene_system.hpp:1366
uint32_t render(float simulationProgress, uint32_t variableStep)
Requests execution of the render pipeline.
Definition scene_system.cpp:830
bool handleAction(std::pair< ActionDefinition, ActionData > pendingAction)
Handles an action received by this viewport, generally by dispatching it to subscribed listeners and ...
Definition scene_system.cpp:909
void joinWorld(ECSWorld &world) override
Joins this node's entity to a world (either its own or its parent viewport's).
Definition scene_system.cpp:413
void setFPSCap(float fpsCap)
If an FPS capped update mode is selected, sets what that cap actually is.
Definition scene_system.cpp:761
RenderConfiguration mRenderConfiguration
The render configuration associated with this viewport.
Definition scene_system.hpp:1398
bool disallowsHandledActionPropagation() const
Returns whether an action handled by one of this viewport's (high precedence) child viewports,...
Definition scene_system.hpp:1164
~ViewportNode() override
Destroys this viewport object.
Definition scene_system.cpp:437
static std::string getResourceTypeName()
Gets the resource type string associated with the ViewportNode.
Definition scene_system.hpp:988
uint32_t getViewportLoadOrdinal() const
(When this viewport is the immediate descendant of a RenderSet::RenderType::ADDITION viewport) The pr...
Definition scene_system.hpp:1179
void setActiveCamera(const std::string &cameraPath)
Sets the active camera for this viewport's RenderSet via path to the camera node.
Definition scene_system.cpp:619
void setRenderConfiguration(const RenderConfiguration &renderConfiguration)
Sets the render configuration for this viewport.
Definition scene_system.cpp:549
bool mPreventHandledActionPropagation
When a child viewport handles an action, determines whether the action is sent along to this viewport...
Definition scene_system.hpp:1360
RenderSetID mRenderSet
The ID of the RenderSet registered with this viewport's RenderSystem corresponding to this ViewportNo...
Definition scene_system.hpp:1384
std::shared_ptr< SceneNodeCore > mActiveCamera
The active camera node associated with this viewport.
Definition scene_system.hpp:1372
float getGamma()
Gets the gamma value used by this viewport's RenderSet.
Definition scene_system.cpp:613
SDL_Rect getCenteredViewportCoordinates() const
Gets the region of this viewport's target texture that the rendered texture should be mapped to.
Definition scene_system.hpp:1315
void viewNextDebugTexture()
Sets the next debug texture listed in this viewport's render set to the texture considered "active" b...
Definition scene_system.cpp:591
void registerDomainCamera(std::shared_ptr< SceneNodeCore > cameraNode)
Registers a camera that belongs to this viewport, which is a descendant of it and not a descendant of...
Definition scene_system.cpp:772
std::vector< std::shared_ptr< ViewportNode > > getActiveDescendantViewports()
Gets active descendant viewports (in DFS order) under this Viewport.
Definition scene_system.cpp:961
void setSkybox(std::shared_ptr< Texture > skybox)
Sets the skybox texture for this object's RenderSystem.
Definition scene_system.cpp:587
std::vector< std::weak_ptr< ECSWorld > > getActiveDescendantWorlds()
Gets weak references to ECSWorlds belonging to descendant viewports.
Definition scene_system.cpp:975
float getExposure()
Gets the exposure value used by this viewport's RenderSet.
Definition scene_system.cpp:607
void onDeactivated() override
Deactivates this viewports ECSWorld (if there is one) when this ViewportNode is retired.
Definition scene_system.cpp:583
void setResizeType(RenderConfiguration::ResizeType type)
Sets this viewport's behaviour when request dimensions (or target dimensions) are changed.
Definition scene_system.cpp:744
std::shared_ptr< ECSWorld > mOwnWorld
The ECS world owned by this viewport (if any), as well as the world this node is a member of.
Definition scene_system.hpp:1202
static std::shared_ptr< ViewportNode > create(const std::string &name, bool inheritsWorld, bool allowActionFlowThrough, const RenderConfiguration &renderConfiguration, std::shared_ptr< Texture > skybox)
Creates a viewport node with components essential to it.
Definition scene_system.cpp:469
void setResizeMode(RenderConfiguration::ResizeMode mode)
When resize is enabled, determines how resized render dimensions are computed.
Definition scene_system.cpp:750
std::shared_ptr< Texture > mTextureResult
The result of rendering from running the rendering pipeline associated with this viewport.
Definition scene_system.hpp:1392
void updateGamma(float newGamma)
Updates the gamma value of this viewport's RenderSet.
Definition scene_system.cpp:601
RenderConfiguration getRenderConfiguration() const
Gets the render configuration for this viewport.
Definition scene_system.cpp:555
void onActivated() override
Override which for a viewport sets its active camera, and initializes the ECSWorld owned by this view...
Definition scene_system.cpp:565
ViewportNode(const Key &key, const Placement &placement, const std::string &name)
Constructs a new ViewportNode using a simplified constructor.
Definition scene_system.hpp:1243
ActionDispatch mActionDispatch
Dispatcher for received actions to their action handlers within the domain of this viewport.
Definition scene_system.hpp:1348
void render_(float simulationProgress)
Implementation responsible for actually computing a new render frame.
Definition scene_system.cpp:871
void setRenderScale(float renderScale)
Sets the scale relative to computed and design dimensions for the render pipeline target.
Definition scene_system.cpp:766
uint32_t mTimeSinceLastRender
The time, in milliseconds, since the last time a render request was honoured by this viewport.
Definition scene_system.hpp:1404
std::shared_ptr< SceneNodeCore > clone() const override
Creates a new ViewportNode using itself as a template.
Definition scene_system.cpp:535
void setUpdateMode(RenderConfiguration::UpdateMode updateMode)
Sets the behaviour for frequency of render updates w.r.t render requests.
Definition scene_system.cpp:756
ToyMaker Engine's implementation of an ECS system.
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:103
std::bitset< kMaxComponents > Signature
A 255 bit number, where each enabled bit represents a relationship between an entity and some ECS rel...
Definition ecs_world.hpp:154
ECSType SystemType
An unsigned integer representing the type of a system.
Definition ecs_world.hpp:112
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:119
std::pair< WorldID, EntityID > UniversalEntityID
An ID that uniquely identifies an entity.
Definition ecs_world.hpp:85
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
std::uint64_t WorldID
An unsigned integer representing the name of an ECS world.
Definition ecs_world.hpp:76
SpecialEntity
(Perhaps unused) Special "reserved" entity IDs which the scene system might use.
Definition scene_system.hpp:69
RelativeTo
(Presently unused) A marker to indicate how transforms should be computed for a given scene node.
Definition scene_system.hpp:52
const std::string kSceneRootName
Special name for the scene root, unusable by any other scene object.
Definition scene_system.cpp:13
@ PARENT
Transform components are inherited from this object's parent.
Definition scene_components.hpp:58
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:25
STL namespace.
Contains definitions relating to the render system defined for this object.
Headers relating to resources and their management for a given project.
Stores structs and classes for common components used by the SceneSystem and other related Systems.
Classes and structs representing data related to the engine's spatial query system (the precursor to ...
A component representing the position, rotation, and scale of an entity.
Definition scene_components.hpp:73
TransformInheritMode mInheritMode
Field specifying which coordinate system this object's transform is specified relative to.
Definition scene_components.hpp:105
TransformComponentSet mInheritedComponents
Field specifying which aspects of its parent's transform this object's transform is specified relativ...
Definition scene_components.hpp:99
RenderType
Enum listing the different rendering pipelines available.
Definition render_system.hpp:59
Component representing hierarchical information related to this entity.
Definition scene_components.hpp:212
A private struct to limit certain sensitive functions to this class and other closely coupled classes...
Definition scene_system.hpp:517
A helper intended to get scene nodes and related objects attached to the scene tree.
Definition scene_system.hpp:546
Helper struct for retrieving nodes based on their UniversalEntityIDs.
Definition scene_system.hpp:1663
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:150
A collection of data that specifies the behaviour and properties of the RenderSystem and target textu...
Definition scene_system.hpp:848
glm::u16vec2 mComputedDimensions
The dimensions finally computed for this viewport, per request from other parts of the application.
Definition scene_system.hpp:916
RenderSet::RenderType RenderType
Specifies the type of render pipeline requested by this viewport.
Definition scene_system.hpp:887
ResizeType
Different resize configurations available for this Viewport node that dictate how render textures (fr...
Definition scene_system.hpp:853
glm::u16vec2 mRequestedDimensions
The dimensions requested by other parts of the application, to which this viewport's render texture m...
Definition scene_system.hpp:922
ResizeMode
Determines which dimensions the end result of the viewport is allowed to expand on.
Definition scene_system.hpp:863
UpdateMode mUpdateMode
The frequency of rendering updates in real time made on this viewport.
Definition scene_system.hpp:940
ResizeMode mResizeMode
The resizing/scaling behaviour from render-> target texture for this viewport.
Definition scene_system.hpp:898
RenderType mRenderType
The type of render pipelien requested by this viewport.
Definition scene_system.hpp:904
glm::u16vec2 mBaseDimensions
The design dimensions for this viewport, specified at the time of its development.
Definition scene_system.hpp:910
float mFPSCap
If an FPS capped update mode is used, specifies the value of that cap.
Definition scene_system.hpp:947
ResizeType mResizeType
The type of resizing/scaling behaviour from render->target texture for this viewport.
Definition scene_system.hpp:893
float mRenderScale
A multiplier applied (in case resizing is enabled) determining multiplier to the base or computed dim...
Definition scene_system.hpp:934
UpdateMode
Configuration value determining when and how often render updates take place for this viewport.
Definition scene_system.hpp:874
Comparator used for determining priority of descendant viewports owned by a RenderSet::RenderType::AD...
Definition scene_system.hpp:1326
Header containing definitions of classes and functions related to loading and using Texture resources...