2 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "supertux/title_screen.hpp"
20 #include "audio/sound_manager.hpp"
21 #include "gui/menu_manager.hpp"
22 #include "lisp/parser.hpp"
23 #include "object/camera.hpp"
24 #include "object/player.hpp"
25 #include "supertux/fadeout.hpp"
26 #include "supertux/gameconfig.hpp"
27 #include "supertux/globals.hpp"
28 #include "supertux/screen_manager.hpp"
29 #include "supertux/menu/addon_menu.hpp"
30 #include "supertux/menu/contrib_world_menu.hpp"
31 #include "supertux/menu/contrib_menu.hpp"
32 #include "supertux/menu/main_menu.hpp"
33 #include "supertux/resources.hpp"
34 #include "supertux/sector.hpp"
35 #include "supertux/textscroller.hpp"
36 #include "supertux/world.hpp"
37 #include "util/file_system.hpp"
38 #include "util/gettext.hpp"
39 #include "util/reader.hpp"
40 #include "video/drawing_context.hpp"
45 TitleScreen::TitleScreen(PlayerStatus* player_status) :
46 main_menu(new MainMenu()),
51 controller.reset(new CodeController());
52 titlesession.reset(new GameSession("levels/misc/menu.stl", player_status));
54 Player* player = titlesession->get_current_sector()->player;
55 player->set_controller(controller.get());
56 player->set_speedlimit(230); //MAX_WALK_XM
58 frame = Surface::create("images/engine/menu/frame.png");
59 copyright_text = "SuperTux " PACKAGE_VERSION "\n" +
60 _("Copyright") + " (c) 2003-2014 SuperTux Devel Team\n" +
61 _("This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
62 "redistribute it under certain conditions; see the file COPYING for details.\n"
67 TitleScreen::get_level_name(const std::string& filename)
71 const lisp::Lisp* root = parser.parse(filename);
73 const lisp::Lisp* level = root->get_lisp("supertux-level");
78 level->get("name", name);
80 } catch(std::exception& e) {
81 log_warning << "Problem getting name of '" << filename << "': "
82 << e.what() << std::endl;
88 TitleScreen::make_tux_jump()
90 static bool jumpWasReleased = true;
91 Sector* sector = titlesession->get_current_sector();
92 Player* tux = sector->player;
95 controller->press(Controller::RIGHT);
97 // Check if we should press the jump button
98 Rectf lookahead = tux->get_bbox();
100 bool pathBlocked = !sector->is_free_of_statics(lookahead);
101 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
102 controller->press(Controller::JUMP);
103 jumpWasReleased = false;
105 jumpWasReleased = true;
108 // Wrap around at the end of the level back to the beginning
109 if(sector->get_width() - 320 < tux->get_pos().x) {
110 sector->activate("main");
111 sector->camera->reset(tux->get_pos());
115 TitleScreen::~TitleScreen()
122 Sector* sector = titlesession->get_current_sector();
123 if(Sector::current() != sector) {
124 sector->play_music(LEVEL_MUSIC);
125 sector->activate(sector->player->get_pos());
128 MenuManager::instance().set_current(main_menu.get());
134 Sector* sector = titlesession->get_current_sector();
135 sector->deactivate();
136 MenuManager::instance().set_current(nullptr);
140 TitleScreen::draw(DrawingContext& context)
142 Sector* sector = titlesession->get_current_sector();
143 sector->draw(context);
145 // FIXME: Add something to scale the frame to the resolution of the screen
146 //context.draw_surface(frame, Vector(0,0),LAYER_FOREGROUND1);
148 context.draw_text(Resources::small_font,
150 Vector(5, SCREEN_HEIGHT - 50),
151 ALIGN_LEFT, LAYER_FOREGROUND1);
155 TitleScreen::update(float elapsed_time)
157 g_screen_manager->set_speed(0.6f);
158 Sector* sector = titlesession->get_current_sector();
159 sector->update(elapsed_time);
163 if (Menu* menu = MenuManager::instance().current())
168 // reopen menu if user closed it (so that the app doesn't close when user
169 // accidently hit ESC)
170 if(MenuManager::instance().current() == 0 && g_screen_manager->has_no_pending_fadeout())
172 MenuManager::instance().set_current(main_menu.get());
177 TitleScreen::start_game(World* world)
179 MenuManager::instance().set_current(NULL);
181 std::string basename = world->get_basedir();
182 basename = basename.substr(0, basename.length()-1);
183 std::string worlddirname = FileSystem::basename(basename);
184 std::ostringstream stream;
185 stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
186 std::string slotfile = stream.str();
190 world->set_savegame_filename(slotfile);
193 catch(std::exception& e)
195 log_fatal << "Couldn't start world: " << e.what() << std::endl;