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 "mainloop.hpp"
38 #include "video/drawing_context.hpp"
39 #include "video/surface.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "gui/menu.hpp"
43 #include "lisp/lisp.hpp"
44 #include "lisp/parser.hpp"
47 #include "game_session.hpp"
48 #include "worldmap/worldmap.hpp"
49 #include "player_status.hpp"
52 #include "object/tilemap.hpp"
53 #include "object/camera.hpp"
54 #include "object/player.hpp"
55 #include "resources.hpp"
56 #include "gettext.hpp"
57 #include "textscroller.hpp"
58 #include "fadeout.hpp"
59 #include "file_system.hpp"
60 #include "control/joystickkeyboardcontroller.hpp"
61 #include "control/codecontroller.hpp"
64 #include "options_menu.hpp"
65 #include "console.hpp"
66 #include "random_generator.hpp"
67 #include "addon_manager.hpp"
80 TitleScreen::update_load_game_menu()
82 load_game_menu.reset(new Menu());
84 load_game_menu->add_label(_("Start Game"));
85 load_game_menu->add_hl();
86 for(int i = 1; i <= 5; ++i) {
87 load_game_menu->add_entry(i, get_slotinfo(i));
89 load_game_menu->add_hl();
90 load_game_menu->add_back(_("Back"));
94 TitleScreen::free_contrib_menu()
96 for(std::vector<World*>::iterator i = contrib_worlds.begin();
97 i != contrib_worlds.end(); ++i)
100 contrib_worlds.clear();
104 TitleScreen::generate_contrib_menu()
106 /** Generating contrib levels list by making use of Level Subset */
107 std::vector<std::string> level_worlds;
108 char** files = PHYSFS_enumerateFiles("levels/");
109 for(const char* const* filename = files; *filename != 0; ++filename) {
110 std::string filepath = std::string("levels/") + *filename;
111 if(PHYSFS_isDirectory(filepath.c_str()))
112 level_worlds.push_back(filepath);
114 PHYSFS_freeList(files);
117 contrib_menu.reset(new Menu());
119 contrib_menu->add_label(_("Contrib Levels"));
120 contrib_menu->add_hl();
123 for (std::vector<std::string>::iterator it = level_worlds.begin();
124 it != level_worlds.end(); ++it) {
126 std::auto_ptr<World> world (new World());
127 world->load(*it + "/info");
128 if(world->hide_from_contribs) {
131 contrib_menu->add_entry(i++, world->title);
132 contrib_worlds.push_back(world.release());
133 } catch(std::exception& e) {
135 log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
140 contrib_menu->add_hl();
141 contrib_menu->add_back(_("Back"));
145 TitleScreen::get_level_name(const std::string& filename)
149 const lisp::Lisp* root = parser.parse(filename);
151 const lisp::Lisp* level = root->get_lisp("supertux-level");
156 level->get("name", name);
158 } catch(std::exception& e) {
159 log_warning << "Problem getting name of '" << filename << "': "
160 << e.what() << std::endl;
166 TitleScreen::check_levels_contrib_menu()
168 int index = contrib_menu->check();
172 current_world = contrib_worlds[index];
174 if(!current_world->is_levelset) {
175 update_load_game_menu();
176 Menu::push_current(load_game_menu.get());
178 contrib_world_menu.reset(new Menu());
180 contrib_world_menu->add_label(current_world->title);
181 contrib_world_menu->add_hl();
183 for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
185 /** get level's title */
186 std::string filename = current_world->get_level_filename(i);
187 std::string title = get_level_name(filename);
188 contrib_world_menu->add_entry(i, title);
191 contrib_world_menu->add_hl();
192 contrib_world_menu->add_back(_("Back"));
194 Menu::push_current(contrib_world_menu.get());
199 TitleScreen::check_contrib_world_menu()
201 int index = contrib_world_menu->check();
203 if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
204 sound_manager->stop_music();
205 GameSession* session =
206 new GameSession(current_world->get_level_filename(index));
207 main_loop->push_screen(session);
213 bool generate_addons_menu_sorter(const Addon& a1, const Addon& a2)
215 return a1.title < a2.title;
218 const int ADDON_LIST_START_ID = 10;
222 TitleScreen::generate_addons_menu()
224 AddonManager& adm = AddonManager::get_instance();
226 // refresh list of installed addons
227 installed_addons = adm.get_installed_addons();
229 // build new Add-on list
232 // add installed addons to list
233 addons.insert(addons.end(), installed_addons.begin(), installed_addons.end());
235 // add available addons to list
236 addons.insert(addons.end(), available_addons.begin(), available_addons.end());
239 std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
241 // remove available addons that are already installed
242 std::vector<Addon>::iterator it2 = addons.begin();
243 while (it2 != addons.end()) {
245 if (addon.isInstalled) {
246 bool restart = false;
247 for (std::vector<Addon>::iterator it = addons.begin(); it != addons.end(); ++it) {
249 if ((addon2.equals(addon)) && (!addon2.isInstalled)) {
256 it2 = addons.begin();
265 addons_menu.reset(new Menu());
267 addons_menu->add_label(_("Add-ons"));
268 addons_menu->add_hl();
271 addons_menu->add_entry(0, std::string(_("Check Online")));
273 addons_menu->add_deactive(0, std::string(_("Check Online (disabled)")));
276 //addons_menu->add_hl();
278 for (unsigned int i = 0; i < addons.size(); i++) {
279 Addon addon = addons[i];
280 std::string text = "";
281 if (addon.kind != "") text += addon.kind + " ";
282 text += std::string("\"") + addon.title + "\"";
283 if (addon.author != "") text += " by \"" + addon.author + "\"";
284 addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.isInstalled);
287 addons_menu->add_hl();
288 addons_menu->add_back(_("Back"));
292 TitleScreen::check_addons_menu()
294 int index = addons_menu->check();
295 if (index == -1) return;
297 // check if "Check Online" was chosen
299 available_addons = AddonManager::get_instance().get_available_addons();
300 generate_addons_menu();
301 Menu::set_current(addons_menu.get());
302 addons_menu->set_active_item(index);
306 // if one of the Addons listed was chosen, take appropriate action
307 if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
308 Addon addon = addons[index - ADDON_LIST_START_ID];
309 if (!addon.isInstalled) {
311 generate_addons_menu();
312 Menu::set_current(addons_menu.get());
313 addons_menu->set_active_item(index);
316 generate_addons_menu();
317 Menu::set_current(addons_menu.get());
318 addons_menu->set_active_item(index);
325 TitleScreen::free_addons_menu()
330 TitleScreen::make_tux_jump()
332 static bool jumpWasReleased = true;
333 Sector* sector = titlesession->get_current_sector();
334 Player* tux = sector->player;
336 controller->update();
337 controller->press(Controller::RIGHT);
339 // Check if we should press the jump button
340 Rect lookahead = tux->get_bbox();
341 lookahead.p2.x += 96;
342 bool pathBlocked = !sector->is_free_of_statics(lookahead);
343 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
344 controller->press(Controller::JUMP);
345 jumpWasReleased = false;
347 jumpWasReleased = true;
350 // Wrap around at the end of the level back to the beginnig
351 if(sector->get_width() - 320 < tux->get_pos().x) {
352 sector->activate("main");
353 sector->camera->reset(tux->get_pos());
357 TitleScreen::TitleScreen()
359 controller.reset(new CodeController());
360 titlesession.reset(new GameSession("levels/misc/menu.stl"));
362 Player* player = titlesession->get_current_sector()->player;
363 player->set_controller(controller.get());
364 player->set_speedlimit(230); //MAX_WALK_XM
366 main_menu.reset(new Menu());
367 main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
368 main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
369 main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
370 main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
371 main_menu->add_submenu(_("Options"), get_options_menu());
372 main_menu->add_entry(MNID_CREDITS, _("Credits"));
373 main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
376 TitleScreen::~TitleScreen()
383 player_status->reset();
385 Sector* sector = titlesession->get_current_sector();
386 if(Sector::current() != sector) {
387 sector->play_music(LEVEL_MUSIC);
388 sector->activate(sector->player->get_pos());
391 Menu::set_current(main_menu.get());
397 Sector* sector = titlesession->get_current_sector();
398 sector->deactivate();
399 Menu::set_current(NULL);
403 TitleScreen::draw(DrawingContext& context)
405 Sector* sector = titlesession->get_current_sector();
406 sector->draw(context);
408 context.draw_text(white_small_text, "SuperTux " PACKAGE_VERSION "\n",
409 Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
410 context.draw_text(white_small_text,
412 "Copyright (c) 2007 SuperTux Devel Team\n"
413 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
414 "redistribute it under certain conditions; see the file COPYING for details.\n"
416 Vector(5, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
417 ALIGN_LEFT, LAYER_FOREGROUND1);
421 TitleScreen::update(float elapsed_time)
423 main_loop->set_speed(0.6f);
424 Sector* sector = titlesession->get_current_sector();
425 sector->update(elapsed_time);
429 Menu* menu = Menu::current();
433 if(menu == main_menu.get()) {
434 switch (main_menu->check()) {
436 // Start Game, ie. goto the slots menu
437 if(main_world.get() == NULL) {
438 main_world.reset(new World());
439 main_world->load("levels/world1/info");
441 current_world = main_world.get();
442 update_load_game_menu();
443 Menu::push_current(load_game_menu.get());
445 case MNID_LEVELS_CONTRIB:
447 generate_contrib_menu();
448 Menu::push_current(contrib_menu.get());
452 generate_addons_menu();
453 Menu::push_current(addons_menu.get());
456 main_loop->push_screen(new TextScroller("credits.txt"),
459 case MNID_QUITMAINMENU:
460 main_loop->quit(new FadeOut(0.25));
463 } else if(menu == load_game_menu.get()) {
465 if(event.key.keysym.sym == SDLK_DELETE) {
466 int slot = menu->get_active_item_id();
467 std::stringstream stream;
469 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
471 if(confirm_dialog(bkg_title, str.c_str())) {
472 str = "save/slot" + stream.str() + ".stsg";
473 log_debug << "Removing: " << str << std::endl;
474 PHYSFS_delete(str.c_str());
477 update_load_save_game_menu(load_game_menu);
478 Menu::set_current(main_menu.get());
480 process_load_game_menu();
481 } else if(menu == contrib_menu.get()) {
482 check_levels_contrib_menu();
483 } else if(menu == addons_menu.get()) {
485 } else if (menu == contrib_world_menu.get()) {
486 check_contrib_world_menu();
490 // reopen menu of user closed it (so that the app doesn't close when user
491 // accidently hit ESC)
492 if(Menu::current() == 0) {
493 Menu::set_current(main_menu.get());
498 TitleScreen::get_slotinfo(int slot)
503 std::string basename = current_world->get_basedir();
504 basename = basename.substr(0, basename.length()-1);
505 std::string worlddirname = FileSystem::basename(basename);
506 std::ostringstream stream;
507 stream << "save/" << worlddirname << "_" << slot << ".stsg";
508 std::string slotfile = stream.str();
512 const lisp::Lisp* root = parser.parse(slotfile);
514 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
516 throw std::runtime_error("file is not a supertux-savegame.");
518 savegame->get("title", title);
519 } catch(std::exception& ) {
520 std::ostringstream slottitle;
521 slottitle << _("Slot") << " " << slot << " - " << _("Free");
522 return slottitle.str();
525 std::ostringstream slottitle;
526 slottitle << _("Slot") << " " << slot << " - " << title;
527 return slottitle.str();
531 TitleScreen::process_load_game_menu()
533 int slot = load_game_menu->check();
538 if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
541 std::string basename = current_world->get_basedir();
542 basename = basename.substr(0, basename.length()-1);
543 std::string worlddirname = FileSystem::basename(basename);
544 std::stringstream stream;
545 stream << "save/" << worlddirname << "_" << slot << ".stsg";
546 std::string slotfile = stream.str();
549 current_world->set_savegame_filename(slotfile);
550 current_world->run();
551 } catch(std::exception& e) {
552 log_fatal << "Couldn't start world: " << e.what() << std::endl;