c++ – What’s the easiest way to implement a scene supervisor?

[ad_1]

I am attempting to create a scene supervisor system for a small framework I am writing on high of SDL2. I’ve carried out a scene administration system that works, however I’m not certain whether it is structured the way it ought to be.

What I’m doing:

I’m following an OO strategy the place every scene inherits from “IScene”, an summary class with the usual on_load, on_update, and so forth. strategies. These scenes are managed by a scene supervisor class, which shops a map of int ids and IScene* scene pointers. Right here is the code to go along with this clarification:

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);
        non-public:
            std::map<int, IScene*> scenes;

            int active_scene;
            IScene* active_scene_ptr;
    };
}

scene.h

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

The Downside:
With the intention to get this relationship to work, I needed to ahead declare IScene in scenemanager.h to keep away from a cyclic dependency. It’s because I would like IScene to have the ability to change the energetic scene from inside its personal on_update technique. I learn some threads that mentioned ahead declaration to keep away from this challenge may be thought-about a “code scent” as a result of it is a signal of tight coupling.

What I want to know

  1. Is my strategy okay?
  2. What can be a greater strategy?

Thanks to your time!

P.S. That is my first time ever posting on Stack Alternate. If there’s something about this publish that’s structured mistaken/unclear please let me know so I can repair it sooner or later.

[ad_2]

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *