Make scene manager store shared pointers to scene to simplify removing scenes
This commit is contained in:
parent
b4c19a2ca7
commit
a3aca66705
3 changed files with 11 additions and 11 deletions
|
|
@ -37,8 +37,8 @@ namespace psemek::app
|
|||
void poll_events();
|
||||
void run();
|
||||
|
||||
void push_scene(std::unique_ptr<scene> s) override;
|
||||
std::unique_ptr<scene> pop_scene() override;
|
||||
void push_scene(std::shared_ptr<scene> s) override;
|
||||
std::shared_ptr<scene> pop_scene() override;
|
||||
|
||||
void show_cursor(bool show);
|
||||
bool vsync() const;
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ namespace psemek::app
|
|||
|
||||
struct scene_manager
|
||||
{
|
||||
virtual void push_scene(std::unique_ptr<scene>) = 0;
|
||||
virtual std::unique_ptr<scene> pop_scene() = 0;
|
||||
virtual void push_scene(std::shared_ptr<scene>) = 0;
|
||||
virtual std::shared_ptr<scene> pop_scene() = 0;
|
||||
|
||||
virtual ~scene_manager() {}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ namespace psemek::app
|
|||
SDL_Window * window = nullptr;
|
||||
SDL_GLContext gl_context = nullptr;
|
||||
|
||||
std::vector<std::unique_ptr<scene>> scene_stack;
|
||||
std::vector<std::shared_ptr<scene>> scene_stack;
|
||||
|
||||
bool running = false;
|
||||
|
||||
|
|
@ -39,11 +39,11 @@ namespace psemek::app
|
|||
if (window) SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
scene * get_scene()
|
||||
std::shared_ptr<scene> get_scene()
|
||||
{
|
||||
if (!scene_stack.empty())
|
||||
return scene_stack.back().get();
|
||||
return parent;
|
||||
return scene_stack.back();
|
||||
return std::shared_ptr<scene>(parent, [](scene *){});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -216,7 +216,7 @@ namespace psemek::app
|
|||
{
|
||||
SDL_ShowWindow(impl().window);
|
||||
impl().running = true;
|
||||
if (impl().get_scene() == this)
|
||||
if (impl().get_scene().get() == this)
|
||||
on_scene_enter(this);
|
||||
while (running())
|
||||
{
|
||||
|
|
@ -240,7 +240,7 @@ namespace psemek::app
|
|||
impl().get_scene()->on_scene_exit();
|
||||
}
|
||||
|
||||
void app::push_scene(std::unique_ptr<scene> s)
|
||||
void app::push_scene(std::shared_ptr<scene> s)
|
||||
{
|
||||
impl().get_scene()->on_scene_exit();
|
||||
|
||||
|
|
@ -250,7 +250,7 @@ namespace psemek::app
|
|||
impl().get_scene()->on_scene_enter(this);
|
||||
}
|
||||
|
||||
std::unique_ptr<scene> app::pop_scene()
|
||||
std::shared_ptr<scene> app::pop_scene()
|
||||
{
|
||||
if (impl().scene_stack.empty())
|
||||
return nullptr;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue