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 "special/sprite_manager.h"
37 #include "audio/sound_manager.h"
38 #include "lisp/parser.h"
39 #include "lisp/lisp.h"
40 #include "lisp/list_iterator.h"
41 #include "lisp/writer.h"
45 #include "resources.h"
47 #include "player_status.h"
48 #include "textscroller.h"
50 #define map_message_TIME 2.8
52 Menu* worldmap_menu = 0;
54 static const float TUXSPEED = 200;
56 namespace WorldMapNS {
58 Direction reverse_dir(Direction direction)
77 direction_to_string(Direction direction)
95 string_to_direction(const std::string& directory)
97 if (directory == "west")
99 else if (directory == "east")
101 else if (directory == "north")
103 else if (directory == "south")
109 //---------------------------------------------------------------------------
111 Tux::Tux(WorldMap* worldmap_)
112 : worldmap(worldmap_)
114 tux_sprite = sprite_manager->create("worldmaptux");
118 tile_pos.x = worldmap->get_start_x();
119 tile_pos.y = worldmap->get_start_y();
121 input_direction = D_NONE;
130 Tux::draw(DrawingContext& context)
132 switch (player_status.bonus) {
134 tux_sprite->set_action("large");
137 tux_sprite->set_action("fire");
140 tux_sprite->set_action("small");
144 std::cerr << "Bonus type not handled in worldmap.\n";
146 tux_sprite->set_action("large");
150 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
157 float x = tile_pos.x * 32;
158 float y = tile_pos.y * 32;
178 return Vector((int)x, (int)y);
186 input_direction = D_NONE;
191 Tux::set_direction(Direction dir)
193 input_direction = dir;
197 Tux::action(float delta)
201 if (input_direction != D_NONE)
203 WorldMap::Level* level = worldmap->at_level();
205 // We got a new direction, so lets start walking when possible
207 if ((!level || level->solved)
208 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
210 tile_pos = next_tile;
212 direction = input_direction;
213 back_direction = reverse_dir(direction);
215 else if (input_direction == back_direction)
218 direction = input_direction;
219 tile_pos = worldmap->get_next_tile(tile_pos, direction);
220 back_direction = reverse_dir(direction);
227 offset += TUXSPEED * delta;
230 { // We reached the next tile, so we check what to do now
233 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
234 if(special_tile && special_tile->passive_message)
235 { // direction and the apply_action_ are opposites, since they "see"
236 // directions in a different way
237 if((direction == D_NORTH && special_tile->apply_action_south) ||
238 (direction == D_SOUTH && special_tile->apply_action_north) ||
239 (direction == D_WEST && special_tile->apply_action_east) ||
240 (direction == D_EAST && special_tile->apply_action_west))
242 worldmap->passive_message = special_tile->map_message;
243 worldmap->passive_message_timer.start(map_message_TIME);
247 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
248 (special_tile && !special_tile->passive_message) ||
249 worldmap->at_level())
251 if(special_tile && !special_tile->map_message.empty() &&
252 !special_tile->passive_message)
253 worldmap->passive_message_timer.start(0);
258 const Tile* tile = worldmap->at(tile_pos);
259 if (direction != input_direction)
261 // Turn to a new direction
262 const Tile* tile = worldmap->at(tile_pos);
264 if((tile->getData() & Tile::WORLDMAP_NORTH
265 && input_direction == D_NORTH) ||
266 (tile->getData() & Tile::WORLDMAP_SOUTH
267 && input_direction == D_SOUTH) ||
268 (tile->getData() & Tile::WORLDMAP_EAST
269 && input_direction == D_EAST) ||
270 (tile->getData() & Tile::WORLDMAP_WEST
271 && input_direction == D_WEST))
272 { // player has changed direction during auto-movement
273 direction = input_direction;
274 back_direction = reverse_dir(direction);
277 { // player has changed to impossible tile
278 back_direction = reverse_dir(direction);
284 Direction dir = D_NONE;
286 if (tile->getData() & Tile::WORLDMAP_NORTH
287 && back_direction != D_NORTH)
289 else if (tile->getData() & Tile::WORLDMAP_SOUTH
290 && back_direction != D_SOUTH)
292 else if (tile->getData() & Tile::WORLDMAP_EAST
293 && back_direction != D_EAST)
295 else if (tile->getData() & Tile::WORLDMAP_WEST
296 && back_direction != D_WEST)
302 input_direction = direction;
303 back_direction = reverse_dir(direction);
307 // Should never be reached if tiledata is good
313 // Walk automatically to the next tile
314 if(direction != D_NONE)
317 if (worldmap->path_ok(direction, tile_pos, &next_tile))
319 tile_pos = next_tile;
323 puts("Tilemap data is buggy");
332 //---------------------------------------------------------------------------
336 tile_manager = new TileManager("images/worldmap/antarctica.stwt");
346 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
347 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
348 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
349 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
354 music = "salcon.mod";
355 intro_displayed = false;
360 WorldMap::~WorldMap()
365 delete leveldot_green;
368 delete teleporterdot;
371 // Don't forget to set map_filename before calling this
375 levels_path = FileSystem::dirname(map_filename);
379 std::string filename = get_resource_filename(map_filename);
380 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
382 const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
384 throw new std::runtime_error("file isn't a supertux-worldmap file.");
386 lisp::ListIterator iter(lisp);
388 if(iter.item() == "tilemap") {
389 if(tilemap.size() > 0)
390 throw new std::runtime_error("multiple tilemaps specified");
392 const lisp::Lisp* tilemap_lisp = iter.lisp();
393 tilemap_lisp->get("width", width);
394 tilemap_lisp->get("height", height);
395 tilemap_lisp->get_vector("data", tilemap);
396 } else if(iter.item() == "properties") {
397 const lisp::Lisp* props = iter.lisp();
398 props->get("name", name);
399 props->get("music", music);
400 props->get("intro-filename", intro_filename);
401 props->get("start_pos_x", start_x);
402 props->get("start_pos_y", start_y);
403 } else if(iter.item() == "special-tiles") {
404 parse_special_tiles(iter.lisp());
406 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
412 } catch(std::exception& e) {
413 std::stringstream msg;
414 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
416 throw std::runtime_error(msg.str());
421 WorldMap::parse_special_tiles(const lisp::Lisp* lisp)
423 lisp::ListIterator iter(lisp);
425 if(iter.item() == "special-tile") {
426 SpecialTile special_tile;
428 const lisp::Lisp* lisp = iter.lisp();
429 lisp->get("x", special_tile.pos.x);
430 lisp->get("y", special_tile.pos.y);
431 lisp->get("map-message", special_tile.map_message);
432 special_tile.passive_message = false;
433 lisp->get("passive-message", special_tile.passive_message);
434 special_tile.teleport_dest = Vector(-1,-1);
435 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
436 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
437 special_tile.invisible = false;
438 lisp->get("invisible-tile", special_tile.invisible);
440 special_tile.apply_action_north = true;
441 special_tile.apply_action_south = true;
442 special_tile.apply_action_east = true;
443 special_tile.apply_action_west = true;
445 std::string apply_direction;
446 lisp->get("apply-to-direction", apply_direction);
447 if(!apply_direction.empty()) {
448 special_tile.apply_action_north = false;
449 special_tile.apply_action_south = false;
450 special_tile.apply_action_east = false;
451 special_tile.apply_action_west = false;
452 if(apply_direction.find("north") != std::string::npos)
453 special_tile.apply_action_north = true;
454 if(apply_direction.find("south") != std::string::npos)
455 special_tile.apply_action_south = true;
456 if(apply_direction.find("east") != std::string::npos)
457 special_tile.apply_action_east = true;
458 if(apply_direction.find("west") != std::string::npos)
459 special_tile.apply_action_west = true;
462 special_tiles.push_back(special_tile);
463 } else if(iter.item() == "level") {
466 lisp::Lisp* level_lisp = iter.lisp();
467 level.solved = false;
474 level_lisp->get("extro-filename", level.extro_filename);
475 level_lisp->get("next-worldmap", level.next_worldmap);
477 level.quit_worldmap = false;
478 level_lisp->get("quit-worldmap", level.quit_worldmap);
480 level_lisp->get("name", level.name);
481 level_lisp->get("x", level.pos.x);
482 level_lisp->get("y", level.pos.y);
484 level.auto_path = true;
485 level_lisp->get("auto-path", level.auto_path);
487 level.vertical_flip = false;
488 level_lisp->get("vertical-flip", level.vertical_flip);
490 levels.push_back(level);
492 std::cerr << "Unknown token '" << iter.item() <<
493 "' in worldmap special-tiles list.";
499 WorldMap::get_level_title(Level& level)
501 /** get special_tile's title */
502 level.title = "<no title>";
506 std::auto_ptr<lisp::Lisp> root (
507 parser.parse(get_resource_filename(levels_path + level.name)));
509 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
513 level_lisp->get("name", level.title);
514 } catch(std::exception& e) {
515 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
520 void WorldMap::calculate_total_stats()
523 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
527 total_stats += i->statistics;
533 WorldMap::on_escape_press()
535 // Show or hide the menu
538 Menu::set_current(worldmap_menu);
539 tux->set_direction(D_NONE); // stop tux movement when menu is called
542 Menu::set_current(0);
546 WorldMap::get_input()
552 while (SDL_PollEvent(&event))
556 Menu::current()->event(event);
563 Termination::abort("Received window close", "");
567 key = event.key.keysym.sym;
569 if(key == SDLK_ESCAPE)
571 else if(key == SDLK_RETURN || key == keymap.power)
573 else if(key == SDLK_LEFT || key == keymap.power)
574 tux->set_direction(D_WEST);
575 else if(key == SDLK_RIGHT || key == keymap.right)
576 tux->set_direction(D_EAST);
577 else if(key == SDLK_UP || key == keymap.up ||
579 // there might be ppl that use jump as up key
580 tux->set_direction(D_NORTH);
581 else if(key == SDLK_DOWN || key == keymap.down)
582 tux->set_direction(D_SOUTH);
585 case SDL_JOYHATMOTION:
586 if(event.jhat.value & SDL_HAT_UP) {
587 tux->set_direction(D_NORTH);
588 } else if(event.jhat.value & SDL_HAT_DOWN) {
589 tux->set_direction(D_SOUTH);
590 } else if(event.jhat.value & SDL_HAT_LEFT) {
591 tux->set_direction(D_WEST);
592 } else if(event.jhat.value & SDL_HAT_RIGHT) {
593 tux->set_direction(D_EAST);
597 case SDL_JOYAXISMOTION:
598 if (event.jaxis.axis == joystick_keymap.x_axis)
600 if (event.jaxis.value < -joystick_keymap.dead_zone)
601 tux->set_direction(D_WEST);
602 else if (event.jaxis.value > joystick_keymap.dead_zone)
603 tux->set_direction(D_EAST);
605 else if (event.jaxis.axis == joystick_keymap.y_axis)
607 if (event.jaxis.value > joystick_keymap.dead_zone)
608 tux->set_direction(D_SOUTH);
609 else if (event.jaxis.value < -joystick_keymap.dead_zone)
610 tux->set_direction(D_NORTH);
614 case SDL_JOYBUTTONDOWN:
615 if (event.jbutton.button == joystick_keymap.b_button)
617 else if (event.jbutton.button == joystick_keymap.start_button)
629 WorldMap::get_next_tile(Vector pos, Direction direction)
652 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
654 *new_pos = get_next_tile(old_pos, direction);
656 if (!(new_pos->x >= 0 && new_pos->x < width
657 && new_pos->y >= 0 && new_pos->y < height))
658 { // New position is outsite the tilemap
662 { // Check if the tile allows us to go to new_pos
666 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
667 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
670 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
671 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
674 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
675 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
678 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
679 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
682 assert(!"path_ok() can't work if direction is NONE");
689 WorldMap::update(float delta)
691 if (enter_level && !tux->is_moving())
693 /* Check special tile action */
694 SpecialTile* special_tile = at_special_tile();
697 if (special_tile->teleport_dest != Vector(-1,-1))
699 // TODO: an animation, camera scrolling or a fading would be a nice touch
700 SoundManager::get()->play_sound(IDToSound(SND_WARP));
701 tux->back_direction = D_NONE;
702 tux->set_tile_pos(special_tile->teleport_dest);
707 /* Check level action */
708 bool level_finished = true;
709 Level* level = at_level();
712 std::cout << "No level to enter at: "
713 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
719 if (level->pos == tux->get_tile_pos())
721 PlayerStatus old_player_status = player_status;
723 std::cout << "Enter the current level: " << level->name << std::endl;
724 // do a shriking fade to the level
725 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
726 (level->pos.y*32 + 16 + offset.y)), 500);
727 GameSession session(get_resource_filename(levels_path + level->name),
728 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
730 switch (session.run())
732 case GameSession::ES_LEVEL_FINISHED:
734 level_finished = true;
735 bool old_level_state = level->solved;
736 level->solved = true;
738 // deal with statistics
739 level->statistics.merge(global_stats);
740 calculate_total_stats();
742 if (old_level_state != level->solved && level->auto_path)
743 { // Try to detect the next direction to which we should walk
744 // FIXME: Mostly a hack
745 Direction dir = D_NONE;
747 const Tile* tile = at(tux->get_tile_pos());
749 if (tile->getData() & Tile::WORLDMAP_NORTH
750 && tux->back_direction != D_NORTH)
752 else if (tile->getData() & Tile::WORLDMAP_SOUTH
753 && tux->back_direction != D_SOUTH)
755 else if (tile->getData() & Tile::WORLDMAP_EAST
756 && tux->back_direction != D_EAST)
758 else if (tile->getData() & Tile::WORLDMAP_WEST
759 && tux->back_direction != D_WEST)
764 tux->set_direction(dir);
765 //tux->update(delta);
768 std::cout << "Walk to dir: " << dir << std::endl;
773 case GameSession::ES_LEVEL_ABORT:
774 level_finished = false;
775 /* In case the player's abort the level, keep it using the old
776 status. But the minimum lives and no bonus. */
777 player_status.distros = old_player_status.distros;
778 player_status.lives = std::min(old_player_status.lives, player_status.lives);
779 player_status.bonus = NO_BONUS;
782 case GameSession::ES_GAME_OVER:
784 level_finished = false;
785 /* draw an end screen */
786 /* TODO: in the future, this should make a dialog a la SuperMario, asking
787 if the player wants to restart the world map with no score and from
791 DrawingContext context;
792 context.draw_gradient(Color (200,240,220), Color(200,200,220),
795 context.draw_text(blue_text, _("GAMEOVER"),
796 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
798 sprintf(str, _("COINS: %d"), player_status.distros);
799 context.draw_text(gold_text, str,
800 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
803 total_stats.draw_message_info(context, _("Total Statistics"));
805 context.do_drawing();
808 wait_for_event(event,2000,6000,true);
811 player_status.reset();
814 case GameSession::ES_NONE:
816 // Should never be reached
820 SoundManager::get()->play_music(song);
821 Menu::set_current(0);
822 if (!savegame_file.empty())
823 savegame(savegame_file);
825 /* The porpose of the next checking is that if the player lost
826 the level (in case there is one), don't show anything */
828 if (!level->extro_filename.empty()) {
829 // Display a text file
830 std::string filename = levels_path + level->extro_filename;
831 display_text_file(filename);
834 if (!level->next_worldmap.empty())
836 // Load given worldmap
837 loadmap(level->next_worldmap);
839 if (level->quit_worldmap)
846 // tux->set_direction(input_direction);
849 Menu* menu = Menu::current();
854 if(menu == worldmap_menu)
856 switch (worldmap_menu->check())
858 case MNID_RETURNWORLDMAP: // Return to game
860 case MNID_QUITWORLDMAP: // Quit Worldmap
865 else if(menu == options_menu)
867 process_options_menu();
873 WorldMap::at(Vector p)
882 return tile_manager->get(tilemap[width * y + x]);
888 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
890 if (i->pos == tux->get_tile_pos())
897 WorldMap::SpecialTile*
898 WorldMap::at_special_tile()
900 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
902 if (i->pos == tux->get_tile_pos())
910 WorldMap::draw(DrawingContext& context)
912 for(int y = 0; y < height; ++y)
913 for(int x = 0; x < width; ++x)
915 const Tile* tile = at(Vector(x, y));
916 tile->draw(context, Vector(x*32, y*32), LAYER_TILES);
919 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
922 context.draw_surface(leveldot_green,
923 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
925 context.draw_surface(leveldot_red,
926 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
929 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
934 if (i->teleport_dest != Vector(-1, -1))
935 context.draw_surface(teleporterdot,
936 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
938 else if (!i->map_message.empty() && !i->passive_message)
939 context.draw_surface(messagedot,
940 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
944 draw_status(context);
948 WorldMap::draw_status(DrawingContext& context)
950 context.push_transform();
951 context.set_translation(Vector(0, 0));
954 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
956 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
957 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
959 sprintf(str, "%d", player_status.distros);
960 context.draw_text(white_text, _("COINS"), Vector(SCREEN_WIDTH/2 - 16*5, 0),
961 LEFT_ALLIGN, LAYER_FOREGROUND1);
962 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + (16*5)/2, 0),
963 LEFT_ALLIGN, LAYER_FOREGROUND1);
965 if (player_status.lives >= 5)
967 sprintf(str, "%dx", player_status.lives);
968 context.draw_text(gold_text, str,
969 Vector(SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w, 0),
970 LEFT_ALLIGN, LAYER_FOREGROUND1);
971 context.draw_surface(tux_life, Vector(SCREEN_WIDTH -
972 gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
976 for(int i= 0; i < player_status.lives; ++i)
977 context.draw_surface(tux_life,
978 Vector(SCREEN_WIDTH - tux_life->w*4 + (tux_life->w*i), 0),
981 context.draw_text(white_text, _("LIVES"),
982 Vector(SCREEN_WIDTH - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
983 LEFT_ALLIGN, LAYER_FOREGROUND1);
985 if (!tux->is_moving())
987 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
989 if (i->pos == tux->get_tile_pos())
994 context.draw_text(white_text, i->title,
995 Vector(SCREEN_WIDTH/2,
996 SCREEN_HEIGHT - white_text->get_height() - 30),
997 CENTER_ALLIGN, LAYER_FOREGROUND1);
999 i->statistics.draw_worldmap_info(context);
1003 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1005 if (i->pos == tux->get_tile_pos())
1007 /* Display an in-map message in the map, if any as been selected */
1008 if(!i->map_message.empty() && !i->passive_message)
1009 context.draw_text(gold_text, i->map_message,
1010 Vector(SCREEN_WIDTH/2,
1011 SCREEN_HEIGHT - white_text->get_height() - 60),
1012 CENTER_ALLIGN, LAYER_FOREGROUND1);
1017 /* Display a passive message in the map, if needed */
1018 if(passive_message_timer.check())
1019 context.draw_text(gold_text, passive_message,
1020 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
1021 CENTER_ALLIGN, LAYER_FOREGROUND1);
1023 context.pop_transform();
1029 Menu::set_current(0);
1033 song = SoundManager::get()->load_music(datadir + "/music/" + music);
1034 SoundManager::get()->play_music(song);
1036 if(!intro_displayed && intro_filename != "") {
1037 std::string filename = levels_path + intro_filename;
1038 display_text_file(filename);
1039 intro_displayed = true;
1042 Uint32 lastticks = SDL_GetTicks();
1043 DrawingContext context;
1045 Uint32 ticks = SDL_GetTicks();
1046 float elapsed_time = float(ticks - lastticks) / 1000;
1047 global_time += elapsed_time;
1051 if(elapsed_time > .025)
1052 elapsed_time = .025;
1054 Vector tux_pos = tux->get_pos();
1056 offset.x = -tux_pos.x + SCREEN_WIDTH/2;
1057 offset.y = -tux_pos.y + SCREEN_HEIGHT/2;
1059 if (offset.x > 0) offset.x = 0;
1060 if (offset.y > 0) offset.y = 0;
1062 if (offset.x < SCREEN_WIDTH - width*32) offset.x = SCREEN_WIDTH - width*32;
1063 if (offset.y < SCREEN_HEIGHT - height*32) offset.y = SCREEN_HEIGHT - height*32;
1065 context.push_transform();
1066 context.set_translation(-offset);
1068 context.pop_transform();
1070 update(elapsed_time);
1072 if(Menu::current()) {
1073 Menu::current()->draw(context);
1074 mouse_cursor->draw(context);
1077 context.do_drawing();
1082 WorldMap::savegame(const std::string& filename)
1087 std::ofstream file(filename.c_str(), std::ios::out);
1088 lisp::Writer writer(file);
1090 int nb_solved_levels = 0, total_levels = 0;
1091 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1096 char nb_solved_levels_str[80], total_levels_str[80];
1097 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1098 sprintf(total_levels_str, "%d", total_levels);
1100 writer.write_comment("Worldmap save file");
1102 writer.start_list("supertux-savegame");
1104 writer.write_int("version", 1);
1105 writer.write_string("title",
1106 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1107 writer.write_string("map", map_filename);
1108 writer.write_bool("intro-displayed", intro_displayed);
1110 writer.start_list("tux");
1112 writer.write_float("x", tux->get_tile_pos().x);
1113 writer.write_float("y", tux->get_tile_pos().y);
1114 writer.write_string("back", direction_to_string(tux->back_direction));
1115 player_status.write(writer);
1116 writer.write_string("back", direction_to_string(tux->back_direction));
1118 writer.end_list("tux");
1120 writer.start_list("levels");
1122 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1126 writer.start_list("level");
1128 writer.write_string("name", i->name);
1129 writer.write_bool("solved", true);
1130 i->statistics.write(writer);
1132 writer.end_list("level");
1136 writer.end_list("levels");
1138 writer.end_list("supertux-savegame");
1142 WorldMap::loadgame(const std::string& filename)
1144 std::cout << "loadgame: " << filename << std::endl;
1145 savegame_file = filename;
1148 lisp::Parser parser;
1149 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1151 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1153 throw std::runtime_error("File is not a supertux-savegame file.");
1155 /* Get the Map filename and then load it before setting level settings */
1156 std::string cur_map_filename = map_filename;
1157 savegame->get("map", map_filename);
1160 savegame->get("intro-displayed", intro_displayed);
1161 savegame->get("lives", player_status.lives);
1162 savegame->get("distros", player_status.distros);
1163 savegame->get("max-score-multiplier", player_status.max_score_multiplier);
1164 if (player_status.lives < 0)
1165 player_status.reset();
1167 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1171 std::string back_str = "none";
1173 tux_lisp->get("x", p.x);
1174 tux_lisp->get("y", p.y);
1175 tux_lisp->get("back", back_str);
1176 player_status.read(*tux_lisp);
1178 tux->back_direction = string_to_direction(back_str);
1179 tux->set_tile_pos(p);
1182 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1184 lisp::ListIterator iter(levels_lisp);
1185 while(iter.next()) {
1186 if(iter.item() == "level") {
1188 bool solved = false;
1190 const lisp::Lisp* level = iter.lisp();
1191 level->get("name", name);
1192 level->get("solved", solved);
1194 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1196 if (name == i->name)
1199 i->statistics.parse(*level);
1204 std::cerr << "Unknown token '" << iter.item()
1205 << "' in levels block in worldmap.\n";
1209 } catch(std::exception& e) {
1210 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1213 player_status.reset();
1216 calculate_total_stats();
1220 WorldMap::loadmap(const std::string& filename)
1223 map_filename = filename;
1227 } // namespace WorldMapNS