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

Author Topic: Graphic problem  (Read 1239 times)

0 Members and 1 Guest are viewing this topic.

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Graphic problem
« on: September 26, 2019, 07:32:34 pm »
I recently switched to using a vertex array for displaying the tiles, but sometimes I get lines between them, as you can see in the first attachment. And when I move it goes back to normal, as you can see in the second attachment. Does anyone know why this happens?

Code:
sf::VertexArray va(sf::Quads, m_game.get_loaded_chunks().size() * 2048);
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() + 32 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
    quad[2].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
    quad[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 32 - 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);
    if(t.get_attribute() != 0){
      sf::Vertex* att = &va[i + 4];
      att[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
      att[1].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
      att[2].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
      att[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
      if(t.get_attribute() == 1){
        att[0].texCoords = sf::Vector2f(221, 153);
        att[1].texCoords = sf::Vector2f(221 + 16, 153);
        att[2].texCoords = sf::Vector2f(221 + 16, 153 + 16);
        att[3].texCoords = sf::Vector2f(221, 153 + 16);
      }
      else if(t.get_attribute() == 2){
        att[0].texCoords = sf::Vector2f(459, 187);
        att[1].texCoords = sf::Vector2f(459 + 16, 187);
        att[2].texCoords = sf::Vector2f(459 + 16, 187 + 16);
        att[3].texCoords = sf::Vector2f(459, 187 + 16);
      }
      else if(t.get_attribute() == 3){
        att[0].texCoords = sf::Vector2f(443, 170);
        att[1].texCoords = sf::Vector2f(443 + 16, 170);
        att[2].texCoords = sf::Vector2f(443 + 16, 170 + 16);
        att[3].texCoords = sf::Vector2f(443, 170 + 16);
      }
    }
    i += 8;
  }
}
m_window.draw(va, &sprite_sheet);

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Graphic problem
« Reply #1 on: September 27, 2019, 03:56:09 pm »
One common cause of this is when your tiles get drawn to non-integer positions due to the view moving or whatever else. This topic comes up fairly often, so if you search around the forums you'll probably find several threads of people talking about why this happens and some possible solutions. Here's one I found with a quick search that might be useful to you.