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/>.
20 #include "supertux/title_screen.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "gui/menu.hpp"
27 #include "gui/menu_manager.hpp"
28 #include "gui/menu_item.hpp"
29 #include "lisp/parser.hpp"
30 #include "object/camera.hpp"
31 #include "object/player.hpp"
32 #include "supertux/fadeout.hpp"
33 #include "supertux/gameconfig.hpp"
34 #include "supertux/globals.hpp"
35 #include "supertux/mainloop.hpp"
36 #include "supertux/menu/menu_storage.hpp"
37 #include "supertux/menu/addon_menu.hpp"
38 #include "supertux/menu/contrib_world_menu.hpp"
39 #include "supertux/menu/contrib_menu.hpp"
40 #include "supertux/menu/main_menu.hpp"
41 #include "supertux/menu/options_menu.hpp"
42 #include "supertux/resources.hpp"
43 #include "supertux/sector.hpp"
44 #include "supertux/textscroller.hpp"
45 #include "supertux/world.hpp"
46 #include "util/file_system.hpp"
47 #include "util/gettext.hpp"
48 #include "util/reader.hpp"
49 #include "video/drawing_context.hpp"
51 TitleScreen::TitleScreen() :
63 controller.reset(new CodeController());
64 titlesession.reset(new GameSession("levels/misc/menu.stl"));
66 Player* player = titlesession->get_current_sector()->player;
67 player->set_controller(controller.get());
68 player->set_speedlimit(230); //MAX_WALK_XM
72 frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
76 TitleScreen::get_level_name(const std::string& filename)
80 const lisp::Lisp* root = parser.parse(filename);
82 const lisp::Lisp* level = root->get_lisp("supertux-level");
87 level->get("name", name);
89 } catch(std::exception& e) {
90 log_warning << "Problem getting name of '" << filename << "': "
91 << e.what() << std::endl;
97 TitleScreen::check_levels_contrib_menu()
99 current_world = contrib_menu->get_current_world();
103 if (!current_world->is_levelset)
109 contrib_world_menu.reset(new ContribWorldMenu(*current_world));
110 MenuManager::push_current(contrib_world_menu.get());
116 TitleScreen::make_tux_jump()
118 static bool jumpWasReleased = true;
119 Sector* sector = titlesession->get_current_sector();
120 Player* tux = sector->player;
122 controller->update();
123 controller->press(Controller::RIGHT);
125 // Check if we should press the jump button
126 Rect lookahead = tux->get_bbox();
127 lookahead.p2.x += 96;
128 bool pathBlocked = !sector->is_free_of_statics(lookahead);
129 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
130 controller->press(Controller::JUMP);
131 jumpWasReleased = false;
133 jumpWasReleased = true;
136 // Wrap around at the end of the level back to the beginning
137 if(sector->get_width() - 320 < tux->get_pos().x) {
138 sector->activate("main");
139 sector->camera->reset(tux->get_pos());
144 TitleScreen::generate_main_menu()
146 main_menu.reset(new MainMenu());
149 TitleScreen::~TitleScreen()
156 player_status->reset();
158 Sector* sector = titlesession->get_current_sector();
159 if(Sector::current() != sector) {
160 sector->play_music(LEVEL_MUSIC);
161 sector->activate(sector->player->get_pos());
164 MenuManager::set_current(main_menu.get());
170 Sector* sector = titlesession->get_current_sector();
171 sector->deactivate();
172 MenuManager::set_current(NULL);
176 TitleScreen::draw(DrawingContext& context)
178 Sector* sector = titlesession->get_current_sector();
179 sector->draw(context);
181 // FIXME: Add something to scale the frame to the resolution of the screen
182 context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
184 context.draw_text(Resources::small_font, "SuperTux " PACKAGE_VERSION "\n",
185 Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
186 context.draw_text(Resources::small_font,
188 "Copyright (c) 2007 SuperTux Devel Team\n"
189 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
190 "redistribute it under certain conditions; see the file COPYING for details.\n"
192 Vector(5, SCREEN_HEIGHT - 50 + Resources::small_font->get_height() + 5),
193 ALIGN_LEFT, LAYER_FOREGROUND1);
197 TitleScreen::update(float elapsed_time)
199 g_main_loop->set_speed(0.6f);
200 Sector* sector = titlesession->get_current_sector();
201 sector->update(elapsed_time);
205 Menu* menu = MenuManager::current();
207 if(menu == main_menu.get()) {
208 switch (main_menu->check()) {
210 // Start Game, ie. goto the slots menu
211 if(main_world.get() == NULL) {
212 main_world.reset(new World());
213 main_world->load("levels/world1/info");
215 current_world = main_world.get();
219 case MNID_LEVELS_CONTRIB:
221 contrib_menu.reset(new ContribMenu());
222 MenuManager::push_current(contrib_menu.get());
227 addons_menu.reset(new AddonMenu());
228 MenuManager::push_current(addons_menu.get());
232 MenuManager::set_current(NULL);
233 g_main_loop->push_screen(new TextScroller("credits.txt"),
237 case MNID_QUITMAINMENU:
238 g_main_loop->quit(new FadeOut(0.25));
239 sound_manager->stop_music(0.25);
242 } else if(menu == contrib_menu.get()) {
243 check_levels_contrib_menu();
244 } else if(menu == addons_menu.get()) {
245 addons_menu->check_menu();
246 } else if (menu == contrib_world_menu.get()) {
247 contrib_world_menu->check_menu();
251 // reopen menu if user closed it (so that the app doesn't close when user
252 // accidently hit ESC)
253 if(MenuManager::current() == 0 && g_main_loop->has_no_pending_fadeout()) {
254 generate_main_menu();
255 MenuManager::set_current(main_menu.get());
260 TitleScreen::start_game()
262 MenuManager::set_current(NULL);
263 std::string basename = current_world->get_basedir();
264 basename = basename.substr(0, basename.length()-1);
265 std::string worlddirname = FileSystem::basename(basename);
266 std::ostringstream stream;
267 stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
268 std::string slotfile = stream.str();
271 current_world->set_savegame_filename(slotfile);
272 current_world->run();
273 } catch(std::exception& e) {
274 log_fatal << "Couldn't start world: " << e.what() << std::endl;