From 340248155dbafeadc719682e8bd551b6e4852afd Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Thu, 28 Nov 2024 23:36:19 -0500 Subject: [PATCH 1/9] Don't call GameSession::restart_level in the constructor This stops testing/the title screen (yes, the title screen) from reparsing the level by simply changing the usage pattern of the TitleScreen. (Cherry picked from swagtoy/supertux:editor2) --- src/supertux/game_session.cpp | 3 --- src/supertux/levelset_screen.cpp | 4 +++- src/supertux/main.cpp | 2 +- src/supertux/title_screen.cpp | 2 +- src/worldmap/worldmap_sector.cpp | 5 ++++- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 375ee469fd4..cf898302632 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -94,9 +94,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 diff --git a/src/supertux/levelset_screen.cpp b/src/supertux/levelset_screen.cpp index fb87b19e0f1..6ab9b3b991b 100644 --- a/src/supertux/levelset_screen.cpp +++ b/src/supertux/levelset_screen.cpp @@ -27,6 +27,8 @@ #include "util/file_system.hpp" #include "util/log.hpp" +// TODO: wtf????????? + LevelsetScreen::LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame, const std::optional>& start_pos) : @@ -88,8 +90,8 @@ LevelsetScreen::setup() m_savegame); if (m_start_pos) { screen->set_start_pos(m_start_pos->first, m_start_pos->second); - screen->restart_level(); } + screen->restart_level(); ScreenManager::current()->push_screen(std::move(screen)); } } diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index 6c814866696..e0a7a76ffb8 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 275913e9bd2..5896be0089a 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -122,7 +122,6 @@ TitleScreen::refresh_level() if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL) new_session = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true); } - if (new_session) { m_titlesession = std::move(new_session); @@ -135,6 +134,7 @@ TitleScreen::refresh_level() m_titlesession = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true); level_init = true; } + m_titlesession->restart_level(); /** Initialize the main sector. */ Sector& sector = m_titlesession->get_current_sector(); diff --git a/src/worldmap/worldmap_sector.cpp b/src/worldmap/worldmap_sector.cpp index b7a637eac0e..93f9ebf3c24 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 gamesession = std::make_unique(levelfile, m_parent.m_savegame, &level_->get_statistics()); + gamesession->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(gamesession), std::make_unique(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1)); m_parent.m_in_level = true; From 0592b0cfd9e033ce31a141e989d5e3f5b9d546d2 Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Fri, 29 Nov 2024 00:49:41 -0500 Subject: [PATCH 2/9] Fix TitleScreen initialization --- src/supertux/title_screen.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 5896be0089a..5977556b852 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -120,8 +120,11 @@ TitleScreen::refresh_level() 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->restart_level(); if (new_session) { m_titlesession = std::move(new_session); @@ -132,9 +135,9 @@ 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->restart_level(); level_init = true; } - m_titlesession->restart_level(); /** Initialize the main sector. */ Sector& sector = m_titlesession->get_current_sector(); From f1b63116360b1263527ca8e773a096db7ab003ab Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Fri, 29 Nov 2024 12:37:08 -0500 Subject: [PATCH 3/9] Fix constructor and music preservation --- src/supertux/game_session.cpp | 3 +-- src/supertux/game_session.hpp | 3 +-- src/supertux/levelset_screen.cpp | 2 -- src/supertux/title_screen.cpp | 10 +++++----- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index cf898302632..305b24fdd62 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -55,8 +55,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), diff --git a/src/supertux/game_session.hpp b/src/supertux/game_session.hpp index f2b81edd479..d9d9bbea54e 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; diff --git a/src/supertux/levelset_screen.cpp b/src/supertux/levelset_screen.cpp index 6ab9b3b991b..a809cd2f86e 100644 --- a/src/supertux/levelset_screen.cpp +++ b/src/supertux/levelset_screen.cpp @@ -27,8 +27,6 @@ #include "util/file_system.hpp" #include "util/log.hpp" -// TODO: wtf????????? - LevelsetScreen::LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame, const std::optional>& start_pos) : diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 5977556b852..fe141e666e4 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -113,7 +113,7 @@ 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) { @@ -121,10 +121,10 @@ TitleScreen::refresh_level() 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(); + new_session->restart_level(false, true); if (new_session) { m_titlesession = std::move(new_session); @@ -134,8 +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->restart_level(); + m_titlesession = std::make_unique(DEFAULT_TITLE_LEVEL, m_savegame, nullptr); + m_titlesession->restart_level(false, true); level_init = true; } From 135b4dec78e6eb848fc12d6a5265f9893a2efc02 Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Fri, 29 Nov 2024 13:09:50 -0500 Subject: [PATCH 4/9] Use exceptions for GameSession::restart_level instead of int --- src/supertux/game_session.cpp | 11 +++++------ src/supertux/game_session.hpp | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 305b24fdd62..adf499d33d5 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" @@ -151,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(); @@ -234,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()) @@ -247,9 +249,8 @@ GameSession::restart_level(bool after_death, bool preserve_music) } } catch (std::exception& e) { - log_fatal << "Couldn't start level: " << e.what() << std::endl; + throw std::runtime_error(fmt::format("Couldn't start level: {}", e.what())); ScreenManager::current()->pop_screen(); - return (-1); } if (m_levelintro_shown) @@ -277,8 +278,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 d9d9bbea54e..57332f538b3 100644 --- a/src/supertux/game_session.hpp +++ b/src/supertux/game_session.hpp @@ -130,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(); From 814be4f311b31a07bbf410eed71851e76856a6f2 Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Fri, 29 Nov 2024 13:48:45 -0500 Subject: [PATCH 5/9] Don't use fmt for basic append --- src/supertux/game_session.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index adf499d33d5..606834b053f 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -249,7 +249,7 @@ GameSession::restart_level(bool after_death, bool preserve_music) } } catch (std::exception& e) { - throw std::runtime_error(fmt::format("Couldn't start level: {}", e.what())); + throw std::runtime_error(std::string("Couldn't start level: ") + e.what()); ScreenManager::current()->pop_screen(); } From d0e72542f0a89e28f486c85badd4c128b97961d5 Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Tue, 3 Dec 2024 15:23:54 -0500 Subject: [PATCH 6/9] Handle restart_level errors --- src/supertux/levelset_screen.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/supertux/levelset_screen.cpp b/src/supertux/levelset_screen.cpp index a809cd2f86e..110a03579cd 100644 --- a/src/supertux/levelset_screen.cpp +++ b/src/supertux/levelset_screen.cpp @@ -26,6 +26,7 @@ #include "supertux/screen_manager.hpp" #include "util/file_system.hpp" #include "util/log.hpp" +#include LevelsetScreen::LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame, @@ -89,8 +90,17 @@ LevelsetScreen::setup() if (m_start_pos) { screen->set_start_pos(m_start_pos->first, m_start_pos->second); } - screen->restart_level(); - ScreenManager::current()->push_screen(std::move(screen)); + + 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(); + } } } } From fe660b2ed82f6ec9d84b401375b610efefc574db Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Tue, 3 Dec 2024 15:33:47 -0500 Subject: [PATCH 7/9] Remove SDL_atomic.h inclusion Emac --- src/supertux/levelset_screen.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/supertux/levelset_screen.cpp b/src/supertux/levelset_screen.cpp index 110a03579cd..3e11998e11f 100644 --- a/src/supertux/levelset_screen.cpp +++ b/src/supertux/levelset_screen.cpp @@ -26,7 +26,6 @@ #include "supertux/screen_manager.hpp" #include "util/file_system.hpp" #include "util/log.hpp" -#include LevelsetScreen::LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame, From 6ca5bb854a639019139de3ab0d943dd5dc03083b Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Tue, 3 Dec 2024 15:39:43 -0500 Subject: [PATCH 8/9] gamesession -> game_session --- src/worldmap/worldmap_sector.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/worldmap/worldmap_sector.cpp b/src/worldmap/worldmap_sector.cpp index 93f9ebf3c24..31b15f2198a 100644 --- a/src/worldmap/worldmap_sector.cpp +++ b/src/worldmap/worldmap_sector.cpp @@ -355,12 +355,12 @@ WorldMapSector::update(float dt_sec) level_->get_pos().y + 8 - m_camera->get_offset().y); std::string levelfile = m_parent.m_levels_path + level_->get_level_filename(); - auto gamesession = std::make_unique(levelfile, m_parent.m_savegame, &level_->get_statistics()); - gamesession->restart_level(); + 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::move(gamesession), + ScreenManager::current()->push_screen(std::move(game_session), std::make_unique(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1)); m_parent.m_in_level = true; From 134a1ae65e1c33f3ee36eb6c3c8d84b16c3b5eb4 Mon Sep 17 00:00:00 2001 From: Swagtoy Date: Tue, 3 Dec 2024 17:49:45 -0500 Subject: [PATCH 9/9] Don't pop the GameSession anymore --- src/supertux/game_session.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 606834b053f..58a60bbdb43 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -250,7 +250,6 @@ GameSession::restart_level(bool after_death, bool preserve_music) } catch (std::exception& e) { throw std::runtime_error(std::string("Couldn't start level: ") + e.what()); - ScreenManager::current()->pop_screen(); } if (m_levelintro_shown)