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

Author Topic: [SOLVED] Can't set origin of sf::Text inside class constructor  (Read 3843 times)

0 Members and 3 Guests are viewing this topic.

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED] Can't set origin of sf::Text inside class constructor
« 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.
« Last Edit: January 26, 2018, 09:49:05 pm by Pinco »
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't set origin of sf::Text inside class constructor
« Reply #1 on: January 26, 2018, 09:03:27 pm »
Don't use globally initialized SFML resources.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't set origin of sf::Text inside class constructor
« Reply #2 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.
« Last Edit: January 26, 2018, 09:11:01 pm by Pinco »
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't set origin of sf::Text inside class constructor
« Reply #3 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't set origin of sf::Text inside class constructor
« Reply #4 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.
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't set origin of sf::Text inside class constructor
« Reply #5 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.
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't set origin of sf::Text inside class constructor
« Reply #6 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.
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SOLVED] Can't set origin of sf::Text inside class constructor
« Reply #7 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [SOLVED] Can't set origin of sf::Text inside class constructor
« Reply #8 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.
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm