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