Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tilemap drawing breaks with different tile sizes  (Read 2109 times)

0 Members and 1 Guest are viewing this topic.

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Tilemap drawing breaks with different tile sizes
« on: October 17, 2013, 04:42:14 am »
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.
« Last Edit: October 17, 2013, 04:53:58 am by chessguy »

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Tilemap drawing breaks with different tile sizes
« Reply #1 on: October 17, 2013, 01:34:07 pm »
The second picture issue:
Both of those for loop code is exactly the same, why not merge it intro one and just change the Layer that is used on second loop cycle:
Code: [Select]
my_class LayerTarget = 1;
for(int i = 0; i < 2; i++)
{
//The positioning,drawing...
LayerTarget = 2;
}

When you added
  sprite.setTextureRect(sf::IntRect(0, 0, 32, 64));
your first draw was fine, but second main loop went true and it was drawing the terrain with textureRect wrong.
If you modified textureRect for second draw, you most set it back for first draw.

If you are interested in best way to draw tiles that is sf::VertexArray.
It is little tricky to start with, but since i have used it i have gotten such a good performance its crazy good.
To provide you with vision, i got same performance drawing ~1000 tile sprites as drawing ~27,000,000 tiles .
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

 

anything