cleanup and fixes to openal code
[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.hpp"
37 #include "video/screen.hpp"
38 #include "video/surface.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "gui/menu.hpp"
41 #include "timer.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/parser.hpp"
44 #include "level.hpp"
45 #include "level_subset.hpp"
46 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "player_status.hpp"
49 #include "tile.hpp"
50 #include "sector.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "resources.hpp"
55 #include "gettext.hpp"
56 #include "misc.hpp"
57 #include "textscroller.hpp"
58 #include "file_system.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "control/codecontroller.hpp"
61 #include "main.hpp"
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     sound_manager->stop_music();
185
186     // some fading
187     fadeout(256);
188     DrawingContext context;
189     context.draw_text(white_text, "Loading...",
190         Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
191     context.do_drawing();
192
193     // TODO: slots should be available for contrib maps
194     worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
195     worldmap.display();  // run the map
196
197     Menu::set_current(main_menu);
198     resume_demo();
199   } else if (current_subset != index) {
200     current_subset = index;
201     LevelSubset& subset = * (contrib_subsets[index]);
202
203     current_contrib_subset = &subset;
204
205     contrib_subset_menu->clear();
206
207     contrib_subset_menu->add_label(subset.title);
208     contrib_subset_menu->add_hl();
209
210     for (int i = 0; i < subset.get_num_levels(); ++i)
211     {
212       /** get level's title */
213       std::string filename = subset.get_level_filename(i);
214       std::string title = get_level_name(filename);
215       contrib_subset_menu->add_entry(i, title);
216     }
217
218     contrib_subset_menu->add_hl();
219     contrib_subset_menu->add_back(_("Back"));
220
221     titlesession->get_current_sector()->activate("main");
222     titlesession->set_current();
223   }
224 }
225
226 void check_contrib_subset_menu()
227 {
228   int index = contrib_subset_menu->check();
229   if (index != -1) {
230     if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
231       sound_manager->stop_music();
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   static float last_tux_x_pos = -1;
245   static float last_tux_y_pos = -1;
246   Sector* sector  = titlesession->get_current_sector();
247   Player* tux = sector->player;
248
249   sector->play_music(LEVEL_MUSIC);
250
251   controller->update();
252   controller->press(Controller::RIGHT);
253   
254   if(random_timer.check() || 
255       (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
256     walking = false;
257   } else {
258       if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
259         random_timer.start(float(rand() % 3000 + 3000) / 1000.);
260         walking = true;
261       }
262   }
263   if(!walking)
264     controller->press(Controller::JUMP);
265   last_tux_x_pos = tux->get_pos().x;
266   last_tux_y_pos = tux->get_pos().y;
267
268   // Wrap around at the end of the level back to the beginnig
269   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
270     sector->activate("main");
271     sector->camera->reset(tux->get_pos());
272   }
273
274   sector->update(elapsed_time);
275   sector->draw(*titlesession->context);
276 }
277
278 /* --- TITLE SCREEN --- */
279 void title()
280 {
281   walking = true;
282   //LevelEditor* leveleditor;
283   controller = new CodeController();
284
285   titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
286
287   /* Load images: */
288   bkg_title = new Surface("images/background/arctis.jpg", false);
289   logo = new Surface("images/engine/menu/logo.png", true);
290   //img_choose_subset = new Surface("images/status/choose-level-subset.png", true);
291
292   titlesession->get_current_sector()->activate("main");
293   titlesession->set_current();
294
295   Player* player = titlesession->get_current_sector()->player;
296   player->set_controller(controller);
297
298   /* --- Main title loop: --- */
299   frame = 0;
300
301   random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
302
303   Uint32 lastticks = SDL_GetTicks();
304   
305   Menu::set_current(main_menu);
306   DrawingContext& context = *titlesession->context;
307   bool running = true;
308   while (running)
309     {
310       // Calculate the movement-factor
311       Uint32 ticks = SDL_GetTicks();
312       float elapsed_time = float(ticks - lastticks) / 1000.;
313       global_time += elapsed_time;
314       lastticks = ticks;
315       // 40fps is minimum
316       if(elapsed_time > .04)
317         elapsed_time = .04;
318       
319       /* Lower the speed so that Tux doesn't jump too hectically throught
320          the demo. */
321       elapsed_time /= 2;
322
323       SDL_Event event;
324       main_controller->update();
325       while (SDL_PollEvent(&event)) {
326         if (Menu::current()) {
327           Menu::current()->event(event);
328         }
329         main_controller->process_event(event);
330         if (event.type == SDL_QUIT)
331           throw std::runtime_error("Received window close");
332       }
333   
334       /* Draw the background: */
335       draw_demo(elapsed_time);
336
337       if (Menu::current() == main_menu)
338         context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->w/2, 30),
339             LAYER_FOREGROUND1+1);
340
341       context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
342               Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
343       context.draw_text(white_small_text,
344         _(
345 "Copyright (c) 2005 SuperTux Devel Team\n"
346 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
347 "redistribute it under certain conditions; see the file COPYING for details.\n"
348         ),
349         Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
350         LEFT_ALLIGN, LAYER_FOREGROUND1);
351
352       /* Don't draw menu, if quit is true */
353       Menu* menu = Menu::current();
354       if(menu)
355         {
356           menu->draw(context);
357           menu->update();
358           
359           if(menu == main_menu)
360             {
361               switch (main_menu->check())
362                 {
363                 case MNID_STARTGAME:
364                   // Start Game, ie. goto the slots menu
365                   update_load_save_game_menu(load_game_menu);
366                   break;
367                 case MNID_LEVELS_CONTRIB:
368                   // Contrib Menu
369                   generate_contrib_menu();
370                   break;
371 #if 0
372                 case MNID_LEVELEDITOR: {
373                   LevelEdtiro* leveleditor = new LevelEditor();
374                   leveleditor->run();
375                   delete leveleditor;
376                   Menu::set_current(main_menu);
377                   resume_demo();
378                   break;
379                 }
380 #endif
381                 case MNID_CREDITS:
382                   sound_manager->stop_music();
383                   fadeout(500);
384                   sound_manager->play_music("music/credits.ogg");
385                   display_text_file("credits.txt");
386                   sound_manager->stop_music();
387                   fadeout(500);
388                   Menu::set_current(main_menu);
389                   break;
390                 case MNID_QUITMAINMENU:
391                   running = false;
392                   break;
393                 }
394             }
395           else if(menu == options_menu)
396             {
397               process_options_menu();
398             }
399           else if(menu == load_game_menu)
400             {
401               if(event.key.keysym.sym == SDLK_DELETE)
402                 {
403                 int slot = menu->get_active_item_id();
404                 std::stringstream stream;
405                 stream << slot;
406                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
407                 
408                 if(confirm_dialog(bkg_title, str.c_str())) {
409                   str = "save/slot" + stream.str() + ".stsg";
410                   printf("Removing: %s\n",str.c_str());
411                   PHYSFS_delete(str.c_str());
412                 }
413
414                 update_load_save_game_menu(load_game_menu);
415                 Menu::set_current(main_menu);
416                 resume_demo();
417                 }
418               else if (process_load_game_menu())
419                 {
420                   resume_demo();
421                 }
422             }
423           else if(menu == contrib_menu)
424             {
425               check_levels_contrib_menu();
426             }
427           else if (menu == contrib_subset_menu)
428             {
429               check_contrib_subset_menu();
430             }
431         }
432
433       // reopen menu of user closed it (so that the app doesn't close when user
434       // accidently hit ESC)
435       if(Menu::current() == 0) {
436         Menu::set_current(main_menu);
437       }
438
439       context.do_drawing();
440       sound_manager->update();
441
442       //frame_rate.update();
443
444       /* Pause: */
445       frame++;
446     }
447   /* Free surfaces: */
448
449   free_contrib_menu();
450   delete titlesession;
451   delete bkg_title;
452   delete logo;
453   //delete img_choose_subset;
454 }