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