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