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