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

Author Topic: Drawing a tilemap with a sf::VertexArray like in the example  (Read 5960 times)

0 Members and 2 Guests are viewing this topic.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Drawing a tilemap with a sf::VertexArray like in the example
« on: August 08, 2013, 09:21:30 am »
Hi, could someone please clarify something for me? I'm trying to draw my tilemap with a big sf::VertexArray sort of like in this example: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

I'm confused on how to texture each quad. I will be loading my big tileset file into a texture right? I guess I'm lost by this part:

                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);

I need to set each vertex texCoords but how does it know where to look based on a sf::Vector2f? I guess the real question is what are texCoords? How do I pick a piece of my tileset to texture the quad with?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #1 on: August 08, 2013, 09:27:22 am »
Have you read the "Texturing" part of the tutorial? What don't you understand? I won't be able to explain it better than what I wrote in the tutorial ;)
Laurent Gomila - SFML developer

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #2 on: August 08, 2013, 09:32:53 am »
I have been drawing everything so far with sf::sprite and sf::texture so this is a little different. I have probably missed something in a previous tutorial. I'll search for it.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #3 on: August 08, 2013, 09:49:45 am »
I think I see now that you can still define a texture in the same way as I was doing before and pass it along with the vertex array to draw.

Let me ask this instead:

If I have multiple objects that all contain their own vertex array defining their quad vertices do I need to combine them all together into one huge vertex array before I draw them or can I iterate through a list of vertex arrays and draw each? I'm sure I CAN but does that negate the performance increase I'm hoping to get by using a vertex array instead of a sf::sprites?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #4 on: August 08, 2013, 10:11:49 am »
Vertex arrays can improve performances because they allow to draw big entities with few draw calls (typical example: a tiled map can be drawn in one call instead of tiles_count calls). If you just replace all your sprites with vertex arrays made of a single quad, you're not going to see any difference (sf::Sprite uses a vertex array internally).

And how is it related to your texturing problem? I think you lost me :P

Quote
I have been drawing everything so far with sf::sprite and sf::texture so this is a little different. I have probably missed something in a previous tutorial.
I was referring to the vertex array tutorial, of course.
And this is not different: the texCoords that you assign to your quad are the same that you would give to your sprite's textureRect.
Laurent Gomila - SFML developer

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #5 on: August 08, 2013, 10:19:01 am »
Yes it was unrelated. I think I understand how it will work. I will still need to load a sf::texture from a file and the texCoords are simply the area within the texture to look. Now I will just need to think of how to create a big vertex array to contain each objects vertex array. One challenge after another I suppose. Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing a tilemap with a sf::VertexArray like in the example
« Reply #6 on: August 08, 2013, 10:36:04 am »
Quote
I think I understand how it will work. I will still need to load a sf::texture from a file and the texCoords are simply the area within the texture to look.
Correct.

Quote
Now I will just need to think of how to create a big vertex array to contain each objects vertex array.
big_vertex_array.clear();
for (each entity)
{
    for (int i = 0; i < entity.vertex_array.size(); ++i)
        big_vertex_array.append(entity.vertex_array[i]);
}

?

And if you can define your entities' vertices directly into the big vertex array, this is even better because you avoid the big copy (unless everything is done at init time anyway, in this case we don't care).
Laurent Gomila - SFML developer

 

anything