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"
48 #include "leveleditor.h"
55 #include "resources.h"
58 static Surface* bkg_title;
60 static Surface* img_choose_subset;
63 static Timer random_timer;
66 static unsigned int last_update_time;
67 static unsigned int update_time;
69 static GameSession* titlesession;
71 static std::vector<LevelSubset*> contrib_subsets;
72 static LevelSubset* current_contrib_subset = 0;
74 static LevelEditor* leveleditor;
76 void free_contrib_menu()
78 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
79 i != contrib_subsets.end(); ++i)
82 contrib_subsets.clear();
83 contrib_menu->clear();
86 void generate_contrib_menu()
88 string_list_type level_subsets = dsubdirs("/levels", "info");
92 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
93 contrib_menu->additem(MN_HL,"",0,0);
95 for (int i = 0; i < level_subsets.num_items; ++i)
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);
104 contrib_menu->additem(MN_HL,"",0,0);
105 contrib_menu->additem(MN_BACK,_("Back"),0,0);
107 string_list_free(&level_subsets);
110 void check_contrib_menu()
112 static int current_subset = -1;
114 int index = contrib_menu->check();
118 if (index >= 0 && index <= int(contrib_subsets.size()))
120 if (current_subset != index)
122 current_subset = index;
123 // FIXME: This shouln't be busy looping
124 LevelSubset& subset = * (contrib_subsets[index]);
126 current_contrib_subset = ⊂
128 contrib_subset_menu->clear();
130 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
131 contrib_subset_menu->additem(MN_HL,"",0,0);
133 for (int i = 0; i < subset.get_num_levels(); ++i)
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);
141 contrib_subset_menu->additem(MN_HL,"",0,0);
142 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
144 titlesession->get_current_sector()->activate();
145 titlesession->set_current();
155 void check_contrib_subset_menu()
157 int index = contrib_subset_menu->check();
160 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
162 std::cout << "Starting level: " << index << std::endl;
165 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
167 player_status.reset();
168 Menu::set_current(main_menu);
169 titlesession->get_current_sector()->activate();
170 titlesession->set_current();
175 void draw_demo(double frame_ratio)
177 Sector* world = titlesession->get_current_sector();
178 Player* tux = world->player;
180 world->play_music(LEVEL_MUSIC);
182 global_frame_counter++;
183 tux->key_event((SDLKey) keymap.right,DOWN);
185 if(random_timer.check())
188 tux->key_event((SDLKey) keymap.jump,UP);
190 tux->key_event((SDLKey) keymap.jump,DOWN);
194 random_timer.start(rand() % 3000 + 3000);
198 // Wrap around at the end of the level back to the beginnig
199 if(world->solids->get_width() * 32 - 320 < tux->base.x)
204 tux->can_jump = true;
205 float last_tux_x_pos = tux->base.x;
206 world->action(frame_ratio);
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)
217 world->draw(*titlesession->context);
220 /* --- TITLE SCREEN --- */
223 random_timer.init(true);
227 st_pause_ticks_init();
229 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
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);
236 /* --- Main title loop: --- */
239 update_time = st_get_ticks();
240 random_timer.start(rand() % 2000 + 2000);
242 Menu::set_current(main_menu);
243 DrawingContext& context = *titlesession->context;
244 while (Menu::current())
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();
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. */
258 while (SDL_PollEvent(&event))
262 Menu::current()->event(event);
264 // FIXME: QUIT signal should be handled more generic, not locally
265 if (event.type == SDL_QUIT)
266 Menu::set_current(0);
269 /* Draw the background: */
270 draw_demo(frame_ratio);
272 if (Menu::current() == main_menu)
273 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
274 LAYER_FOREGROUND1+1);
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);
283 /* Don't draw menu, if quit is true */
284 Menu* menu = Menu::current();
290 if(menu == main_menu)
292 switch (main_menu->check())
295 // Start Game, ie. goto the slots menu
296 update_load_save_game_menu(load_game_menu);
300 puts("Entering contrib menu");
301 generate_contrib_menu();
303 case MNID_LEVELEDITOR:
304 leveleditor = new LevelEditor();
307 Menu::set_current(main_menu);
310 display_text_file("CREDITS", bkg_title, SCROLL_SPEED_CREDITS);
311 Menu::set_current(main_menu);
313 case MNID_QUITMAINMENU:
314 Menu::set_current(0);
318 else if(menu == options_menu)
320 process_options_menu();
322 else if(menu == load_game_menu)
324 if(event.key.keysym.sym == SDLK_DELETE)
326 int slot = menu->get_active_item_id();
328 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
330 if(confirm_dialog(bkg_title, str))
332 sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
333 printf("Removing: %s\n",str);
337 update_load_save_game_menu(load_game_menu);
338 Menu::set_current(main_menu);
339 update_time = st_get_ticks();
341 else if (process_load_game_menu())
343 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
344 titlesession->get_current_sector()->activate();
345 titlesession->set_current();
346 //titletux.level_begin();
347 update_time = st_get_ticks();
350 else if(menu == contrib_menu)
352 check_contrib_menu();
354 else if (menu == contrib_subset_menu)
356 check_contrib_subset_menu();
360 mouse_cursor->draw(context);
362 context.do_drawing();
364 /* Set the time of the last update and the time of the current update */
365 last_update_time = update_time;
366 update_time = st_get_ticks();
378 delete img_choose_subset;