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

Author Topic: sf::VertexArray issue  (Read 2169 times)

0 Members and 1 Guest are viewing this topic.

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
sf::VertexArray issue
« 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::VertexArray issue
« Reply #1 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)

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::VertexArray issue
« Reply #2 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...


robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::VertexArray issue
« Reply #3 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);

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::VertexArray issue
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::VertexArray issue
« Reply #5 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!