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 "addon/addon_manager.hpp"
26 #include "audio/sound_manager.hpp"
27 #include "gui/menu.hpp"
28 #include "gui/menu_manager.hpp"
29 #include "gui/menu_item.hpp"
30 #include "lisp/parser.hpp"
31 #include "object/camera.hpp"
32 #include "object/player.hpp"
33 #include "supertux/fadeout.hpp"
34 #include "supertux/gameconfig.hpp"
35 #include "supertux/globals.hpp"
36 #include "supertux/mainloop.hpp"
37 #include "supertux/menu/menu_storage.hpp"
38 #include "supertux/menu/addon_menu.hpp"
39 #include "supertux/menu/contrib_world_menu.hpp"
40 #include "supertux/menu/contrib_menu.hpp"
41 #include "supertux/menu/main_menu.hpp"
42 #include "supertux/menu/options_menu.hpp"
43 #include "supertux/resources.hpp"
44 #include "supertux/sector.hpp"
45 #include "supertux/textscroller.hpp"
46 #include "supertux/world.hpp"
47 #include "util/file_system.hpp"
48 #include "util/gettext.hpp"
49 #include "util/reader.hpp"
50 #include "video/drawing_context.hpp"
52 TitleScreen::TitleScreen() :
65 controller.reset(new CodeController());
66 titlesession.reset(new GameSession("levels/misc/menu.stl"));
68 Player* player = titlesession->get_current_sector()->player;
69 player->set_controller(controller.get());
70 player->set_speedlimit(230); //MAX_WALK_XM
74 frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
78 TitleScreen::update_load_game_menu()
83 TitleScreen::free_contrib_menu()
85 for(std::vector<World*>::iterator i = contrib_worlds.begin();
86 i != contrib_worlds.end(); ++i)
89 contrib_worlds.clear();
93 TitleScreen::generate_contrib_menu()
95 /** Generating contrib levels list by making use of Level Subset */
96 std::vector<std::string> level_worlds;
97 char** files = PHYSFS_enumerateFiles("levels/");
98 for(const char* const* filename = files; *filename != 0; ++filename) {
99 std::string filepath = std::string("levels/") + *filename;
100 if(PHYSFS_isDirectory(filepath.c_str()))
101 level_worlds.push_back(filepath);
103 PHYSFS_freeList(files);
106 contrib_menu.reset(new ContribMenu(level_worlds,
111 TitleScreen::get_level_name(const std::string& filename)
115 const lisp::Lisp* root = parser.parse(filename);
117 const lisp::Lisp* level = root->get_lisp("supertux-level");
122 level->get("name", name);
124 } catch(std::exception& e) {
125 log_warning << "Problem getting name of '" << filename << "': "
126 << e.what() << std::endl;
132 TitleScreen::check_levels_contrib_menu()
134 int index = contrib_menu->check();
138 current_world = contrib_worlds[index];
140 if(!current_world->is_levelset) {
143 contrib_world_menu.reset(new ContribWorldMenu(*current_world));
144 MenuManager::push_current(contrib_world_menu.get());
149 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
151 return a1->title < a2->title;
156 TitleScreen::generate_addons_menu()
158 AddonManager& adm = AddonManager::get_instance();
160 // refresh list of addons
161 addons = adm.get_addons();
164 std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
168 addons_menu.reset(new AddonMenu(addons));
172 TitleScreen::check_addons_menu()
174 int index = addons_menu->check();
175 if (index == -1) return;
177 // check if "Check Online" was chosen
180 AddonManager::get_instance().check_online();
181 generate_addons_menu();
182 MenuManager::set_current(addons_menu.get());
183 addons_menu->set_active_item(index);
185 catch (std::runtime_error e) {
186 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
191 // if one of the Addons listed was chosen, take appropriate action
192 if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
193 Addon& addon = *addons[index - ADDON_LIST_START_ID];
194 if (!addon.installed) {
196 AddonManager::get_instance().install(&addon);
198 catch (std::runtime_error e) {
199 log_warning << "Installing Add-on failed: " << e.what() << std::endl;
201 addons_menu->set_toggled(index, addon.loaded);
202 } else if (!addon.loaded) {
204 AddonManager::get_instance().enable(&addon);
206 catch (std::runtime_error e) {
207 log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
209 addons_menu->set_toggled(index, addon.loaded);
212 AddonManager::get_instance().disable(&addon);
214 catch (std::runtime_error e) {
215 log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
217 addons_menu->set_toggled(index, addon.loaded);
223 TitleScreen::free_addons_menu()
228 TitleScreen::make_tux_jump()
230 static bool jumpWasReleased = true;
231 Sector* sector = titlesession->get_current_sector();
232 Player* tux = sector->player;
234 controller->update();
235 controller->press(Controller::RIGHT);
237 // Check if we should press the jump button
238 Rect lookahead = tux->get_bbox();
239 lookahead.p2.x += 96;
240 bool pathBlocked = !sector->is_free_of_statics(lookahead);
241 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
242 controller->press(Controller::JUMP);
243 jumpWasReleased = false;
245 jumpWasReleased = true;
248 // Wrap around at the end of the level back to the beginning
249 if(sector->get_width() - 320 < tux->get_pos().x) {
250 sector->activate("main");
251 sector->camera->reset(tux->get_pos());
256 TitleScreen::generate_main_menu()
258 main_menu.reset(new MainMenu());
261 TitleScreen::~TitleScreen()
268 player_status->reset();
270 Sector* sector = titlesession->get_current_sector();
271 if(Sector::current() != sector) {
272 sector->play_music(LEVEL_MUSIC);
273 sector->activate(sector->player->get_pos());
276 MenuManager::set_current(main_menu.get());
282 Sector* sector = titlesession->get_current_sector();
283 sector->deactivate();
284 MenuManager::set_current(NULL);
288 TitleScreen::draw(DrawingContext& context)
290 Sector* sector = titlesession->get_current_sector();
291 sector->draw(context);
293 // FIXME: Add something to scale the frame to the resolution of the screen
294 context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
296 context.draw_text(Resources::small_font, "SuperTux " PACKAGE_VERSION "\n",
297 Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
298 context.draw_text(Resources::small_font,
300 "Copyright (c) 2007 SuperTux Devel Team\n"
301 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
302 "redistribute it under certain conditions; see the file COPYING for details.\n"
304 Vector(5, SCREEN_HEIGHT - 50 + Resources::small_font->get_height() + 5),
305 ALIGN_LEFT, LAYER_FOREGROUND1);
309 TitleScreen::update(float elapsed_time)
311 g_main_loop->set_speed(0.6f);
312 Sector* sector = titlesession->get_current_sector();
313 sector->update(elapsed_time);
317 Menu* menu = MenuManager::current();
319 if(menu == main_menu.get()) {
320 switch (main_menu->check()) {
322 // Start Game, ie. goto the slots menu
323 if(main_world.get() == NULL) {
324 main_world.reset(new World());
325 main_world->load("levels/world1/info");
327 current_world = main_world.get();
331 case MNID_LEVELS_CONTRIB:
333 generate_contrib_menu();
334 MenuManager::push_current(contrib_menu.get());
339 generate_addons_menu();
340 MenuManager::push_current(addons_menu.get());
344 MenuManager::set_current(NULL);
345 g_main_loop->push_screen(new TextScroller("credits.txt"),
349 case MNID_QUITMAINMENU:
350 g_main_loop->quit(new FadeOut(0.25));
351 sound_manager->stop_music(0.25);
354 } else if(menu == contrib_menu.get()) {
355 check_levels_contrib_menu();
356 } else if(menu == addons_menu.get()) {
358 } else if (menu == contrib_world_menu.get()) {
359 contrib_world_menu->check_menu();
363 // reopen menu if user closed it (so that the app doesn't close when user
364 // accidently hit ESC)
365 if(MenuManager::current() == 0 && g_main_loop->has_no_pending_fadeout()) {
366 generate_main_menu();
367 MenuManager::set_current(main_menu.get());
372 TitleScreen::start_game()
374 MenuManager::set_current(NULL);
375 std::string basename = current_world->get_basedir();
376 basename = basename.substr(0, basename.length()-1);
377 std::string worlddirname = FileSystem::basename(basename);
378 std::ostringstream stream;
379 stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
380 std::string slotfile = stream.str();
383 current_world->set_savegame_filename(slotfile);
384 current_world->run();
385 } catch(std::exception& e) {
386 log_fatal << "Couldn't start world: " << e.what() << std::endl;