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

Author Topic: sf::Text crash the program  (Read 3575 times)

0 Members and 4 Guests are viewing this topic.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
sf::Text crash the program
« on: April 18, 2014, 06:40:37 pm »
Visual Studio 2013
sfml 2.1 [myself compiled]

sf::Text fps;

void gameWindow::setTextProperties(sf::Text &text, std::string string, unsigned char_size, sf::Color color, sf::Font font, sf::Vector2f position, float angle, sf::Uint32 style)
{
        text.setString(string);

        text.setCharacterSize(char_size);
        text.setColor(color);
        text.setFont(font);
        text.setPosition(position);
        text.setRotation(angle);
        text.setStyle(style);

        text.setOrigin(gameMath::setOrigin(text.getGlobalBounds()));
}

setTextProperties(fps, "", 12, sf::Color::Magenta, *gameFilesMgr::instance().getFont("font-orbitron-medium"), sf::Vector2f(2,2), 0, sf::Text::Regular);

fps.setString("string");

window.draw(fps);

 

it crashes when i call setTextProperties.
i had this problem even in sfml 2.0 .

font-orbitron-medium -> not the problem because same line is used in my npc's names

I have no idea where is the problem.
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: sf::Text crash the program
« Reply #1 on: April 18, 2014, 06:50:14 pm »
I think here it is:
*gameFilesMgr::instance().
Looks dangerous.
ps And use debug build.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: sf::Text crash the program
« Reply #2 on: April 18, 2014, 06:52:17 pm »
I think here it is:
*gameFilesMgr::instance().
Looks dangerous.
ps And use debug build.

inline static gameFilesMgr &instance()
{
        static gameFilesMgr i;
        return i;
}

sf::Font *getFont(std::string);
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: sf::Text crash the program
« Reply #3 on: April 18, 2014, 06:53:55 pm »
And getFont returns non nullptr?
Try this:

sf::Font* fnt = gameFilesMgr::instance().getFont("font-orbitron-medium");
if (fnt)
{
    setTextProperties(fps, "", 12, sf::Color::Magenta, *fnt, sf::Vector2f(2,2), 0, sf::Text::Regular);
}
ps And, and again, read this book http://www.gotw.ca/publications/c++cs.htm =)
« Last Edit: April 18, 2014, 06:57:36 pm by ChronicRat »

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: sf::Text crash the program
« Reply #4 on: April 18, 2014, 07:01:16 pm »
"Divide and rule", don't try to do everything in one line. It will be much more easily to find bugs in code. And you can skip checking of pointer to nullptr if you absolutly sure only.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: sf::Text crash the program
« Reply #5 on: April 18, 2014, 07:06:09 pm »
problem solved:

void gameWindow::setTextProperties(sf::Text &text, std::string string, unsigned char_size, sf::Color color, sf::Font font, sf::Vector2f position, float angle, sf::Uint32 style)
to:
void gameWindow::setTextProperties(sf::Text &text, std::string string, unsigned char_size, sf::Color color, sf::Font &font, sf::Vector2f position, float angle, sf::Uint32 style)
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: sf::Text crash the program
« Reply #6 on: April 18, 2014, 07:14:20 pm »
but thanks for your help. :)
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: sf::Text crash the program
« Reply #7 on: April 19, 2014, 12:17:22 am »
Statics/Globals/Singletons are not recommended and create crashes with SFML in certain cases, you're better off with a nicer code design. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything