diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 375ee469fd..58a60bbdb4 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -17,6 +17,8 @@ #include "supertux/game_session.hpp" #include +#include +#include #include "audio/sound_manager.hpp" #include "control/input_manager.hpp" @@ -55,8 +57,7 @@ static const int SHRINKFADE_LAYER = LAYER_LIGHTMAP - 1; static const float TELEPORT_FADE_TIME = 1.0f; -GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Statistics* statistics, - bool preserve_music) : +GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Statistics* statistics) : reset_button(false), reset_checkpoint_button(false), m_prevent_death(false), @@ -94,9 +95,6 @@ GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Stat m_boni_at_start.resize(InputManager::current()->get_num_users(), BONUS_NONE); m_data_table.clear(); - - if (restart_level(false, preserve_music) != 0) - throw std::runtime_error ("Initializing the level failed."); } void @@ -155,7 +153,7 @@ GameSession::on_player_removed(int id) return false; } -int +void GameSession::restart_level(bool after_death, bool preserve_music) { const PlayerStatus& currentStatus = m_savegame.get_player_status(); @@ -238,7 +236,7 @@ GameSession::restart_level(bool after_death, bool preserve_music) m_currentsector = m_level->get_sector(spawnpoint->sector); if (!m_currentsector) { - throw std::runtime_error("Couldn't find sector '" + spawnpoint->sector + "' to spawn/respawn Tux."); + throw std::runtime_error(fmt::format("Couldn't find sector '{}' to spawn/respawn Tux.", spawnpoint->sector)); } // Activate on either the spawnpoint (if set), or the spawn position. if (spawnpoint->spawnpoint.empty()) @@ -251,9 +249,7 @@ GameSession::restart_level(bool after_death, bool preserve_music) } } catch (std::exception& e) { - log_fatal << "Couldn't start level: " << e.what() << std::endl; - ScreenManager::current()->pop_screen(); - return (-1); + throw std::runtime_error(std::string("Couldn't start level: ") + e.what()); } if (m_levelintro_shown) @@ -281,8 +277,6 @@ GameSession::restart_level(bool after_death, bool preserve_music) it->set_time(it->get_time() - m_play_time); it++; } - - return (0); } void diff --git a/src/supertux/game_session.hpp b/src/supertux/game_session.hpp index f2b81edd47..57332f538b 100644 --- a/src/supertux/game_session.hpp +++ b/src/supertux/game_session.hpp @@ -78,8 +78,7 @@ class GameSession final : public Screen, }; public: - GameSession(const std::string& levelfile, Savegame& savegame, Statistics* statistics = nullptr, - bool preserve_music = false); + GameSession(const std::string& levelfile, Savegame& savegame, Statistics* statistics = nullptr); virtual void draw(Compositor& compositor) override; virtual void update(float dt_sec, const Controller& controller) override; @@ -131,7 +130,7 @@ class GameSession final : public Screen, std::string get_working_directory() const; inline const std::string& get_level_file() const { return m_levelfile; } inline bool has_active_sequence() const { return m_end_sequence; } - int restart_level(bool after_death = false, bool preserve_music = false); + void restart_level(bool after_death = false, bool preserve_music = false); void toggle_pause(); void abort_level(); diff --git a/src/supertux/levelset_screen.cpp b/src/supertux/levelset_screen.cpp index fb87b19e0f..3e11998e11 100644 --- a/src/supertux/levelset_screen.cpp +++ b/src/supertux/levelset_screen.cpp @@ -88,9 +88,18 @@ LevelsetScreen::setup() m_savegame); if (m_start_pos) { screen->set_start_pos(m_start_pos->first, m_start_pos->second); + } + + try + { screen->restart_level(); + ScreenManager::current()->push_screen(std::move(screen)); + } + catch (const std::runtime_error& e) + { + log_warning << "Couldn't load level: " << e.what() << std::endl; + ScreenManager::current()->pop_screen(); } - ScreenManager::current()->push_screen(std::move(screen)); } } } diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index 6c81486669..e0a7a76ffb 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -624,7 +624,6 @@ Main::launch_game(const CommandLineArguments& args) std::string spawnpointname = args.spawnpoint.value_or(default_spawnpoint); session->set_start_point(sectorname, spawnpointname); - session->restart_level(); } if (g_config->tux_spawn_pos) @@ -633,6 +632,7 @@ Main::launch_game(const CommandLineArguments& args) session->get_current_sector().get_players()[0]->set_pos(*g_config->tux_spawn_pos); } + session->restart_level(); m_screen_manager->push_screen(std::move(session)); } } diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 275913e9bd..fe141e666e 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -113,16 +113,18 @@ TitleScreen::refresh_level() std::unique_ptr new_session; try { - new_session = std::make_unique(title_level, m_savegame, nullptr, true); + new_session = std::make_unique(title_level, m_savegame, nullptr); } catch (const std::exception& err) { log_warning << "Error loading custom title screen level '" << title_level << "': " << err.what() << std::endl; if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL) - new_session = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true); + { + new_session = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr); + } } - + new_session->restart_level(false, true); if (new_session) { m_titlesession = std::move(new_session); @@ -132,7 +134,8 @@ TitleScreen::refresh_level() } else if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL) { - m_titlesession = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true); + m_titlesession = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr); + m_titlesession->restart_level(false, true); level_init = true; } diff --git a/src/worldmap/worldmap_sector.cpp b/src/worldmap/worldmap_sector.cpp index b7a637eac0..31b15f2198 100644 --- a/src/worldmap/worldmap_sector.cpp +++ b/src/worldmap/worldmap_sector.cpp @@ -354,10 +354,13 @@ WorldMapSector::update(float dt_sec) Vector shrinkpos = Vector(level_->get_pos().x + 16 - m_camera->get_offset().x, level_->get_pos().y + 8 - m_camera->get_offset().y); std::string levelfile = m_parent.m_levels_path + level_->get_level_filename(); + + auto game_session = std::make_unique(levelfile, m_parent.m_savegame, &level_->get_statistics()); + game_session->restart_level(); // update state and savegame m_parent.save_state(); - ScreenManager::current()->push_screen(std::make_unique(levelfile, m_parent.m_savegame, &level_->get_statistics()), + ScreenManager::current()->push_screen(std::move(game_session), std::make_unique(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1)); m_parent.m_in_level = true;