4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 #include <SDL_image.h>
37 #include "gameconfig.hpp"
39 #include "mainloop.hpp"
40 #include "video/drawing_context.hpp"
41 #include "video/surface.hpp"
42 #include "audio/sound_manager.hpp"
43 #include "gui/menu.hpp"
45 #include "lisp/lisp.hpp"
46 #include "lisp/parser.hpp"
49 #include "game_session.hpp"
50 #include "worldmap/worldmap.hpp"
51 #include "player_status.hpp"
54 #include "object/tilemap.hpp"
55 #include "object/camera.hpp"
56 #include "object/player.hpp"
57 #include "resources.hpp"
58 #include "gettext.hpp"
59 #include "textscroller.hpp"
60 #include "fadeout.hpp"
61 #include "file_system.hpp"
62 #include "control/joystickkeyboardcontroller.hpp"
63 #include "control/codecontroller.hpp"
66 #include "options_menu.hpp"
67 #include "console.hpp"
68 #include "random_generator.hpp"
69 #include "addon_manager.hpp"
82 TitleScreen::update_load_game_menu()
84 load_game_menu.reset(new Menu());
86 load_game_menu->add_label(_("Start Game"));
87 load_game_menu->add_hl();
88 for(int i = 1; i <= 5; ++i) {
89 load_game_menu->add_entry(i, get_slotinfo(i));
91 load_game_menu->add_hl();
92 load_game_menu->add_back(_("Back"));
96 TitleScreen::free_contrib_menu()
98 for(std::vector<World*>::iterator i = contrib_worlds.begin();
99 i != contrib_worlds.end(); ++i)
102 contrib_worlds.clear();
106 TitleScreen::generate_contrib_menu()
108 /** Generating contrib levels list by making use of Level Subset */
109 std::vector<std::string> level_worlds;
110 char** files = PHYSFS_enumerateFiles("levels/");
111 for(const char* const* filename = files; *filename != 0; ++filename) {
112 std::string filepath = std::string("levels/") + *filename;
113 if(PHYSFS_isDirectory(filepath.c_str()))
114 level_worlds.push_back(filepath);
116 PHYSFS_freeList(files);
119 contrib_menu.reset(new Menu());
121 contrib_menu->add_label(_("Contrib Levels"));
122 contrib_menu->add_hl();
125 for (std::vector<std::string>::iterator it = level_worlds.begin();
126 it != level_worlds.end(); ++it) {
128 std::auto_ptr<World> world (new World());
129 world->load(*it + "/info");
130 if(world->hide_from_contribs) {
133 contrib_menu->add_entry(i++, world->title);
134 contrib_worlds.push_back(world.release());
135 } catch(std::exception& e) {
137 log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
142 contrib_menu->add_hl();
143 contrib_menu->add_back(_("Back"));
147 TitleScreen::get_level_name(const std::string& filename)
151 const lisp::Lisp* root = parser.parse(filename);
153 const lisp::Lisp* level = root->get_lisp("supertux-level");
158 level->get("name", name);
160 } catch(std::exception& e) {
161 log_warning << "Problem getting name of '" << filename << "': "
162 << e.what() << std::endl;
168 TitleScreen::check_levels_contrib_menu()
170 int index = contrib_menu->check();
174 current_world = contrib_worlds[index];
176 if(!current_world->is_levelset) {
177 update_load_game_menu();
178 Menu::push_current(load_game_menu.get());
180 contrib_world_menu.reset(new Menu());
182 contrib_world_menu->add_label(current_world->title);
183 contrib_world_menu->add_hl();
185 for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
187 /** get level's title */
188 std::string filename = current_world->get_level_filename(i);
189 std::string title = get_level_name(filename);
190 contrib_world_menu->add_entry(i, title);
193 contrib_world_menu->add_hl();
194 contrib_world_menu->add_back(_("Back"));
196 Menu::push_current(contrib_world_menu.get());
201 TitleScreen::check_contrib_world_menu()
203 int index = contrib_world_menu->check();
205 if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
206 sound_manager->stop_music();
207 GameSession* session =
208 new GameSession(current_world->get_level_filename(index));
209 main_loop->push_screen(session);
215 bool generate_addons_menu_sorter(const Addon& a1, const Addon& a2)
217 return a1.title < a2.title;
220 const int ADDON_LIST_START_ID = 10;
224 TitleScreen::generate_addons_menu()
226 AddonManager& adm = AddonManager::get_instance();
228 // refresh list of installed addons
229 installed_addons = adm.get_installed_addons();
231 // build new Add-on list
234 // add installed addons to list
235 addons.insert(addons.end(), installed_addons.begin(), installed_addons.end());
237 // add available addons to list
238 addons.insert(addons.end(), available_addons.begin(), available_addons.end());
241 std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
243 // remove available addons that are already installed
244 std::vector<Addon>::iterator it2 = addons.begin();
245 while (it2 != addons.end()) {
247 if (addon.isInstalled) {
248 bool restart = false;
249 for (std::vector<Addon>::iterator it = addons.begin(); it != addons.end(); ++it) {
251 if ((addon2.equals(addon)) && (!addon2.isInstalled)) {
258 it2 = addons.begin();
267 addons_menu.reset(new Menu());
269 addons_menu->add_label(_("Add-ons"));
270 addons_menu->add_hl();
273 addons_menu->add_entry(0, std::string(_("Check Online")));
275 addons_menu->add_deactive(0, std::string(_("Check Online (disabled)")));
278 //addons_menu->add_hl();
280 for (unsigned int i = 0; i < addons.size(); i++) {
281 Addon addon = addons[i];
282 std::string text = "";
283 if (addon.kind != "") text += addon.kind + " ";
284 text += std::string("\"") + addon.title + "\"";
285 if (addon.author != "") text += " by \"" + addon.author + "\"";
286 addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.isInstalled);
289 addons_menu->add_hl();
290 addons_menu->add_back(_("Back"));
294 TitleScreen::check_addons_menu()
296 int index = addons_menu->check();
297 if (index == -1) return;
299 // check if "Check Online" was chosen
302 available_addons = AddonManager::get_instance().get_available_addons();
303 generate_addons_menu();
304 Menu::set_current(addons_menu.get());
305 addons_menu->set_active_item(index);
307 catch (std::runtime_error e) {
308 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
313 // if one of the Addons listed was chosen, take appropriate action
314 if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
315 Addon addon = addons[index - ADDON_LIST_START_ID];
316 if (!addon.isInstalled) {
319 //generate_addons_menu();
320 //Menu::set_current(addons_menu.get());
321 //addons_menu->set_active_item(index);
322 Menu::set_current(0);
324 catch (std::runtime_error e) {
325 log_warning << "Installation of Add-on failed: " << e.what() << std::endl;
330 //generate_addons_menu();
331 //Menu::set_current(addons_menu.get());
332 //addons_menu->set_active_item(index);
333 Menu::set_current(0);
335 catch (std::runtime_error e) {
336 log_warning << "Removal of Add-on failed: " << e.what() << std::endl;
344 TitleScreen::free_addons_menu()
349 TitleScreen::make_tux_jump()
351 static bool jumpWasReleased = true;
352 Sector* sector = titlesession->get_current_sector();
353 Player* tux = sector->player;
355 controller->update();
356 controller->press(Controller::RIGHT);
358 // Check if we should press the jump button
359 Rect lookahead = tux->get_bbox();
360 lookahead.p2.x += 96;
361 bool pathBlocked = !sector->is_free_of_statics(lookahead);
362 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
363 controller->press(Controller::JUMP);
364 jumpWasReleased = false;
366 jumpWasReleased = true;
369 // Wrap around at the end of the level back to the beginnig
370 if(sector->get_width() - 320 < tux->get_pos().x) {
371 sector->activate("main");
372 sector->camera->reset(tux->get_pos());
376 TitleScreen::TitleScreen()
378 controller.reset(new CodeController());
379 titlesession.reset(new GameSession("levels/misc/menu.stl"));
381 Player* player = titlesession->get_current_sector()->player;
382 player->set_controller(controller.get());
383 player->set_speedlimit(230); //MAX_WALK_XM
385 generate_main_menu();
389 TitleScreen::generate_main_menu()
391 main_menu.reset(new Menu());
392 main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
393 main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
394 main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
395 main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
396 main_menu->add_submenu(_("Options"), get_options_menu());
397 main_menu->add_entry(MNID_CREDITS, _("Credits"));
398 main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
401 TitleScreen::~TitleScreen()
408 player_status->reset();
410 Sector* sector = titlesession->get_current_sector();
411 if(Sector::current() != sector) {
412 sector->play_music(LEVEL_MUSIC);
413 sector->activate(sector->player->get_pos());
416 Menu::set_current(main_menu.get());
422 Sector* sector = titlesession->get_current_sector();
423 sector->deactivate();
424 Menu::set_current(NULL);
428 TitleScreen::draw(DrawingContext& context)
430 Sector* sector = titlesession->get_current_sector();
431 sector->draw(context);
433 context.draw_text(white_small_text, "SuperTux " PACKAGE_VERSION "\n",
434 Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
435 context.draw_text(white_small_text,
437 "Copyright (c) 2007 SuperTux Devel Team\n"
438 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
439 "redistribute it under certain conditions; see the file COPYING for details.\n"
441 Vector(5, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
442 ALIGN_LEFT, LAYER_FOREGROUND1);
446 TitleScreen::update(float elapsed_time)
448 main_loop->set_speed(0.6f);
449 Sector* sector = titlesession->get_current_sector();
450 sector->update(elapsed_time);
454 Menu* menu = Menu::current();
458 if(menu == main_menu.get()) {
459 switch (main_menu->check()) {
461 // Start Game, ie. goto the slots menu
462 if(main_world.get() == NULL) {
463 main_world.reset(new World());
464 main_world->load("levels/world1/info");
466 current_world = main_world.get();
467 update_load_game_menu();
468 Menu::push_current(load_game_menu.get());
470 case MNID_LEVELS_CONTRIB:
472 generate_contrib_menu();
473 Menu::push_current(contrib_menu.get());
477 generate_addons_menu();
478 Menu::push_current(addons_menu.get());
481 main_loop->push_screen(new TextScroller("credits.txt"),
484 case MNID_QUITMAINMENU:
485 main_loop->quit(new FadeOut(0.25));
486 sound_manager->stop_music(0.25);
489 } else if(menu == load_game_menu.get()) {
491 if(event.key.keysym.sym == SDLK_DELETE) {
492 int slot = menu->get_active_item_id();
493 std::stringstream stream;
495 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
497 if(confirm_dialog(bkg_title, str.c_str())) {
498 str = "profile1/slot" + stream.str() + ".stsg";
499 log_debug << "Removing: " << str << std::endl;
500 PHYSFS_delete(str.c_str());
503 update_load_save_game_menu(load_game_menu);
504 Menu::set_current(main_menu.get());
506 process_load_game_menu();
507 } else if(menu == contrib_menu.get()) {
508 check_levels_contrib_menu();
509 } else if(menu == addons_menu.get()) {
511 } else if (menu == contrib_world_menu.get()) {
512 check_contrib_world_menu();
516 // reopen menu of user closed it (so that the app doesn't close when user
517 // accidently hit ESC)
518 if(Menu::current() == 0) {
519 generate_main_menu();
520 Menu::set_current(main_menu.get());
525 TitleScreen::get_slotinfo(int slot)
530 std::string basename = current_world->get_basedir();
531 basename = basename.substr(0, basename.length()-1);
532 std::string worlddirname = FileSystem::basename(basename);
533 std::ostringstream stream;
534 stream << "profile" << config->profile << "/" << worlddirname << "_" << slot << ".stsg";
535 std::string slotfile = stream.str();
539 const lisp::Lisp* root = parser.parse(slotfile);
541 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
543 throw std::runtime_error("file is not a supertux-savegame.");
545 savegame->get("title", title);
546 } catch(std::exception& ) {
547 std::ostringstream slottitle;
548 slottitle << _("Slot") << " " << slot << " - " << _("Free");
549 return slottitle.str();
552 std::ostringstream slottitle;
553 slottitle << _("Slot") << " " << slot << " - " << title;
554 return slottitle.str();
558 TitleScreen::process_load_game_menu()
560 int slot = load_game_menu->check();
565 if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
568 std::string basename = current_world->get_basedir();
569 basename = basename.substr(0, basename.length()-1);
570 std::string worlddirname = FileSystem::basename(basename);
571 std::ostringstream stream;
572 stream << "profile" << config->profile << "/" << worlddirname << "_" << slot << ".stsg";
573 std::string slotfile = stream.str();
576 current_world->set_savegame_filename(slotfile);
577 current_world->run();
578 } catch(std::exception& e) {
579 log_fatal << "Couldn't start world: " << e.what() << std::endl;