4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
29 #include <SDL_image.h>
32 #include <sys/types.h>
39 #include "screen/screen.h"
40 #include "screen/surface.h"
41 #include "high_scores.h"
46 #include "level_subset.h"
49 #include "leveleditor.h"
56 #include "resources.h"
60 static Surface* bkg_title;
62 static Surface* img_choose_subset;
65 static Timer random_timer;
68 static unsigned int last_update_time;
69 static unsigned int update_time;
71 static GameSession* titlesession;
73 static std::vector<LevelSubset*> contrib_subsets;
74 static LevelSubset* current_contrib_subset = 0;
76 static string_list_type worldmap_list;
78 static LevelEditor* leveleditor;
80 void free_contrib_menu()
82 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
83 i != contrib_subsets.end(); ++i)
86 contrib_subsets.clear();
87 contrib_menu->clear();
90 void generate_contrib_menu()
92 /** Generating contrib levels list by making use of Level Subset */
93 string_list_type level_subsets = dsubdirs("/levels", "info");
97 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
98 contrib_menu->additem(MN_HL,"",0,0);
100 for (int i = 0; i < level_subsets.num_items; ++i)
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);
109 contrib_menu->additem(MN_HL,"",0,0);
110 contrib_menu->additem(MN_BACK,_("Back"),0,0);
112 string_list_free(&level_subsets);
115 void check_levels_contrib_menu()
117 static int current_subset = -1;
119 int index = contrib_menu->check();
123 if (index >= 0 && index <= int(contrib_subsets.size()))
125 if (current_subset != index)
127 current_subset = index;
128 // FIXME: This shouln't be busy looping
129 LevelSubset& subset = * (contrib_subsets[index]);
131 current_contrib_subset = ⊂
133 contrib_subset_menu->clear();
135 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
136 contrib_subset_menu->additem(MN_HL,"",0,0);
138 for (int i = 0; i < subset.get_num_levels(); ++i)
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);
146 contrib_subset_menu->additem(MN_HL,"",0,0);
147 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
149 titlesession->get_current_sector()->activate();
150 titlesession->set_current();
160 void check_contrib_subset_menu()
162 int index = contrib_subset_menu->check();
165 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
167 std::cout << "Starting level: " << index << std::endl;
170 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
172 player_status.reset();
173 Menu::set_current(main_menu);
174 titlesession->get_current_sector()->activate();
175 titlesession->set_current();
180 void check_contrib_worldmap_menu()
182 int index = contrib_worldmap_menu->check();
185 WorldMapNS::WorldMap worldmap;
186 worldmap.loadmap(worldmap_list.item[index]);
189 Menu::set_current(main_menu);
193 void draw_demo(double frame_ratio)
195 Sector* world = titlesession->get_current_sector();
196 Player* tux = world->player;
198 world->play_music(LEVEL_MUSIC);
200 global_frame_counter++;
201 tux->key_event((SDLKey) keymap.right,DOWN);
203 if(random_timer.check())
206 tux->key_event((SDLKey) keymap.jump,UP);
208 tux->key_event((SDLKey) keymap.jump,DOWN);
212 random_timer.start(rand() % 3000 + 3000);
216 // Wrap around at the end of the level back to the beginnig
217 if(world->solids->get_width() * 32 - 320 < tux->base.x)
222 tux->can_jump = true;
223 float last_tux_x_pos = tux->base.x;
224 world->action(frame_ratio);
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)
235 world->draw(*titlesession->context);
238 /* --- TITLE SCREEN --- */
241 random_timer.init(true);
245 st_pause_ticks_init();
247 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
250 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
251 logo = new Surface(datadir + "/images/title/logo.png", true);
252 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
254 /* Generating contrib maps by only using a string_list */
255 worldmap_list = dfiles("levels/worldmap", NULL, NULL);
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++)
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);
265 contrib_worldmap_menu->additem(MN_HL,"",0,0);
266 contrib_worldmap_menu->additem(MN_BACK,"Back",0,0);
268 titlesession->get_current_sector()->activate();
269 titlesession->set_current();
271 /* --- Main title loop: --- */
274 update_time = st_get_ticks();
275 random_timer.start(rand() % 2000 + 2000);
277 Menu::set_current(main_menu);
278 DrawingContext& context = *titlesession->context;
279 while (Menu::current())
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();
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. */
293 while (SDL_PollEvent(&event))
297 Menu::current()->event(event);
299 // FIXME: QUIT signal should be handled more generic, not locally
300 if (event.type == SDL_QUIT)
301 Menu::set_current(0);
304 /* Draw the background: */
305 draw_demo(frame_ratio);
307 if (Menu::current() == main_menu)
308 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
309 LAYER_FOREGROUND1+1);
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);
318 /* Don't draw menu, if quit is true */
319 Menu* menu = Menu::current();
325 if(menu == main_menu)
327 switch (main_menu->check())
330 // Start Game, ie. goto the slots menu
331 update_load_save_game_menu(load_game_menu);
333 case MNID_WORLDMAP_CONTRIB:
335 case MNID_LEVELS_CONTRIB:
337 puts("Entering contrib menu");
338 generate_contrib_menu();
340 case MNID_LEVELEDITOR:
341 leveleditor = new LevelEditor();
344 Menu::set_current(main_menu);
345 update_time = st_get_ticks();
348 display_text_file("CREDITS", SCROLL_SPEED_CREDITS);
349 Menu::set_current(main_menu);
351 case MNID_QUITMAINMENU:
352 Menu::set_current(0);
356 else if(menu == options_menu)
358 process_options_menu();
360 else if(menu == load_game_menu)
362 if(event.key.keysym.sym == SDLK_DELETE)
364 int slot = menu->get_active_item_id();
366 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
368 if(confirm_dialog(bkg_title, str))
370 sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
371 printf("Removing: %s\n",str);
375 update_load_save_game_menu(load_game_menu);
376 Menu::set_current(main_menu);
377 update_time = st_get_ticks();
379 else if (process_load_game_menu())
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();
388 else if(menu == contrib_menu)
390 check_levels_contrib_menu();
392 else if (menu == contrib_subset_menu)
394 check_contrib_subset_menu();
396 else if(menu == contrib_worldmap_menu)
398 check_contrib_worldmap_menu();
402 mouse_cursor->draw(context);
404 context.do_drawing();
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();
420 delete img_choose_subset;