X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fworldmap.cpp;h=7f87ad4aee6e759906fdf4a17accb085c88dec8e;hb=6ed1900da4edf7d7922f7a4626f95a83e34dff82;hp=cd91cb2a846191b66edf49376863d4a03377f064;hpb=2388a02493831f3ec11f1df66a0cf7b09995279b;p=supertux.git diff --git a/src/worldmap.cpp b/src/worldmap.cpp index cd91cb2a8..7f87ad4ae 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -20,15 +20,24 @@ #include #include #include -#include -#include "globals.h" -#include "texture.h" -#include "screen.h" -#include "lispreader.h" +#include +#include + +#include "app/globals.h" +#include "video/surface.h" +#include "video/screen.h" +#include "video/drawing_context.h" +#include "utils/lispreader.h" #include "gameloop.h" -#include "setup.h" +#include "app/setup.h" +#include "sector.h" #include "worldmap.h" +#include "audio/sound_manager.h" #include "resources.h" +#include "app/gettext.h" +#include "misc.h" + +Menu* worldmap_menu = 0; namespace WorldMapNS { @@ -36,18 +45,18 @@ Direction reverse_dir(Direction direction) { switch(direction) { - case WEST: - return EAST; - case EAST: - return WEST; - case NORTH: - return SOUTH; - case SOUTH: - return NORTH; - case NONE: - return NONE; + case D_WEST: + return D_EAST; + case D_EAST: + return D_WEST; + case D_NORTH: + return D_SOUTH; + case D_SOUTH: + return D_NORTH; + case D_NONE: + return D_NONE; } - return NONE; + return D_NONE; } std::string @@ -55,13 +64,13 @@ direction_to_string(Direction direction) { switch(direction) { - case WEST: + case D_WEST: return "west"; - case EAST: + case D_EAST: return "east"; - case NORTH: + case D_NORTH: return "north"; - case SOUTH: + case D_SOUTH: return "south"; default: return "none"; @@ -72,15 +81,15 @@ Direction string_to_direction(const std::string& directory) { if (directory == "west") - return WEST; + return D_WEST; else if (directory == "east") - return EAST; + return D_EAST; else if (directory == "north") - return NORTH; + return D_NORTH; else if (directory == "south") - return SOUTH; + return D_SOUTH; else - return NONE; + return D_NONE; } TileManager::TileManager() @@ -89,7 +98,7 @@ TileManager::TileManager() lisp_object_t* root_obj = lisp_read_from_file(stwt_filename); if (!root_obj) - st_abort("Couldn't load file", stwt_filename); + Termination::abort("Couldn't load file", stwt_filename); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0) { @@ -113,18 +122,18 @@ TileManager::TileManager() tile->auto_walk = false; LispReader reader(lisp_cdr(element)); - reader.read_int("id", &id); - reader.read_bool("north", &tile->north); - reader.read_bool("south", &tile->south); - reader.read_bool("west", &tile->west); - reader.read_bool("east", &tile->east); - reader.read_bool("stop", &tile->stop); - reader.read_bool("auto-walk", &tile->auto_walk); - reader.read_string("image", &filename); + reader.read_int("id", id); + reader.read_bool("north", tile->north); + reader.read_bool("south", tile->south); + reader.read_bool("west", tile->west); + reader.read_bool("east", tile->east); + reader.read_bool("stop", tile->stop); + reader.read_bool("auto-walk", tile->auto_walk); + reader.read_string("image", filename); tile->sprite = new Surface( datadir + "/images/worldmap/" + filename, - USE_ALPHA); + true); if (id >= int(tiles.size())) tiles.resize(id+1); @@ -165,16 +174,16 @@ TileManager::get(int i) Tux::Tux(WorldMap* worldmap_) : worldmap(worldmap_) { - largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA); - firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", USE_ALPHA); - smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", USE_ALPHA); + largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true); + firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true); + smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true); offset = 0; moving = false; - tile_pos.x = 4; - tile_pos.y = 5; - direction = NONE; - input_direction = NONE; + tile_pos.x = worldmap->get_start_x(); + tile_pos.y = worldmap->get_start_y(); + direction = D_NONE; + input_direction = D_NONE; } Tux::~Tux() @@ -185,28 +194,28 @@ Tux::~Tux() } void -Tux::draw(const Point& offset) +Tux::draw(DrawingContext& context, const Vector& offset) { - Point pos = get_pos(); + Vector pos = get_pos(); switch (player_status.bonus) { case PlayerStatus::GROWUP_BONUS: - largetux_sprite->draw(pos.x + offset.x, - pos.y + offset.y - 10); + context.draw_surface(largetux_sprite, + Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS); break; case PlayerStatus::FLOWER_BONUS: - firetux_sprite->draw(pos.x + offset.x, - pos.y + offset.y - 10); + context.draw_surface(firetux_sprite, + Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS); break; case PlayerStatus::NO_BONUS: - smalltux_sprite->draw(pos.x + offset.x, - pos.y + offset.y - 10); + context.draw_surface(smalltux_sprite, + Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS); break; } } -Point +Vector Tux::get_pos() { float x = tile_pos.x * 32; @@ -214,44 +223,44 @@ Tux::get_pos() switch(direction) { - case WEST: + case D_WEST: x -= offset - 32; break; - case EAST: + case D_EAST: x += offset - 32; break; - case NORTH: + case D_NORTH: y -= offset - 32; break; - case SOUTH: + case D_SOUTH: y += offset - 32; break; - case NONE: + case D_NONE: break; } - return Point((int)x, (int)y); + return Vector((int)x, (int)y); } void Tux::stop() { offset = 0; - direction = NONE; + direction = D_NONE; moving = false; } void -Tux::update(float delta) +Tux::action(float delta) { if (!moving) { - if (input_direction != NONE) + if (input_direction != D_NONE) { WorldMap::Level* level = worldmap->at_level(); // We got a new direction, so lets start walking when possible - Point next_tile; + Vector next_tile; if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile)) { @@ -288,18 +297,18 @@ Tux::update(float delta) if (worldmap->at(tile_pos)->auto_walk) { // Turn to a new direction Tile* tile = worldmap->at(tile_pos); - Direction dir = NONE; + Direction dir = D_NONE; - if (tile->north && back_direction != NORTH) - dir = NORTH; - else if (tile->south && back_direction != SOUTH) - dir = SOUTH; - else if (tile->east && back_direction != EAST) - dir = EAST; - else if (tile->west && back_direction != WEST) - dir = WEST; - - if (dir != NONE) + if (tile->north && back_direction != D_NORTH) + dir = D_NORTH; + else if (tile->south && back_direction != D_SOUTH) + dir = D_SOUTH; + else if (tile->east && back_direction != D_EAST) + dir = D_EAST; + else if (tile->west && back_direction != D_WEST) + dir = D_WEST; + + if (dir != D_NONE) { direction = dir; back_direction = reverse_dir(direction); @@ -313,7 +322,7 @@ Tux::update(float delta) } // Walk automatically to the next tile - Point next_tile; + Vector next_tile; if (worldmap->path_ok(direction, tile_pos, &next_tile)) { tile_pos = next_tile; @@ -343,22 +352,23 @@ Tile::~Tile() WorldMap::WorldMap() { tile_manager = new TileManager(); - tux = new Tux(this); - + //tux = new Tux(this); + width = 20; height = 15; + + start_x = 4; + start_y = 5; + + level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", true); + leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true); + leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true); - level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA); - leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA); - leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA); - - input_direction = NONE; + input_direction = D_NONE; enter_level = false; - name = ""; + name = ""; music = "SALCON.MOD"; - - load_map(); } WorldMap::~WorldMap() @@ -374,12 +384,10 @@ WorldMap::~WorldMap() void WorldMap::load_map() { - std::string filename = datadir + "/levels/default/worldmap.stwm"; - - lisp_object_t* root_obj = lisp_read_from_file(filename); + lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename); if (!root_obj) - st_abort("Couldn't load file", filename); - + Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename); + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0) { lisp_object_t* cur = lisp_cdr(root_obj); @@ -391,15 +399,17 @@ WorldMap::load_map() if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0) { LispReader reader(lisp_cdr(element)); - reader.read_int("width", &width); - reader.read_int("height", &height); - reader.read_int_vector("data", &tilemap); + reader.read_int("width", width); + reader.read_int("height", height); + reader.read_int_vector("data", tilemap); } else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0) { LispReader reader(lisp_cdr(element)); - reader.read_string("name", &name); - reader.read_string("music", &music); + reader.read_string("name", name, true); + reader.read_string("music", music); + reader.read_int("start_pos_x", start_x); + reader.read_int("start_pos_y", start_y); } else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0) { @@ -408,8 +418,8 @@ WorldMap::load_map() while(!lisp_nil_p(cur)) { lisp_object_t* element = lisp_car(cur); - - if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0) + + if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0) { Level level; LispReader reader(lisp_cdr(element)); @@ -420,12 +430,45 @@ WorldMap::load_map() level.south = true; level.west = true; - reader.read_string("extro-filename", &level.extro_filename); - reader.read_string("name", &level.name); - reader.read_int("x", &level.x); - reader.read_int("y", &level.y); + reader.read_string("extro-filename", level.extro_filename); + reader.read_string("map-message", level.display_map_message); + reader.read_string("next-world", level.next_worldmap); + reader.read_string("level", level.name, true); + reader.read_int("x", level.x); + reader.read_int("y", level.y); + level.auto_path = true; + reader.read_bool("auto-path", level.auto_path); + level.swap_x = level.swap_y = -1; + reader.read_int("swap-x", level.swap_x); + reader.read_int("swap-y", level.swap_y); + level.vertical_flip = false; + reader.read_bool("flip-level", level.vertical_flip); + level.quit_worldmap = false; + reader.read_bool("exit-game", level.quit_worldmap); + + levels.push_back(level); + } + + /* Kept for backward compability */ + else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0) + { + Level level; + LispReader reader(lisp_cdr(element)); + level.solved = false; + + level.north = true; + level.east = true; + level.south = true; + level.west = true; - get_level_title(&level); // get level's title + reader.read_string("extro-filename", level.extro_filename); + if(!level.extro_filename.empty()) + level.quit_worldmap = true; + reader.read_string("name", level.name, true); + reader.read_int("x", level.x); + reader.read_int("y", level.y); + level.vertical_flip = false; + level.swap_x = level.swap_y = -1; levels.push_back(level); } @@ -443,40 +486,23 @@ WorldMap::load_map() } lisp_free(root_obj); + tux = new Tux(this); } -void WorldMap::get_level_title(Levels::pointer level) +void WorldMap::get_level_title(Level& level) { /** get level's title */ - level->title = ""; - - FILE * fi; - lisp_object_t* root_obj = 0; - fi = fopen((datadir + "/levels/" + level->name).c_str(), "r"); - if (fi == NULL) - { - perror((datadir + "/levels/" + level->name).c_str()); - return; - } - - lisp_stream_t stream; - lisp_stream_init_file (&stream, fi); - root_obj = lisp_read (&stream); - - if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) - { - printf("World: Parse Error in file %s", level->name.c_str()); - } + level.title = ""; - if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0) - { - LispReader reader(lisp_cdr(root_obj)); - reader.read_string("name", &level->title); - } - - lisp_free(root_obj); + LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level"); + if(!reader) + { + std::cerr << "Error: Could not open level file. Ignoring...\n"; + return; + } - fclose(fi); + reader->read_string("name", level.title, true); + delete reader; } void @@ -493,7 +519,7 @@ void WorldMap::get_input() { enter_level = false; - input_direction = NONE; + input_direction = D_NONE; SDL_Event event; while (SDL_PollEvent(&event)) @@ -507,7 +533,7 @@ WorldMap::get_input() switch(event.type) { case SDL_QUIT: - st_abort("Received window close", ""); + Termination::abort("Received window close", ""); break; case SDL_KEYDOWN: @@ -529,16 +555,16 @@ WorldMap::get_input() if (event.jaxis.axis == joystick_keymap.x_axis) { if (event.jaxis.value < -joystick_keymap.dead_zone) - input_direction = WEST; + input_direction = D_WEST; else if (event.jaxis.value > joystick_keymap.dead_zone) - input_direction = EAST; + input_direction = D_EAST; } else if (event.jaxis.axis == joystick_keymap.y_axis) { if (event.jaxis.value > joystick_keymap.dead_zone) - input_direction = SOUTH; + input_direction = D_SOUTH; else if (event.jaxis.value < -joystick_keymap.dead_zone) - input_direction = NORTH; + input_direction = D_NORTH; } break; @@ -560,41 +586,41 @@ WorldMap::get_input() Uint8 *keystate = SDL_GetKeyState(NULL); if (keystate[SDLK_LEFT]) - input_direction = WEST; + input_direction = D_WEST; else if (keystate[SDLK_RIGHT]) - input_direction = EAST; + input_direction = D_EAST; else if (keystate[SDLK_UP]) - input_direction = NORTH; + input_direction = D_NORTH; else if (keystate[SDLK_DOWN]) - input_direction = SOUTH; + input_direction = D_SOUTH; } } -Point -WorldMap::get_next_tile(Point pos, Direction direction) +Vector +WorldMap::get_next_tile(Vector pos, Direction direction) { switch(direction) { - case WEST: + case D_WEST: pos.x -= 1; break; - case EAST: + case D_EAST: pos.x += 1; break; - case NORTH: + case D_NORTH: pos.y -= 1; break; - case SOUTH: + case D_SOUTH: pos.y += 1; break; - case NONE: + case D_NONE: break; } return pos; } bool -WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos) +WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos) { *new_pos = get_next_tile(old_pos, direction); @@ -607,19 +633,19 @@ WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos) { // Check if we the tile allows us to go to new_pos switch(direction) { - case WEST: + case D_WEST: return (at(old_pos)->west && at(*new_pos)->east); - case EAST: + case D_EAST: return (at(old_pos)->east && at(*new_pos)->west); - case NORTH: + case D_NORTH: return (at(old_pos)->north && at(*new_pos)->south); - case SOUTH: + case D_SOUTH: return (at(old_pos)->south && at(*new_pos)->north); - case NONE: + case D_NONE: assert(!"path_ok() can't work if direction is NONE"); } return false; @@ -631,47 +657,63 @@ WorldMap::update(float delta) { if (enter_level && !tux->is_moving()) { + bool level_finished = true; Level* level = at_level(); - if (level) + if (!level) + { + std::cout << "Nothing to enter at: " + << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl; + return; + } + + + if(!level->name.empty()) { if (level->x == tux->get_tile_pos().x && level->y == tux->get_tile_pos().y) { - std::cout << "Enter the current level: " << level->name << std::endl;; + PlayerStatus old_player_status = player_status; + + std::cout << "Enter the current level: " << level->name << std::endl; + // do a shriking fade to the level + shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16 + + offset.y)), 500); GameSession session(datadir + "/levels/" + level->name, - 1, ST_GL_LOAD_LEVEL_FILE); + ST_GL_LOAD_LEVEL_FILE, level->vertical_flip); switch (session.run()) { - case GameSession::LEVEL_FINISHED: + case GameSession::ES_LEVEL_FINISHED: { + level_finished = true; bool old_level_state = level->solved; level->solved = true; - if (session.get_world()->get_tux()->got_coffee) + if (session.get_current_sector()->player->got_power != + session.get_current_sector()->player->NONE_POWER) player_status.bonus = PlayerStatus::FLOWER_BONUS; - else if (session.get_world()->get_tux()->size == BIG) + else if (session.get_current_sector()->player->size == BIG) player_status.bonus = PlayerStatus::GROWUP_BONUS; else player_status.bonus = PlayerStatus::NO_BONUS; - if (old_level_state != level->solved) + if (old_level_state != level->solved && level->auto_path) { // Try to detect the next direction to which we should walk // FIXME: Mostly a hack - Direction dir = NONE; + Direction dir = D_NONE; Tile* tile = at(tux->get_tile_pos()); - if (tile->north && tux->back_direction != NORTH) - dir = NORTH; - else if (tile->south && tux->back_direction != SOUTH) - dir = SOUTH; - else if (tile->east && tux->back_direction != EAST) - dir = EAST; - else if (tile->west && tux->back_direction != WEST) - dir = WEST; + if (tile->north && tux->back_direction != D_NORTH) + dir = D_NORTH; + else if (tile->south && tux->back_direction != D_SOUTH) + dir = D_SOUTH; + else if (tile->east && tux->back_direction != D_EAST) + dir = D_EAST; + else if (tile->west && tux->back_direction != D_WEST) + dir = D_WEST; - if (dir != NONE) + if (dir != D_NONE) { tux->set_direction(dir); //tux->update(delta); @@ -679,56 +721,90 @@ WorldMap::update(float delta) std::cout << "Walk to dir: " << dir << std::endl; } - - if (!level->extro_filename.empty()) - { - MusicRef theme = - music_manager->load_music(datadir + "/music/theme.mod"); - music_manager->play_music(theme); - // Display final credits and go back to the main menu - display_text_file(level->extro_filename, - "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE); - display_text_file("CREDITS", - "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS); - quit = true; - } } break; - case GameSession::LEVEL_ABORT: - // Reseting the player_status might be a worthy - // consideration, but I don't think we need it - // 'cause only the bad players will use it to - // 'cheat' a few items and that isn't necesarry a - // bad thing (ie. better they continue that way, - // then stop playing the game all together since it - // is to hard) + case GameSession::ES_LEVEL_ABORT: + level_finished = false; + /* In case the player's abort the level, keep it using the old + status. But the minimum lives and no bonus. */ + player_status.score = old_player_status.score; + player_status.distros = old_player_status.distros; + player_status.lives = std::min(old_player_status.lives, player_status.lives); + player_status.bonus = player_status.NO_BONUS; + break; - case GameSession::GAME_OVER: + case GameSession::ES_GAME_OVER: + { + level_finished = false; + /* draw an end screen */ + /* in the future, this should make a dialog a la SuperMario, asking + if the player wants to restart the world map with no score and from + level 1 */ + char str[80]; + + DrawingContext context; + context.draw_gradient(Color (200,240,220), Color(200,200,220), + LAYER_BACKGROUND0); + + context.draw_text_center(blue_text, _("GAMEOVER"), + Vector(0, 200), LAYER_FOREGROUND1); + + sprintf(str, _("SCORE: %d"), player_status.score); + context.draw_text_center(gold_text, str, + Vector(0, 230), LAYER_FOREGROUND1); + + sprintf(str, _("COINS: %d"), player_status.distros); + context.draw_text_center(gold_text, str, + Vector(0, screen->w - 32), LAYER_FOREGROUND1); + + context.do_drawing(); + + SDL_Event event; + wait_for_event(event,2000,5000,true); + quit = true; player_status.reset(); break; - case GameSession::NONE: + } + case GameSession::ES_NONE: + assert(false); // Should never be reached break; } - music_manager->play_music(song); + SoundManager::get()->play_music(song); Menu::set_current(0); if (!savegame_file.empty()) savegame(savegame_file); - return; } } - else + /* The porpose of the next checking is that if the player lost + the level (in case there is one), don't show anything */ + if(level_finished) { - std::cout << "Nothing to enter at: " - << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl; + if (!level->extro_filename.empty()) + { + // Display a text file + display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text ); + } + if (level->swap_x != -1 && level->swap_y != -1) + { + // TODO: add an effect, like a camera scrolling, or at least, a fading + tux->set_tile_pos(Vector(level->swap_x, level->swap_y)); + } + if (!level->next_worldmap.empty()) + { + // Load given worldmap + loadmap(level->next_worldmap); + } + if (level->quit_worldmap) + quit = true; } } else { - tux->update(delta); + tux->action(delta); tux->set_direction(input_direction); } @@ -743,11 +819,6 @@ WorldMap::update(float delta) { case MNID_RETURNWORLDMAP: // Return to game break; - case MNID_SAVEGAME: - if (!savegame_file.empty()) - savegame(savegame_file); - break; - case MNID_QUITWORLDMAP: // Quit Worldmap quit = true; break; @@ -761,14 +832,16 @@ WorldMap::update(float delta) } Tile* -WorldMap::at(Point p) +WorldMap::at(Vector p) { assert(p.x >= 0 && p.x < width && p.y >= 0 && p.y < height); - return tile_manager->get(tilemap[width * p.y + p.x]); + int x = int(p.x); + int y = int(p.y); + return tile_manager->get(tilemap[width * y + x]); } WorldMap::Level* @@ -786,54 +859,67 @@ WorldMap::at_level() void -WorldMap::draw(const Point& offset) +WorldMap::draw(DrawingContext& context, const Vector& offset) { for(int y = 0; y < height; ++y) for(int x = 0; x < width; ++x) { - Tile* tile = at(Point(x, y)); - tile->sprite->draw(x*32 + offset.x, - y*32 + offset.y); + Tile* tile = at(Vector(x, y)); + context.draw_surface(tile->sprite, + Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES); } for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { + if(i->name.empty()) + continue; + if (i->solved) - leveldot_green->draw(i->x*32 + offset.x, - i->y*32 + offset.y); + context.draw_surface(leveldot_green, + Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1); else - leveldot_red->draw(i->x*32 + offset.x, - i->y*32 + offset.y); + context.draw_surface(leveldot_red, + Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1); } - tux->draw(offset); - draw_status(); + tux->draw(context, offset); + draw_status(context); } void -WorldMap::draw_status() +WorldMap::draw_status(DrawingContext& context) { char str[80]; - sprintf(str, "%d", player_status.score); - white_text->draw("SCORE", 0, 0); - gold_text->draw(str, 96, 0); + sprintf(str, " %d", player_status.score); + + context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1); + context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1); sprintf(str, "%d", player_status.distros); - white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP); - gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP); + context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0), + LAYER_FOREGROUND1); + context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0), + LAYER_FOREGROUND1); - white_text->draw("LIVES", 480, 0); if (player_status.lives >= 5) { sprintf(str, "%dx", player_status.lives); - gold_text->draw_align(str, 617, 0, A_RIGHT, A_TOP); - tux_life->draw(565+(18*3), 0); + context.draw_text(gold_text, str, + Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0), + LAYER_FOREGROUND1); + context.draw_surface(tux_life, Vector(screen->w - + gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1); } else { for(int i= 0; i < player_status.lives; ++i) - tux_life->draw(565+(18*i),0); + context.draw_surface(tux_life, + Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0), + LAYER_FOREGROUND1); } + context.draw_text(white_text, _("LIVES"), + Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0), + LAYER_FOREGROUND1); if (!tux->is_moving()) { @@ -842,7 +928,21 @@ WorldMap::draw_status() if (i->x == tux->get_tile_pos().x && i->y == tux->get_tile_pos().y) { - white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM); + if(!i->name.empty()) + { + if(i->title == "") + get_level_title(*i); + + context.draw_text_center(white_text, i->title, + Vector(0, screen->h - white_text->get_height() - 30), + LAYER_FOREGROUND1); + } + + /* Display a message in the map, if any as been selected */ + if(!i->display_map_message.empty()) + context.draw_text_center(gold_text, i->display_map_message, + Vector(0, screen->h - white_text->get_height() - 60), + LAYER_FOREGROUND1); break; } } @@ -856,14 +956,15 @@ WorldMap::display() quit = false; - song = music_manager->load_music(datadir + "/music/" + music); - music_manager->play_music(song); - + song = SoundManager::get()->load_music(datadir + "/music/" + music); + SoundManager::get()->play_music(song); + unsigned int last_update_time; unsigned int update_time; - last_update_time = update_time = st_get_ticks(); + last_update_time = update_time = Ticks::get(); + DrawingContext context; while(!quit) { float delta = ((float)(update_time-last_update_time))/100.0; @@ -874,9 +975,9 @@ WorldMap::display() delta = .3f; last_update_time = update_time; - update_time = st_get_ticks(); + update_time = Ticks::get(); - Point tux_pos = tux->get_pos(); + Vector tux_pos = tux->get_pos(); if (1) { offset.x = -tux_pos.x + screen->w/2; @@ -889,16 +990,17 @@ WorldMap::display() if (offset.y < screen->h - height*32) offset.y = screen->h - height*32; } - draw(offset); + draw(context, offset); get_input(); update(delta); if(Menu::current()) { - Menu::current()->draw(); - mouse_cursor->draw(); + Menu::current()->draw(context); + mouse_cursor->draw(context); } - flipscreen(); + + context.do_drawing(); SDL_Delay(20); } @@ -907,6 +1009,9 @@ WorldMap::display() void WorldMap::savegame(const std::string& filename) { + if(filename != "") + return; + std::cout << "savegame: " << filename << std::endl; std::ofstream out(filename.c_str()); @@ -919,7 +1024,8 @@ WorldMap::savegame(const std::string& filename) out << "(supertux-savegame\n" << " (version 1)\n" - << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n" + << " (title \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n" + << " (map \"" << map_filename << "\")\n" << " (lives " << player_status.lives << ")\n" << " (score " << player_status.score << ")\n" << " (distros " << player_status.distros << ")\n" @@ -946,44 +1052,56 @@ WorldMap::loadgame(const std::string& filename) { std::cout << "loadgame: " << filename << std::endl; savegame_file = filename; + map_filename = "icyisland.stwm"; if (access(filename.c_str(), F_OK) != 0) + { + load_map(); return; + } lisp_object_t* savegame = lisp_read_from_file(filename); if (!savegame) { std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl; + load_map(); return; } lisp_object_t* cur = savegame; if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0) + { + load_map(); return; + } cur = lisp_cdr(cur); LispReader reader(cur); - reader.read_int("lives", &player_status.lives); - reader.read_int("score", &player_status.score); - reader.read_int("distros", &player_status.distros); + /* Get the Map filename and then load it before setting level settings */ + reader.read_string("map", map_filename); + load_map(); + + reader.read_int("lives", player_status.lives); + reader.read_int("score", player_status.score); + reader.read_int("distros", player_status.distros); if (player_status.lives < 0) player_status.lives = START_LIVES; lisp_object_t* tux_cur = 0; - if (reader.read_lisp("tux", &tux_cur)) + if (reader.read_lisp("tux", tux_cur)) { - Point p; + Vector p; std::string back_str = "none"; std::string bonus_str = "none"; LispReader tux_reader(tux_cur); - tux_reader.read_int("x", &p.x); - tux_reader.read_int("y", &p.y); - tux_reader.read_string("back", &back_str); - tux_reader.read_string("bonus", &bonus_str); + tux_reader.read_float("x", p.x); + tux_reader.read_float("y", p.y); + tux_reader.read_string("back", back_str); + tux_reader.read_string("bonus", bonus_str); player_status.bonus = string_to_bonus(bonus_str); tux->back_direction = string_to_direction(back_str); @@ -991,7 +1109,7 @@ WorldMap::loadgame(const std::string& filename) } lisp_object_t* level_cur = 0; - if (reader.read_lisp("levels", &level_cur)) + if (reader.read_lisp("levels", level_cur)) { while(level_cur) { @@ -1004,8 +1122,8 @@ WorldMap::loadgame(const std::string& filename) bool solved = false; LispReader level_reader(data); - level_reader.read_string("name", &name); - level_reader.read_bool("solved", &solved); + level_reader.read_string("name", name, true); + level_reader.read_bool("solved", solved); for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { @@ -1021,6 +1139,14 @@ WorldMap::loadgame(const std::string& filename) lisp_free(savegame); } +void +WorldMap::loadmap(const std::string& filename) +{ + savegame_file = ""; + map_filename = filename; + load_map(); +} + } // namespace WorldMapNS /* Local Variables: */