Hello everyone!
I'm making an Android app and I have a weird problem that I can't explain. When I draw something to a sf::RenderTexture, retrieve the Texture and then copy it to an image the image is flipped. So, when I do something like this:
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 800), "Test");
window.setVerticalSyncEnabled(true);
sf::RectangleShape rect({ 100.f, 200.f }); rect.setOrigin(rect.getSize()/2.f);
rect.rotate(40.f);
rect.setPosition(rect.getGlobalBounds().width/2.f, rect.getGlobalBounds().height/2.f);
sf::RenderTexture renderTex;
renderTex.create(rect.getGlobalBounds().width, rect.getGlobalBounds().height);
renderTex.draw(rect); renderTex.display();
sf::Image img = renderTex.getTexture().copyToImage(); // Flipped here
sf::Texture imgTex; imgTex.loadFromImage(img);
sf::Sprite spriteTex(renderTex.getTexture());
sf::Sprite spriteImg(imgTex);
spriteImg.setPosition(spriteTex.getGlobalBounds().width, 0.f);
while(window.isOpen())
{
window.clear(); window.draw(spriteTex); window.draw(spriteImg); window.display();
sf::Event e; window.pollEvent(e);
if(e.type == sf::Event::Closed) window.close();
}
}
on my Android device (or emulator) I get a rectangle on the left that is rotated clockwise (as it should be) and next to it the rectangle from the image that is flipped. If I set a Texture to the RectangleShape you can see it is flipped vertically. But on Linux/Windows with the same code I get two identical rectangles.
The same thing also happens if the Texture from the RenderTexture is copied to another Texture. And as the Copy-Constructor for sf::Texture (or operator=) also internally calls sf::Texture::copyToImage that function is probably the problem. There does seem to be some OpenGL ES specific code in that function (
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Texture.cpp#L322), but I don't really understand what happens there. If the copied Texture is copied again (or if a normal Texture is loaded from a file and copied) it won't get flipped, only when the Texture bound to the RenderTexture is copied.
Usually if you forget to call sf::RenderTexture::display the resulting Texture is flipped vertically, as it happens here. This does happen if the referenced Texture is given directly to a sprite. But if it is copied without calling display() it won't get flipped again. Thus, copying the Texture acts like display() was never called.
Did I miss something? Or did I find a new bug? I should have the latest SFML version. I temporarily fixed it by calling sf::Image::flipVertically, but an explanation would be nice.
Edit: Shortened the post.
Edit2: Almost 2 weeks and no response. Did I do something wrong?