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