added a bell object which is a new better way to do reset points
[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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <cmath>
31 #include <SDL.h>
32 #include <SDL_image.h>
33
34 #ifndef WIN32
35 #include <sys/types.h>
36 #include <ctype.h>
37 #endif
38
39 #include "app/globals.h"
40 #include "title.h"
41 #include "video/screen.h"
42 #include "video/surface.h"
43 #include "gui/menu.h"
44 #include "timer.h"
45 #include "special/frame_rate.h"
46 #include "app/setup.h"
47 #include "lisp/lisp.h"
48 #include "lisp/parser.h"
49 #include "level.h"
50 #include "level_subset.h"
51 #include "gameloop.h"
52 #include "worldmap.h"
53 #include "leveleditor.h"
54 #include "player_status.h"
55 #include "tile.h"
56 #include "sector.h"
57 #include "object/tilemap.h"
58 #include "object/camera.h"
59 #include "object/player.h"
60 #include "resources.h"
61 #include "app/gettext.h"
62 #include "misc.h"
63
64 static Surface* bkg_title;
65 static Surface* logo;
66 static Surface* img_choose_subset;
67
68 static bool walking;
69 static Timer2 random_timer;
70
71 static int frame;
72
73 static GameSession* titlesession;
74
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77
78 static FrameRate frame_rate(100);  
79
80 /* If the demo was stopped - because game started, level
81    editor was excuted, etc - call this when you get back
82    to the title code.
83  */
84 void resume_demo()
85 {
86   // FIXME: shouldn't be needed if GameSession
87   // didn't relay on global variables
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* pmenu)
95 {
96   for(int i = 2; i < 7; ++i)
97     {
98       // FIXME: Insert a real savegame struct/class here instead of
99       // doing string vodoo
100       std::string tmp = slotinfo(i - 1);
101       pmenu->item[i].kind = MN_ACTION;
102       pmenu->item[i].change_text(tmp.c_str());
103     }
104 }
105
106 void free_contrib_menu()
107 {
108   for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
109       i != contrib_subsets.end(); ++i)
110     delete *i;
111
112   contrib_subsets.clear();
113   contrib_menu->clear();
114 }
115
116 void generate_contrib_menu()
117 {
118   /** Generating contrib levels list by making use of Level Subset  */
119   std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
120
121   free_contrib_menu();
122
123   contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
124   contrib_menu->additem(MN_HL,"",0,0);
125   
126   int i = 0;
127   for (std::set<std::string>::iterator it = level_subsets.begin();
128       it != level_subsets.end(); ++it)
129     {
130       LevelSubset* subset = new LevelSubset();
131       subset->load(*it);
132       if(subset->hide_from_contribs) {
133         delete subset;
134         continue;
135       }
136       contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
137       contrib_subsets.push_back(subset);
138       ++i;
139     }
140
141   contrib_menu->additem(MN_HL,"",0,0);
142   contrib_menu->additem(MN_BACK,_("Back"),0,0);
143
144   level_subsets.clear();
145 }
146
147 std::string get_level_name(const std::string& filename)
148 {
149   try {
150     lisp::Parser parser;
151     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
152
153     const lisp::Lisp* level = root->get_lisp("supertux-level");
154     if(!level)
155       return "";
156
157     std::string name;
158     level->get("name", name);
159     return name;
160   } catch(std::exception& e) {
161     std::cerr << "Problem getting name of '" << filename << "'.\n";
162     return "";
163   }
164 }
165
166 void check_levels_contrib_menu()
167 {
168   static int current_subset = -1;
169
170   int index = contrib_menu->check();
171   if (index == -1)
172     return;
173
174   LevelSubset& subset = * (contrib_subsets[index]);
175   
176   if(subset.has_worldmap) {
177     WorldMapNS::WorldMap worldmap;
178     worldmap.set_map_filename(subset.get_worldmap_filename());
179
180     // some fading
181     fadeout(256);
182     DrawingContext context;
183     context.draw_text(white_text, "Loading...",
184         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
185     context.do_drawing();
186
187     // TODO: slots should be available for contrib maps
188     worldmap.loadgame(st_save_dir + "/" + subset.name + "-slot1.stsg");
189
190     worldmap.display();  // run the map
191
192     Menu::set_current(main_menu);
193     resume_demo();
194   } else if (current_subset != index) {
195     current_subset = index;
196     // FIXME: This shouln't be busy looping
197     LevelSubset& subset = * (contrib_subsets[index]);
198
199     current_contrib_subset = &subset;
200
201     contrib_subset_menu->clear();
202
203     contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
204     contrib_subset_menu->additem(MN_HL,"",0,0);
205
206     for (int i = 0; i < subset.get_num_levels(); ++i)
207     {
208       /** get level's title */
209       std::string filename = subset.get_level_filename(i);
210       std::string title = get_level_name(filename);
211       contrib_subset_menu->additem(MN_ACTION, title, 0, 0, i);
212     }
213
214     contrib_subset_menu->additem(MN_HL,"",0,0);      
215     contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
216
217     titlesession->get_current_sector()->activate("main");
218     titlesession->set_current();
219   }
220 }
221
222 void check_contrib_subset_menu()
223 {
224   int index = contrib_subset_menu->check();
225   if (index != -1)
226     {
227       if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
228         {
229           std::cout << "Starting level: " << index << std::endl;
230           
231           GameSession session(
232               current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
233           session.run();
234           player_status.reset();
235           Menu::set_current(main_menu);
236           resume_demo();
237         }
238     }  
239 }
240
241 void draw_demo(float elapsed_time)
242 {
243   Sector* world  = titlesession->get_current_sector();
244   Player* tux = world->player;
245
246   world->play_music(LEVEL_MUSIC);
247   
248   tux->key_event((SDLKey) keymap.right, true);
249   
250   if(random_timer.check()) {
251     random_timer.start(float(rand() % 3000 + 3000) / 1000.);
252     walking = !walking;
253   } else {
254       if(walking)
255         tux->key_event((SDLKey) keymap.jump, false);
256       else
257         tux->key_event((SDLKey) keymap.jump, true);
258   }
259
260   // Wrap around at the end of the level back to the beginnig
261   if(world->solids->get_width() * 32 - 320 < tux->get_pos().x)
262     {
263       tux->level_begin();
264       world->camera->reset(tux->get_pos());
265     }
266
267   tux->can_jump = true;
268   float last_tux_x_pos = tux->get_pos().x;
269   world->action(elapsed_time);
270   
271
272   // disabled for now, since with the new jump code we easily get deadlocks
273   // Jump if tux stays in the same position for one loop, ie. if he is
274   // stuck behind a wall
275   if (last_tux_x_pos == tux->get_pos().x)
276     {
277       walking = false;
278     }
279
280   world->draw(*titlesession->context);
281 }
282
283 /* --- TITLE SCREEN --- */
284 void title(void)
285 {
286   walking = true;
287   LevelEditor* leveleditor;
288   MusicRef credits_music;
289
290   Ticks::pause_init();
291
292   titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
293       ST_GL_DEMO_GAME);
294
295   /* Load images: */
296   bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
297   logo = new Surface(datadir + "/images/title/logo.png", true);
298   img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
299
300   titlesession->get_current_sector()->activate("main");
301   titlesession->set_current();
302
303   /* --- Main title loop: --- */
304   frame = 0;
305
306   random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
307
308   Uint32 lastticks = SDL_GetTicks();
309   
310   Menu::set_current(main_menu);
311   DrawingContext& context = *titlesession->context;
312   while (Menu::current())
313     {
314       // Calculate the movement-factor
315       Uint32 ticks = SDL_GetTicks();
316       float elapsed_time = float(ticks - lastticks) / 1000.;
317       global_time += elapsed_time;
318       lastticks = ticks;
319       // 40fps is minimum
320       if(elapsed_time > .04)
321         elapsed_time = .04;
322       
323       /* Lower the speed so that Tux doesn't jump too hectically throught
324          the demo. */
325       elapsed_time /= 2;
326
327       SDL_Event event;
328       while (SDL_PollEvent(&event))
329         {
330           if (Menu::current())
331             {
332               Menu::current()->event(event);
333             }
334          // FIXME: QUIT signal should be handled more generic, not locally
335           if (event.type == SDL_QUIT)
336             Menu::set_current(0);
337         }
338   
339       /* Draw the background: */
340       draw_demo(elapsed_time);
341       
342       
343       if (Menu::current() == main_menu)
344         context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
345             LAYER_FOREGROUND1+1);
346
347       context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
348               Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
349       context.draw_text(white_small_text,
350         _(
351 "Copyright (c) 2003 SuperTux Devel Team\n"
352 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
353 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
354 "for details.\n"
355         ),
356         Vector(0, screen->h - 70 + white_small_text->get_height()),
357         LEFT_ALLIGN, LAYER_FOREGROUND1);
358
359       /* Don't draw menu, if quit is true */
360       Menu* menu = Menu::current();
361       if(menu)
362         {
363           menu->draw(context);
364           menu->action();
365           
366           if(menu == main_menu)
367             {
368               switch (main_menu->check())
369                 {
370                 case MNID_STARTGAME:
371                   // Start Game, ie. goto the slots menu
372                   update_load_save_game_menu(load_game_menu);
373                   break;
374                 case MNID_LEVELS_CONTRIB:
375                   // Contrib Menu
376                   puts("Entering contrib menu");
377                   generate_contrib_menu();
378                   break;
379                 case MNID_LEVELEDITOR:
380                   leveleditor = new LevelEditor();
381                   leveleditor->run();
382                   delete leveleditor;
383                   Menu::set_current(main_menu);
384                   resume_demo();
385                   break;
386                 case MNID_CREDITS:
387                   fadeout(500);
388                   credits_music = SoundManager::get()->load_music(datadir + "/music/credits.ogg");
389                   SoundManager::get()->play_music(credits_music);
390                   display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
391                   fadeout(500);
392                   Menu::set_current(main_menu);
393                   break;
394                 case MNID_QUITMAINMENU:
395                   Menu::set_current(0);
396                   break;
397                 }
398             }
399           else if(menu == options_menu)
400             {
401               process_options_menu();
402             }
403           else if(menu == load_game_menu)
404             {
405               if(event.key.keysym.sym == SDLK_DELETE)
406                 {
407                 int slot = menu->get_active_item_id();
408                 std::stringstream stream;
409                 stream << slot;
410                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
411                 
412                 if(confirm_dialog(bkg_title, str.c_str()))
413                   {
414                   str = st_save_dir + "/slot" + stream.str() + ".stsg";
415                   printf("Removing: %s\n",str.c_str());
416                   remove(str.c_str());
417                   }
418
419                 update_load_save_game_menu(load_game_menu);
420                 Menu::set_current(main_menu);
421                 resume_demo();
422                 }
423               else if (process_load_game_menu())
424                 {
425                   resume_demo();
426                 }
427             }
428           else if(menu == contrib_menu)
429             {
430               check_levels_contrib_menu();
431             }
432           else if (menu == contrib_subset_menu)
433             {
434               check_contrib_subset_menu();
435             }
436         }
437
438       mouse_cursor->draw(context);
439      
440       context.do_drawing();
441
442       frame_rate.update();
443
444       /* Pause: */
445       frame++;
446     }
447   /* Free surfaces: */
448
449   free_contrib_menu();
450   delete titlesession;
451   delete bkg_title;
452   delete logo;
453   delete img_choose_subset;
454 }
455