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