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