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