- added geometry option which allows SuperTux to run at any resolution
[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 #include "textscroller.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   player_status.reset();
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_WIDTH/2, SCREEN_HEIGHT/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       world->activate("main");
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_WIDTH/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_HEIGHT - 50), 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 are welcome to\n"
353 "redistribute it under certain conditions; see the file COPYING for details.\n"
354         ),
355         Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
356         LEFT_ALLIGN, LAYER_FOREGROUND1);
357
358       /* Don't draw menu, if quit is true */
359       Menu* menu = Menu::current();
360       if(menu)
361         {
362           menu->draw(context);
363           menu->action();
364           
365           if(menu == main_menu)
366             {
367               switch (main_menu->check())
368                 {
369                 case MNID_STARTGAME:
370                   // Start Game, ie. goto the slots menu
371                   update_load_save_game_menu(load_game_menu);
372                   break;
373                 case MNID_LEVELS_CONTRIB:
374                   // Contrib Menu
375                   puts("Entering contrib menu");
376                   generate_contrib_menu();
377                   break;
378                 case MNID_LEVELEDITOR:
379                   leveleditor = new LevelEditor();
380                   leveleditor->run();
381                   delete leveleditor;
382                   Menu::set_current(main_menu);
383                   resume_demo();
384                   break;
385                 case MNID_CREDITS:
386                   fadeout(500);
387                   credits_music = SoundManager::get()->load_music(datadir + "/music/credits.ogg");
388                   SoundManager::get()->play_music(credits_music);
389                   display_text_file("credits.txt");
390                   fadeout(500);
391                   Menu::set_current(main_menu);
392                   break;
393                 case MNID_QUITMAINMENU:
394                   Menu::set_current(0);
395                   break;
396                 }
397             }
398           else if(menu == options_menu)
399             {
400               process_options_menu();
401             }
402           else if(menu == load_game_menu)
403             {
404               if(event.key.keysym.sym == SDLK_DELETE)
405                 {
406                 int slot = menu->get_active_item_id();
407                 std::stringstream stream;
408                 stream << slot;
409                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
410                 
411                 if(confirm_dialog(bkg_title, str.c_str()))
412                   {
413                   str = st_save_dir + "/slot" + stream.str() + ".stsg";
414                   printf("Removing: %s\n",str.c_str());
415                   remove(str.c_str());
416                   }
417
418                 update_load_save_game_menu(load_game_menu);
419                 Menu::set_current(main_menu);
420                 resume_demo();
421                 }
422               else if (process_load_game_menu())
423                 {
424                   resume_demo();
425                 }
426             }
427           else if(menu == contrib_menu)
428             {
429               check_levels_contrib_menu();
430             }
431           else if (menu == contrib_subset_menu)
432             {
433               check_contrib_subset_menu();
434             }
435         }
436
437       mouse_cursor->draw(context);
438      
439       context.do_drawing();
440
441       frame_rate.update();
442
443       /* Pause: */
444       frame++;
445     }
446   /* Free surfaces: */
447
448   free_contrib_menu();
449   delete titlesession;
450   delete bkg_title;
451   delete logo;
452   delete img_choose_subset;
453 }
454