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