Search code examples
c++functionmethodssfml

How can I use a sf::window in a function/method


I want to make a class that takes a string, and gives out the string in a box in sfml. To make the drawing easier, I made a function that takes a pointer of a renderwindow and draws the box with the text(I didn't implement the Box yet) But when I run it I get an error, that it is not allowed to read this

I already tried to pass the window by reference, but it didn't work, and the result was the same

//Textbox.h

class textbox
{
public:
    sf::Text txtinbox;

    void draw(sf::RenderWindow* on)
    {
        on->draw(this->txtinbox);
    }//Error

    textbox(std::string txinbox)
    {

        sf::Font font;
        font.loadFromFile(fontfile);
        sf::Text text(txinbox , font);
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setFillColor(sf::Color::White);
        txtinbox = text;

    }


    ~textbox()
    {}
private:

};

Solution

  • You have undefined behaviour.

    Go on this site and find this sentence:

    It is important to note that the sf::Text instance doesn't copy the font that it uses, it only keeps a reference to it.

    and this one

    never write a function that uses a local sf::Font instance for creating a text

    Now look at your constructor:

    {
        sf::Font font;                           // LOCAL VARIABLE
        font.loadFromFile(fontfile);
        sf::Text text(txinbox , font);           // GET REFERENCE TO LOCAL
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setFillColor(sf::Color::White);
        txtinbox = text;
                                                 // LOCAL IS DELETED
                                                 // txtinbox HAS DANGLING REFERENCE
    }
    

    so you have to extend lifetime of font, for example by making it be data member of your class.

    class textbox
    {
    public:
        sf::Text txtinbox;
        sf::Font font;
    
         textbox(std::string txinbox)
         {
             font.loadFromFile(fontfile);