This used to work, but it showed all 9 health bars in a single 64x64 tile. I fixed the way it calculates how to select the correct one instead of all 9, so that "should" work.
Health tiles, 640x64 image with 64x64 tiles, first 64x64 tile is blank for 0 health
Code checks the health's percentage by increments of 10%, returning 0-10, then selects the correct tile. It should work...
if (int_durability < 10 && int_durability > 0)
{
std::cout << "oh" << std::endl;
health_quad = &health_vertices[(i + j * map_width) * 4];
health_quad[0].position = sf::Vector2f(i * tile_size.x, j * tile_size.y);
health_quad[1].position = sf::Vector2f((i + 1) * tile_size.x, j * tile_size.y);
health_quad[2].position = sf::Vector2f((i + 1) * tile_size.x, (j + 1) * tile_size.y);
health_quad[3].position = sf::Vector2f(i * tile_size.x, (j + 1) * tile_size.y);
health_quad[0].texCoords = sf::Vector2f(int_durability * tile_size.x, 0);
health_quad[1].texCoords = sf::Vector2f((int_durability + 1) * tile_size.x, 0);
health_quad[2].texCoords = sf::Vector2f((int_durability + 1) * tile_size.x, tile_size.y);
health_quad[3].texCoords = sf::Vector2f(int_durability * tile_size.x, tile_size.y);
}
else
{
health_quad[0].position = sf::Vector2f(i * tile_size.x, j * tile_size.y);
health_quad[1].position = sf::Vector2f((i + 1) * tile_size.x, j * tile_size.y);
health_quad[2].position = sf::Vector2f((i + 1) * tile_size.x, (j + 1) * tile_size.y);
health_quad[3].position = sf::Vector2f(i * tile_size.x, (j + 1) * tile_size.y);
health_quad[0].texCoords = sf::Vector2f(0, 0);
health_quad[1].texCoords = sf::Vector2f(64, 0);
health_quad[2].texCoords = sf::Vector2f(64, 64);
health_quad[3].texCoords = sf::Vector2f(0, 64);
}
This is the part of the code that turns the current health sf::RenderTexture that displays over the tilemap into a sprite, which is then called to draw from the main game loop.
///DISPLAY TILES
states.transform *= getTransform();
states.texture = &m_tileset;
r_texture.clear();
r_texture.draw(environment_vertices, states);
r_texture.draw(floor_vertices, states);
r_texture.draw(block_vertices, states);
r_texture.draw(object_vertices, states);
r_texture.display();
loaded_map.setTexture(r_texture.getTexture());
///DISPLAY HEALTH READINGS
states.texture = &h_texture;
r_texture_health.clear();
r_texture_health.draw(health_vertices, states);
r_texture_health.display();
loaded_health_displays.setTexture(r_texture_health.getTexture());
Accessed in the main loop by:
window.draw(tmap.getHealthTexture());
Code to return the sprite:
sf::Sprite getHealthTexture()
{
return loaded_health_displays;
}
health_vertices is declared like every other tile vertex:
object_vertices.setPrimitiveType(sf::Quads);
object_vertices.resize(map_width * map_height * 4);
health_vertices.setPrimitiveType(sf::Quads);
health_vertices.resize(map_width * map_height * 4);
Any ideas? Like I previously mentioned, it correctly gets the texture because it used to display it incorrectly, but when I "fixed" that I encountered this flaw in my design. It also correctly selects the tiles that have less than maximum durability, so that's not the issue. (aka it says "oh" for every damaged tile)
Thanks in advance!