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>
37 #include "app/defines.h"
38 #include "app/globals.h"
40 #include "video/screen.h"
41 #include "video/surface.h"
42 #include "high_scores.h"
44 #include "special/timer.h"
45 #include "app/setup.h"
47 #include "level_subset.h"
50 #include "leveleditor.h"
56 #include "resources.h"
57 #include "special/base.h"
58 #include "app/gettext.h"
61 static Surface* bkg_title;
63 static Surface* img_choose_subset;
66 static Timer random_timer;
69 static unsigned int last_update_time;
70 static unsigned int update_time;
72 static GameSession* titlesession;
74 static std::vector<LevelSubset*> contrib_subsets;
75 static LevelSubset* current_contrib_subset = 0;
77 static string_list_type worldmap_list;
79 static LevelEditor* leveleditor;
81 void update_load_save_game_menu(Menu* pmenu)
83 for(int i = 2; i < 7; ++i)
85 // FIXME: Insert a real savegame struct/class here instead of
87 std::string tmp = slotinfo(i - 1);
88 pmenu->item[i].kind = MN_ACTION;
89 pmenu->item[i].change_text(tmp.c_str());
93 void free_contrib_menu()
95 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
96 i != contrib_subsets.end(); ++i)
99 contrib_subsets.clear();
100 contrib_menu->clear();
103 void generate_contrib_menu()
105 /** Generating contrib levels list by making use of Level Subset */
106 string_list_type level_subsets = dsubdirs("/levels", "info");
110 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
111 contrib_menu->additem(MN_HL,"",0,0);
113 for (int i = 0; i < level_subsets.num_items; ++i)
115 LevelSubset* subset = new LevelSubset();
116 subset->load(level_subsets.item[i]);
117 contrib_menu->additem(MN_GOTO, subset->title.c_str(), i,
118 contrib_subset_menu, i);
119 contrib_subsets.push_back(subset);
122 for(int i = 0; i < worldmap_list.num_items; i++)
124 WorldMapNS::WorldMap worldmap;
125 worldmap.loadmap(worldmap_list.item[i]);
126 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i + level_subsets.num_items);
129 contrib_menu->additem(MN_HL,"",0,0);
130 contrib_menu->additem(MN_BACK,_("Back"),0,0);
132 string_list_free(&level_subsets);
135 void check_levels_contrib_menu()
137 static int current_subset = -1;
139 int index = contrib_menu->check();
143 if (index < (int)contrib_subsets.size())
145 if (current_subset != index)
147 current_subset = index;
148 // FIXME: This shouln't be busy looping
149 LevelSubset& subset = * (contrib_subsets[index]);
151 current_contrib_subset = ⊂
153 contrib_subset_menu->clear();
155 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
156 contrib_subset_menu->additem(MN_HL,"",0,0);
158 for (int i = 0; i < subset.get_num_levels(); ++i)
160 /** get level's title */
161 std::string level_title = "<no title>";
163 LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
166 std::cerr << "Error: Could not open level file. Ignoring...\n";
170 reader->read_string("name", level_title, true);
173 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
176 contrib_subset_menu->additem(MN_HL,"",0,0);
177 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
179 titlesession->get_current_sector()->activate();
180 titlesession->set_current();
183 else if(index < worldmap_list.num_items + (int)contrib_subsets.size())
185 WorldMapNS::WorldMap worldmap;
186 worldmap.loadmap(worldmap_list.item[index - contrib_subsets.size()]);
189 Menu::set_current(main_menu);
193 void check_contrib_subset_menu()
195 int index = contrib_subset_menu->check();
198 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
200 std::cout << "Starting level: " << index << std::endl;
203 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
205 player_status.reset();
206 Menu::set_current(main_menu);
207 titlesession->get_current_sector()->activate();
208 titlesession->set_current();
213 void draw_demo(double frame_ratio)
215 Sector* world = titlesession->get_current_sector();
216 Player* tux = world->player;
218 world->play_music(LEVEL_MUSIC);
220 global_frame_counter++;
221 tux->key_event((SDLKey) keymap.right,DOWN);
223 if(random_timer.check())
226 tux->key_event((SDLKey) keymap.jump,UP);
228 tux->key_event((SDLKey) keymap.jump,DOWN);
232 random_timer.start(rand() % 3000 + 3000);
236 // Wrap around at the end of the level back to the beginnig
237 if(world->solids->get_width() * 32 - 320 < tux->base.x)
242 tux->can_jump = true;
243 float last_tux_x_pos = tux->base.x;
244 world->action(frame_ratio);
247 // disabled for now, since with the new jump code we easily get deadlocks
248 // Jump if tux stays in the same position for one loop, ie. if he is
249 // stuck behind a wall
250 if (last_tux_x_pos == tux->base.x)
255 world->draw(*titlesession->context);
258 /* --- TITLE SCREEN --- */
261 random_timer.init(true);
265 st_pause_ticks_init();
267 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
270 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
271 logo = new Surface(datadir + "/images/title/logo.png", true);
272 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
274 /* Generating contrib maps by only using a string_list */
275 worldmap_list = dfiles("levels/worldmap", NULL, "icyisland.stwm");
277 titlesession->get_current_sector()->activate();
278 titlesession->set_current();
280 /* --- Main title loop: --- */
283 update_time = st_get_ticks();
284 random_timer.start(rand() % 2000 + 2000);
286 Menu::set_current(main_menu);
287 DrawingContext& context = *titlesession->context;
288 while (Menu::current())
290 // if we spent to much time on a menu entry
291 if( (update_time - last_update_time) > 1000)
292 update_time = last_update_time = st_get_ticks();
294 // Calculate the movement-factor
295 double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
296 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
297 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
298 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
302 while (SDL_PollEvent(&event))
306 Menu::current()->event(event);
308 // FIXME: QUIT signal should be handled more generic, not locally
309 if (event.type == SDL_QUIT)
310 Menu::set_current(0);
313 /* Draw the background: */
314 draw_demo(frame_ratio);
316 if (Menu::current() == main_menu)
317 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
318 LAYER_FOREGROUND1+1);
320 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LAYER_FOREGROUND1);
321 context.draw_text(white_small_text,
322 _("Copyright (c) 2003 SuperTux Devel Team\n"
323 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
324 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
325 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LAYER_FOREGROUND1);
327 /* Don't draw menu, if quit is true */
328 Menu* menu = Menu::current();
334 if(menu == main_menu)
336 switch (main_menu->check())
339 // Start Game, ie. goto the slots menu
340 update_load_save_game_menu(load_game_menu);
342 case MNID_LEVELS_CONTRIB:
344 puts("Entering contrib menu");
345 generate_contrib_menu();
347 case MNID_LEVELEDITOR:
348 leveleditor = new LevelEditor();
351 Menu::set_current(main_menu);
352 update_time = st_get_ticks();
355 display_text_file("CREDITS", SCROLL_SPEED_CREDITS);
356 Menu::set_current(main_menu);
358 case MNID_QUITMAINMENU:
359 Menu::set_current(0);
363 else if(menu == options_menu)
365 process_options_menu();
367 else if(menu == load_game_menu)
369 if(event.key.keysym.sym == SDLK_DELETE)
371 int slot = menu->get_active_item_id();
373 sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
375 if(confirm_dialog(bkg_title, str))
377 sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
378 printf("Removing: %s\n",str);
382 update_load_save_game_menu(load_game_menu);
383 Menu::set_current(main_menu);
384 update_time = st_get_ticks();
386 else if (process_load_game_menu())
388 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
389 titlesession->get_current_sector()->activate();
390 titlesession->set_current();
391 //titletux.level_begin();
392 update_time = st_get_ticks();
395 else if(menu == contrib_menu)
397 check_levels_contrib_menu();
399 else if (menu == contrib_subset_menu)
401 check_contrib_subset_menu();
405 mouse_cursor->draw(context);
407 context.do_drawing();
409 /* Set the time of the last update and the time of the current update */
410 last_update_time = update_time;
411 update_time = st_get_ticks();
420 string_list_free(&worldmap_list);
424 delete img_choose_subset;