Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: No error, but cannot display sf::Text from class.  (Read 2702 times)

0 Members and 2 Guests are viewing this topic.

Alg450

  • Newbie
  • *
  • Posts: 2
    • View Profile
No error, but cannot display sf::Text from class.
« on: July 09, 2013, 04:54:21 pm »
Hi,

I'm sort of new to C++ and SFML, so if this is something obvious I apologize. I want to save myself some time by defining a button class, which will contain an instance of an sf::RectangleShape (for the background / body of the button) and an instance of sf::Text (for the tooltip / functional discription, etc). Right now I'm just trying to display both the text and the button, there is no logic for checking for clicks, or anything like that. When I compile and run the code shown below, I get no errors. I expect the code to draw a background (which it does) a small red square (defined within the class button, which it does) a line of text in blue (defined within the class button, which it doesn't draw). and a line of text in green (defined within main(), which it does, this was primarily to test if I was just using the sf::Font and sf::Text member functions incorrectly). I want to know why the text defined in the class isn't being drawn.

#include <SFML/Graphics.hpp>
sf::RenderWindow window(sf::VideoMode(800,600), "Class Test",sf::Style::Titlebar|sf::Style::Close);

class button
{
private:
    sf::RectangleShape rect;
    sf::Text text;
public:
    void setup(sf::Font font)
    {
        rect.setFillColor(sf::Color::Red);
        rect.setSize(sf::Vector2f(10,10));
        rect.setPosition(5,5);

        text.setColor(sf::Color::Blue);
        text.setString("Hello World");
        text.setPosition(30,30);

        text.setFont(font);
    }
    void drawit()
    {
        window.draw(rect);
        window.draw(text);
    }
};

int main()
{
    sf::Texture backgroundt;
    if (!backgroundt.loadFromFile("resources/background.png"))
    {
        return EXIT_FAILURE;
    }
    sf::Sprite backgrounds(backgroundt);
    sf::Event event;
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(true);

    sf::Font fonty;
    if(!fonty.loadFromFile("resources/sansation.ttf"))
    {
        return EXIT_FAILURE;
    }

    sf::Text texty;

    texty.setColor(sf::Color::Green);
    texty.setString("Hello World");
    texty.setPosition(50,50);

    texty.setFont(fonty);

    button buddy;
    buddy.setup(fonty);

    while(window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if ((event.type == sf::Event::Closed)||((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Escape)))
            {
                window.close();
            }
        }
        window.clear();
        window.draw(backgrounds);
        window.draw(texty);
        buddy.drawit();
        window.display();
    }
    return EXIT_SUCCESS;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: No error, but cannot display sf::Text from class.
« Reply #1 on: July 09, 2013, 05:02:51 pm »
Quote
I want to know why the text defined in the class isn't being drawn.
Bevcause it uses a local copy of the font, which is destroyed at the end of the setup function. You should rather pass the font by reference.
Laurent Gomila - SFML developer

Alg450

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: No error, but cannot display sf::Text from class.
« Reply #2 on: July 09, 2013, 05:07:37 pm »
Thanks, I modified my code thusly and it worked:
    void setup(sf::Font* font)
    {
        rect.setFillColor(sf::Color::Red);
        rect.setSize(sf::Vector2f(10,10));
        rect.setPosition(5,5);

        text.setColor(sf::Color::Blue);
        text.setString("Hello World");
        text.setPosition(30,30);

        text.setFont(*font);
    }
...
buddy.setup(&fonty);

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: No error, but cannot display sf::Text from class.
« Reply #3 on: July 09, 2013, 05:39:50 pm »
Quick tip: use a reference instead of a pointer.

Why? Because with a pointer you can get nullptr (or 0, or NULL). With a reference you know the variable is bound to an actual sf::Font object. Hence, less bugs.

Beside that, you don't need to use the dereference operator on your pointer.
SFML / OS X developer

 

anything