[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
- Is my approach okay?
- 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]