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
32 #include <SDL_image.h>
35 #include <sys/types.h>
39 #include "app/globals.h"
41 #include "video/screen.h"
42 #include "video/surface.h"
45 #include "special/frame_rate.h"
46 #include "app/setup.h"
47 #include "lisp/lisp.h"
48 #include "lisp/parser.h"
50 #include "level_subset.h"
53 #include "leveleditor.h"
54 #include "player_status.h"
57 #include "object/tilemap.h"
58 #include "object/camera.h"
59 #include "object/player.h"
60 #include "resources.h"
61 #include "app/gettext.h"
64 static Surface* bkg_title;
66 static Surface* img_choose_subset;
69 static Timer2 random_timer;
73 static GameSession* titlesession;
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
78 static FrameRate frame_rate(100);
80 /* If the demo was stopped - because game started, level
81 editor was excuted, etc - call this when you get back
86 // FIXME: shouldn't be needed if GameSession
87 // didn't relay on global variables
88 titlesession->get_current_sector()->activate();
89 titlesession->set_current();
94 void update_load_save_game_menu(Menu* pmenu)
96 for(int i = 2; i < 7; ++i)
98 // FIXME: Insert a real savegame struct/class here instead of
100 std::string tmp = slotinfo(i - 1);
101 pmenu->item[i].kind = MN_ACTION;
102 pmenu->item[i].change_text(tmp.c_str());
106 void free_contrib_menu()
108 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
109 i != contrib_subsets.end(); ++i)
112 contrib_subsets.clear();
113 contrib_menu->clear();
116 void generate_contrib_menu()
118 /** Generating contrib levels list by making use of Level Subset */
119 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
123 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
124 contrib_menu->additem(MN_HL,"",0,0);
127 for (std::set<std::string>::iterator it = level_subsets.begin();
128 it != level_subsets.end(); ++it)
130 LevelSubset* subset = new LevelSubset();
132 if(subset->hide_from_contribs) {
136 contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
137 contrib_subsets.push_back(subset);
141 contrib_menu->additem(MN_HL,"",0,0);
142 contrib_menu->additem(MN_BACK,_("Back"),0,0);
144 level_subsets.clear();
147 std::string get_level_name(const std::string& filename)
151 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
153 const lisp::Lisp* level = root->get_lisp("supertux-level");
158 level->get("name", name);
160 } catch(std::exception& e) {
161 std::cerr << "Problem getting name of '" << filename << "'.\n";
166 void check_levels_contrib_menu()
168 static int current_subset = -1;
170 int index = contrib_menu->check();
174 LevelSubset& subset = * (contrib_subsets[index]);
176 if(subset.has_worldmap) {
177 WorldMapNS::WorldMap worldmap;
178 worldmap.set_map_filename(subset.get_worldmap_filename());
182 DrawingContext context;
183 context.draw_text(white_text, "Loading...",
184 Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
185 context.do_drawing();
187 // TODO: slots should be available for contrib maps
188 worldmap.loadgame(st_save_dir + "/" + subset.name + "-slot1.stsg");
190 worldmap.display(); // run the map
192 Menu::set_current(main_menu);
194 } else if (current_subset != index) {
195 current_subset = index;
196 // FIXME: This shouln't be busy looping
197 LevelSubset& subset = * (contrib_subsets[index]);
199 current_contrib_subset = ⊂
201 contrib_subset_menu->clear();
203 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
204 contrib_subset_menu->additem(MN_HL,"",0,0);
206 for (int i = 0; i < subset.get_num_levels(); ++i)
208 /** get level's title */
209 std::string filename = subset.get_level_filename(i);
210 std::string title = get_level_name(filename);
211 contrib_subset_menu->additem(MN_ACTION, title, 0, 0, i);
214 contrib_subset_menu->additem(MN_HL,"",0,0);
215 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
217 titlesession->get_current_sector()->activate();
218 titlesession->set_current();
222 void check_contrib_subset_menu()
224 int index = contrib_subset_menu->check();
227 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
229 std::cout << "Starting level: " << index << std::endl;
232 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
234 player_status.reset();
235 Menu::set_current(main_menu);
241 void draw_demo(float elapsed_time)
243 Sector* world = titlesession->get_current_sector();
244 Player* tux = world->player;
246 world->play_music(LEVEL_MUSIC);
248 tux->key_event((SDLKey) keymap.right, true);
250 if(random_timer.check()) {
251 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
255 tux->key_event((SDLKey) keymap.jump, false);
257 tux->key_event((SDLKey) keymap.jump, true);
260 // Wrap around at the end of the level back to the beginnig
261 if(world->solids->get_width() * 32 - 320 < tux->get_pos().x)
264 world->camera->reset(tux->get_pos());
267 tux->can_jump = true;
268 float last_tux_x_pos = tux->get_pos().x;
269 world->action(elapsed_time);
272 // disabled for now, since with the new jump code we easily get deadlocks
273 // Jump if tux stays in the same position for one loop, ie. if he is
274 // stuck behind a wall
275 if (last_tux_x_pos == tux->get_pos().x)
280 world->draw(*titlesession->context);
283 /* --- TITLE SCREEN --- */
287 LevelEditor* leveleditor;
288 MusicRef credits_music;
292 titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
296 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
297 logo = new Surface(datadir + "/images/title/logo.png", true);
298 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
300 titlesession->get_current_sector()->activate();
301 titlesession->set_current();
303 /* --- Main title loop: --- */
306 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
308 Uint32 lastticks = SDL_GetTicks();
310 Menu::set_current(main_menu);
311 DrawingContext& context = *titlesession->context;
312 while (Menu::current())
314 // Calculate the movement-factor
315 Uint32 ticks = SDL_GetTicks();
316 float elapsed_time = float(ticks - lastticks) / 1000.;
317 global_time += elapsed_time;
320 if(elapsed_time > .04)
323 /* Lower the speed so that Tux doesn't jump too hectically throught
328 while (SDL_PollEvent(&event))
332 Menu::current()->event(event);
334 // FIXME: QUIT signal should be handled more generic, not locally
335 if (event.type == SDL_QUIT)
336 Menu::set_current(0);
339 /* Draw the background: */
340 draw_demo(elapsed_time);
343 if (Menu::current() == main_menu)
344 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
345 LAYER_FOREGROUND1+1);
347 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
348 Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
349 context.draw_text(white_small_text,
351 "Copyright (c) 2003 SuperTux Devel Team\n"
352 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
353 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
356 Vector(0, screen->h - 70 + white_small_text->get_height()),
357 LEFT_ALLIGN, LAYER_FOREGROUND1);
359 /* Don't draw menu, if quit is true */
360 Menu* menu = Menu::current();
366 if(menu == main_menu)
368 switch (main_menu->check())
371 // Start Game, ie. goto the slots menu
372 update_load_save_game_menu(load_game_menu);
374 case MNID_LEVELS_CONTRIB:
376 puts("Entering contrib menu");
377 generate_contrib_menu();
379 case MNID_LEVELEDITOR:
380 leveleditor = new LevelEditor();
383 Menu::set_current(main_menu);
388 credits_music = SoundManager::get()->load_music(datadir + "/music/credits.ogg");
389 SoundManager::get()->play_music(credits_music);
390 display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
392 Menu::set_current(main_menu);
394 case MNID_QUITMAINMENU:
395 Menu::set_current(0);
399 else if(menu == options_menu)
401 process_options_menu();
403 else if(menu == load_game_menu)
405 if(event.key.keysym.sym == SDLK_DELETE)
407 int slot = menu->get_active_item_id();
408 std::stringstream stream;
410 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
412 if(confirm_dialog(bkg_title, str.c_str()))
414 str = st_save_dir + "/slot" + stream.str() + ".stsg";
415 printf("Removing: %s\n",str.c_str());
419 update_load_save_game_menu(load_game_menu);
420 Menu::set_current(main_menu);
423 else if (process_load_game_menu())
428 else if(menu == contrib_menu)
430 check_levels_contrib_menu();
432 else if (menu == contrib_subset_menu)
434 check_contrib_subset_menu();
438 mouse_cursor->draw(context);
440 context.do_drawing();
453 delete img_choose_subset;