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