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 /** get level's title */
148 std::string level_title = "<no title>";
150 LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
153 std::cerr << "Error: Could not open level file. Ignoring...\n";
157 reader->read_string("name", level_title, true);
160 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
163 contrib_subset_menu->additem(MN_HL,"",0,0);
164 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
166 titlesession->get_current_sector()->activate();
167 titlesession->set_current();
170 else if(index < worldmap_list.num_items + (int)contrib_subsets.size())
172 WorldMapNS::WorldMap worldmap;
173 worldmap.loadmap(worldmap_list.item[index - contrib_subsets.size()]);
176 Menu::set_current(main_menu);
180 void check_contrib_subset_menu()
182 int index = contrib_subset_menu->check();
185 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
187 std::cout << "Starting level: " << index << std::endl;
190 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
192 player_status.reset();
193 Menu::set_current(main_menu);
194 titlesession->get_current_sector()->activate();
195 titlesession->set_current();
200 void draw_demo(double frame_ratio)
202 Sector* world = titlesession->get_current_sector();
203 Player* tux = world->player;
205 world->play_music(LEVEL_MUSIC);
207 global_frame_counter++;
208 tux->key_event((SDLKey) keymap.right,DOWN);
210 if(random_timer.check())
213 tux->key_event((SDLKey) keymap.jump,UP);
215 tux->key_event((SDLKey) keymap.jump,DOWN);
219 random_timer.start(rand() % 3000 + 3000);
223 // Wrap around at the end of the level back to the beginnig
224 if(world->solids->get_width() * 32 - 320 < tux->base.x)
229 tux->can_jump = true;
230 float last_tux_x_pos = tux->base.x;
231 world->action(frame_ratio);
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)
242 world->draw(*titlesession->context);
245 /* --- TITLE SCREEN --- */
248 random_timer.init(true);
252 st_pause_ticks_init();
254 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
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);
261 /* Generating contrib maps by only using a string_list */
262 worldmap_list = dfiles("levels/worldmap", NULL, "icyisland.stwm");
264 titlesession->get_current_sector()->activate();
265 titlesession->set_current();
267 /* --- Main title loop: --- */
270 update_time = st_get_ticks();
271 random_timer.start(rand() % 2000 + 2000);
273 Menu::set_current(main_menu);
274 DrawingContext& context = *titlesession->context;
275 while (Menu::current())
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();
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. */
289 while (SDL_PollEvent(&event))
293 Menu::current()->event(event);
295 // FIXME: QUIT signal should be handled more generic, not locally
296 if (event.type == SDL_QUIT)
297 Menu::set_current(0);
300 /* Draw the background: */
301 draw_demo(frame_ratio);
303 if (Menu::current() == main_menu)
304 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
305 LAYER_FOREGROUND1+1);
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);
314 /* Don't draw menu, if quit is true */
315 Menu* menu = Menu::current();
321 if(menu == main_menu)
323 switch (main_menu->check())
326 // Start Game, ie. goto the slots menu
327 update_load_save_game_menu(load_game_menu);
329 case MNID_LEVELS_CONTRIB:
331 puts("Entering contrib menu");
332 generate_contrib_menu();
334 case MNID_LEVELEDITOR:
335 leveleditor = new LevelEditor();
338 Menu::set_current(main_menu);
339 update_time = st_get_ticks();
342 display_text_file("CREDITS", SCROLL_SPEED_CREDITS);
343 Menu::set_current(main_menu);
345 case MNID_QUITMAINMENU:
346 Menu::set_current(0);
350 else if(menu == options_menu)
352 process_options_menu();
354 else if(menu == load_game_menu)
356 if(event.key.keysym.sym == SDLK_DELETE)
358 int slot = menu->get_active_item_id();
360 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
362 if(confirm_dialog(bkg_title, str))
364 sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
365 printf("Removing: %s\n",str);
369 update_load_save_game_menu(load_game_menu);
370 Menu::set_current(main_menu);
371 update_time = st_get_ticks();
373 else if (process_load_game_menu())
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();
382 else if(menu == contrib_menu)
384 check_levels_contrib_menu();
386 else if (menu == contrib_subset_menu)
388 check_contrib_subset_menu();
392 mouse_cursor->draw(context);
394 context.do_drawing();
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();
407 string_list_free(&worldmap_list);
411 delete img_choose_subset;