Norwegian Nynorsk translation by Karl Ove Hufthammer!
[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         /** get level's title */
148         std::string level_title = "<no title>";
149
150         LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
151         if(!reader)
152           {
153           std::cerr << "Error: Could not open level file. Ignoring...\n";
154           return;
155           }
156
157         reader->read_string("name", level_title, true);
158         delete reader;
159
160         contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
161         }
162
163       contrib_subset_menu->additem(MN_HL,"",0,0);      
164       contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
165
166       titlesession->get_current_sector()->activate();
167       titlesession->set_current();
168       }
169     }
170   else if(index < worldmap_list.num_items + (int)contrib_subsets.size())
171     {
172     WorldMapNS::WorldMap worldmap;
173     worldmap.loadmap(worldmap_list.item[index - contrib_subsets.size()]);
174     worldmap.display();
175
176     Menu::set_current(main_menu);
177     }
178 }
179
180 void check_contrib_subset_menu()
181 {
182   int index = contrib_subset_menu->check();
183   if (index != -1)
184     {
185       if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
186         {
187           std::cout << "Starting level: " << index << std::endl;
188           
189           GameSession session(
190               current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
191           session.run();
192           player_status.reset();
193           Menu::set_current(main_menu);
194           titlesession->get_current_sector()->activate();
195           titlesession->set_current();
196         }
197     }  
198 }
199
200 void draw_demo(double frame_ratio)
201 {
202   Sector* world  = titlesession->get_current_sector();
203   Player* tux = world->player;
204
205   world->play_music(LEVEL_MUSIC);
206   
207   global_frame_counter++;
208   tux->key_event((SDLKey) keymap.right,DOWN);
209   
210   if(random_timer.check())
211     {
212       if(walking)
213         tux->key_event((SDLKey) keymap.jump,UP);
214       else
215         tux->key_event((SDLKey) keymap.jump,DOWN);
216     }
217   else
218     {
219       random_timer.start(rand() % 3000 + 3000);
220       walking = !walking;
221     }
222
223   // Wrap around at the end of the level back to the beginnig
224   if(world->solids->get_width() * 32 - 320 < tux->base.x)
225     {
226       tux->level_begin();
227     }
228
229   tux->can_jump = true;
230   float last_tux_x_pos = tux->base.x;
231   world->action(frame_ratio);
232   
233
234   // disabled for now, since with the new jump code we easily get deadlocks
235   // Jump if tux stays in the same position for one loop, ie. if he is
236   // stuck behind a wall
237   if (last_tux_x_pos == tux->base.x)
238     {
239       walking = false;
240     }
241
242   world->draw(*titlesession->context);
243 }
244
245 /* --- TITLE SCREEN --- */
246 void title(void)
247 {
248   random_timer.init(true);
249
250   walking = true;
251
252   st_pause_ticks_init();
253
254   titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
255
256   /* Load images: */
257   bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
258   logo = new Surface(datadir + "/images/title/logo.png", true);
259   img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
260
261   /* Generating contrib maps by only using a string_list */
262   worldmap_list = dfiles("levels/worldmap", NULL, "icyisland.stwm");
263
264   titlesession->get_current_sector()->activate();
265   titlesession->set_current();
266
267   /* --- Main title loop: --- */
268   frame = 0;
269
270   update_time = st_get_ticks();
271   random_timer.start(rand() % 2000 + 2000);
272
273   Menu::set_current(main_menu);
274   DrawingContext& context = *titlesession->context;
275   while (Menu::current())
276     {
277       // if we spent to much time on a menu entry
278       if( (update_time - last_update_time) > 1000)
279         update_time = last_update_time = st_get_ticks();
280
281       // Calculate the movement-factor
282       double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
283       if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
284         frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
285       /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
286       frame_ratio /= 2;
287
288       SDL_Event event;
289       while (SDL_PollEvent(&event))
290         {
291           if (Menu::current())
292             {
293               Menu::current()->event(event);
294             }
295          // FIXME: QUIT signal should be handled more generic, not locally
296           if (event.type == SDL_QUIT)
297             Menu::set_current(0);
298         }
299
300       /* Draw the background: */
301       draw_demo(frame_ratio);
302      
303       if (Menu::current() == main_menu)
304         context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
305             LAYER_FOREGROUND1+1);
306
307       context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LAYER_FOREGROUND1);
308       context.draw_text(white_small_text,
309         _("Copyright (c) 2003 SuperTux Devel Team\n"
310           "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
311           "are welcome to redistribute it under certain conditions; see the file COPYING\n"
312           "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LAYER_FOREGROUND1);
313
314       /* Don't draw menu, if quit is true */
315       Menu* menu = Menu::current();
316       if(menu)
317         {
318           menu->draw(context);
319           menu->action();
320         
321           if(menu == main_menu)
322             {
323               switch (main_menu->check())
324                 {
325                 case MNID_STARTGAME:
326                   // Start Game, ie. goto the slots menu
327                   update_load_save_game_menu(load_game_menu);
328                   break;
329                 case MNID_LEVELS_CONTRIB:
330                   // Contrib Menu
331                   puts("Entering contrib menu");
332                   generate_contrib_menu();
333                   break;
334                 case MNID_LEVELEDITOR:
335                   leveleditor = new LevelEditor();
336                   leveleditor->run();
337                   delete leveleditor;
338                   Menu::set_current(main_menu);
339                   update_time = st_get_ticks();
340                   break;
341                 case MNID_CREDITS:
342                   display_text_file("CREDITS", SCROLL_SPEED_CREDITS);
343                   Menu::set_current(main_menu);
344                   break;
345                 case MNID_QUITMAINMENU:
346                   Menu::set_current(0);
347                   break;
348                 }
349             }
350           else if(menu == options_menu)
351             {
352               process_options_menu();
353             }
354           else if(menu == load_game_menu)
355             {
356               if(event.key.keysym.sym == SDLK_DELETE)
357                 {
358                 int slot = menu->get_active_item_id();
359                 char str[1024];
360                 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
361                 
362                 if(confirm_dialog(bkg_title, str))
363                   {
364                   sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
365                   printf("Removing: %s\n",str);
366                   remove(str);
367                   }
368
369                 update_load_save_game_menu(load_game_menu);
370                 Menu::set_current(main_menu);
371                 update_time = st_get_ticks();
372                 }
373               else if (process_load_game_menu())
374                 {
375                   // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
376                   titlesession->get_current_sector()->activate();
377                   titlesession->set_current();
378                   //titletux.level_begin();
379                   update_time = st_get_ticks();
380                 }
381             }
382           else if(menu == contrib_menu)
383             {
384               check_levels_contrib_menu();
385             }
386           else if (menu == contrib_subset_menu)
387             {
388               check_contrib_subset_menu();
389             }
390         }
391
392       mouse_cursor->draw(context);
393      
394       context.do_drawing();
395
396       /* Set the time of the last update and the time of the current update */
397       last_update_time = update_time;
398       update_time = st_get_ticks();
399
400       /* Pause: */
401       frame++;
402       SDL_Delay(25);
403     }
404   /* Free surfaces: */
405
406   free_contrib_menu();
407   string_list_free(&worldmap_list);
408   delete titlesession;
409   delete bkg_title;
410   delete logo;
411   delete img_choose_subset;
412 }
413
414 // EOF //
415