d2ed7daf0f08077daec2e1307733e396fcd607a0
[supertux.git] / src / title.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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 #include <config.h>
22
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <cmath>
32 #include <SDL.h>
33 #include <SDL_image.h>
34 #include <physfs.h>
35
36 #include "title.hpp"
37 #include "mainloop.hpp"
38 #include "video/screen.hpp"
39 #include "video/drawing_context.hpp"
40 #include "video/surface.hpp"
41 #include "audio/sound_manager.hpp"
42 #include "gui/menu.hpp"
43 #include "timer.hpp"
44 #include "lisp/lisp.hpp"
45 #include "lisp/parser.hpp"
46 #include "level.hpp"
47 #include "world.hpp"
48 #include "game_session.hpp"
49 #include "worldmap.hpp"
50 #include "player_status.hpp"
51 #include "tile.hpp"
52 #include "sector.hpp"
53 #include "object/tilemap.hpp"
54 #include "object/camera.hpp"
55 #include "object/player.hpp"
56 #include "resources.hpp"
57 #include "gettext.hpp"
58 #include "misc.hpp"
59 #include "textscroller.hpp"
60 #include "file_system.hpp"
61 #include "control/joystickkeyboardcontroller.hpp"
62 #include "control/codecontroller.hpp"
63 #include "main.hpp"
64 #include "msg.hpp"
65 #include "console.hpp"
66
67 void
68 TitleScreen::update_load_game_menu()
69 {
70   load_game_menu.reset(new Menu());
71
72   load_game_menu->add_label(_("Start Game"));
73   load_game_menu->add_hl();
74   for(int i = 1; i <= 5; ++i) {
75     load_game_menu->add_entry(i, get_slotinfo(i));
76   }
77   load_game_menu->add_hl();
78   load_game_menu->add_back(_("Back"));
79 }
80
81 void
82 TitleScreen::free_contrib_menu()
83 {
84   for(std::vector<World*>::iterator i = contrib_worlds.begin();
85       i != contrib_worlds.end(); ++i)
86     delete *i;
87
88   contrib_worlds.clear();
89   current_contrib_world = 0;
90   current_world = -1;
91 }
92
93 void
94 TitleScreen::generate_contrib_menu()
95 {
96   /** Generating contrib levels list by making use of Level Subset  */
97   std::vector<std::string> level_worlds; 
98   char** files = PHYSFS_enumerateFiles("levels/");
99   for(const char* const* filename = files; *filename != 0; ++filename) {
100     std::string filepath = std::string("levels/") + *filename;
101     if(PHYSFS_isDirectory(filepath.c_str()))
102       level_worlds.push_back(filepath);
103   }
104   PHYSFS_freeList(files);
105
106   free_contrib_menu();
107   contrib_menu.reset(new Menu());
108
109   contrib_menu->add_label(_("Contrib Levels"));
110   contrib_menu->add_hl();
111   
112   int i = 0;
113   for (std::vector<std::string>::iterator it = level_worlds.begin();
114       it != level_worlds.end(); ++it) {
115     try {
116       std::auto_ptr<World> world (new World());
117       world->load(*it + "/info");
118       if(world->hide_from_contribs) {
119         continue;
120       }
121       contrib_menu->add_entry(i++, world->title);
122       contrib_worlds.push_back(world.release());
123     } catch(std::exception& e) {
124 #ifdef DEBUG
125       msg_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
126 #endif
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     std::auto_ptr<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     msg_warning << "Problem getting name of '" << filename << "'." << std::endl;
150     return "";
151   }
152 }
153
154 void
155 TitleScreen::check_levels_contrib_menu()
156 {
157   int index = contrib_menu->check();
158   if (index == -1)
159     return;
160
161   World& world = * (contrib_worlds[index]);
162
163   if(!world.is_levelset) {
164     // TODO fade out
165     world.run();
166   }
167
168   if (current_world != index) {
169     current_world = index;
170     World& world = * (contrib_worlds[index]);
171
172     current_contrib_world = &world;
173
174     contrib_world_menu.reset(new Menu());
175
176     contrib_world_menu->add_label(world.title);
177     contrib_world_menu->add_hl();
178
179     for (unsigned int i = 0; i < world.get_num_levels(); ++i)
180     {
181       /** get level's title */
182       std::string filename = world.get_level_filename(i);
183       std::string title = get_level_name(filename);
184       contrib_world_menu->add_entry(i, title);
185     }
186
187     contrib_world_menu->add_hl();
188     contrib_world_menu->add_back(_("Back"));
189
190     Menu::push_current(contrib_world_menu.get());
191   }
192 }
193
194 void
195 TitleScreen::check_contrib_world_menu()
196 {
197   int index = contrib_world_menu->check();
198   if (index != -1) {
199     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
200       sound_manager->stop_music();
201       GameSession* session =
202         new GameSession(
203           current_contrib_world->get_level_filename(index), ST_GL_PLAY);
204       main_loop->push_screen(session);
205     }
206   }  
207 }
208
209 void
210 TitleScreen::make_tux_jump()
211 {
212   static Timer randomWaitTimer;
213   static Timer jumpPushTimer;
214   static float last_tux_x_pos = -1;
215   static float last_tux_y_pos = -1;
216
217   Sector* sector  = titlesession->get_current_sector();
218   Player* tux = sector->player;
219
220   //sector->play_music(LEVEL_MUSIC);
221
222   controller->update();
223   controller->press(Controller::RIGHT);
224
225   // Determine how far we moved since last frame
226   float dx = fabsf(last_tux_x_pos - tux->get_pos().x); 
227   float dy = fabsf(last_tux_y_pos - tux->get_pos().y); 
228  
229   // Calculate space to check for obstacles 
230   Rect lookahead = tux->get_bbox();
231   lookahead.move(Vector(96, 0));
232   
233   // Check if we should press the jump button
234   bool randomJump = !randomWaitTimer.started();
235   bool notMoving = (fabsf(dx) + fabsf(dy)) < 0.1;
236   bool pathBlocked = !sector->is_free_space(lookahead); 
237   if (!controller->released(Controller::JUMP)
238       && (notMoving || pathBlocked || randomJump)) {
239     float jumpDuration;
240     if(pathBlocked)
241       jumpDuration = 0.5;
242     else
243       jumpDuration = float(rand() % 500 + 300) / 1000.0;
244     jumpPushTimer.start(jumpDuration);
245     randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
246   }
247
248   // Keep jump button pressed
249   if (jumpPushTimer.started())
250     controller->press(Controller::JUMP);
251
252   // Remember last position, so we can determine if we moved
253   last_tux_x_pos = tux->get_pos().x;
254   last_tux_y_pos = tux->get_pos().y;
255
256   // Wrap around at the end of the level back to the beginnig
257   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
258     sector->activate("main");
259     sector->camera->reset(tux->get_pos());
260   }
261 }
262
263 TitleScreen::TitleScreen()
264 {
265   controller.reset(new CodeController());
266   titlesession.reset(new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME));
267
268   // delete contrib_world_menu;
269   // contrib_world_menu = new Menu();
270
271   titlesession->get_current_sector()->activate("main");
272   titlesession->set_current();
273
274   Player* player = titlesession->get_current_sector()->player;
275   player->set_controller(controller.get());
276
277   Menu::set_current(main_menu); 
278 }
279
280 TitleScreen::~TitleScreen()
281 {
282 }
283
284 void
285 TitleScreen::setup()
286 {
287   player_status->reset();
288
289   Sector* sector = titlesession->get_current_sector();
290   sector->play_music(LEVEL_MUSIC);
291   sector->activate(sector->player->get_pos());
292
293   Menu::set_current(main_menu);
294 }
295
296 void
297 TitleScreen::leave()
298 {
299   Sector* sector = titlesession->get_current_sector();
300   sector->deactivate();
301 }
302
303 void
304 TitleScreen::draw(DrawingContext& context)
305 {
306   Sector* sector  = titlesession->get_current_sector();
307   sector->draw(context);
308  
309   /*
310   if (Menu::current() == main_menu)
311     context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
312             LAYER_FOREGROUND1+1);
313   */
314
315   context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
316       Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
317   context.draw_text(white_small_text,
318       _(
319 "Copyright (c) 2006 SuperTux Devel Team\n"
320 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
321 "redistribute it under certain conditions; see the file COPYING for details.\n"
322 ),
323       Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
324       LEFT_ALLIGN, LAYER_FOREGROUND1);
325 }
326
327 void
328 TitleScreen::update(float elapsed_time)
329 {
330   main_loop->set_speed(0.6);
331   Sector* sector  = titlesession->get_current_sector();
332   sector->update(elapsed_time);
333
334   make_tux_jump();
335   
336   Menu* menu = Menu::current();
337   if(menu) {
338     menu->update();
339           
340     if(menu == main_menu) {
341       switch (main_menu->check()) {
342         case MNID_STARTGAME:
343           // Start Game, ie. goto the slots menu
344           update_load_game_menu();
345           Menu::push_current(load_game_menu.get());
346           break;
347         case MNID_LEVELS_CONTRIB:
348           // Contrib Menu
349           generate_contrib_menu();
350           Menu::push_current(contrib_menu.get());
351           break;
352         case MNID_CREDITS:
353           fadeout(500);
354           main_loop->push_screen(new TextScroller("credits.txt"));
355           break;
356         case MNID_QUITMAINMENU:
357           main_loop->quit();
358           break;
359       }
360     } else if(menu == options_menu) {
361       process_options_menu();
362     } else if(menu == load_game_menu.get()) {
363       /*
364       if(event.key.keysym.sym == SDLK_DELETE) {
365         int slot = menu->get_active_item_id();
366         std::stringstream stream;
367         stream << slot;
368         std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
369         
370         if(confirm_dialog(bkg_title, str.c_str())) {
371           str = "save/slot" + stream.str() + ".stsg";
372           msg_debug << "Removing: " << str << std::endl;
373           PHYSFS_delete(str.c_str());
374         }
375
376         update_load_save_game_menu(load_game_menu);
377         Menu::set_current(main_menu);
378       }*/
379       process_load_game_menu();
380     } else if(menu == contrib_menu.get()) {
381       check_levels_contrib_menu();
382     } else if (menu == contrib_world_menu.get()) {
383       check_contrib_world_menu();
384     }
385   }
386
387   // reopen menu of user closed it (so that the app doesn't close when user
388   // accidently hit ESC)
389   if(Menu::current() == 0) {
390     Menu::set_current(main_menu);
391   }
392 }
393
394 std::string
395 TitleScreen::get_slotinfo(int slot)
396 {
397   std::string tmp;
398   std::string slotfile;
399   std::string title;
400   std::stringstream stream;
401   stream << slot;
402   slotfile = "save/slot" + stream.str() + ".stsg";
403
404   try {
405     lisp::Parser parser;
406     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
407
408     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
409     if(!savegame)
410       throw std::runtime_error("file is not a supertux-savegame.");
411
412     savegame->get("title", title);
413   } catch(std::exception& e) {
414     return std::string(_("Slot")) + " " + stream.str() + " - " +
415       std::string(_("Free"));
416   }
417
418   return std::string("Slot ") + stream.str() + " - " + title;
419 }
420
421 bool
422 TitleScreen::process_load_game_menu()
423 {
424   int slot = load_game_menu->check();
425
426   if(slot == -1)
427     return false;
428
429   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
430     return false;
431
432   std::stringstream stream;
433   stream << slot;
434   std::string slotfile = "save/slot" + stream.str() + ".stsg";
435
436   sound_manager->stop_music();
437   fadeout(256);
438   DrawingContext context;
439   context.draw_text(white_text, "Loading...",
440                     Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
441                     CENTER_ALLIGN, LAYER_FOREGROUND1);
442   context.do_drawing();
443
444   WorldMapNS::WorldMap* worldmap = new WorldMapNS::WorldMap();
445
446   worldmap->set_map_filename("/levels/world1/worldmap.stwm");
447   // Load the game or at least set the savegame_file variable
448   worldmap->loadgame(slotfile);
449
450   main_loop->push_screen(worldmap);
451
452   //Menu::set_current(main_menu);
453
454   return true;
455 }
456