Greetings. So, I made this little this that renders tiles. It looks like this:
So far, so good. But I wanted to have tiles bigger than 32x32. Or a different size,anyway. This gave me this:
The tree texture is 32x64. It has three leaf tiers: what you see is the top half.
Here is the drawing code:
static sf::Sprite sprite;
for (int x = 0; x < world.getSize().x; ++x)
for (int y = 0; y < world.getSize().y; ++y)
{
sprite.setTexture(getTextureForType(world.getFirstLayer(x, y), resource, sf::Vector2i(x, y)));
sprite.setPosition(x*32, (target.getSize().y - (y*32 + 32)) - (sprite.getTexture()->getSize().y - 32));
target.draw(sprite);
}
for (int x = 0; x < world.getSize().x; ++x)
for (int y = 0; y < world.getSize().y; ++y)
{
sprite.setTexture(getTextureForType(world.getSecondLayer(x, y), resource, sf::Vector2i(x, y)));
sprite.setPosition(x*32, (target.getSize().y - (y*32 + 32)) - (sprite.getTexture()->getSize().y - 32));
target.draw(sprite);
}
The first layer is things like the ground, second is trees, etc.
But, if I comment the first loop, I get this:
When I, between the loops, add:
sprite.setTextureRect(sf::IntRect(0, 0, 32, 64));
I get this:
Note this is drawn multiple times, which is why (presumably) the ground is distorted like that.
What is going on here? I cannot figure it out. Also, I reverse the y coordinates of the tiles, so when generating, y + 1 is up the screen, not down.
A while back, I saw a thread stating that sprites are not a drawing tool. For example, a sprite should always be a spaceship, not switching between tiles. If need be, I can try that (one sprite per tile), but I am still curious.
Or perhaps a tilesheet, but I am yet to discover a good way to load them easily, and editing them in the past didn't work well for me.
EDIT: I was checking SFML's source, and apparently setTexture has a parameter to reset the rectangle. When I pass true for this, it seems to work correctly.
I am still interested in best practice for tilemaps, though.