SFML community forums

Help => Graphics => Topic started by: achpile on October 30, 2017, 02:21:56 pm

Title: sf::RenderTexture without 'display' call.
Post by: achpile on October 30, 2017, 02:21:56 pm
Don't know if it's a bug, but if I don't call 'display' function - render texture draws upside down.

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
        sf::Event event;
        sf::Font font;
        sf::Text text;
        sf::RectangleShape shape;
        sf::Sprite spr;
        sf::RenderTexture tex;

        font.loadFromFile("font.ttf");

        text.setFont(font);
        text.setString("HELLO");
        text.setCharacterSize(200);
        text.setFillColor(sf::Color::Black);

        shape.setSize(sf::Vector2f(100,100));
        shape.setPosition(100, 300);
        shape.setFillColor(sf::Color::Black);

        tex.create(800,600);
        tex.setActive(true);

        tex.clear(sf::Color::White);
        tex.draw(text);
        tex.draw(shape);
        //tex.display();

        spr.setTexture(tex.getTexture());

        while (window.isOpen()) {
                while (window.pollEvent(event)) if (event.type == sf::Event::Closed) window.close();

                window.clear(sf::Color::White);
                window.draw(spr);
                window.display();
        }

        return 0;
}
 

1.png - with 'display' call
2.png - without
Title: Re: sf::RenderTexture without 'display' call.
Post by: eXpl0it3r on October 30, 2017, 02:28:29 pm
You have to call display, it's not optional, so anything that you get when not calling display is more or less "undefined behavior".
Title: Re: sf::RenderTexture without 'display' call.
Post by: achpile on October 30, 2017, 02:30:22 pm
You have to call display, it's not optional, so anything that you get when not calling display is more or less "undefined behavior".

oh, okay :) thanks for explanation  ;) i just spent 15 minutes to figure out, why my text was upside down  :D
Title: Re: sf::RenderTexture without 'display' call.
Post by: eXpl0it3r on October 30, 2017, 02:47:16 pm
Yes, it's a common "problem" when not calling display() on a render texture.