I wrote a function that allows you to draw a sf::Texture without using a sf::Sprite, and also allows you to flip/mirror the texture. I needed this for my own project, but hopefully others find it useful as well.
Here's the code:
void SfmlDrawTexture(sf::RenderTarget &destination, const sf::Vector2f &location, const sf::Texture &texture,
sf::IntRect subRect, const sf::Color &coloration, float rotation,
bool flipHorizontally, bool flipVertically, sf::BlendMode blendMode, const sf::Shader *shader)
{
//If no rect is specified, use the entire texture.
if(subRect.width == 0 || subRect.height == 0)
{
subRect.top = 0;
subRect.left = 0;
subRect.width = texture.getSize().x;
subRect.height = texture.getSize().y;
}
//Set the position in space.
sf::Transform translation;
translation.translate(location);
//Set the rotation (rotated around the center, since this sf::Transform wasn't moved).
sf::Transform rotationTransform;
rotationTransform.rotate(rotation);
//Setup the render state.
sf::RenderStates states(blendMode, (translation * rotationTransform), &texture, shader);
//Setup the vertices and their attributes.
sf::Vertex vertices[4];
//The transparency:
vertices[0].color = coloration;
vertices[1].color = coloration;
vertices[2].color = coloration;
vertices[3].color = coloration;
//The pre-transform position and size:
float widthBeforeTransform = static_cast<float>(subRect.width);
float heightBeforeTransform = static_cast<float>(subRect.height);
vertices[0].position = sf::Vector2f(0, 0);
vertices[1].position = sf::Vector2f(0, heightBeforeTransform);
vertices[2].position = sf::Vector2f(widthBeforeTransform, heightBeforeTransform);
vertices[3].position = sf::Vector2f(widthBeforeTransform, 0);
//Calculate the texture coordinates:
float left = static_cast<float>(subRect.left);
float right = left + subRect.width;
float top = static_cast<float>(subRect.top);
float bottom = top + subRect.height;
//If we're mirroring, swap the texture coordinates vertically and/or horizontally.
if(flipVertically) std::swap(left, right);
if(flipHorizontally) std::swap(top, bottom);
//Set the texture coordinates:
vertices[0].texCoords = sf::Vector2f(left, top);
vertices[1].texCoords = sf::Vector2f(left, bottom);
vertices[2].texCoords = sf::Vector2f(right, bottom);
vertices[3].texCoords = sf::Vector2f(right, top);
//Use the sf::RenderTarget to draw the vertices using the sf::RenderStates we set up.
destination.draw(vertices, 4, sf::Quads, states);
}
To handle mirroring, I swapped the texture coordinates... currently this only works because SFML doesn't disable back-face culling (at least, not in SFML 2.0), but whatever. =)
It'd be nice if this (flipping/mirroring) was available as a feature for sf::Sprites.