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