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