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