Skip to content

Commit

Permalink
made render order property of tmx maps optional for backward compatib…
Browse files Browse the repository at this point in the history
…ility with maps created in older versions of Tiled

made chunks in tile map renderer respect the bounds of the map rather than repeat tiles
  • Loading branch information
fallahn committed Nov 26, 2016
1 parent 855103b commit 4372d5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
10 changes: 7 additions & 3 deletions xygine/src/components/ComponentTileMapLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ void TileMapLayer::setTileData(const tmx::TileLayer* layer, const std::vector<tm
chunk.bounds = { x * floatRes.x, y * floatRes.y, floatRes.x, floatRes.y };
chunk.bounds.left += layer->getOffset().x;
chunk.bounds.top += layer->getOffset().y;

float right = std::min(chunk.bounds.left + floatRes.x, map.getBounds().width + map.getBounds().left);
float bottom = std::min(chunk.bounds.top + floatRes.y, map.getBounds().height + map.getBounds().top);

chunk.vertices =
{
sf::Vertex(sf::Vector2f(chunk.bounds.left, chunk.bounds.top), sf::Vector2f()),
sf::Vertex(sf::Vector2f(chunk.bounds.left + floatRes.x, chunk.bounds.top), sf::Vector2f(floatRes.x, 0.f)),
sf::Vertex(sf::Vector2f(chunk.bounds.left + floatRes.x, chunk.bounds.top + floatRes.y), floatRes),
sf::Vertex(sf::Vector2f(chunk.bounds.left, chunk.bounds.top + floatRes.y), sf::Vector2f(0.f, floatRes.y))
sf::Vertex(sf::Vector2f(right, chunk.bounds.top), sf::Vector2f(right - chunk.bounds.left, 0.f)),
sf::Vertex(sf::Vector2f(right, bottom), { right - chunk.bounds.left, bottom - chunk.bounds.top }),
sf::Vertex(sf::Vector2f(chunk.bounds.left, bottom), sf::Vector2f(0.f, bottom - chunk.bounds.top))
};

//check each used tileset, and if it's used by this chunk create a lookup texture
Expand Down
52 changes: 25 additions & 27 deletions xygine/src/tilemap/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,32 +192,29 @@ bool Map::load(const std::string& path)
}

attribString = mapNode.attribute("renderorder").as_string();
if (attribString.empty())
{
Logger::log("missing render-order attribute, map not loaded.", Logger::Type::Error);
return reset();
}

if (attribString == "right-down")
{
m_renderOrder = RenderOrder::RightDown;
}
else if (attribString == "right-up")
{
m_renderOrder = RenderOrder::RightUp;
}
else if (attribString == "left-down")
{
m_renderOrder = RenderOrder::LeftDown;
}
else if (attribString == "left-up")
{
m_renderOrder = RenderOrder::LeftUp;
}
else
if (!attribString.empty())
{
Logger::log(attribString + ": invalid render order. Map not loaded.", Logger::Type::Error);
return reset();
if (attribString == "right-down")
{
m_renderOrder = RenderOrder::RightDown;
}
else if (attribString == "right-up")
{
m_renderOrder = RenderOrder::RightUp;
}
else if (attribString == "left-down")
{
m_renderOrder = RenderOrder::LeftDown;
}
else if (attribString == "left-up")
{
m_renderOrder = RenderOrder::LeftUp;
}
else
{
Logger::log(attribString + ": invalid render order. Map not loaded.", Logger::Type::Error);
return reset();
}
}

unsigned width = mapNode.attribute("width").as_int();
Expand Down Expand Up @@ -359,8 +356,9 @@ std::unique_ptr<TileMapLayer> Map::getDrawable(xy::MessageBus& mb, const Layer&

//calculate chunk size based on map's tile size (ie so it fits tiles perfectly)
sf::Vector2u chunkSize(1024u, 1024u);
while (chunkSize.x % getTileSize().x != 0) { chunkSize.x--; }
while (chunkSize.y % getTileSize().y != 0) { chunkSize.y--; }
auto tileSize = getTileSize();
while (chunkSize.x % tileSize.x != 0) { chunkSize.x--; }
while (chunkSize.y % tileSize.y != 0) { chunkSize.y--; }
LOG("Set chunk size to " + std::to_string(chunkSize.x), Logger::Type::Info);

auto tml = xy::Component::create<TileMapLayer>(mb, Key(), chunkSize);
Expand Down

0 comments on commit 4372d5d

Please sign in to comment.