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"
47 #include "textscroller.h"
49 #define map_message_TIME 2.8
51 Menu* worldmap_menu = 0;
53 static const float TUXSPEED = 200;
55 namespace WorldMapNS {
57 Direction reverse_dir(Direction direction)
76 direction_to_string(Direction direction)
94 string_to_direction(const std::string& directory)
96 if (directory == "west")
98 else if (directory == "east")
100 else if (directory == "north")
102 else if (directory == "south")
108 //---------------------------------------------------------------------------
110 Tux::Tux(WorldMap* worldmap_)
111 : worldmap(worldmap_)
113 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
114 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
115 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
119 tile_pos.x = worldmap->get_start_x();
120 tile_pos.y = worldmap->get_start_y();
122 input_direction = D_NONE;
127 delete smalltux_sprite;
128 delete firetux_sprite;
129 delete largetux_sprite;
133 Tux::draw(DrawingContext& context, const Vector& offset)
135 Vector pos = get_pos();
136 switch (player_status.bonus)
138 case PlayerStatus::GROWUP_BONUS:
139 context.draw_surface(largetux_sprite,
140 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
142 case PlayerStatus::FLOWER_BONUS:
143 context.draw_surface(firetux_sprite,
144 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
146 case PlayerStatus::NO_BONUS:
147 context.draw_surface(smalltux_sprite,
148 Vector(pos.x + offset.x, pos.y + offset.y - 10), 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 (session.get_current_sector()->player->got_power !=
743 session.get_current_sector()->player->NONE_POWER)
744 player_status.bonus = PlayerStatus::FLOWER_BONUS;
745 else if (session.get_current_sector()->player->size == BIG)
746 player_status.bonus = PlayerStatus::GROWUP_BONUS;
748 player_status.bonus = PlayerStatus::NO_BONUS;
750 if (old_level_state != level->solved && level->auto_path)
751 { // Try to detect the next direction to which we should walk
752 // FIXME: Mostly a hack
753 Direction dir = D_NONE;
755 const Tile* tile = at(tux->get_tile_pos());
757 if (tile->getData() & Tile::WORLDMAP_NORTH
758 && tux->back_direction != D_NORTH)
760 else if (tile->getData() & Tile::WORLDMAP_SOUTH
761 && tux->back_direction != D_SOUTH)
763 else if (tile->getData() & Tile::WORLDMAP_EAST
764 && tux->back_direction != D_EAST)
766 else if (tile->getData() & Tile::WORLDMAP_WEST
767 && tux->back_direction != D_WEST)
772 tux->set_direction(dir);
773 //tux->update(delta);
776 std::cout << "Walk to dir: " << dir << std::endl;
781 case GameSession::ES_LEVEL_ABORT:
782 level_finished = false;
783 /* In case the player's abort the level, keep it using the old
784 status. But the minimum lives and no bonus. */
785 player_status.distros = old_player_status.distros;
786 player_status.lives = std::min(old_player_status.lives, player_status.lives);
787 player_status.bonus = player_status.NO_BONUS;
790 case GameSession::ES_GAME_OVER:
792 level_finished = false;
793 /* draw an end screen */
794 /* TODO: in the future, this should make a dialog a la SuperMario, asking
795 if the player wants to restart the world map with no score and from
799 DrawingContext context;
800 context.draw_gradient(Color (200,240,220), Color(200,200,220),
803 context.draw_text(blue_text, _("GAMEOVER"),
804 Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
806 sprintf(str, _("COINS: %d"), player_status.distros);
807 context.draw_text(gold_text, str,
808 Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN,
811 total_stats.draw_message_info(context, _("Total Statistics"));
813 context.do_drawing();
816 wait_for_event(event,2000,6000,true);
819 player_status.reset();
822 case GameSession::ES_NONE:
824 // Should never be reached
828 SoundManager::get()->play_music(song);
829 Menu::set_current(0);
830 if (!savegame_file.empty())
831 savegame(savegame_file);
833 /* The porpose of the next checking is that if the player lost
834 the level (in case there is one), don't show anything */
836 if (!level->extro_filename.empty()) {
837 // Display a text file
838 std::string filename = levels_path + level->extro_filename;
839 display_text_file(filename);
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);
1044 intro_displayed = true;
1047 Uint32 lastticks = SDL_GetTicks();
1048 DrawingContext context;
1050 Uint32 ticks = SDL_GetTicks();
1051 float elapsed_time = float(ticks - lastticks) / 1000;
1052 global_time += elapsed_time;
1056 if(elapsed_time > .025)
1057 elapsed_time = .025;
1059 Vector tux_pos = tux->get_pos();
1061 offset.x = -tux_pos.x + screen->w/2;
1062 offset.y = -tux_pos.y + screen->h/2;
1064 if (offset.x > 0) offset.x = 0;
1065 if (offset.y > 0) offset.y = 0;
1067 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1068 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1070 draw(context, offset);
1072 update(elapsed_time);
1074 if(Menu::current()) {
1075 Menu::current()->draw(context);
1076 mouse_cursor->draw(context);
1079 context.do_drawing();
1084 WorldMap::savegame(const std::string& filename)
1089 std::ofstream file(filename.c_str(), std::ios::out);
1090 lisp::Writer writer(file);
1092 int nb_solved_levels = 0, total_levels = 0;
1093 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1098 char nb_solved_levels_str[80], total_levels_str[80];
1099 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1100 sprintf(total_levels_str, "%d", total_levels);
1102 writer.write_comment("Worldmap save file");
1104 writer.start_list("supertux-savegame");
1106 writer.write_int("version", 1);
1107 writer.write_string("title",
1108 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1109 writer.write_string("map", map_filename);
1110 writer.write_bool("intro-displayed", intro_displayed);
1112 writer.start_list("tux");
1114 writer.write_float("x", tux->get_tile_pos().x);
1115 writer.write_float("y", tux->get_tile_pos().y);
1116 writer.write_string("back", direction_to_string(tux->back_direction));
1117 player_status.write(writer);
1118 writer.write_string("back", direction_to_string(tux->back_direction));
1120 writer.end_list("tux");
1122 writer.start_list("levels");
1124 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1128 writer.start_list("level");
1130 writer.write_string("name", i->name);
1131 writer.write_bool("solved", true);
1132 i->statistics.write(writer);
1134 writer.end_list("level");
1138 writer.end_list("levels");
1140 writer.end_list("supertux-savegame");
1144 WorldMap::loadgame(const std::string& filename)
1146 std::cout << "loadgame: " << filename << std::endl;
1147 savegame_file = filename;
1150 lisp::Parser parser;
1151 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1153 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1155 throw std::runtime_error("File is not a supertux-savegame file.");
1157 /* Get the Map filename and then load it before setting level settings */
1158 std::string cur_map_filename = map_filename;
1159 savegame->get("map", map_filename);
1162 savegame->get("intro-displayed", intro_displayed);
1163 savegame->get("lives", player_status.lives);
1164 savegame->get("distros", player_status.distros);
1165 savegame->get("max-score-multiplier", player_status.max_score_multiplier);
1166 if (player_status.lives < 0)
1167 player_status.reset();
1169 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1173 std::string back_str = "none";
1175 tux_lisp->get("x", p.x);
1176 tux_lisp->get("y", p.y);
1177 tux_lisp->get("back", back_str);
1178 player_status.read(*tux_lisp);
1180 tux->back_direction = string_to_direction(back_str);
1181 tux->set_tile_pos(p);
1184 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1186 lisp::ListIterator iter(levels_lisp);
1187 while(iter.next()) {
1188 if(iter.item() == "level") {
1190 bool solved = false;
1192 const lisp::Lisp* level = iter.lisp();
1193 level->get("name", name);
1194 level->get("solved", solved);
1196 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1198 if (name == i->name)
1201 i->statistics.parse(*level);
1206 std::cerr << "Unknown token '" << iter.item()
1207 << "' in levels block in worldmap.\n";
1211 } catch(std::exception& e) {
1212 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1215 player_status.reset();
1218 calculate_total_stats();
1222 WorldMap::loadmap(const std::string& filename)
1225 map_filename = filename;
1229 } // namespace WorldMapNS