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