Home Game Development c++ – What is the best way to implement a scene manager?

c++ – What is the best way to implement a scene manager?

0
c++ – What is the best way to implement a scene manager?

[ad_1]

I’m trying to create a scene manager system for a small framework I’m writing on top of SDL2. I have implemented a scene management system that works, but I am not sure if it is structured how it should be.

What I am doing:

I am following an OO approach where each scene inherits from “IScene”, an abstract class with the standard on_load, on_update, etc. methods. These scenes are managed by a scene manager class, which stores a map of int ids and IScene* scene pointers. Here is the code to go with this explanation:

namespace se {
    class IScene;
}

namespace se::managers {
    class SceneManager {
        public:
            SceneManager();
            ~SceneManager();

            void add_scene(int scene_id, IScene* scene);
            void remove_scene(int scene_id);
            IScene* pop(int scene_id);
            IScene* get_scene();
            void set_scene(int scene_id);
        private:
            std::map<int, IScene*> scenes;

            int active_scene;
            IScene* active_scene_ptr;
    };
}

scene.h

namespace se {
    class IScene {
        public:
            IScene(
                IApp* app
            );
            virtual ~IScene();
            virtual void on_load() = 0;
            virtual void on_unload() = 0;
            virtual void on_scene_enter() = 0;
            virtual void on_scene_exit() = 0;
            virtual void on_update() = 0;
            virtual void on_draw() = 0;
        protected:
            IApp* app;
    };
}

The Problem:
In order to get this relationship to work, I had to forward declare IScene in scenemanager.h to avoid a cyclic dependency. This is because I want IScene to be able to change the active scene from within its own on_update method. I read some threads that said forward declaration to avoid this issue can be considered a “code smell” because it’s a sign of tight coupling.

What I would like to know

  1. Is my approach okay?
  2. What would be a better approach?

Thank you for your time!

P.S. This is my first time ever posting on Stack Exchange. If there’s anything about this post that is structured wrong/unclear please let me know so I can fix it in the future.

[ad_2]

Previous article Call of Duty 2024 Reportedly Returns to Black Ops
Next article Monarch: Legacy of Monsters makes a good case for Hollywood nepotism
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!