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