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