--- /dev/null
+// SuperTux
+// Copyright (C) 2013 Ingo Ruhnke <grumbel@gmx.de>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include "supertux/game_manager.hpp"
+
+#include <sstream>
+
+#include "gui/menu_manager.hpp"
+#include "supertux/game_session.hpp"
+#include "supertux/gameconfig.hpp"
+#include "supertux/globals.hpp"
+#include "supertux/screen.hpp"
+#include "supertux/screen_fade.hpp"
+#include "supertux/screen_manager.hpp"
+#include "supertux/world.hpp"
+#include "util/file_system.hpp"
+#include "util/log.hpp"
+
+GameManager::GameManager() :
+ m_world()
+{
+}
+
+GameManager::~GameManager()
+{
+}
+
+void
+GameManager::start_level(std::unique_ptr<World> world, int index)
+{
+ m_world = std::move(world);
+
+ std::unique_ptr<Screen> screen(new GameSession(m_world->get_level_filename(index),
+ m_world->get_player_status()));
+ g_screen_manager->push_screen(std::move(screen));
+}
+
+void
+GameManager::start_game(std::unique_ptr<World> world)
+{
+ m_world = std::move(world);
+
+ MenuManager::instance().clear_menu_stack();
+
+ std::string basename = m_world->get_basedir();
+ basename = basename.substr(0, basename.length()-1);
+ std::string worlddirname = FileSystem::basename(basename);
+ std::ostringstream stream;
+ stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
+ std::string slotfile = stream.str();
+
+ try
+ {
+ m_world->set_savegame_filename(slotfile);
+ m_world->run();
+ }
+ catch(std::exception& e)
+ {
+ log_fatal << "Couldn't start world: " << e.what() << std::endl;
+ }
+}
+
+/* EOF */
--- /dev/null
+// SuperTux
+// Copyright (C) 2013 Ingo Ruhnke <grumbel@gmx.de>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP
+#define HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP
+
+#include <memory>
+
+#include "util/currenton.hpp"
+
+class World;
+
+class GameManager : public Currenton<GameManager>
+{
+private:
+ std::unique_ptr<World> m_world;
+
+public:
+ GameManager();
+ ~GameManager();
+
+ void start_game(std::unique_ptr<World> world);
+ void start_level(std::unique_ptr<World> world, int index);
+
+private:
+ GameManager(const GameManager&) = delete;
+ GameManager& operator=(const GameManager&) = delete;
+};
+
+#endif
+
+/* EOF */
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
+#include "supertux/main.hpp"
+
#include <config.h>
#include <version.h>
#include <findlocale.h>
}
-#include "video/renderer.hpp"
-#include "video/lightmap.hpp"
-#include "supertux/main.hpp"
-
#include "addon/addon_manager.hpp"
#include "audio/sound_manager.hpp"
#include "control/input_manager.hpp"
#include "physfs/physfs_file_system.hpp"
#include "physfs/physfs_sdl.hpp"
#include "scripting/squirrel_util.hpp"
+#include "supertux/game_manager.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/player_status.hpp"
#include "util/file_system.hpp"
#include "util/gettext.hpp"
#include "video/drawing_context.hpp"
+#include "video/lightmap.hpp"
+#include "video/renderer.hpp"
#include "worldmap/worldmap.hpp"
namespace { DrawingContext *context_pointer; }
const std::unique_ptr<PlayerStatus> default_playerstatus(new PlayerStatus());
+ GameManager game_manager;
g_screen_manager = new ScreenManager();
init_rand();
#include <physfs.h>
#include "gui/menu_manager.hpp"
+#include "supertux/game_manager.hpp"
#include "supertux/menu/contrib_world_menu.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "supertux/title_screen.hpp"
add_hl();
add_back(_("Back"));
-
- std::cout << "ContribMenu" << std::endl;
}
ContribMenu::~ContribMenu()
{
- std::cout << "~ContribMenu" << std::endl;
}
void
if (index != -1)
{
World* world = m_contrib_worlds[index].get();
-
if (!world->is_levelset)
{
- TitleScreen::start_game(world);
+ // FIXME: not the most elegant of solutions to std::move() the
+ // World, but the ContribMenu should get destructed after this,
+ // so it might be ok
+ GameManager::current()->start_game(std::move(m_contrib_worlds[index]));
}
else
{
- MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribWorldMenu(*world)));
+ MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribWorldMenu(std::move(m_contrib_worlds[index]))));
}
}
}
#include "audio/sound_manager.hpp"
#include "gui/menu_item.hpp"
+#include "supertux/game_manager.hpp"
#include "supertux/globals.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/world.hpp"
#include "util/gettext.hpp"
-ContribWorldMenu::ContribWorldMenu(const World& current_world) :
- m_current_world(current_world)
+ContribWorldMenu::ContribWorldMenu(std::unique_ptr<World> world) :
+ m_world(std::move(world))
{
- add_label(m_current_world.get_title());
+ add_label(m_world->get_title());
add_hl();
- for (unsigned int i = 0; i < m_current_world.get_num_levels(); ++i)
+ for (unsigned int i = 0; i < m_world->get_num_levels(); ++i)
{
/** get level's title */
- std::string filename = m_current_world.get_level_filename(i);
+ std::string filename = m_world->get_level_filename(i);
std::string title = TitleScreen::get_level_name(filename);
add_entry(i, title);
}
if (get_item_by_id(index).kind == MN_ACTION)
{
sound_manager->stop_music();
- g_screen_manager->push_screen(
- std::unique_ptr<Screen>(
- new GameSession(m_current_world.get_level_filename(index), m_current_world.get_player_status())));
+ GameManager::current()->start_level(std::move(m_world), index);
}
}
}
class ContribWorldMenu : public Menu
{
private:
- const World& m_current_world;
+ std::unique_ptr<World> m_world;
public:
- ContribWorldMenu(const World& current_world);
+ ContribWorldMenu(std::unique_ptr<World> current_world);
void check_menu();
#include "audio/sound_manager.hpp"
#include "gui/menu_manager.hpp"
#include "supertux/fadeout.hpp"
+#include "supertux/game_manager.hpp"
#include "supertux/globals.hpp"
-#include "supertux/menu/menu_storage.hpp"
#include "supertux/menu/addon_menu.hpp"
-#include "supertux/menu/options_menu.hpp"
#include "supertux/menu/contrib_menu.hpp"
+#include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/options_menu.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/textscroller.hpp"
#include "supertux/world.hpp"
#include "util/gettext.hpp"
-MainMenu::MainMenu() :
- m_main_world()
+MainMenu::MainMenu()
{
set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
add_entry(MNID_STARTGAME, _("Start Game"));
{
std::unique_ptr<World> world(new World);
world->load("levels/world1/info");
- TitleScreen::start_game(std::move(world));
+ GameManager::current()->start_game(std::move(world));
}
break;
}
}
-void
-TitleScreen::start_game(std::unique_ptr<World> world)
-{
- MenuManager::instance().clear_menu_stack();
-
- std::string basename = world->get_basedir();
- basename = basename.substr(0, basename.length()-1);
- std::string worlddirname = FileSystem::basename(basename);
- std::ostringstream stream;
- stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
- std::string slotfile = stream.str();
-
- try
- {
- world->set_savegame_filename(slotfile);
- world->run();
- }
- catch(std::exception& e)
- {
- log_fatal << "Couldn't start world: " << e.what() << std::endl;
- }
-}
-
/* EOF */
virtual void update(float elapsed_time);
-public:
- static void start_game(std::unique_ptr<World> world);
-
private:
void make_tux_jump();
void
World::run()
{
- using namespace scripting;
-
current_ = this;
// create new squirrel table for persistent game state
IFileStreambuf ins(filename);
std::istream in(&ins);
- sq_release(global_vm, &world_thread);
- world_thread = create_thread(global_vm);
- compile_and_run(object_to_vm(world_thread), in, filename);
+ sq_release(scripting::global_vm, &world_thread);
+ world_thread = scripting::create_thread(scripting::global_vm);
+ scripting::compile_and_run(scripting::object_to_vm(world_thread), in, filename);
} catch(std::exception& ) {
// fallback: try to load worldmap worldmap.stwm
using namespace worldmap;