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