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/addon_manager.hpp"
82 TitleScreen::update_load_game_menu()
87 TitleScreen::free_contrib_menu()
89 for(std::vector<World*>::iterator i = contrib_worlds.begin();
90 i != contrib_worlds.end(); ++i)
93 contrib_worlds.clear();
97 TitleScreen::generate_contrib_menu()
99 /** Generating contrib levels list by making use of Level Subset */
100 std::vector<std::string> level_worlds;
101 char** files = PHYSFS_enumerateFiles("levels/");
102 for(const char* const* filename = files; *filename != 0; ++filename) {
103 std::string filepath = std::string("levels/") + *filename;
104 if(PHYSFS_isDirectory(filepath.c_str()))
105 level_worlds.push_back(filepath);
107 PHYSFS_freeList(files);
110 contrib_menu.reset(new Menu());
112 contrib_menu->add_label(_("Contrib Levels"));
113 contrib_menu->add_hl();
116 for (std::vector<std::string>::iterator it = level_worlds.begin();
117 it != level_worlds.end(); ++it) {
119 std::auto_ptr<World> world (new World());
120 world->load(*it + "/info");
121 if(world->hide_from_contribs) {
124 contrib_menu->add_entry(i++, world->title);
125 contrib_worlds.push_back(world.release());
126 } catch(std::exception& e) {
128 log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
133 contrib_menu->add_hl();
134 contrib_menu->add_back(_("Back"));
138 TitleScreen::get_level_name(const std::string& filename)
142 const lisp::Lisp* root = parser.parse(filename);
144 const lisp::Lisp* level = root->get_lisp("supertux-level");
149 level->get("name", name);
151 } catch(std::exception& e) {
152 log_warning << "Problem getting name of '" << filename << "': "
153 << e.what() << std::endl;
159 TitleScreen::check_levels_contrib_menu()
161 int index = contrib_menu->check();
165 current_world = contrib_worlds[index];
167 if(!current_world->is_levelset) {
170 contrib_world_menu.reset(new Menu());
172 contrib_world_menu->add_label(current_world->title);
173 contrib_world_menu->add_hl();
175 for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
177 /** get level's title */
178 std::string filename = current_world->get_level_filename(i);
179 std::string title = get_level_name(filename);
180 contrib_world_menu->add_entry(i, title);
183 contrib_world_menu->add_hl();
184 contrib_world_menu->add_back(_("Back"));
186 Menu::push_current(contrib_world_menu.get());
191 TitleScreen::check_contrib_world_menu()
193 int index = contrib_world_menu->check();
195 if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
196 sound_manager->stop_music();
197 GameSession* session =
198 new GameSession(current_world->get_level_filename(index));
199 main_loop->push_screen(session);
205 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
207 return a1->title < a2->title;
210 const int ADDON_LIST_START_ID = 10;
214 TitleScreen::generate_addons_menu()
216 AddonManager& adm = AddonManager::get_instance();
218 // refresh list of addons
219 addons = adm.get_addons();
222 std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
226 addons_menu.reset(new Menu());
228 addons_menu->add_label(_("Add-ons"));
229 addons_menu->add_hl();
232 addons_menu->add_entry(0, std::string(_("Check Online")));
234 addons_menu->add_inactive(0, std::string(_("Check Online (disabled)")));
237 //addons_menu->add_hl();
239 for (unsigned int i = 0; i < addons.size(); i++) {
240 const Addon& addon = *addons[i];
241 std::string text = "";
242 if (addon.kind != "") text += addon.kind + " ";
243 text += std::string("\"") + addon.title + "\"";
244 if (addon.author != "") text += " by \"" + addon.author + "\"";
245 addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
248 addons_menu->add_hl();
249 addons_menu->add_back(_("Back"));
253 TitleScreen::check_addons_menu()
255 int index = addons_menu->check();
256 if (index == -1) return;
258 // check if "Check Online" was chosen
261 AddonManager::get_instance().check_online();
262 generate_addons_menu();
263 Menu::set_current(addons_menu.get());
264 addons_menu->set_active_item(index);
266 catch (std::runtime_error e) {
267 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
272 // if one of the Addons listed was chosen, take appropriate action
273 if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
274 Addon& addon = *addons[index - ADDON_LIST_START_ID];
275 if (!addon.installed) {
277 AddonManager::get_instance().install(&addon);
279 catch (std::runtime_error e) {
280 log_warning << "Installing Add-on failed: " << e.what() << std::endl;
282 addons_menu->set_toggled(index, addon.loaded);
283 } else if (!addon.loaded) {
285 AddonManager::get_instance().enable(&addon);
287 catch (std::runtime_error e) {
288 log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
290 addons_menu->set_toggled(index, addon.loaded);
293 AddonManager::get_instance().disable(&addon);
295 catch (std::runtime_error e) {
296 log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
298 addons_menu->set_toggled(index, addon.loaded);
304 TitleScreen::free_addons_menu()
309 TitleScreen::make_tux_jump()
311 static bool jumpWasReleased = true;
312 Sector* sector = titlesession->get_current_sector();
313 Player* tux = sector->player;
315 controller->update();
316 controller->press(Controller::RIGHT);
318 // Check if we should press the jump button
319 Rect lookahead = tux->get_bbox();
320 lookahead.p2.x += 96;
321 bool pathBlocked = !sector->is_free_of_statics(lookahead);
322 if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
323 controller->press(Controller::JUMP);
324 jumpWasReleased = false;
326 jumpWasReleased = true;
329 // Wrap around at the end of the level back to the beginning
330 if(sector->get_width() - 320 < tux->get_pos().x) {
331 sector->activate("main");
332 sector->camera->reset(tux->get_pos());
336 TitleScreen::TitleScreen()
338 controller.reset(new CodeController());
339 titlesession.reset(new GameSession("levels/misc/menu.stl"));
341 Player* player = titlesession->get_current_sector()->player;
342 player->set_controller(controller.get());
343 player->set_speedlimit(230); //MAX_WALK_XM
345 generate_main_menu();
347 frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
351 TitleScreen::generate_main_menu()
353 main_menu.reset(new Menu());
354 main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
355 main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
356 main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
357 main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
358 main_menu->add_submenu(_("Options"), get_options_menu());
359 main_menu->add_entry(MNID_CREDITS, _("Credits"));
360 main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
363 TitleScreen::~TitleScreen()
370 player_status->reset();
372 Sector* sector = titlesession->get_current_sector();
373 if(Sector::current() != sector) {
374 sector->play_music(LEVEL_MUSIC);
375 sector->activate(sector->player->get_pos());
378 Menu::set_current(main_menu.get());
384 Sector* sector = titlesession->get_current_sector();
385 sector->deactivate();
386 Menu::set_current(NULL);
390 TitleScreen::draw(DrawingContext& context)
392 Sector* sector = titlesession->get_current_sector();
393 sector->draw(context);
395 // FIXME: Add something to scale the frame to the resolution of the screen
396 context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
398 context.draw_text(white_small_text, "SuperTux " PACKAGE_VERSION "\n",
399 Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
400 context.draw_text(white_small_text,
402 "Copyright (c) 2007 SuperTux Devel Team\n"
403 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
404 "redistribute it under certain conditions; see the file COPYING for details.\n"
406 Vector(5, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
407 ALIGN_LEFT, LAYER_FOREGROUND1);
411 TitleScreen::update(float elapsed_time)
413 main_loop->set_speed(0.6f);
414 Sector* sector = titlesession->get_current_sector();
415 sector->update(elapsed_time);
419 Menu* menu = Menu::current();
423 if(menu == main_menu.get()) {
424 switch (main_menu->check()) {
426 // Start Game, ie. goto the slots menu
427 if(main_world.get() == NULL) {
428 main_world.reset(new World());
429 main_world->load("levels/world1/info");
431 current_world = main_world.get();
435 case MNID_LEVELS_CONTRIB:
437 generate_contrib_menu();
438 Menu::push_current(contrib_menu.get());
443 generate_addons_menu();
444 Menu::push_current(addons_menu.get());
448 main_loop->push_screen(new TextScroller("credits.txt"),
452 case MNID_QUITMAINMENU:
453 main_loop->quit(new FadeOut(0.25));
454 sound_manager->stop_music(0.25);
457 } else if(menu == contrib_menu.get()) {
458 check_levels_contrib_menu();
459 } else if(menu == addons_menu.get()) {
461 } else if (menu == contrib_world_menu.get()) {
462 check_contrib_world_menu();
466 // reopen menu of user closed it (so that the app doesn't close when user
467 // accidently hit ESC)
468 if(Menu::current() == 0) {
469 generate_main_menu();
470 Menu::set_current(main_menu.get());
475 TitleScreen::start_game()
477 std::string basename = current_world->get_basedir();
478 basename = basename.substr(0, basename.length()-1);
479 std::string worlddirname = FileSystem::basename(basename);
480 std::ostringstream stream;
481 stream << "profile" << config->profile << "/" << worlddirname << ".stsg";
482 std::string slotfile = stream.str();
485 current_world->set_savegame_filename(slotfile);
486 current_world->run();
487 } catch(std::exception& e) {
488 log_fatal << "Couldn't start world: " << e.what() << std::endl;