New grow and skid sounds from remaxim
[supertux.git] / src / title.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
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.
11 //
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.
16 //
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
20 //  02111-1307, USA.
21
22 #include <config.h>
23 #include <version.h>
24
25 #include "title.hpp"
26
27 #include "addon/addon_manager.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "fadeout.hpp"
30 #include "file_system.hpp"
31 #include "gameconfig.hpp"
32 #include "gettext.hpp"
33 #include "gui/menu.hpp"
34 #include "lisp/parser.hpp"
35 #include "log.hpp"
36 #include "main.hpp"
37 #include "mainloop.hpp"
38 #include "object/camera.hpp"
39 #include "object/player.hpp"
40 #include "options_menu.hpp"
41 #include "resources.hpp"
42 #include "sector.hpp"
43 #include "textscroller.hpp"
44 #include "video/drawing_context.hpp"
45 #include "world.hpp"
46
47 #include <algorithm>
48 #include <physfs.h>
49
50 enum MainMenuIDs {
51   MNID_STARTGAME,
52   MNID_LEVELS_CONTRIB,
53   MNID_ADDONS,
54   MNID_OPTIONMENU,
55   MNID_LEVELEDITOR,
56   MNID_CREDITS,
57   MNID_QUITMAINMENU
58 };
59
60 void
61 TitleScreen::update_load_game_menu()
62 {
63 }
64
65 void
66 TitleScreen::free_contrib_menu()
67 {
68   for(std::vector<World*>::iterator i = contrib_worlds.begin();
69       i != contrib_worlds.end(); ++i)
70     delete *i;
71
72   contrib_worlds.clear();
73 }
74
75 void
76 TitleScreen::generate_contrib_menu()
77 {
78   /** Generating contrib levels list by making use of Level Subset  */
79   std::vector<std::string> level_worlds;
80   char** files = PHYSFS_enumerateFiles("levels/");
81   for(const char* const* filename = files; *filename != 0; ++filename) {
82     std::string filepath = std::string("levels/") + *filename;
83     if(PHYSFS_isDirectory(filepath.c_str()))
84       level_worlds.push_back(filepath);
85   }
86   PHYSFS_freeList(files);
87
88   free_contrib_menu();
89   contrib_menu.reset(new Menu());
90
91   contrib_menu->add_label(_("Contrib Levels"));
92   contrib_menu->add_hl();
93
94   int i = 0;
95   for (std::vector<std::string>::iterator it = level_worlds.begin();
96       it != level_worlds.end(); ++it) {
97     try {
98       std::auto_ptr<World> world (new World());
99       world->load(*it + "/info");
100       if(world->hide_from_contribs) {
101         continue;
102       }
103       contrib_menu->add_entry(i++, world->title);
104       contrib_worlds.push_back(world.release());
105     } catch(std::exception& e) {
106       log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
107     }
108   }
109
110   contrib_menu->add_hl();
111   contrib_menu->add_back(_("Back"));
112 }
113
114 std::string
115 TitleScreen::get_level_name(const std::string& filename)
116 {
117   try {
118     lisp::Parser parser;
119     const lisp::Lisp* root = parser.parse(filename);
120
121     const lisp::Lisp* level = root->get_lisp("supertux-level");
122     if(!level)
123       return "";
124
125     std::string name;
126     level->get("name", name);
127     return name;
128   } catch(std::exception& e) {
129       log_warning << "Problem getting name of '" << filename << "': "
130                   << e.what() << std::endl;
131     return "";
132   }
133 }
134
135 void
136 TitleScreen::check_levels_contrib_menu()
137 {
138   int index = contrib_menu->check();
139   if (index == -1)
140     return;
141
142   current_world = contrib_worlds[index];
143
144   if(!current_world->is_levelset) {
145     start_game();
146   } else {
147     contrib_world_menu.reset(new Menu());
148
149     contrib_world_menu->add_label(current_world->title);
150     contrib_world_menu->add_hl();
151
152     for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
153     {
154       /** get level's title */
155       std::string filename = current_world->get_level_filename(i);
156       std::string title = get_level_name(filename);
157       contrib_world_menu->add_entry(i, title);
158     }
159
160     contrib_world_menu->add_hl();
161     contrib_world_menu->add_back(_("Back"));
162
163     Menu::push_current(contrib_world_menu.get());
164   }
165 }
166
167 void
168 TitleScreen::check_contrib_world_menu()
169 {
170   int index = contrib_world_menu->check();
171   if (index != -1) {
172     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
173       sound_manager->stop_music();
174       GameSession* session =
175         new GameSession(current_world->get_level_filename(index));
176       main_loop->push_screen(session);
177     }
178   }
179 }
180
181 namespace {
182   bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
183   {
184     return a1->title < a2->title;
185   }
186
187   const int ADDON_LIST_START_ID = 10;
188 }
189
190 void
191 TitleScreen::generate_addons_menu()
192 {
193   AddonManager& adm = AddonManager::get_instance();
194
195   // refresh list of addons
196   addons = adm.get_addons();
197   
198   // sort list
199   std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
200
201   // (re)generate menu
202   free_addons_menu();
203   addons_menu.reset(new Menu());
204
205   addons_menu->add_label(_("Add-ons"));
206   addons_menu->add_hl();
207   
208 #ifdef HAVE_LIBCURL
209   addons_menu->add_entry(0, std::string(_("Check Online")));
210 #else
211   addons_menu->add_inactive(0, std::string(_("Check Online (disabled)")));
212 #endif
213
214   //addons_menu->add_hl();
215
216   for (unsigned int i = 0; i < addons.size(); i++) {
217     const Addon& addon = *addons[i];
218     std::string text = "";
219     if (addon.kind != "") text += addon.kind + " ";
220     text += std::string("\"") + addon.title + "\"";
221     if (addon.author != "") text += " by \"" + addon.author + "\"";
222     addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
223   }
224
225   addons_menu->add_hl();
226   addons_menu->add_back(_("Back"));
227 }
228
229 void
230 TitleScreen::check_addons_menu()
231 {
232   int index = addons_menu->check();
233   if (index == -1) return;
234
235   // check if "Check Online" was chosen
236   if (index == 0) {
237     try {
238       AddonManager::get_instance().check_online();
239       generate_addons_menu();
240       Menu::set_current(addons_menu.get());
241       addons_menu->set_active_item(index);
242     } 
243     catch (std::runtime_error e) {
244       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
245     }
246     return;
247   }
248
249   // if one of the Addons listed was chosen, take appropriate action
250   if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
251     Addon& addon = *addons[index - ADDON_LIST_START_ID];
252     if (!addon.installed) {
253       try {
254         AddonManager::get_instance().install(&addon);
255       } 
256       catch (std::runtime_error e) {
257         log_warning << "Installing Add-on failed: " << e.what() << std::endl;
258       }
259       addons_menu->set_toggled(index, addon.loaded);
260     } else if (!addon.loaded) {
261       try {
262         AddonManager::get_instance().enable(&addon);
263       } 
264       catch (std::runtime_error e) {
265         log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
266       }
267       addons_menu->set_toggled(index, addon.loaded);
268     } else {
269       try {
270         AddonManager::get_instance().disable(&addon);
271       } 
272       catch (std::runtime_error e) {
273         log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
274       }
275       addons_menu->set_toggled(index, addon.loaded);
276     }
277   }
278 }
279
280 void
281 TitleScreen::free_addons_menu()
282 {
283 }
284
285 void
286 TitleScreen::make_tux_jump()
287 {
288   static bool jumpWasReleased = true;
289   Sector* sector  = titlesession->get_current_sector();
290   Player* tux = sector->player;
291
292   controller->update();
293   controller->press(Controller::RIGHT);
294
295   // Check if we should press the jump button
296   Rect lookahead = tux->get_bbox();
297   lookahead.p2.x += 96;
298   bool pathBlocked = !sector->is_free_of_statics(lookahead);
299   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
300     controller->press(Controller::JUMP);
301     jumpWasReleased = false;
302   } else {
303     jumpWasReleased = true;
304   }
305
306   // Wrap around at the end of the level back to the beginning
307   if(sector->get_width() - 320 < tux->get_pos().x) {
308     sector->activate("main");
309     sector->camera->reset(tux->get_pos());
310   }
311 }
312
313 TitleScreen::TitleScreen()
314 {
315   controller.reset(new CodeController());
316   titlesession.reset(new GameSession("levels/misc/menu.stl"));
317
318   Player* player = titlesession->get_current_sector()->player;
319   player->set_controller(controller.get());
320   player->set_speedlimit(230); //MAX_WALK_XM
321
322   generate_main_menu();
323
324   frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
325 }
326
327 void
328 TitleScreen::generate_main_menu()
329 {
330   main_menu.reset(new Menu());
331   main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
332   main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
333   main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
334   main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
335   main_menu->add_submenu(_("Options"), get_options_menu());
336   main_menu->add_entry(MNID_CREDITS, _("Credits"));
337   main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
338 }
339
340 TitleScreen::~TitleScreen()
341 {
342 }
343
344 void
345 TitleScreen::setup()
346 {
347   player_status->reset();
348
349   Sector* sector = titlesession->get_current_sector();
350   if(Sector::current() != sector) {
351     sector->play_music(LEVEL_MUSIC);
352     sector->activate(sector->player->get_pos());
353   }
354
355   Menu::set_current(main_menu.get());
356 }
357
358 void
359 TitleScreen::leave()
360 {
361   Sector* sector = titlesession->get_current_sector();
362   sector->deactivate();
363   Menu::set_current(NULL);
364 }
365
366 void
367 TitleScreen::draw(DrawingContext& context)
368 {
369   Sector* sector  = titlesession->get_current_sector();
370   sector->draw(context);
371
372   // FIXME: Add something to scale the frame to the resolution of the screen
373   context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
374
375   context.draw_text(small_font, "SuperTux " PACKAGE_VERSION "\n",
376       Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
377   context.draw_text(small_font,
378       _(
379 "Copyright (c) 2007 SuperTux Devel Team\n"
380 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
381 "redistribute it under certain conditions; see the file COPYING for details.\n"
382 ),
383       Vector(5, SCREEN_HEIGHT - 50 + small_font->get_height() + 5),
384       ALIGN_LEFT, LAYER_FOREGROUND1);
385 }
386
387 void
388 TitleScreen::update(float elapsed_time)
389 {
390   main_loop->set_speed(0.6f);
391   Sector* sector  = titlesession->get_current_sector();
392   sector->update(elapsed_time);
393
394   make_tux_jump();
395
396   Menu* menu = Menu::current();
397   if(menu) {
398     if(menu == main_menu.get()) {
399       switch (main_menu->check()) {
400         case MNID_STARTGAME:
401           // Start Game, ie. goto the slots menu
402           if(main_world.get() == NULL) {
403             main_world.reset(new World());
404             main_world->load("levels/world1/info");
405           }
406           current_world = main_world.get();
407           start_game();
408           break;
409
410         case MNID_LEVELS_CONTRIB:
411           // Contrib Menu
412           generate_contrib_menu();
413           Menu::push_current(contrib_menu.get());
414           break;
415
416         case MNID_ADDONS:
417           // Add-ons Menu
418           generate_addons_menu();
419           Menu::push_current(addons_menu.get());
420           break;
421
422         case MNID_CREDITS:
423           Menu::set_current(NULL);
424           main_loop->push_screen(new TextScroller("credits.txt"),
425                                  new FadeOut(0.5));
426           break;
427
428         case MNID_QUITMAINMENU:
429           main_loop->quit(new FadeOut(0.25));
430           sound_manager->stop_music(0.25);
431           break;
432       }
433     } else if(menu == contrib_menu.get()) {
434       check_levels_contrib_menu();
435     } else if(menu == addons_menu.get()) {
436       check_addons_menu();
437     } else if (menu == contrib_world_menu.get()) {
438       check_contrib_world_menu();
439     }
440   }
441
442   // reopen menu if user closed it (so that the app doesn't close when user
443   // accidently hit ESC)
444   if(Menu::current() == 0 && main_loop->has_no_pending_fadeout()) {
445     generate_main_menu();
446     Menu::set_current(main_menu.get());
447   }
448 }
449
450 void
451 TitleScreen::start_game()
452 {
453   Menu::set_current(NULL);
454   std::string basename = current_world->get_basedir();
455   basename = basename.substr(0, basename.length()-1);
456   std::string worlddirname = FileSystem::basename(basename);
457   std::ostringstream stream;
458   stream << "profile" << config->profile << "/" << worlddirname << ".stsg";
459   std::string slotfile = stream.str();
460
461   try {
462     current_world->set_savegame_filename(slotfile);
463     current_world->run();
464   } catch(std::exception& e) {
465     log_fatal << "Couldn't start world: " << e.what() << std::endl;
466   }
467 }