Add support for centering multihex terrain images...
...using the center=(x,y) attribute.
This commit is contained in:
parent
cf9eac3aa1
commit
1f9b17b954
4 changed files with 58 additions and 8 deletions
|
@ -48,12 +48,14 @@ static const int UNITPOS = 36 + 18;
|
|||
*/
|
||||
static const int BASE_Y_INTERVAL = 100000;
|
||||
|
||||
terrain_builder::rule_image::rule_image(int layer, int x, int y, bool global_image) :
|
||||
terrain_builder::rule_image::rule_image(int layer, int x, int y, bool global_image, int cx, int cy) :
|
||||
layer(layer),
|
||||
basex(x),
|
||||
basey(y),
|
||||
variants(),
|
||||
global_image(global_image)
|
||||
global_image(global_image),
|
||||
center_x(cx),
|
||||
center_y(cy)
|
||||
{}
|
||||
|
||||
terrain_builder::tile::tile() :
|
||||
|
@ -306,7 +308,7 @@ bool terrain_builder::start_animation(building_rule &rule)
|
|||
time = 100;
|
||||
}
|
||||
if(image->global_image) {
|
||||
image_vector.push_back(animated<image::locator>::frame_description(time,image::locator("terrain/" + str + ".png",constraint->second.loc)));
|
||||
image_vector.push_back(animated<image::locator>::frame_description(time,image::locator("terrain/" + str + ".png",constraint->second.loc, image->center_x, image->center_y)));
|
||||
} else {
|
||||
image_vector.push_back(animated<image::locator>::frame_description(time,image::locator("terrain/" + str + ".png")));
|
||||
}
|
||||
|
@ -535,8 +537,17 @@ void terrain_builder::add_images_from_config(rule_imagelist& images, const confi
|
|||
}
|
||||
}
|
||||
|
||||
images.push_back(rule_image(layer, basex - dx, basey - dy, global));
|
||||
int center_x = -1, center_y = -1;
|
||||
if( !(**img)["center"].empty()) {
|
||||
std::vector<std::string> center = utils::split((**img)["center"]);
|
||||
|
||||
if(center.size() >= 2) {
|
||||
center_x = atoi(center[0].c_str());
|
||||
center_y = atoi(center[1].c_str());
|
||||
}
|
||||
}
|
||||
|
||||
images.push_back(rule_image(layer, basex - dx, basey - dy, global, center_x, center_y));
|
||||
|
||||
// Adds the main (default) variant of the image, if present
|
||||
images.back().variants.insert(std::pair<std::string, rule_image_variant>("", rule_image_variant(name,"")));
|
||||
|
|
|
@ -169,7 +169,7 @@ public:
|
|||
* The rule_image structure represents one such image.
|
||||
*/
|
||||
struct rule_image {
|
||||
rule_image(int layer, int x, int y, bool global_image=false);
|
||||
rule_image(int layer, int x, int y, bool global_image=false, int center_x=-1, int center_y=-1);
|
||||
|
||||
/** The layer of the image for horizontal layering */
|
||||
int layer;
|
||||
|
@ -186,6 +186,10 @@ public:
|
|||
* [terrain_graphics] tag, set to false if it was defined as a
|
||||
* child of a [tile] tag */
|
||||
bool global_image;
|
||||
|
||||
/** The position where the center of the image base should be
|
||||
*/
|
||||
int center_x, center_y;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -186,6 +186,12 @@ locator::locator(const std::string &filename, const gamemap::location &loc, cons
|
|||
init_index();
|
||||
}
|
||||
|
||||
locator::locator(const std::string &filename, const gamemap::location &loc, int center_x, int center_y, const std::string& modifications) :
|
||||
val_(filename, loc, center_x, center_y, modifications)
|
||||
{
|
||||
init_index();
|
||||
}
|
||||
|
||||
locator& locator::operator=(const locator &a)
|
||||
{
|
||||
index_ = a.index_;
|
||||
|
@ -196,7 +202,8 @@ locator& locator::operator=(const locator &a)
|
|||
|
||||
locator::value::value(const locator::value& a) :
|
||||
type_(a.type_), filename_(a.filename_), loc_(a.loc_),
|
||||
modifications_(a.modifications_)
|
||||
modifications_(a.modifications_),
|
||||
center_x_(a.center_x_), center_y_(a.center_y_)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -230,6 +237,11 @@ locator::value::value(const std::string& filename, const gamemap::location& loc,
|
|||
{
|
||||
}
|
||||
|
||||
locator::value::value(const std::string& filename, const gamemap::location& loc, int center_x, int center_y, const std::string& modifications) :
|
||||
type_(SUB_FILE), filename_(filename), loc_(loc), modifications_(modifications), center_x_(center_x), center_y_(center_y)
|
||||
{
|
||||
}
|
||||
|
||||
bool locator::value::operator==(const value& a) const
|
||||
{
|
||||
if(a.type_ != type_) {
|
||||
|
@ -237,7 +249,8 @@ bool locator::value::operator==(const value& a) const
|
|||
} else if(type_ == FILE) {
|
||||
return filename_ == a.filename_;
|
||||
} else if(type_ == SUB_FILE) {
|
||||
return filename_ == a.filename_ && loc_ == a.loc_ && modifications_ == a.modifications_;
|
||||
return filename_ == a.filename_ && loc_ == a.loc_ && modifications_ == a.modifications_
|
||||
&& center_x_ == a.center_x_ && center_y_ == a.center_y_;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -254,6 +267,11 @@ bool locator::value::operator<(const value& a) const
|
|||
return filename_ < a.filename_;
|
||||
if(loc_ != a.loc_)
|
||||
return loc_ < a.loc_;
|
||||
if(center_x_ != a.center_x_)
|
||||
return center_x_ < a.center_x_;
|
||||
if(center_y_ != a.center_y_)
|
||||
return center_y_ < a.center_y_;
|
||||
|
||||
return(modifications_ < a.modifications_);
|
||||
} else {
|
||||
return false;
|
||||
|
@ -298,7 +316,19 @@ surface locator::load_image_sub_file() const
|
|||
return surface(NULL);
|
||||
|
||||
surface surf=mother_surface;
|
||||
if(val_.loc_.x>-1 && val_.loc_.y>-1){
|
||||
if(val_.loc_.x>-1 && val_.loc_.y>-1 && val_.center_x_>-1 && val_.center_y_>-1){
|
||||
int offset_x = mother_surface->w/2 - val_.center_x_;
|
||||
int offset_y = mother_surface->h/2 - val_.center_y_;
|
||||
SDL_Rect srcrect = {
|
||||
((tile_size*3) / 4) * val_.loc_.x + offset_x,
|
||||
tile_size * val_.loc_.y + (tile_size/2) * (val_.loc_.x % 2) + offset_y,
|
||||
tile_size, tile_size
|
||||
};
|
||||
|
||||
surface tmp(cut_surface(mother_surface, srcrect));
|
||||
surf=mask_surface(tmp, mask);
|
||||
}
|
||||
else if(val_.loc_.x>-1 && val_.loc_.y>-1 ){
|
||||
SDL_Rect srcrect = {
|
||||
((tile_size*3) / 4) * val_.loc_.x,
|
||||
tile_size * val_.loc_.y + (tile_size/2) * (val_.loc_.x % 2),
|
||||
|
@ -309,6 +339,7 @@ surface locator::load_image_sub_file() const
|
|||
surf=mask_surface(tmp, mask);
|
||||
}
|
||||
|
||||
|
||||
if(val_.modifications_.size()){
|
||||
bool xflip = false;
|
||||
bool yflip = false;
|
||||
|
|
|
@ -67,6 +67,7 @@ namespace image {
|
|||
value(const std::string& filename);
|
||||
value(const std::string& filename, const std::string& modifications);
|
||||
value(const std::string& filename, const gamemap::location& loc, const std::string& modifications);
|
||||
value(const std::string& filename, const gamemap::location& loc, int center_x, int center_y, const std::string& modifications);
|
||||
|
||||
bool operator==(const value& a) const;
|
||||
bool operator<(const value& a) const;
|
||||
|
@ -75,6 +76,8 @@ namespace image {
|
|||
std::string filename_;
|
||||
gamemap::location loc_;
|
||||
std::string modifications_;
|
||||
int center_x_;
|
||||
int center_y_;
|
||||
};
|
||||
|
||||
// Constructing locators is somewhat slow, accessing image
|
||||
|
@ -88,6 +91,7 @@ namespace image {
|
|||
locator(const std::string& filename);
|
||||
locator(const std::string& filename, const std::string& modifications);
|
||||
locator(const std::string& filename, const gamemap::location& loc, const std::string& modifications="");
|
||||
locator(const std::string& filename, const gamemap::location& loc, int center_x, int center_y, const std::string& modifications="");
|
||||
|
||||
locator& operator=(const locator &a);
|
||||
bool operator==(const locator &a) const { return index_ == a.index_; }
|
||||
|
|
Loading…
Add table
Reference in a new issue