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

Author Topic: Empty texture for vertex array quads  (Read 7933 times)

0 Members and 1 Guest are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1846
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Empty texture for vertex array quads
« Reply #15 on: September 13, 2012, 02:07:31 am »
Quote
Accidentally, I discovered that this works:
[sarcasm]Nooo waaay...using if statements in programming? Srsly?[/sarcasm]
Quote
But I can't believe it was so easy, is it safe to use at all?
Why wouldn't it be? But don't declare your array as
sf::VertexArray layer2(sf::Quads, layer_width * layer_height * 4);
you're having too much quads if something gets skipped, start with 0 size and use append in the if block.
Quote
And is there a built-in way to render the quads as wireframe, or draw some kind of bouding box around them to see how the layer looks like on the polygon level?
Not really. You can take pointer to first vertex and use overloaded draw with lines or line strip but one will skip every second line and other will connect quads together.
« Last Edit: September 13, 2012, 02:10:00 am by FRex »
Back to C++ gamedev with SFML in May 2023

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Empty texture for vertex array quads
« Reply #16 on: September 13, 2012, 02:14:09 am »
[sarcasm]Nooo waaay...using if statements in programming? Srsly?[/sarcasm]

Well I had no idea where to start and what to do, so I did this, and if I remember correctly, this is the first time that something worked on the first try. And I'm surprised nobody mentioned this (well, Laurent suggested to not to draw the empty quads at all, but I had no idea this could be done this easily).  :P

Why wouldn't it be? But don't declare your array as
sf::VertexArray layer2(sf::Quads, layer_width * layer_height * 4);
you're having too much quads if something gets skipped, start with 0 size and use append in the if block

See? You just basically told me it's not entirely safe. :)
And thanks for the suggestion, but what do you mean by "having too much quads if something gets skipped"?
« Last Edit: September 13, 2012, 02:15:53 am by N_K »

FRex

  • Hero Member
  • *****
  • Posts: 1846
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Empty texture for vertex array quads
« Reply #17 on: September 13, 2012, 02:32:52 am »
Quote
And thanks for the suggestion, but what do you mean by "having too much quads if something gets skipped"?
Seriously? ???
You're leaving the vertices of tiles that have 0 id in the array but untouched. If you replaced your operators [] with appends then it'd be ok.
Back to C++ gamedev with SFML in May 2023

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Empty texture for vertex array quads
« Reply #18 on: September 13, 2012, 02:39:28 am »
You're leaving the vertices of tiles that have 0 id in the array but untouched. If you replaced your operators [] with appends then it'd be ok.

So I have to do something with these parts:
layer2[current2 + 0].position = sf::Vector2f((x2 + 0) * size, (y2 + 0) * size);

If I understand correctly, instead of setting the position/texture coordinates controlled by the
int current2 = (x2 + y2 * layer_width) * 4;
array, I need to "append" them (to something).

I still don't get it, but it could be just me (being sleepy at 2:30 AM), I'll take a look at it again later.

FRex

  • Hero Member
  • *****
  • Posts: 1846
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Empty texture for vertex array quads
« Reply #19 on: September 13, 2012, 02:45:09 am »
sf::VertexArray layer2(sf::Quads);

for(int x2=0; x2 < layer_width; ++x2)
{
        for(int y2=0; y2 < layer_height; ++y2)
        {
             

                if(spriteLayer->GetTileId(x2, y2) != 0)
                {
                                                int currentTile2 = spriteLayer->GetTileId(x2, y2);
                        int tx2 = currentTile2 % horizontalTiles;
                        int ty2 = currentTile2 / horizontalTiles;
                                               
                                                layer2.append(sf::Vertex(sf::Vector2f((x2 + 0) * size, (y2 + 0) * size)
                                                ,sf::Vector2f((tx2 + 0) * size, (ty2 + 0) * size)));
                                               
                                                layer2.append(sf::Vertex(sf::Vector2f((x2 + 0) * size, (y2 + 1) * size)
                                                ,sf::Vector2f((tx2 + 0) * size, (ty2 + 1) * size)));
                                               
                                                layer2.append(sf::Vertex(sf::Vector2f((x2 + 1) * size, (y2 + 1) * size)
                                                ,sf::Vector2f((tx2 + 1) * size, (ty2 + 1) * size)));
                                               
                                                layer2.append(sf::Vertex(sf::Vector2f((x2 + 1) * size, (y2 + 0) * size)
                                                ,sf::Vector2f((tx2 + 1) * size, (ty2 + 0) * size)));
                }
        }
}
Back to C++ gamedev with SFML in May 2023

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Empty texture for vertex array quads
« Reply #20 on: September 13, 2012, 02:51:17 am »
Oh well, somehow I missed this part of the documentation... Thank you.

EDIT:
Works when the array is still defined as:
sf::VertexArray layer2(sf::Quads, layer_width * layer_height * 4);
but without any change in memory/CPU usage.

When the array is defined as:
sf::VertexArray layer2(sf::Quads);
it crashes with heap corruption.
« Last Edit: September 13, 2012, 03:04:11 am by N_K »

FRex

  • Hero Member
  • *****
  • Posts: 1846
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Empty texture for vertex array quads
« Reply #21 on: September 13, 2012, 03:11:31 am »
Step thru with debugger to see when it crashes? How do you know it's heap corruption.
Append ads to end so the first definition is leaving a lot of garbage vertices in. Vertex array is glorified vector so all that is happening are stl vector push_backs, almost no way they'd corrupt the heap.
Back to C++ gamedev with SFML in May 2023

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Empty texture for vertex array quads
« Reply #22 on: September 13, 2012, 03:56:05 am »
EDIT: It works, I was just completely stupid, and not realizing that the texture coordinates has been already set up in:

layer2.append(sf::Vertex(sf::Vector2f((x2 + 0) * size, (y2 + 0) * size), sf::Vector2f((tx2 + 0) * size, (ty2 + 0) * size)));

So basically the first sf::Vector2f defines the position of the vertex, and the second defines the texture coordinates.

And what it does now is to create an empty vertex array at startup, and append a quad if the tile ID is larger than zero, if I understand correctly. And what got me confused was that I used to believe that all the vertices in the array needs to be connected, not realizing that just because they are in the same array, they can be positioned individually (similar to the way like the polygons are stored in a 3D mesh file, they can be scattered around without being connected, while still belonging to the same model (array)).
« Last Edit: September 13, 2012, 04:24:52 am by N_K »