fad1e81004050ba4d32d0833baf4613aba817fee
[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
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();
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         _("Copyright (c) 2003 SuperTux Devel Team\n"
351           "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
352           "are welcome to redistribute it under certain conditions; see the file COPYING\n"
353           "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
354
355       /* Don't draw menu, if quit is true */
356       Menu* menu = Menu::current();
357       if(menu)
358         {
359           menu->draw(context);
360           menu->action();
361           
362           if(menu == main_menu)
363             {
364               switch (main_menu->check())
365                 {
366                 case MNID_STARTGAME:
367                   // Start Game, ie. goto the slots menu
368                   update_load_save_game_menu(load_game_menu);
369                   break;
370                 case MNID_LEVELS_CONTRIB:
371                   // Contrib Menu
372                   puts("Entering contrib menu");
373                   generate_contrib_menu();
374                   break;
375                 case MNID_LEVELEDITOR:
376                   leveleditor = new LevelEditor();
377                   leveleditor->run();
378                   delete leveleditor;
379                   Menu::set_current(main_menu);
380                   resume_demo();
381                   break;
382                 case MNID_CREDITS:
383                   fadeout(500);
384                   display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
385                   fadeout(500);
386                   Menu::set_current(main_menu);
387                   break;
388                 case MNID_QUITMAINMENU:
389                   Menu::set_current(0);
390                   break;
391                 }
392             }
393           else if(menu == options_menu)
394             {
395               process_options_menu();
396             }
397           else if(menu == load_game_menu)
398             {
399               if(event.key.keysym.sym == SDLK_DELETE)
400                 {
401                 int slot = menu->get_active_item_id();
402                 std::stringstream stream;
403                 stream << slot;
404                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
405                 
406                 if(confirm_dialog(bkg_title, str.c_str()))
407                   {
408                   str = st_save_dir + "/slot" + stream.str() + ".stsg";
409                   printf("Removing: %s\n",str.c_str());
410                   remove(str.c_str());
411                   }
412
413                 update_load_save_game_menu(load_game_menu);
414                 Menu::set_current(main_menu);
415                 resume_demo();
416                 }
417               else if (process_load_game_menu())
418                 {
419                   resume_demo();
420                 }
421             }
422           else if(menu == contrib_menu)
423             {
424               check_levels_contrib_menu();
425             }
426           else if (menu == contrib_subset_menu)
427             {
428               check_contrib_subset_menu();
429             }
430         }
431
432       mouse_cursor->draw(context);
433      
434       context.do_drawing();
435
436       frame_rate.update();
437
438       /* Pause: */
439       frame++;
440     }
441   /* Free surfaces: */
442
443   free_contrib_menu();
444   delete titlesession;
445   delete bkg_title;
446   delete logo;
447   delete img_choose_subset;
448 }
449