3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "app/globals.h"
30 #include "app/gettext.h"
31 #include "app/setup.h"
32 #include "video/surface.h"
33 #include "video/screen.h"
34 #include "video/drawing_context.h"
35 #include "special/frame_rate.h"
36 #include "audio/sound_manager.h"
37 #include "lisp/parser.h"
38 #include "lisp/lisp.h"
39 #include "lisp/list_iterator.h"
40 #include "lisp/writer.h"
44 #include "resources.h"
46 #include "player_status.h"
48 #define map_message_TIME 2.8
50 Menu* worldmap_menu = 0;
52 static const float TUXSPEED = 200;
54 namespace WorldMapNS {
56 Direction reverse_dir(Direction direction)
75 direction_to_string(Direction direction)
93 string_to_direction(const std::string& directory)
95 if (directory == "west")
97 else if (directory == "east")
99 else if (directory == "north")
101 else if (directory == "south")
107 //---------------------------------------------------------------------------
109 Tux::Tux(WorldMap* worldmap_)
110 : worldmap(worldmap_)
112 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
113 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
114 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
118 tile_pos.x = worldmap->get_start_x();
119 tile_pos.y = worldmap->get_start_y();
121 input_direction = D_NONE;
126 delete smalltux_sprite;
127 delete firetux_sprite;
128 delete largetux_sprite;
132 Tux::draw(DrawingContext& context, const Vector& offset)
134 Vector pos = get_pos();
135 switch (player_status.bonus)
137 case PlayerStatus::GROWUP_BONUS:
138 context.draw_surface(largetux_sprite,
139 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
141 case PlayerStatus::FLOWER_BONUS:
142 context.draw_surface(firetux_sprite,
143 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
145 case PlayerStatus::NO_BONUS:
146 context.draw_surface(smalltux_sprite,
147 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
156 float x = tile_pos.x * 32;
157 float y = tile_pos.y * 32;
177 return Vector((int)x, (int)y);
185 input_direction = D_NONE;
190 Tux::set_direction(Direction dir)
192 input_direction = dir;
196 Tux::action(float delta)
200 if (input_direction != D_NONE)
202 WorldMap::Level* level = worldmap->at_level();
204 // We got a new direction, so lets start walking when possible
206 if ((!level || level->solved)
207 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
209 tile_pos = next_tile;
211 direction = input_direction;
212 back_direction = reverse_dir(direction);
214 else if (input_direction == back_direction)
217 direction = input_direction;
218 tile_pos = worldmap->get_next_tile(tile_pos, direction);
219 back_direction = reverse_dir(direction);
226 offset += TUXSPEED * delta;
229 { // We reached the next tile, so we check what to do now
232 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
233 if(special_tile && special_tile->passive_message)
234 { // direction and the apply_action_ are opposites, since they "see"
235 // directions in a different way
236 if((direction == D_NORTH && special_tile->apply_action_south) ||
237 (direction == D_SOUTH && special_tile->apply_action_north) ||
238 (direction == D_WEST && special_tile->apply_action_east) ||
239 (direction == D_EAST && special_tile->apply_action_west))
241 worldmap->passive_message = special_tile->map_message;
242 worldmap->passive_message_timer.start(map_message_TIME);
246 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
247 (special_tile && !special_tile->passive_message) ||
248 worldmap->at_level())
250 if(special_tile && !special_tile->map_message.empty() &&
251 !special_tile->passive_message)
252 worldmap->passive_message_timer.start(0);
257 const Tile* tile = worldmap->at(tile_pos);
258 if (direction != input_direction)
260 // Turn to a new direction
261 const Tile* tile = worldmap->at(tile_pos);
263 if((tile->getData() & Tile::WORLDMAP_NORTH
264 && input_direction == D_NORTH) ||
265 (tile->getData() & Tile::WORLDMAP_SOUTH
266 && input_direction == D_SOUTH) ||
267 (tile->getData() & Tile::WORLDMAP_EAST
268 && input_direction == D_EAST) ||
269 (tile->getData() & Tile::WORLDMAP_WEST
270 && input_direction == D_WEST))
271 { // player has changed direction during auto-movement
272 direction = input_direction;
273 back_direction = reverse_dir(direction);
276 { // player has changed to impossible tile
277 back_direction = reverse_dir(direction);
283 Direction dir = D_NONE;
285 if (tile->getData() & Tile::WORLDMAP_NORTH
286 && back_direction != D_NORTH)
288 else if (tile->getData() & Tile::WORLDMAP_SOUTH
289 && back_direction != D_SOUTH)
291 else if (tile->getData() & Tile::WORLDMAP_EAST
292 && back_direction != D_EAST)
294 else if (tile->getData() & Tile::WORLDMAP_WEST
295 && back_direction != D_WEST)
301 input_direction = direction;
302 back_direction = reverse_dir(direction);
306 // Should never be reached if tiledata is good
312 // Walk automatically to the next tile
313 if(direction != D_NONE)
316 if (worldmap->path_ok(direction, tile_pos, &next_tile))
318 tile_pos = next_tile;
322 puts("Tilemap data is buggy");
331 //---------------------------------------------------------------------------
335 tile_manager = new TileManager("images/worldmap/antarctica.stwt");
345 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
346 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
347 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
348 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
353 music = "salcon.mod";
354 intro_displayed = false;
359 WorldMap::~WorldMap()
364 delete leveldot_green;
367 delete teleporterdot;
370 // Don't forget to set map_filename before calling this
374 levels_path = FileSystem::dirname(map_filename);
378 std::string filename = get_resource_filename(map_filename);
379 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
381 const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
383 throw new std::runtime_error("file isn't a supertux-worldmap file.");
385 lisp::ListIterator iter(lisp);
387 if(iter.item() == "tilemap") {
388 if(tilemap.size() > 0)
389 throw new std::runtime_error("multiple tilemaps specified");
391 const lisp::Lisp* tilemap_lisp = iter.lisp();
392 tilemap_lisp->get("width", width);
393 tilemap_lisp->get("height", height);
394 tilemap_lisp->get_vector("data", tilemap);
395 } else if(iter.item() == "properties") {
396 const lisp::Lisp* props = iter.lisp();
397 props->get("name", name);
398 props->get("music", music);
399 props->get("intro-filename", intro_filename);
400 props->get("start_pos_x", start_x);
401 props->get("start_pos_y", start_y);
402 } else if(iter.item() == "special-tiles") {
403 parse_special_tiles(iter.lisp());
405 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
411 } catch(std::exception& e) {
412 std::stringstream msg;
413 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
415 throw std::runtime_error(msg.str());
420 WorldMap::parse_special_tiles(const lisp::Lisp* lisp)
422 lisp::ListIterator iter(lisp);
424 if(iter.item() == "special-tile") {
425 SpecialTile special_tile;
427 const lisp::Lisp* lisp = iter.lisp();
428 lisp->get("x", special_tile.pos.x);
429 lisp->get("y", special_tile.pos.y);
430 lisp->get("map-message", special_tile.map_message);
431 special_tile.passive_message = false;
432 lisp->get("passive-message", special_tile.passive_message);
433 special_tile.teleport_dest = Vector(-1,-1);
434 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
435 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
436 special_tile.invisible = false;
437 lisp->get("invisible-tile", special_tile.invisible);
439 special_tile.apply_action_north = true;
440 special_tile.apply_action_south = true;
441 special_tile.apply_action_east = true;
442 special_tile.apply_action_west = true;
444 std::string apply_direction;
445 lisp->get("apply-to-direction", apply_direction);
446 if(!apply_direction.empty()) {
447 special_tile.apply_action_north = false;
448 special_tile.apply_action_south = false;
449 special_tile.apply_action_east = false;
450 special_tile.apply_action_west = false;
451 if(apply_direction.find("north") != std::string::npos)
452 special_tile.apply_action_north = true;
453 if(apply_direction.find("south") != std::string::npos)
454 special_tile.apply_action_south = true;
455 if(apply_direction.find("east") != std::string::npos)
456 special_tile.apply_action_east = true;
457 if(apply_direction.find("west") != std::string::npos)
458 special_tile.apply_action_west = true;
461 special_tiles.push_back(special_tile);
462 } else if(iter.item() == "level") {
465 lisp::Lisp* level_lisp = iter.lisp();
466 level.solved = false;
473 level_lisp->get("extro-filename", level.extro_filename);
474 level_lisp->get("next-worldmap", level.next_worldmap);
476 level.quit_worldmap = false;
477 level_lisp->get("quit-worldmap", level.quit_worldmap);
479 level_lisp->get("name", level.name);
480 level_lisp->get("x", level.pos.x);
481 level_lisp->get("y", level.pos.y);
483 level.auto_path = true;
484 level_lisp->get("auto-path", level.auto_path);
486 level.vertical_flip = false;
487 level_lisp->get("vertical-flip", level.vertical_flip);
489 levels.push_back(level);
491 std::cerr << "Unknown token '" << iter.item() <<
492 "' in worldmap special-tiles list.";
498 WorldMap::get_level_title(Level& level)
500 /** get special_tile's title */
501 level.title = "<no title>";
505 std::auto_ptr<lisp::Lisp> root (
506 parser.parse(get_resource_filename(levels_path + level.name)));
508 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
512 level_lisp->get("name", level.title);
513 } catch(std::exception& e) {
514 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
519 void WorldMap::calculate_total_stats()
522 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
526 total_stats += i->statistics;
532 WorldMap::on_escape_press()
534 // Show or hide the menu
537 Menu::set_current(worldmap_menu);
538 tux->set_direction(D_NONE); // stop tux movement when menu is called
541 Menu::set_current(0);
545 WorldMap::get_input()
551 while (SDL_PollEvent(&event))
555 Menu::current()->event(event);
562 Termination::abort("Received window close", "");
566 key = event.key.keysym.sym;
568 if(key == SDLK_ESCAPE)
570 else if(key == SDLK_RETURN || key == keymap.power)
572 else if(key == SDLK_LEFT || key == keymap.power)
573 tux->set_direction(D_WEST);
574 else if(key == SDLK_RIGHT || key == keymap.right)
575 tux->set_direction(D_EAST);
576 else if(key == SDLK_UP || key == keymap.up ||
578 // there might be ppl that use jump as up key
579 tux->set_direction(D_NORTH);
580 else if(key == SDLK_DOWN || key == keymap.down)
581 tux->set_direction(D_SOUTH);
584 case SDL_JOYHATMOTION:
585 if(event.jhat.value & SDL_HAT_UP) {
586 tux->set_direction(D_NORTH);
587 } else if(event.jhat.value & SDL_HAT_DOWN) {
588 tux->set_direction(D_SOUTH);
589 } else if(event.jhat.value & SDL_HAT_LEFT) {
590 tux->set_direction(D_WEST);
591 } else if(event.jhat.value & SDL_HAT_RIGHT) {
592 tux->set_direction(D_EAST);
596 case SDL_JOYAXISMOTION:
597 if (event.jaxis.axis == joystick_keymap.x_axis)
599 if (event.jaxis.value < -joystick_keymap.dead_zone)
600 tux->set_direction(D_WEST);
601 else if (event.jaxis.value > joystick_keymap.dead_zone)
602 tux->set_direction(D_EAST);
604 else if (event.jaxis.axis == joystick_keymap.y_axis)
606 if (event.jaxis.value > joystick_keymap.dead_zone)
607 tux->set_direction(D_SOUTH);
608 else if (event.jaxis.value < -joystick_keymap.dead_zone)
609 tux->set_direction(D_NORTH);
613 case SDL_JOYBUTTONDOWN:
614 if (event.jbutton.button == joystick_keymap.b_button)
616 else if (event.jbutton.button == joystick_keymap.start_button)
628 WorldMap::get_next_tile(Vector pos, Direction direction)
651 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
653 *new_pos = get_next_tile(old_pos, direction);
655 if (!(new_pos->x >= 0 && new_pos->x < width
656 && new_pos->y >= 0 && new_pos->y < height))
657 { // New position is outsite the tilemap
661 { // Check if the tile allows us to go to new_pos
665 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
666 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
669 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
670 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
673 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
674 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
677 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
678 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
681 assert(!"path_ok() can't work if direction is NONE");
688 WorldMap::update(float delta)
690 if (enter_level && !tux->is_moving())
692 /* Check special tile action */
693 SpecialTile* special_tile = at_special_tile();
696 if (special_tile->teleport_dest != Vector(-1,-1))
698 // TODO: an animation, camera scrolling or a fading would be a nice touch
699 SoundManager::get()->play_sound(IDToSound(SND_WARP));
700 tux->back_direction = D_NONE;
701 tux->set_tile_pos(special_tile->teleport_dest);
706 /* Check level action */
707 bool level_finished = true;
708 Level* level = at_level();
711 std::cout << "No level to enter at: "
712 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
718 if (level->pos == tux->get_tile_pos())
720 PlayerStatus old_player_status = player_status;
722 std::cout << "Enter the current level: " << level->name << std::endl;
723 // do a shriking fade to the level
724 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
725 (level->pos.y*32 + 16 + offset.y)), 500);
726 GameSession session(get_resource_filename(levels_path + level->name),
727 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
729 switch (session.run())
731 case GameSession::ES_LEVEL_FINISHED:
733 level_finished = true;
734 bool old_level_state = level->solved;
735 level->solved = true;
737 // deal with statistics
738 level->statistics.merge(global_stats);
739 calculate_total_stats();
741 if (session.get_current_sector()->player->got_power !=
742 session.get_current_sector()->player->NONE_POWER)
743 player_status.bonus = PlayerStatus::FLOWER_BONUS;
744 else if (session.get_current_sector()->player->size == BIG)
745 player_status.bonus = PlayerStatus::GROWUP_BONUS;
747 player_status.bonus = PlayerStatus::NO_BONUS;
749 if (old_level_state != level->solved && level->auto_path)
750 { // Try to detect the next direction to which we should walk
751 // FIXME: Mostly a hack
752 Direction dir = D_NONE;
754 const Tile* tile = at(tux->get_tile_pos());
756 if (tile->getData() & Tile::WORLDMAP_NORTH
757 && tux->back_direction != D_NORTH)
759 else if (tile->getData() & Tile::WORLDMAP_SOUTH
760 && tux->back_direction != D_SOUTH)
762 else if (tile->getData() & Tile::WORLDMAP_EAST
763 && tux->back_direction != D_EAST)
765 else if (tile->getData() & Tile::WORLDMAP_WEST
766 && tux->back_direction != D_WEST)
771 tux->set_direction(dir);
772 //tux->update(delta);
775 std::cout << "Walk to dir: " << dir << std::endl;
780 case GameSession::ES_LEVEL_ABORT:
781 level_finished = false;
782 /* In case the player's abort the level, keep it using the old
783 status. But the minimum lives and no bonus. */
784 player_status.distros = old_player_status.distros;
785 player_status.lives = std::min(old_player_status.lives, player_status.lives);
786 player_status.bonus = player_status.NO_BONUS;
789 case GameSession::ES_GAME_OVER:
791 level_finished = false;
792 /* draw an end screen */
793 /* TODO: in the future, this should make a dialog a la SuperMario, asking
794 if the player wants to restart the world map with no score and from
798 DrawingContext context;
799 context.draw_gradient(Color (200,240,220), Color(200,200,220),
802 context.draw_text(blue_text, _("GAMEOVER"),
803 Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
805 sprintf(str, _("COINS: %d"), player_status.distros);
806 context.draw_text(gold_text, str,
807 Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN,
810 total_stats.draw_message_info(context, _("Total Statistics"));
812 context.do_drawing();
815 wait_for_event(event,2000,6000,true);
818 player_status.reset();
821 case GameSession::ES_NONE:
823 // Should never be reached
827 SoundManager::get()->play_music(song);
828 Menu::set_current(0);
829 if (!savegame_file.empty())
830 savegame(savegame_file);
832 /* The porpose of the next checking is that if the player lost
833 the level (in case there is one), don't show anything */
835 if (!level->extro_filename.empty()) {
836 // Display a text file
837 std::string filename = levels_path + level->extro_filename;
838 display_text_file(filename, SCROLL_SPEED_MESSAGE,
839 white_big_text , white_text, white_small_text, blue_text );
842 if (!level->next_worldmap.empty())
844 // Load given worldmap
845 loadmap(level->next_worldmap);
847 if (level->quit_worldmap)
854 // tux->set_direction(input_direction);
857 Menu* menu = Menu::current();
862 if(menu == worldmap_menu)
864 switch (worldmap_menu->check())
866 case MNID_RETURNWORLDMAP: // Return to game
868 case MNID_QUITWORLDMAP: // Quit Worldmap
873 else if(menu == options_menu)
875 process_options_menu();
881 WorldMap::at(Vector p)
890 return tile_manager->get(tilemap[width * y + x]);
896 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
898 if (i->pos == tux->get_tile_pos())
905 WorldMap::SpecialTile*
906 WorldMap::at_special_tile()
908 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
910 if (i->pos == tux->get_tile_pos())
919 WorldMap::draw(DrawingContext& context, const Vector& offset)
921 for(int y = 0; y < height; ++y)
922 for(int x = 0; x < width; ++x)
924 const Tile* tile = at(Vector(x, y));
925 tile->draw(context, Vector(x*32 + offset.x, y*32 + offset.y),
929 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
932 context.draw_surface(leveldot_green,
933 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
935 context.draw_surface(leveldot_red,
936 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
939 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
944 if (i->teleport_dest != Vector(-1, -1))
945 context.draw_surface(teleporterdot,
946 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
948 else if (!i->map_message.empty() && !i->passive_message)
949 context.draw_surface(messagedot,
950 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
953 tux->draw(context, offset);
954 draw_status(context);
958 WorldMap::draw_status(DrawingContext& context)
961 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
963 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
964 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
966 sprintf(str, "%d", player_status.distros);
967 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
968 LEFT_ALLIGN, LAYER_FOREGROUND1);
969 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
970 LEFT_ALLIGN, LAYER_FOREGROUND1);
972 if (player_status.lives >= 5)
974 sprintf(str, "%dx", player_status.lives);
975 context.draw_text(gold_text, str,
976 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
977 LEFT_ALLIGN, LAYER_FOREGROUND1);
978 context.draw_surface(tux_life, Vector(screen->w -
979 gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
983 for(int i= 0; i < player_status.lives; ++i)
984 context.draw_surface(tux_life,
985 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
988 context.draw_text(white_text, _("LIVES"),
989 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
990 LEFT_ALLIGN, LAYER_FOREGROUND1);
992 if (!tux->is_moving())
994 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
996 if (i->pos == tux->get_tile_pos())
1001 context.draw_text(white_text, i->title,
1003 screen->h - white_text->get_height() - 30),
1004 CENTER_ALLIGN, LAYER_FOREGROUND1);
1006 i->statistics.draw_worldmap_info(context);
1010 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1012 if (i->pos == tux->get_tile_pos())
1014 /* Display an in-map message in the map, if any as been selected */
1015 if(!i->map_message.empty() && !i->passive_message)
1016 context.draw_text(gold_text, i->map_message,
1018 screen->h - white_text->get_height() - 60),
1019 CENTER_ALLIGN, LAYER_FOREGROUND1);
1024 /* Display a passive message in the map, if needed */
1025 if(passive_message_timer.check())
1026 context.draw_text(gold_text, passive_message,
1027 Vector(screen->w/2, screen->h - white_text->get_height() - 60),
1028 CENTER_ALLIGN, LAYER_FOREGROUND1);
1034 Menu::set_current(0);
1038 song = SoundManager::get()->load_music(datadir + "/music/" + music);
1039 SoundManager::get()->play_music(song);
1041 if(!intro_displayed && intro_filename != "") {
1042 std::string filename = levels_path + intro_filename;
1043 display_text_file(filename, SCROLL_SPEED_MESSAGE,
1044 white_big_text, white_text, white_small_text, blue_text);
1045 intro_displayed = true;
1048 Uint32 lastticks = SDL_GetTicks();
1049 DrawingContext context;
1051 Uint32 ticks = SDL_GetTicks();
1052 float elapsed_time = float(ticks - lastticks) / 1000;
1053 global_time += elapsed_time;
1057 if(elapsed_time > .025)
1058 elapsed_time = .025;
1060 Vector tux_pos = tux->get_pos();
1062 offset.x = -tux_pos.x + screen->w/2;
1063 offset.y = -tux_pos.y + screen->h/2;
1065 if (offset.x > 0) offset.x = 0;
1066 if (offset.y > 0) offset.y = 0;
1068 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1069 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1071 draw(context, offset);
1073 update(elapsed_time);
1075 if(Menu::current()) {
1076 Menu::current()->draw(context);
1077 mouse_cursor->draw(context);
1080 context.do_drawing();
1085 WorldMap::savegame(const std::string& filename)
1090 std::ofstream file(filename.c_str(), std::ios::out);
1091 lisp::Writer writer(file);
1093 int nb_solved_levels = 0, total_levels = 0;
1094 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1099 char nb_solved_levels_str[80], total_levels_str[80];
1100 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1101 sprintf(total_levels_str, "%d", total_levels);
1103 writer.write_comment("Worldmap save file");
1105 writer.start_list("supertux-savegame");
1107 writer.write_int("version", 1);
1108 writer.write_string("title",
1109 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1110 writer.write_string("map", map_filename);
1111 writer.write_bool("intro-displayed", intro_displayed);
1113 writer.start_list("tux");
1115 writer.write_float("x", tux->get_tile_pos().x);
1116 writer.write_float("y", tux->get_tile_pos().y);
1117 writer.write_string("back", direction_to_string(tux->back_direction));
1118 player_status.write(writer);
1119 writer.write_string("back", direction_to_string(tux->back_direction));
1121 writer.end_list("tux");
1123 writer.start_list("levels");
1125 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1129 writer.start_list("level");
1131 writer.write_string("name", i->name);
1132 writer.write_bool("solved", true);
1133 i->statistics.write(writer);
1135 writer.end_list("level");
1139 writer.end_list("levels");
1141 writer.end_list("supertux-savegame");
1145 WorldMap::loadgame(const std::string& filename)
1147 std::cout << "loadgame: " << filename << std::endl;
1148 savegame_file = filename;
1151 lisp::Parser parser;
1152 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1154 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1156 throw std::runtime_error("File is not a supertux-savegame file.");
1158 /* Get the Map filename and then load it before setting level settings */
1159 std::string cur_map_filename = map_filename;
1160 savegame->get("map", map_filename);
1163 savegame->get("intro-displayed", intro_displayed);
1164 savegame->get("lives", player_status.lives);
1165 savegame->get("distros", player_status.distros);
1166 savegame->get("max-score-multiplier", player_status.max_score_multiplier);
1167 if (player_status.lives < 0)
1168 player_status.reset();
1170 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1174 std::string back_str = "none";
1176 tux_lisp->get("x", p.x);
1177 tux_lisp->get("y", p.y);
1178 tux_lisp->get("back", back_str);
1179 player_status.read(*tux_lisp);
1181 tux->back_direction = string_to_direction(back_str);
1182 tux->set_tile_pos(p);
1185 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1187 lisp::ListIterator iter(levels_lisp);
1188 while(iter.next()) {
1189 if(iter.item() == "level") {
1191 bool solved = false;
1193 const lisp::Lisp* level = iter.lisp();
1194 level->get("name", name);
1195 level->get("solved", solved);
1197 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1199 if (name == i->name)
1202 i->statistics.parse(*level);
1207 std::cerr << "Unknown token '" << iter.item()
1208 << "' in levels block in worldmap.\n";
1212 } catch(std::exception& e) {
1213 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1216 player_status.reset();
1219 calculate_total_stats();
1223 WorldMap::loadmap(const std::string& filename)
1226 map_filename = filename;
1230 } // namespace WorldMapNS