SFML community forums

Help => Graphics => Topic started by: StDH on April 18, 2014, 06:40:37 pm

Title: sf::Text crash the program
Post by: StDH 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.
Title: Re: sf::Text crash the program
Post by: ChronicRat on April 18, 2014, 06:50:14 pm
I think here it is:
*gameFilesMgr::instance().
Looks dangerous.
ps And use debug build.
Title: Re: sf::Text crash the program
Post by: StDH 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);
Title: Re: sf::Text crash the program
Post by: ChronicRat 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 =)
Title: Re: sf::Text crash the program
Post by: ChronicRat 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.
Title: Re: sf::Text crash the program
Post by: StDH 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)
Title: Re: sf::Text crash the program
Post by: StDH on April 18, 2014, 07:14:20 pm
but thanks for your help. :)
Title: AW: sf::Text crash the program
Post by: eXpl0it3r 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. ;)