4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 // Copyright (C) 2006 Matthias Braun <matze@braunis.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
33 #include <SDL_image.h>
37 #include "mainloop.hpp"
38 #include "video/screen.hpp"
39 #include "video/drawing_context.hpp"
40 #include "video/surface.hpp"
41 #include "audio/sound_manager.hpp"
42 #include "gui/menu.hpp"
44 #include "lisp/lisp.hpp"
45 #include "lisp/parser.hpp"
48 #include "game_session.hpp"
49 #include "worldmap.hpp"
50 #include "player_status.hpp"
53 #include "object/tilemap.hpp"
54 #include "object/camera.hpp"
55 #include "object/player.hpp"
56 #include "resources.hpp"
57 #include "gettext.hpp"
59 #include "textscroller.hpp"
60 #include "file_system.hpp"
61 #include "control/joystickkeyboardcontroller.hpp"
62 #include "control/codecontroller.hpp"
65 #include "console.hpp"
68 TitleScreen::update_load_game_menu()
70 load_game_menu.reset(new Menu());
72 load_game_menu->add_label(_("Start Game"));
73 load_game_menu->add_hl();
74 for(int i = 1; i <= 5; ++i) {
75 load_game_menu->add_entry(i, get_slotinfo(i));
77 load_game_menu->add_hl();
78 load_game_menu->add_back(_("Back"));
82 TitleScreen::free_contrib_menu()
84 for(std::vector<World*>::iterator i = contrib_worlds.begin();
85 i != contrib_worlds.end(); ++i)
88 contrib_worlds.clear();
92 TitleScreen::generate_contrib_menu()
94 /** Generating contrib levels list by making use of Level Subset */
95 std::vector<std::string> level_worlds;
96 char** files = PHYSFS_enumerateFiles("levels/");
97 for(const char* const* filename = files; *filename != 0; ++filename) {
98 std::string filepath = std::string("levels/") + *filename;
99 if(PHYSFS_isDirectory(filepath.c_str()))
100 level_worlds.push_back(filepath);
102 PHYSFS_freeList(files);
105 contrib_menu.reset(new Menu());
107 contrib_menu->add_label(_("Contrib Levels"));
108 contrib_menu->add_hl();
111 for (std::vector<std::string>::iterator it = level_worlds.begin();
112 it != level_worlds.end(); ++it) {
114 std::auto_ptr<World> world (new World());
115 world->load(*it + "/info");
116 if(world->hide_from_contribs) {
119 contrib_menu->add_entry(i++, world->title);
120 contrib_worlds.push_back(world.release());
121 } catch(std::exception& e) {
123 log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
128 contrib_menu->add_hl();
129 contrib_menu->add_back(_("Back"));
133 TitleScreen::get_level_name(const std::string& filename)
137 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
139 const lisp::Lisp* level = root->get_lisp("supertux-level");
144 level->get("name", name);
146 } catch(std::exception& e) {
147 log_warning << "Problem getting name of '" << filename << "'." << std::endl;
153 TitleScreen::check_levels_contrib_menu()
155 int index = contrib_menu->check();
159 current_world = contrib_worlds[index];
161 if(!current_world->is_levelset) {
162 update_load_game_menu();
163 Menu::push_current(load_game_menu.get());
165 contrib_world_menu.reset(new Menu());
167 contrib_world_menu->add_label(current_world->title);
168 contrib_world_menu->add_hl();
170 for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
172 /** get level's title */
173 std::string filename = current_world->get_level_filename(i);
174 std::string title = get_level_name(filename);
175 contrib_world_menu->add_entry(i, title);
178 contrib_world_menu->add_hl();
179 contrib_world_menu->add_back(_("Back"));
181 Menu::push_current(contrib_world_menu.get());
186 TitleScreen::check_contrib_world_menu()
188 int index = contrib_world_menu->check();
190 if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
191 sound_manager->stop_music();
192 GameSession* session =
194 current_world->get_level_filename(index), ST_GL_PLAY);
195 main_loop->push_screen(session);
201 TitleScreen::make_tux_jump()
203 static Timer randomWaitTimer;
204 static Timer jumpPushTimer;
205 static float last_tux_x_pos = -1;
206 static float last_tux_y_pos = -1;
208 Sector* sector = titlesession->get_current_sector();
209 Player* tux = sector->player;
211 //sector->play_music(LEVEL_MUSIC);
213 controller->update();
214 controller->press(Controller::RIGHT);
216 // Determine how far we moved since last frame
217 float dx = fabsf(last_tux_x_pos - tux->get_pos().x);
218 float dy = fabsf(last_tux_y_pos - tux->get_pos().y);
220 // Calculate space to check for obstacles
221 Rect lookahead = tux->get_bbox();
222 lookahead.move(Vector(96, 0));
224 // Check if we should press the jump button
225 bool randomJump = !randomWaitTimer.started();
226 bool notMoving = (fabsf(dx) + fabsf(dy)) < 0.1;
227 bool pathBlocked = !sector->is_free_space(lookahead);
228 if (!controller->released(Controller::JUMP)
229 && (notMoving || pathBlocked || randomJump)) {
234 jumpDuration = float(rand() % 500 + 300) / 1000.0;
235 jumpPushTimer.start(jumpDuration);
236 randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
239 // Keep jump button pressed
240 if (jumpPushTimer.started())
241 controller->press(Controller::JUMP);
243 // Remember last position, so we can determine if we moved
244 last_tux_x_pos = tux->get_pos().x;
245 last_tux_y_pos = tux->get_pos().y;
247 // Wrap around at the end of the level back to the beginnig
248 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
249 sector->activate("main");
250 sector->camera->reset(tux->get_pos());
254 TitleScreen::TitleScreen()
256 controller.reset(new CodeController());
257 titlesession.reset(new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME));
259 Player* player = titlesession->get_current_sector()->player;
260 player->set_controller(controller.get());
263 TitleScreen::~TitleScreen()
270 player_status->reset();
272 Sector* sector = titlesession->get_current_sector();
273 if(Sector::current() != sector) {
274 sector->play_music(LEVEL_MUSIC);
275 sector->activate(sector->player->get_pos());
278 Menu::set_current(main_menu);
284 Sector* sector = titlesession->get_current_sector();
285 sector->deactivate();
289 TitleScreen::draw(DrawingContext& context)
291 Sector* sector = titlesession->get_current_sector();
292 sector->draw(context);
295 if (Menu::current() == main_menu)
296 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
297 LAYER_FOREGROUND1+1);
300 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
301 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
302 context.draw_text(white_small_text,
304 "Copyright (c) 2006 SuperTux Devel Team\n"
305 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
306 "redistribute it under certain conditions; see the file COPYING for details.\n"
308 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
309 LEFT_ALLIGN, LAYER_FOREGROUND1);
313 TitleScreen::update(float elapsed_time)
315 main_loop->set_speed(0.6);
316 Sector* sector = titlesession->get_current_sector();
317 sector->update(elapsed_time);
321 Menu* menu = Menu::current();
325 if(menu == main_menu) {
326 switch (main_menu->check()) {
328 // Start Game, ie. goto the slots menu
329 if(main_world.get() == NULL) {
330 main_world.reset(new World());
331 main_world->load("levels/world1/info");
333 current_world = main_world.get();
334 update_load_game_menu();
335 Menu::push_current(load_game_menu.get());
337 case MNID_LEVELS_CONTRIB:
339 generate_contrib_menu();
340 Menu::push_current(contrib_menu.get());
344 main_loop->push_screen(new TextScroller("credits.txt"));
346 case MNID_QUITMAINMENU:
350 } else if(menu == load_game_menu.get()) {
352 if(event.key.keysym.sym == SDLK_DELETE) {
353 int slot = menu->get_active_item_id();
354 std::stringstream stream;
356 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
358 if(confirm_dialog(bkg_title, str.c_str())) {
359 str = "save/slot" + stream.str() + ".stsg";
360 log_debug << "Removing: " << str << std::endl;
361 PHYSFS_delete(str.c_str());
364 update_load_save_game_menu(load_game_menu);
365 Menu::set_current(main_menu);
367 process_load_game_menu();
368 } else if(menu == contrib_menu.get()) {
369 check_levels_contrib_menu();
370 } else if (menu == contrib_world_menu.get()) {
371 check_contrib_world_menu();
375 // reopen menu of user closed it (so that the app doesn't close when user
376 // accidently hit ESC)
377 if(Menu::current() == 0) {
378 Menu::set_current(main_menu);
383 TitleScreen::get_slotinfo(int slot)
388 std::string basename = current_world->get_basedir();
389 basename = basename.substr(0, basename.length()-1);
390 std::string worlddirname = FileSystem::basename(basename);
391 std::ostringstream stream;
392 stream << "save/" << worlddirname << "_" << slot << ".stsg";
393 std::string slotfile = stream.str();
397 std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
399 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
401 throw std::runtime_error("file is not a supertux-savegame.");
403 savegame->get("title", title);
404 } catch(std::exception& e) {
405 std::ostringstream slottitle;
406 slottitle << _("Slot") << " " << slot << " - " << _("Free");
407 return slottitle.str();
410 std::ostringstream slottitle;
411 slottitle << _("Slot") << " " << slot << " - " << title;
412 return slottitle.str();
416 TitleScreen::process_load_game_menu()
418 int slot = load_game_menu->check();
423 if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
426 std::string basename = current_world->get_basedir();
427 basename = basename.substr(0, basename.length()-1);
428 std::string worlddirname = FileSystem::basename(basename);
429 std::stringstream stream;
430 stream << "save/" << worlddirname << "_" << slot << ".stsg";
431 std::string slotfile = stream.str();
436 current_world->set_savegame_filename(slotfile);
437 current_world->run();
438 } catch(std::exception& e) {
439 log_fatal << "Couldn't start world: " << e.what() << std::endl;