From c916e1a6cf817715b743032b67f7e02f1b6e573d Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 11 Jul 2024 08:43:50 -0500 Subject: [PATCH] Scene system roughly how I like --- src/dawn/game/Game.cpp | 18 +++++++++++++++ src/dawn/game/Game.hpp | 14 ++++++++++++ src/dawn/scene/Scene.cpp | 19 ++++++++++++++- src/dawn/scene/Scene.hpp | 23 ++++++++++++++++++- src/dawn/scene/item/SceneItem.cpp | 11 ++++++--- src/dawn/scene/item/SceneItem.hpp | 28 ++++++++++++++++++++--- src/dawn/scene/item/SceneItem2D.cpp | 7 ++++-- src/dawn/scene/item/SceneItem2D.hpp | 5 +++- src/dawn/scene/item/display/Rectangle.cpp | 7 ++++-- src/dawn/scene/item/display/Rectangle.hpp | 5 +++- 10 files changed, 123 insertions(+), 14 deletions(-) diff --git a/src/dawn/game/Game.cpp b/src/dawn/game/Game.cpp index fc19ef38..2e48cfc4 100644 --- a/src/dawn/game/Game.cpp +++ b/src/dawn/game/Game.cpp @@ -14,6 +14,7 @@ Game::Game() { } void Game::init() { + currentScene = std::make_shared(weak_from_this()); auto myItem = currentScene->addSceneItem("myItem"); } @@ -22,6 +23,23 @@ void Game::update() { } +std::shared_ptr Game::getCurrentScene() { + return currentScene; +} + +void Game::setActiveScene(std::shared_ptr scene) { + auto previousCurrent = currentScene; + if(previousCurrent != nullptr) { + previousCurrent->inactive(); + } + + currentScene = scene; + + if(currentScene != nullptr) { + currentScene->active(); + } +} + Game::~Game() { } \ No newline at end of file diff --git a/src/dawn/game/Game.hpp b/src/dawn/game/Game.hpp index dc14d885..33e705cc 100644 --- a/src/dawn/game/Game.hpp +++ b/src/dawn/game/Game.hpp @@ -31,6 +31,20 @@ namespace Dawn { */ void update(); + /** + * Returns the current active scene. + * + * @return The current active scene. + */ + std::shared_ptr getCurrentScene(); + + /** + * Sets the active scene for the game. + * + * @param scene The scene to set as active. + */ + void setActiveScene(std::shared_ptr scene); + /** * Deloads game and all sub managers used by game. */ diff --git a/src/dawn/scene/Scene.cpp b/src/dawn/scene/Scene.cpp index ae114c86..b5d5cb5b 100644 --- a/src/dawn/scene/Scene.cpp +++ b/src/dawn/scene/Scene.cpp @@ -7,10 +7,27 @@ using namespace Dawn; -Scene::Scene(std::weak_ptr game) : SceneItem(std::weak_ptr()) { +Scene::Scene(std::weak_ptr game) : SceneItem( + std::weak_ptr(), + std::enable_shared_from_this::weak_from_this() +) { this->game = game; } +void Scene::active() { + auto itItems = items.begin(); +} + +void Scene::inactive() { + +} + +bool_t Scene::isActiveScene() { + auto g = game.lock(); + if(g == nullptr) return false; + return g->getCurrentScene() == this->getScene(); +} + Scene::~Scene() { } \ No newline at end of file diff --git a/src/dawn/scene/Scene.hpp b/src/dawn/scene/Scene.hpp index 9296732a..ec0d0a49 100644 --- a/src/dawn/scene/Scene.hpp +++ b/src/dawn/scene/Scene.hpp @@ -8,10 +8,22 @@ #include "game/Game.hpp" namespace Dawn { - class Scene : public SceneItem { + class Scene : public SceneItem, public std::enable_shared_from_this { private: std::weak_ptr game; + /** + * Invoked by the game whenever this scene has been set as the active + * scene. + */ + void active(); + + /** + * Invoked by the game whenever this scene has been set as the inactive + * scene. + */ + void inactive(); + public: /** * Creates a new Scene. @@ -20,9 +32,18 @@ namespace Dawn { */ Scene(std::weak_ptr game); + /** + * Returns true if this scene is the active scene. + * + * @return True if this scene is the active scene. + */ + bool_t isActiveScene(); + /** * Destroys this scene and all of its items. */ virtual ~Scene(); + + friend class Game; }; } \ No newline at end of file diff --git a/src/dawn/scene/item/SceneItem.cpp b/src/dawn/scene/item/SceneItem.cpp index 158a637f..42a36238 100644 --- a/src/dawn/scene/item/SceneItem.cpp +++ b/src/dawn/scene/item/SceneItem.cpp @@ -8,17 +8,22 @@ using namespace Dawn; SceneItem::SceneItem( - std::weak_ptr parent + std::weak_ptr parent, + std::weak_ptr scene ) : - parent(parent) + parent(parent), + scene(scene) { - } std::shared_ptr SceneItem::getParent() { return parent.lock(); } +std::shared_ptr SceneItem::getScene() { + return scene.lock(); +} + void SceneItem::removeSceneItem(std::shared_ptr item) { // I don't need to go through the trouble of removing items if I'm already // disposing, since I'll be clearing them all anyway. diff --git a/src/dawn/scene/item/SceneItem.hpp b/src/dawn/scene/item/SceneItem.hpp index 1e5ff699..85a052db 100644 --- a/src/dawn/scene/item/SceneItem.hpp +++ b/src/dawn/scene/item/SceneItem.hpp @@ -5,23 +5,35 @@ #pragma once #include "dawn.hpp" +#include "event/Event.hpp" namespace Dawn { + class Scene; + class SceneItem : public std::enable_shared_from_this { private: std::weak_ptr parent; - std::vector> items; + std::weak_ptr scene; bool_t isDisposing = false; + protected: + std::vector> items; + public: std::string name; + Event<> onEnter; + Event<> onLeave; /** * Creates a new SceneItem. * * @param parent The parent SceneItem, or nullptr if root (unlikely). + * @param scene The Scene this SceneItem belongs to. */ - SceneItem(std::weak_ptr parent); + SceneItem( + std::weak_ptr parent, + std::weak_ptr scene + ); /** * Adds a SceneItem as a child of this SceneItem. @@ -31,13 +43,14 @@ namespace Dawn { */ template std::shared_ptr addSceneItem(const std::string &name = "") { - auto item = std::make_shared(weak_from_this()); + auto item = std::make_shared(weak_from_this(), scene); auto asItem = std::dynamic_pointer_cast(item); if(asItem == nullptr) { throw std::runtime_error("SceneItem must be a SceneItem."); } asItem->name = name; items.push_back(asItem); + return item; } @@ -80,9 +93,18 @@ namespace Dawn { */ std::shared_ptr getParent(); + /** + * Gets the Scene this SceneItem belongs to. + * + * @return The Scene this SceneItem belongs to. + */ + std::shared_ptr getScene(); + /** * Destroys this SceneItem. */ virtual ~SceneItem(); + + friend class Scene; }; } \ No newline at end of file diff --git a/src/dawn/scene/item/SceneItem2D.cpp b/src/dawn/scene/item/SceneItem2D.cpp index 9b7d0571..0e7d79ca 100644 --- a/src/dawn/scene/item/SceneItem2D.cpp +++ b/src/dawn/scene/item/SceneItem2D.cpp @@ -7,8 +7,11 @@ using namespace Dawn; -SceneItem2D::SceneItem2D(std::weak_ptr parent) : - SceneItem(parent) +SceneItem2D::SceneItem2D( + std::weak_ptr parent, + std::weak_ptr scene +) : + SceneItem(parent, scene) { } diff --git a/src/dawn/scene/item/SceneItem2D.hpp b/src/dawn/scene/item/SceneItem2D.hpp index ca7cc28a..b343eb44 100644 --- a/src/dawn/scene/item/SceneItem2D.hpp +++ b/src/dawn/scene/item/SceneItem2D.hpp @@ -20,7 +20,10 @@ namespace Dawn { * * @param parent The parent item. */ - SceneItem2D(std::weak_ptr parent); + SceneItem2D( + std::weak_ptr parent, + std::weak_ptr scene + ); /** * Destroys this SceneItem2D. diff --git a/src/dawn/scene/item/display/Rectangle.cpp b/src/dawn/scene/item/display/Rectangle.cpp index 7961340a..97e0bbec 100644 --- a/src/dawn/scene/item/display/Rectangle.cpp +++ b/src/dawn/scene/item/display/Rectangle.cpp @@ -7,8 +7,11 @@ using namespace Dawn; -Rectangle::Rectangle(std::weak_ptr parent) : - SceneItem2D(parent) +Rectangle::Rectangle( + std::weak_ptr parent, + std::weak_ptr scene +) : + SceneItem2D(parent, scene) { } diff --git a/src/dawn/scene/item/display/Rectangle.hpp b/src/dawn/scene/item/display/Rectangle.hpp index 73bf86ec..d0e7ca94 100644 --- a/src/dawn/scene/item/display/Rectangle.hpp +++ b/src/dawn/scene/item/display/Rectangle.hpp @@ -11,13 +11,16 @@ namespace Dawn { class Rectangle : public SceneItem2D { public: struct Color color = COLOR_WHITE; + float_t width = 32.0f; + float_t height = 32.0f; /** * Creates a new Rectangle. * * @param parent The parent SceneItem, or nullptr if root (unlikely). + * @param scene The Scene this item belongs to. */ - Rectangle(std::weak_ptr parent); + Rectangle(std::weak_ptr parent, std::weak_ptr scene); /** * Destroys the Rectangle.