SFML community forums

Help => Graphics => Topic started by: robkruger on September 17, 2019, 03:32:59 pm

Title: sf::VertexArray issue
Post by: robkruger on September 17, 2019, 03:32:59 pm
I have this to draw a tile map:

sf::VertexArray va(sf::Quads, 10240);
int y = 0;
int x = 0;
sf::Texture sprite_sheet = sfml_resources::get().get_spritesheet();
int i = 0;
for(chunk c : m_game.get_loaded_chunks()){
   for(tile t : c.get_tiles()){
     y = (16 * (t.get_type() / 57)) / 16;
     x = (17 * t.get_type()) % (57 * 17);
     sf::Vertex* quad = &va[i];
     quad[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
     quad[1].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
     quad[2].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
     quad[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
     quad[0].texCoords = sf::Vector2f(x, y);
     quad[1].texCoords = sf::Vector2f(x + 16, y);
     quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
     quad[3].texCoords = sf::Vector2f(x, y + 16);
     i++;
   }
}
m_window.draw(va, &sprite_sheet);

But when I run it the position is weird, see screenshot. Why is this happening?
Title: Re: sf::VertexArray issue
Post by: G. on September 17, 2019, 03:54:40 pm
If a quad is 4 vertices, shouldn't you do i += 4; instead of i++;?

What's the point of multiplying something by 16 then dividing it by 16? (none)
Title: Re: sf::VertexArray issue
Post by: robkruger on September 17, 2019, 06:02:25 pm
If a quad is 4 vertices, shouldn't you do i += 4; instead of i++;?
Ah, thank you.

What's the point of multiplying something by 16 then dividing it by 16? (none)
I don't know why I did that, thanks for pointing out...

Title: Re: sf::VertexArray issue
Post by: robkruger on September 17, 2019, 06:49:37 pm
Now I have this problem. Can anybody spot the issue?

Updated code:

sf::VertexArray va(sf::Quads, m_game.get_loaded_chunks().size() * 1024);
    int y = 0;
    int x = 0;
    sf::Texture sprite_sheet;
    sprite_sheet = sfml_resources::get().get_spritesheet();
    int i = 0;
    for(chunk c : m_game.get_loaded_chunks()){
      for(tile t : c.get_tiles()){
        y = (t.get_type() / 57) * 16;
        x = (17 * t.get_type()) % (57 * 17);
        sf::Vertex* quad = &va[i];
        quad[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
        quad[1].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
        quad[2].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
        quad[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
        quad[0].texCoords = sf::Vector2f(x, y);
        quad[1].texCoords = sf::Vector2f(x + 16, y);
        quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
        quad[3].texCoords = sf::Vector2f(x, y + 16);
        i += 4;
      }
    }
    m_window.draw(va, &sprite_sheet);
Title: Re: sf::VertexArray issue
Post by: eXpl0it3r on September 18, 2019, 12:03:34 am
I suggest to sit down and draw the points out on paper and maybe even the formulas to get there, that way you can think of the math independent of the code and are less tempted to just trial-and-error some solution.

Secondly, I recommend to use your debugger and step through the code step by step, checking the values with the expected values that you calculated on paper.
Title: Re: sf::VertexArray issue
Post by: robkruger on September 18, 2019, 06:27:21 pm
I suggest to sit down and draw the points out on paper and maybe even the formulas to get there, that way you can think of the math independent of the code and are less tempted to just trial-and-error some solution.

Secondly, I recommend to use your debugger and step through the code step by step, checking the values with the expected values that you calculated on paper.

Thanks for the reply, however I fixed it by something unrelated a couple of minutes later and forgot to mention it. Anyway, thanks for the tips if I ever get an issue like this again!