9e4504912e9db60ab5d18cf80a7223a9ff4c0f6f
[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
22 #include <iostream>
23 #include <sstream>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <cmath>
30 #include <SDL.h>
31 #include <SDL_image.h>
32
33 #ifndef WIN32
34 #include <sys/types.h>
35 #include <ctype.h>
36 #endif
37
38 #include "defines.h"
39 #include "app/globals.h"
40 #include "title.h"
41 #include "video/screen.h"
42 #include "video/surface.h"
43 #include "high_scores.h"
44 #include "gui/menu.h"
45 #include "special/timer.h"
46 #include "special/frame_rate.h"
47 #include "app/setup.h"
48 #include "level.h"
49 #include "level_subset.h"
50 #include "gameloop.h"
51 #include "worldmap.h"
52 #include "leveleditor.h"
53 #include "scene.h"
54 #include "player.h"
55 #include "tile.h"
56 #include "sector.h"
57 #include "tilemap.h"
58 #include "resources.h"
59 #include "special/base.h"
60 #include "app/gettext.h"
61 #include "misc.h"
62 #include "camera.h"
63
64 static Surface* bkg_title;
65 static Surface* logo;
66 static Surface* img_choose_subset;
67
68 static bool walking;
69 static Timer random_timer;
70
71 static int frame;
72
73 static GameSession* titlesession;
74
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77 static int first_level_index;
78
79 static std::set<std::string> worldmap_list;
80
81 static FrameRate frame_rate(100);  
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   // FIXME: shouldn't be needed if GameSession
90   // didn't relay on global variables
91   titlesession->get_current_sector()->activate();
92   titlesession->set_current();
93
94   frame_rate.update();
95 }
96
97 void update_load_save_game_menu(Menu* pmenu)
98 {
99   for(int i = 2; i < 7; ++i)
100     {
101       // FIXME: Insert a real savegame struct/class here instead of
102       // doing string vodoo
103       std::string tmp = slotinfo(i - 1);
104       pmenu->item[i].kind = MN_ACTION;
105       pmenu->item[i].change_text(tmp.c_str());
106     }
107 }
108
109 void free_contrib_menu()
110 {
111   for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
112       i != contrib_subsets.end(); ++i)
113     delete *i;
114
115   contrib_subsets.clear();
116   contrib_menu->clear();
117 }
118
119 void generate_contrib_menu()
120 {
121   /** Generating contrib levels list by making use of Level Subset */
122   std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
123
124   free_contrib_menu();
125
126   contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
127   contrib_menu->additem(MN_HL,"",0,0);
128   int i = 0;
129
130   for(std::set<std::string>::iterator it = worldmap_list.begin(); it != worldmap_list.end(); ++it)
131     {
132     WorldMapNS::WorldMap worldmap;
133     worldmap.loadmap((*it).c_str());
134     contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i);
135     ++i;
136     }
137
138   contrib_menu->additem(MN_HL,"",0,0);
139
140   first_level_index = i;
141   for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
142     {
143       LevelSubset* subset = new LevelSubset();
144       subset->load(*it);
145       if(subset->hide_from_contribs)
146         {
147         delete subset;
148         continue;
149         }
150       contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
151       contrib_subsets.push_back(subset);
152       ++i;
153     }
154
155   contrib_menu->additem(MN_HL,"",0,0);
156   contrib_menu->additem(MN_BACK,_("Back"),0,0);
157
158   level_subsets.clear();
159 }
160
161 void check_levels_contrib_menu()
162 {
163   static int current_subset = -1;
164
165   int index = contrib_menu->check();
166   if (index == -1)
167     return;
168
169   if((unsigned)index < worldmap_list.size())
170     {
171     WorldMapNS::WorldMap worldmap;
172     std::set<std::string>::iterator it = worldmap_list.begin();
173     for(int i = index; i > 0; --i)
174     ++it;
175
176     std::string map_filename = *it;
177
178     // some fading
179     fadeout(256);
180     DrawingContext context;
181       context.draw_text(white_text, "Loading...",
182                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
183       context.do_drawing();
184
185     worldmap.set_map_filename(map_filename);
186
187     // hack to erase the extension
188     unsigned int ext_pos = it->find_last_of(".");
189     if(ext_pos != std::string::npos)
190       map_filename.erase(ext_pos, map_filename.size() - ext_pos);
191
192     // TODO: slots should be available for contrib maps
193     worldmap.loadgame(st_save_dir + "/" + map_filename + "-slot1.stsg");
194
195     worldmap.display();  // run the map
196
197     Menu::set_current(main_menu);
198     resume_demo();
199     }
200   else if (index < (int)contrib_subsets.size() + first_level_index)
201     {
202     index -= first_level_index;
203     if (current_subset != index)
204       {
205       current_subset = index;
206       // FIXME: This shouln't be busy looping
207       LevelSubset& subset = * (contrib_subsets[index]);
208
209       current_contrib_subset = &subset;
210
211       contrib_subset_menu->clear();
212
213       contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
214       contrib_subset_menu->additem(MN_HL,"",0,0);
215
216       for (int i = 0; i < subset.get_num_levels(); ++i)
217         {
218         /** get level's title */
219         std::string level_title = "<no title>";
220
221         std::string filename = subset.get_level_filename(i);
222         std::string filepath;
223         filepath = st_dir + "/levels/" + filename;
224         if (access(filepath.c_str(), R_OK) != 0)
225         {
226           filepath = datadir + "/levels/" + filename;
227           if (access(filepath.c_str(), R_OK) != 0)
228           {
229             std::cerr << "Error: Level: couldn't find level: " << filename << std::endl;
230             continue;
231           }
232         }
233         
234         LispReader* reader = LispReader::load(filepath, "supertux-level");
235         if(!reader)
236           {
237           std::cerr << "Error: Could not open level file. Ignoring...\n";
238           continue;
239           }
240
241         reader->read_string("name", level_title, true);
242         delete reader;
243
244         contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
245         }
246
247       contrib_subset_menu->additem(MN_HL,"",0,0);      
248       contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
249
250       titlesession->get_current_sector()->activate();
251       titlesession->set_current();
252       }
253     }
254 }
255
256 void check_contrib_subset_menu()
257 {
258   int index = contrib_subset_menu->check();
259   if (index != -1)
260     {
261       if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
262         {
263           std::cout << "Starting level: " << index << std::endl;
264           
265           GameSession session(
266               current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
267           session.run();
268           player_status.reset();
269           Menu::set_current(main_menu);
270           resume_demo();
271         }
272     }  
273 }
274
275 void draw_demo(double frame_ratio)
276 {
277   Sector* world  = titlesession->get_current_sector();
278   Player* tux = world->player;
279
280   world->play_music(LEVEL_MUSIC);
281   
282   global_frame_counter++;
283   tux->key_event((SDLKey) keymap.right,DOWN);
284   
285   if(random_timer.check())
286     {
287       if(walking)
288         tux->key_event((SDLKey) keymap.jump,UP);
289       else
290         tux->key_event((SDLKey) keymap.jump,DOWN);
291     }
292   else
293     {
294       random_timer.start(rand() % 3000 + 3000);
295       walking = !walking;
296     }
297
298   // Wrap around at the end of the level back to the beginnig
299   if(world->solids->get_width() * 32 - 320 < tux->base.x)
300     {
301       tux->level_begin();
302       world->camera->reset(Vector(tux->base.x, tux->base.y));
303     }
304
305   tux->can_jump = true;
306   float last_tux_x_pos = tux->base.x;
307   world->action(frame_ratio);
308   
309
310   // disabled for now, since with the new jump code we easily get deadlocks
311   // Jump if tux stays in the same position for one loop, ie. if he is
312   // stuck behind a wall
313   if (last_tux_x_pos == tux->base.x)
314     {
315       walking = false;
316     }
317
318   world->draw(*titlesession->context);
319 }
320
321 /* --- TITLE SCREEN --- */
322 void title(void)
323 {
324   walking = true;
325   LevelEditor* leveleditor;
326
327   random_timer.init(true);
328   Ticks::pause_init();
329
330   titlesession = new GameSession("misc/menu.stl", ST_GL_DEMO_GAME);
331
332   /* Load images: */
333   bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
334   logo = new Surface(datadir + "/images/title/logo.png", true);
335   img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
336
337   /* Generating contrib maps by only using a string_list */
338   worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
339
340   titlesession->get_current_sector()->activate();
341   titlesession->set_current();
342
343   /* --- Main title loop: --- */
344   frame = 0;
345
346   frame_rate.set_frame_limit(false);
347   
348   random_timer.start(rand() % 2000 + 2000);
349
350   Menu::set_current(main_menu);
351   DrawingContext& context = *titlesession->context;
352   while (Menu::current())
353     {
354       // if we spent to much time on a menu entry
355       frame_rate.smooth_hanger();
356     
357       // Calculate the movement-factor
358       double frame_ratio = frame_rate.get();
359       
360       if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
361         frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
362       /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
363       frame_ratio /= 2;
364
365       SDL_Event event;
366       while (SDL_PollEvent(&event))
367         {
368           if (Menu::current())
369             {
370               Menu::current()->event(event);
371             }
372          // FIXME: QUIT signal should be handled more generic, not locally
373           if (event.type == SDL_QUIT)
374             Menu::set_current(0);
375         }
376   
377       /* Draw the background: */
378       draw_demo(frame_ratio);
379       
380       
381       if (Menu::current() == main_menu)
382         context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
383             LAYER_FOREGROUND1+1);
384
385       context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
386       context.draw_text(white_small_text,
387         _("Copyright (c) 2003 SuperTux Devel Team\n"
388           "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
389           "are welcome to redistribute it under certain conditions; see the file COPYING\n"
390           "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
391
392       /* Don't draw menu, if quit is true */
393       Menu* menu = Menu::current();
394       if(menu)
395         {
396           menu->draw(context);
397           menu->action();
398           
399           if(menu == main_menu)
400             {
401               switch (main_menu->check())
402                 {
403                 case MNID_STARTGAME:
404                   // Start Game, ie. goto the slots menu
405                   update_load_save_game_menu(load_game_menu);
406                   break;
407                 case MNID_LEVELS_CONTRIB:
408                   // Contrib Menu
409                   puts("Entering contrib menu");
410                   generate_contrib_menu();
411                   break;
412                 case MNID_LEVELEDITOR:
413                   leveleditor = new LevelEditor();
414                   leveleditor->run();
415                   delete leveleditor;
416                   Menu::set_current(main_menu);
417                   resume_demo();
418                   break;
419                 case MNID_CREDITS:
420                   fadeout(500);
421                   display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
422                   fadeout(500);
423                   Menu::set_current(main_menu);
424                   break;
425                 case MNID_QUITMAINMENU:
426                   Menu::set_current(0);
427                   break;
428                 }
429             }
430           else if(menu == options_menu)
431             {
432               process_options_menu();
433             }
434           else if(menu == load_game_menu)
435             {
436               if(event.key.keysym.sym == SDLK_DELETE)
437                 {
438                 int slot = menu->get_active_item_id();
439                 std::stringstream stream;
440                 stream << slot;
441                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
442                 
443                 if(confirm_dialog(bkg_title, str.c_str()))
444                   {
445                   str = st_save_dir + "/slot" + stream.str() + ".stsg";
446                   printf("Removing: %s\n",str.c_str());
447                   remove(str.c_str());
448                   }
449
450                 update_load_save_game_menu(load_game_menu);
451                 Menu::set_current(main_menu);
452                 resume_demo();
453                 }
454               else if (process_load_game_menu())
455                 {
456                   resume_demo();
457                 }
458             }
459           else if(menu == contrib_menu)
460             {
461               check_levels_contrib_menu();
462             }
463           else if (menu == contrib_subset_menu)
464             {
465               check_contrib_subset_menu();
466             }
467         }
468
469       mouse_cursor->draw(context);
470      
471       context.do_drawing();
472
473       frame_rate.update();
474
475       /* Pause: */
476       frame++;
477       SDL_Delay(25);
478     }
479   /* Free surfaces: */
480
481   free_contrib_menu();
482   worldmap_list.clear();
483   delete titlesession;
484   delete bkg_title;
485   delete logo;
486   delete img_choose_subset;
487 }
488
489
490 // EOF //
491