e5e5aa58d3efbc8ccf2184ca6f6c53b2c75ef915
[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 "video/screen.hpp"
38 #include "video/surface.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "gui/menu.hpp"
41 #include "timer.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/parser.hpp"
44 #include "level.hpp"
45 #include "level_subset.hpp"
46 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "player_status.hpp"
49 #include "tile.hpp"
50 #include "sector.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "resources.hpp"
55 #include "gettext.hpp"
56 #include "misc.hpp"
57 #include "textscroller.hpp"
58 #include "file_system.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "control/codecontroller.hpp"
61 #include "main.hpp"
62 #include "exceptions.hpp"
63 #include "msg.hpp"
64 #include "console.hpp"
65
66 static Surface* bkg_title;
67 static Surface* logo;
68 //static Surface* img_choose_subset;
69
70 static int frame;
71
72 static GameSession* titlesession;
73 static CodeController* controller;
74
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77 static int current_subset = -1;
78
79 static Console* console;
80
81 /* If the demo was stopped - because game started, level
82    editor was excuted, etc - call this when you get back
83    to the title code.
84  */
85 void resume_demo()
86 {
87   player_status->reset();
88   titlesession->get_current_sector()->activate("main");
89   titlesession->set_current();
90
91   //frame_rate.update();
92 }
93
94 void update_load_save_game_menu(Menu* menu)
95 {
96   msg_debug("update loadsavemenu");
97   for(int i = 1; i < 6; ++i) {
98     MenuItem& item = menu->get_item_by_id(i);
99     item.kind = MN_ACTION;
100     item.change_text(slotinfo(i));
101   }
102 }
103
104 void free_contrib_menu()
105 {
106   for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
107       i != contrib_subsets.end(); ++i)
108     delete *i;
109
110   contrib_subsets.clear();
111   contrib_menu->clear();
112   current_contrib_subset = 0;
113   current_subset = -1;
114 }
115
116 void generate_contrib_menu()
117 {
118   /** Generating contrib levels list by making use of Level Subset  */
119   std::vector<std::string> level_subsets; 
120   char** files = PHYSFS_enumerateFiles("levels/");
121   for(const char* const* filename = files; *filename != 0; ++filename) {
122     std::string filepath = std::string("levels/") + *filename;
123     if(PHYSFS_isDirectory(filepath.c_str()))
124       level_subsets.push_back(filepath);
125   }
126   PHYSFS_freeList(files);
127
128   free_contrib_menu();
129
130   contrib_menu->add_label(_("Contrib Levels"));
131   contrib_menu->add_hl();
132   
133   int i = 0;
134   for (std::vector<std::string>::iterator it = level_subsets.begin();
135       it != level_subsets.end(); ++it) {
136     try {
137       std::auto_ptr<LevelSubset> subset (new LevelSubset());
138       subset->load(*it);
139       if(subset->hide_from_contribs) {
140         continue;
141       }
142       contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
143       contrib_subsets.push_back(subset.release());
144     } catch(std::exception& e) {
145 #ifdef DEBUG
146       msg_warning("Couldn't parse levelset info for '"
147         << *it << "': " << e.what() << "");
148 #endif
149     }
150   }
151
152   contrib_menu->add_hl();
153   contrib_menu->add_back(_("Back"));
154 }
155
156 std::string get_level_name(const std::string& filename)
157 {
158   try {
159     lisp::Parser parser;
160     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
161
162     const lisp::Lisp* level = root->get_lisp("supertux-level");
163     if(!level)
164       return "";
165
166     std::string name;
167     level->get("name", name);
168     return name;
169   } catch(std::exception& e) {
170     msg_warning("Problem getting name of '" << filename << "'.");
171     return "";
172   }
173 }
174
175 void check_levels_contrib_menu()
176 {
177   int index = contrib_menu->check();
178   if (index == -1)
179     return;
180
181   LevelSubset& subset = * (contrib_subsets[index]);
182   
183   if(subset.has_worldmap) {
184     WorldMapNS::WorldMap worldmap;
185     worldmap.set_map_filename(subset.get_worldmap_filename());
186     sound_manager->stop_music();
187
188     // some fading
189     fadeout(256);
190     DrawingContext context;
191     context.draw_text(white_text, "Loading...",
192         Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
193     context.do_drawing();
194
195     // TODO: slots should be available for contrib maps
196     worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
197     worldmap.display();  // run the map
198
199     Menu::set_current(main_menu);
200     resume_demo();
201   } else if (current_subset != index) {
202     current_subset = index;
203     LevelSubset& subset = * (contrib_subsets[index]);
204
205     current_contrib_subset = &subset;
206
207     contrib_subset_menu->clear();
208
209     contrib_subset_menu->add_label(subset.title);
210     contrib_subset_menu->add_hl();
211
212     for (int i = 0; i < subset.get_num_levels(); ++i)
213     {
214       /** get level's title */
215       std::string filename = subset.get_level_filename(i);
216       std::string title = get_level_name(filename);
217       contrib_subset_menu->add_entry(i, title);
218     }
219
220     contrib_subset_menu->add_hl();
221     contrib_subset_menu->add_back(_("Back"));
222
223     titlesession->get_current_sector()->activate("main");
224     titlesession->set_current();
225   }
226 }
227
228 void check_contrib_subset_menu()
229 {
230   int index = contrib_subset_menu->check();
231   if (index != -1) {
232     if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
233       sound_manager->stop_music();
234       GameSession session(
235           current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
236       session.run();
237       player_status->reset();
238       Menu::set_current(main_menu);
239       resume_demo();
240     }
241   }  
242 }
243
244 void draw_demo(float elapsed_time)
245 {
246   static Timer randomWaitTimer;
247   static Timer jumpPushTimer;
248   static Timer jumpRecoverTimer;
249   static float last_tux_x_pos = -1;
250   static float last_tux_y_pos = -1;
251
252   Sector* sector  = titlesession->get_current_sector();
253   Player* tux = sector->player;
254
255   sector->play_music(LEVEL_MUSIC);
256
257   controller->update();
258   controller->press(Controller::RIGHT);
259
260   // Determine how far we moved since last frame
261   float dx = fabsf(last_tux_x_pos - tux->get_pos().x); 
262   float dy = fabsf(last_tux_y_pos - tux->get_pos().y); 
263  
264   // Calculate space to check for obstacles 
265   Rect lookahead = Rect(tux->get_bbox());
266   lookahead.move(Vector(lookahead.get_width()*2,0));
267   
268   // Check if we should press the jump button
269   bool randomJump = !randomWaitTimer.started();
270   bool mayJump = !jumpRecoverTimer.started();
271   bool notMoving = (dx+dy < 0.1);
272   bool pathBlocked = !sector->is_free_space(lookahead); 
273   if ((notMoving || pathBlocked || randomJump) && mayJump) {
274     float jumpDuration = float(rand() % 200 + 500) / 1000.0;
275     jumpPushTimer.start(jumpDuration);
276     jumpRecoverTimer.start(jumpDuration+0.1);
277     randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
278   }
279
280   // Keep jump button pressed
281   if (jumpPushTimer.started()) controller->press(Controller::JUMP);
282
283   // Remember last position, so we can determine if we moved
284   last_tux_x_pos = tux->get_pos().x;
285   last_tux_y_pos = tux->get_pos().y;
286
287   // Wrap around at the end of the level back to the beginnig
288   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
289     sector->activate("main");
290     sector->camera->reset(tux->get_pos());
291   }
292
293   sector->update(elapsed_time);
294   sector->draw(*titlesession->context);
295 }
296
297 /* --- TITLE SCREEN --- */
298 void title()
299 {
300   //LevelEditor* leveleditor;
301   controller = new CodeController();
302
303   titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
304
305   /* Load images: */
306   bkg_title = new Surface("images/background/arctis.jpg");
307   logo = new Surface("images/engine/menu/logo.png");
308   //img_choose_subset = new Surface("images/status/choose-level-subset.png");
309
310   titlesession->get_current_sector()->activate("main");
311   titlesession->set_current();
312
313   Player* player = titlesession->get_current_sector()->player;
314   player->set_controller(controller);
315
316   /* --- Main title loop: --- */
317   frame = 0;
318
319   Uint32 lastticks = SDL_GetTicks();
320   
321   Menu::set_current(main_menu);
322   DrawingContext& context = *titlesession->context;
323
324   console = new Console(&context);
325
326   bool running = true;
327   while (running)
328     {
329       // Calculate the movement-factor
330       Uint32 ticks = SDL_GetTicks();
331       float elapsed_time = float(ticks - lastticks) / 1000.;
332       game_time += elapsed_time;
333       lastticks = ticks;
334       // 40fps is minimum
335       if(elapsed_time > .04)
336         elapsed_time = .04;
337       
338       /* Lower the speed so that Tux doesn't jump too hectically throught
339          the demo. */
340       elapsed_time /= 2;
341
342       SDL_Event event;
343       main_controller->update();
344       while (SDL_PollEvent(&event)) {
345         if (Menu::current()) {
346           Menu::current()->event(event);
347         }
348         main_controller->process_event(event);
349         if (event.type == SDL_QUIT)
350           throw graceful_shutdown();
351       }
352   
353       /* Draw the background: */
354       draw_demo(elapsed_time);
355
356       if (Menu::current() == main_menu)
357         context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
358             LAYER_FOREGROUND1+1);
359
360       context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
361               Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
362       context.draw_text(white_small_text,
363         _(
364 "Copyright (c) 2006 SuperTux Devel Team\n"
365 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
366 "redistribute it under certain conditions; see the file COPYING for details.\n"
367         ),
368         Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
369         LEFT_ALLIGN, LAYER_FOREGROUND1);
370
371       /* Don't draw menu, if quit is true */
372       Menu* menu = Menu::current();
373       if(menu)
374         {
375           menu->draw(context);
376           menu->update();
377           
378           if(menu == main_menu)
379             {
380               switch (main_menu->check())
381                 {
382                 case MNID_STARTGAME:
383                   // Start Game, ie. goto the slots menu
384                   update_load_save_game_menu(load_game_menu);
385                   break;
386                 case MNID_LEVELS_CONTRIB:
387                   // Contrib Menu
388                   generate_contrib_menu();
389                   break;
390                 case MNID_CREDITS:
391                   sound_manager->stop_music();
392                   fadeout(500);
393                   sound_manager->play_music("music/credits.ogg");
394                   display_text_file("credits.txt");
395                   sound_manager->stop_music();
396                   fadeout(500);
397                   Menu::set_current(main_menu);
398                   break;
399                 case MNID_QUITMAINMENU:
400                   running = false;
401                   break;
402                 }
403             }
404           else if(menu == options_menu)
405             {
406               process_options_menu();
407             }
408           else if(menu == load_game_menu)
409             {
410               if(event.key.keysym.sym == SDLK_DELETE)
411                 {
412                 int slot = menu->get_active_item_id();
413                 std::stringstream stream;
414                 stream << slot;
415                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
416                 
417                 if(confirm_dialog(bkg_title, str.c_str())) {
418                   str = "save/slot" + stream.str() + ".stsg";
419                   msg_debug("Removing: " << str);
420                   PHYSFS_delete(str.c_str());
421                 }
422
423                 update_load_save_game_menu(load_game_menu);
424                 Menu::set_current(main_menu);
425                 resume_demo();
426                 }
427               else if (process_load_game_menu())
428                 {
429                   resume_demo();
430                 }
431             }
432           else if(menu == contrib_menu)
433             {
434               check_levels_contrib_menu();
435             }
436           else if (menu == contrib_subset_menu)
437             {
438               check_contrib_subset_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         Menu::set_current(main_menu);
446       }
447
448       console->draw();
449
450       context.do_drawing();
451       sound_manager->update();
452
453       //frame_rate.update();
454
455       /* Pause: */
456       frame++;
457     }
458   /* Free surfaces: */
459
460   free_contrib_menu();
461   delete titlesession;
462   delete bkg_title;
463   delete logo;
464   delete console;
465   //delete img_choose_subset;
466 }