add a reload_map member function...

...that allows the display object to stay in sync when the map
dimensions have changed
This commit is contained in:
Tomasz Śniatowski 2008-07-19 22:07:30 +01:00
parent 9f830ac63b
commit c048b0595b
4 changed files with 41 additions and 2 deletions

View file

@ -119,6 +119,13 @@ void terrain_builder::tilemap::reset()
it->clear();
}
void terrain_builder::tilemap::reload(int x, int y)
{
x_ = x;
y_ = y;
map_.resize((x + 4) * (y + 4));
}
bool terrain_builder::tilemap::on_map(const gamemap::location &loc) const
{
if(loc.x < -2 || loc.y < -2 || loc.x > (x_ + 1) || loc.y > (y_ + 1)) {
@ -162,6 +169,13 @@ terrain_builder::terrain_builder(const config& cfg, const config& level,
build_terrains();
}
void terrain_builder::reload_map()
{
tile_map_.reload(map_.w(), map_.h());
terrain_by_type_.clear();
build_terrains();
}
const terrain_builder::imagelist *terrain_builder::get_terrain_at(const gamemap::location &loc,
const std::string &tod, const ADJACENT_TERRAIN_TYPE terrain_type)
{

View file

@ -76,6 +76,12 @@ public:
*/
terrain_builder(const config& cfg, const config &level,
const gamemap& map, const std::string& offmap_image);
/**
* Updates internals that cache map size. This should be called when the map
* size has changed.
*/
void reload_map();
/** Returns a vector of strings representing the images to load & blit
* together to get the built content for this tile.
@ -387,6 +393,11 @@ private:
* Resets the whole tile map
*/
void reset();
/**
* Rebuilds the map to a new set of dimensions
*/
void reload(int x, int y);
private:
/** The map */
std::vector<tile> map_;

View file

@ -79,8 +79,7 @@ display::display(CVideo& video, const gamemap& map, const config& theme_cfg, con
viewpoint_(NULL),
xpos_(0),
ypos_(0),
theme_(theme_cfg,
screen_area()),
theme_(theme_cfg, screen_area()),
zoom_(DefaultZoom),
last_zoom_(SmallZoom),
builder_(cfg, level, map, theme_.border().tile_image),
@ -139,6 +138,15 @@ display::~display()
{
}
void display::reload_map()
{
builder_.reload_map();
SDL_Rect m = map_area();
if (!hex_clicked_on(m.x + m.w, m.y + m.h).valid()) {
scroll_to_tile(gamemap::location(0, 0), WARP);
}
}
const SDL_Rect& display::max_map_area() const
{
static SDL_Rect max_area = {0, 0, 0, 0};

View file

@ -64,6 +64,12 @@ public:
display(CVideo& video, const gamemap& map, const config& theme_cfg,
const config& cfg, const config& level);
virtual ~display();
/**
* Updates internals that cache map size. This should be called when the map
* size has changed.
*/
void reload_map();
static Uint32 rgb(Uint8 red, Uint8 green, Uint8 blue)
{ return 0xFF000000 | (red << 16) | (green << 8) | blue; }