void Game_state::update(Context& context)
{
if (pause_game)
- {
- context.running_game = std::move(static_cast<Game_state*>(context.current_state));
- context.next_state = new Pause_menu{};
+ {
+ context.running_game.reset(static_cast<Game_state*>(context.current_state.release()));
+
+ context.next_state = std::make_unique<Pause_menu>();
pause_game = false;
return;
}
{
if (exit_menu)
{
- context.next_state = new Game_state{};
+ context.next_state = std::make_unique<Game_state>();
}
}
}
}
-Pause_menu::Pause_menu() : texture{}, sprite{}, exit_game{false}, resume_game{false}
+Pause_menu::Pause_menu() : texture{}, sprite{}, resume_game{false}, exit_game{false}
{
texture.loadFromFile("assets/kir.png");
sprite.setTexture(texture);
}
else if (exit_game)
{
- context.next_state = new Start_menu{};
+ context.next_state = std::make_unique<Start_menu>();
+ context.running_game.release();
}
}
struct Context
{
- State* current_state {nullptr};
- State* next_state {nullptr};
- Game_state* running_game {nullptr};
+ std::unique_ptr<State> current_state {nullptr};
+ std::unique_ptr<State> next_state {nullptr};
+ std::unique_ptr<Game_state> running_game {nullptr};
};
#endif
\ No newline at end of file
};
Context game_context{};
- game_context.current_state = new Start_menu{};
+ game_context.current_state = std::make_unique<Start_menu>();
sf::Clock game_clock;
while (window.isOpen())
if (game_context.next_state != nullptr)
{
+ game_context.current_state.release();
game_context.current_state = std::move(game_context.next_state);
game_context.next_state = nullptr;
}