1
Graphics / VertexArray with different textures per vertex
« on: November 27, 2019, 11:21:19 am »
I'm trying to make a terraria clone, and draw the tiles with a VertexArray. The problem is that I don't see a way to set a custom texture for every vertex. Is the only solution I can do to create a container for every tile type and draw a VertexArray for every container?
sf::VertexArray va(sf::Quads, 68 * 18);
int y = 0;
int x = 0;
sf::Texture sprite_sheet;
sprite_sheet = sfml_resources::get().get_spritesheet();
unsigned i = 0;
for(tile& t : m_game.get_tiles()){
y = (17 * (t.get_state() / 16));
x = (18 * t.get_state()) % (16 * 18);
sf::Vertex* quad = &va[i];
const int tx = static_cast<int>(t.get_x());
const int ty = static_cast<int>(t.get_y());
quad[0].position = sf::Vector2f(tx, ty);
quad[1].position = sf::Vector2f(tx + 32, ty);
quad[2].position = sf::Vector2f(tx + 32, ty + 32);
quad[3].position = sf::Vector2f(tx, ty + 32);
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);
int y = 0;
int x = 0;
sf::Texture sprite_sheet;
sprite_sheet = sfml_resources::get().get_spritesheet();
unsigned i = 0;
for(tile& t : m_game.get_tiles()){
y = (17 * (t.get_state() / 16));
x = (18 * t.get_state()) % (16 * 18);
sf::Vertex* quad = &va[i];
const int tx = static_cast<int>(t.get_x());
const int ty = static_cast<int>(t.get_y());
quad[0].position = sf::Vector2f(tx, ty);
quad[1].position = sf::Vector2f(tx + 32, ty);
quad[2].position = sf::Vector2f(tx + 32, ty + 32);
quad[3].position = sf::Vector2f(tx, ty + 32);
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);