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
30 #include <SDL_image.h>
33 #include <sys/types.h>
40 #include "screen/screen.h"
41 #include "screen/surface.h"
42 #include "high_scores.h"
47 #include "level_subset.h"
50 #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);
106 contrib_subsets.push_back(subset);
109 for(int i = 0; i < worldmap_list.num_items; i++)
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);
116 contrib_menu->additem(MN_HL,"",0,0);
117 contrib_menu->additem(MN_BACK,_("Back"),0,0);
119 string_list_free(&level_subsets);
122 void check_levels_contrib_menu()
124 static int current_subset = -1;
126 int index = contrib_menu->check();
130 if (index < (int)contrib_subsets.size())
132 if (current_subset != index)
134 current_subset = index;
135 // FIXME: This shouln't be busy looping
136 LevelSubset& subset = * (contrib_subsets[index]);
138 current_contrib_subset = ⊂
140 contrib_subset_menu->clear();
142 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
143 contrib_subset_menu->additem(MN_HL,"",0,0);
145 for (int i = 0; i < subset.get_num_levels(); ++i)
147 Level* level = new Level;
148 level->load(subset.get_level_filename(i));
149 contrib_subset_menu->additem(MN_ACTION, level->get_name(), 0, 0, i);
153 contrib_subset_menu->additem(MN_HL,"",0,0);
154 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
156 titlesession->get_current_sector()->activate();
157 titlesession->set_current();
160 else if(index < worldmap_list.num_items + (int)contrib_subsets.size())
162 WorldMapNS::WorldMap worldmap;
163 worldmap.loadmap(worldmap_list.item[index - contrib_subsets.size()]);
166 Menu::set_current(main_menu);
170 void check_contrib_subset_menu()
172 int index = contrib_subset_menu->check();
175 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
177 std::cout << "Starting level: " << index << std::endl;
180 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
182 player_status.reset();
183 Menu::set_current(main_menu);
184 titlesession->get_current_sector()->activate();
185 titlesession->set_current();
190 void draw_demo(double frame_ratio)
192 Sector* world = titlesession->get_current_sector();
193 Player* tux = world->player;
195 world->play_music(LEVEL_MUSIC);
197 global_frame_counter++;
198 tux->key_event((SDLKey) keymap.right,DOWN);
200 if(random_timer.check())
203 tux->key_event((SDLKey) keymap.jump,UP);
205 tux->key_event((SDLKey) keymap.jump,DOWN);
209 random_timer.start(rand() % 3000 + 3000);
213 // Wrap around at the end of the level back to the beginnig
214 if(world->solids->get_width() * 32 - 320 < tux->base.x)
219 tux->can_jump = true;
220 float last_tux_x_pos = tux->base.x;
221 world->action(frame_ratio);
224 // disabled for now, since with the new jump code we easily get deadlocks
225 // Jump if tux stays in the same position for one loop, ie. if he is
226 // stuck behind a wall
227 if (last_tux_x_pos == tux->base.x)
232 world->draw(*titlesession->context);
235 /* --- TITLE SCREEN --- */
238 random_timer.init(true);
242 st_pause_ticks_init();
244 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
247 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
248 logo = new Surface(datadir + "/images/title/logo.png", true);
249 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
251 /* Generating contrib maps by only using a string_list */
252 worldmap_list = dfiles("levels/worldmap", NULL, "icyisland.stwm");
254 titlesession->get_current_sector()->activate();
255 titlesession->set_current();
257 /* --- Main title loop: --- */
260 update_time = st_get_ticks();
261 random_timer.start(rand() % 2000 + 2000);
263 Menu::set_current(main_menu);
264 DrawingContext& context = *titlesession->context;
265 while (Menu::current())
267 // if we spent to much time on a menu entry
268 if( (update_time - last_update_time) > 1000)
269 update_time = last_update_time = st_get_ticks();
271 // Calculate the movement-factor
272 double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
273 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
274 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
275 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
279 while (SDL_PollEvent(&event))
283 Menu::current()->event(event);
285 // FIXME: QUIT signal should be handled more generic, not locally
286 if (event.type == SDL_QUIT)
287 Menu::set_current(0);
290 /* Draw the background: */
291 draw_demo(frame_ratio);
293 if (Menu::current() == main_menu)
294 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
295 LAYER_FOREGROUND1+1);
297 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LAYER_FOREGROUND1);
298 context.draw_text(white_small_text,
299 _("Copyright (c) 2003 SuperTux Devel Team\n"
300 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
301 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
302 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LAYER_FOREGROUND1);
304 /* Don't draw menu, if quit is true */
305 Menu* menu = Menu::current();
311 if(menu == main_menu)
313 switch (main_menu->check())
316 // Start Game, ie. goto the slots menu
317 update_load_save_game_menu(load_game_menu);
319 case MNID_LEVELS_CONTRIB:
321 puts("Entering contrib menu");
322 generate_contrib_menu();
324 case MNID_LEVELEDITOR:
325 leveleditor = new LevelEditor();
328 Menu::set_current(main_menu);
329 update_time = st_get_ticks();
332 display_text_file("CREDITS", SCROLL_SPEED_CREDITS);
333 Menu::set_current(main_menu);
335 case MNID_QUITMAINMENU:
336 Menu::set_current(0);
340 else if(menu == options_menu)
342 process_options_menu();
344 else if(menu == load_game_menu)
346 if(event.key.keysym.sym == SDLK_DELETE)
348 int slot = menu->get_active_item_id();
350 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
352 if(confirm_dialog(bkg_title, str))
354 sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
355 printf("Removing: %s\n",str);
359 update_load_save_game_menu(load_game_menu);
360 Menu::set_current(main_menu);
361 update_time = st_get_ticks();
363 else if (process_load_game_menu())
365 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
366 titlesession->get_current_sector()->activate();
367 titlesession->set_current();
368 //titletux.level_begin();
369 update_time = st_get_ticks();
372 else if(menu == contrib_menu)
374 check_levels_contrib_menu();
376 else if (menu == contrib_subset_menu)
378 check_contrib_subset_menu();
382 mouse_cursor->draw(context);
384 context.do_drawing();
386 /* Set the time of the last update and the time of the current update */
387 last_update_time = update_time;
388 update_time = st_get_ticks();
397 string_list_free(&worldmap_list);
401 delete img_choose_subset;