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