Skip to content

Commit

Permalink
Don't call GameSession::restart_level in the constructor
Browse files Browse the repository at this point in the history
This stops testing (on the "Test from here")/the title screen (yes, the title screen) from reparsing the level by simply changing the usage pattern of the GameSession constructor.

Cherry picked from commit swagtoy@0d25b71 upon the request of Vankata.
  • Loading branch information
swagtoy authored Dec 3, 2024
1 parent b189f1e commit e34bc46
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
18 changes: 6 additions & 12 deletions src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "supertux/game_session.hpp"

#include <cfloat>
#include <fmt/format.h>
#include <stdexcept>

#include "audio/sound_manager.hpp"
#include "control/input_manager.hpp"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/supertux/game_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 10 additions & 1 deletion src/supertux/levelset_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/supertux/title_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ TitleScreen::refresh_level()
std::unique_ptr<GameSession> new_session;
try
{
new_session = std::make_unique<GameSession>(title_level, m_savegame, nullptr, true);
new_session = std::make_unique<GameSession>(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<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true);
{
new_session = std::make_unique<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr);
}
}

new_session->restart_level(false, true);
if (new_session)
{
m_titlesession = std::move(new_session);
Expand All @@ -132,7 +134,8 @@ TitleScreen::refresh_level()
}
else if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL)
{
m_titlesession = std::make_unique<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true);
m_titlesession = std::make_unique<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr);
m_titlesession->restart_level(false, true);
level_init = true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/worldmap/worldmap_sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameSession>(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<GameSession>(levelfile, m_parent.m_savegame, &level_->get_statistics()),
ScreenManager::current()->push_screen(std::move(game_session),
std::make_unique<ShrinkFade>(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1));

m_parent.m_in_level = true;
Expand Down

0 comments on commit e34bc46

Please sign in to comment.