SFML community forums

Help => Graphics => Topic started by: Pinco on January 26, 2018, 08:48:03 pm

Title: [SOLVED] Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 26, 2018, 08:48:03 pm
So I have this 'Label' class based on sf::Text and I'm trying to make it so that Label.getPosition() returns the top-left coordinates of the text (as you would do by adding the .top and .left offset). However my code sets the new origin to (0, -25) thus making it invisible, how can I fix this?

Label constructor:
Label::Label(string text) {
        // Object constructor, black text 25 pixel high.

        m_label.setFont(default_font);
        m_label.setCharacterSize(25);
        m_label.setFillColor(Color::Black);
        m_label.setString(text);
        m_label.setOrigin(m_label.getLocalBounds().width - m_label.getLocalBounds().left, m_label.getLocalBounds().height - m_label.getLocalBounds().top);
       
}

Pseudo code for main loop:
Label text("Hello World!");

int main() {
        // Basic window function and event handling here

        text.setPosition(10, 10);
       
        window.clear();
        window.draw(text);
        window.display();
}

The outcome is just an empty window.

[S O L U T I O N]
Looks like eXpl0it3r was TOTALLY correct, the font must be declared locally. You are an experienced dev!
Thanks for the help.
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: eXpl0it3r on January 26, 2018, 09:03:27 pm
Don't use globally initialized SFML resources.
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 26, 2018, 09:05:19 pm
Don't use globally initialized SFML resources.

You mean I should put the Label text("Hello World!") declaration inside main? That doesn't really solve the issue.
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: eXpl0it3r on January 26, 2018, 09:17:40 pm
By "doesn't really" do you mean, you have made all SFML resources non-global (even your font object)? Or does it mean, you just guess the issue must be somewhere else?
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 26, 2018, 09:21:26 pm
By "doesn't really" do you mean, you have made all SFML resources non-global (even your font object)? Or does it mean, you just guess the issue must be somewhere else?

I guesstimated, I've made every resource local except for the font as I need it app-wide. I'll try it anyways and get back to you.
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 26, 2018, 09:27:43 pm
By "doesn't really" do you mean, you have made all SFML resources non-global (even your font object)? Or does it mean, you just guess the issue must be somewhere else?

I guesstimated, I've made every resource local except for the font as I need it app-wide. I'll try it anyways and get back to you.

So I tried and that's not the issue.
I also found out that the issue lies in how I'm using setOrigin() because very similar classes (where I don't change the origin) don't have this problem, but I haven't found the solution yet.
Title: Re: Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 26, 2018, 09:47:49 pm
Looks like eXpl0it3r was TOTALLY correct, the font must be declared locally. You are an experienced dev!
Thanks for the help.
Title: Re: [SOLVED] Can't set origin of sf::Text inside class constructor
Post by: Hapax on January 27, 2018, 10:25:47 pm
I have to ask: if you want the top-left of the text to be the origin (and therefore the position) of the object, are you sure that you need the width and height of the text?
Title: Re: [SOLVED] Can't set origin of sf::Text inside class constructor
Post by: Pinco on January 28, 2018, 01:01:08 pm
I have to ask: if you want the top-left of the text to be the origin (and therefore the position) of the object, are you sure that you need the width and height of the text?

Yeah that was a mistake on my part, you just need .top and .left.