SFML community forums

Help => Graphics => Topic started by: ThatOneGuyThatDoesStuff on August 23, 2020, 10:28:10 pm

Title: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on August 23, 2020, 10:28:10 pm
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <ioStream>

int main()
{
    sf::Text testText;
    sf::Font font;
    font.loadFromFile("cour.ttf");
    testText.setFont(font);
    testText.setString("Hello World");
    testText.setPosition(200, 200);
    testText.setCharacterSize(25);
    // Create the main window
    sf::RenderWindow renderWindow(sf::VideoMode(WIDTH, HEIGHT), "SFML window");

        // Start the game loop
    while (renderWindow.isOpen())
    {
        // Process events
        sf::Event event;
        while (renderWindow.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                renderWindow.close();
        }

        // Clear screen
        renderWindow.clear();

        // Draw sprites
        renderWindow.draw(testText);

        // Update the window
        renderWindow.display();
    }

    return EXIT_SUCCESS;
}
 

No matter what I do, I cannot get an sf::Text object to draw without the app instantly crashing. Been plagued by this problem for months. I've asked reddit, stack overflow and even here. I'm hoping for better results, because I have no idea what else to do to make it work. I have reinstalled both code::blocks and sfml. I installed sfml and Code::Blocks on a different pc, same problem. I have tried different fonts, same problem. All other SFML features function exactly as they should.

I'm using Code::Blocks v17.12 and SFML v2.5.1

If I comment out
font.setFont
, it will run (though not draw obviously). If I comment out
renderWindow.draw(testText)
it will run without a crash.

If I run debugger I get the info that the problem file is C:\Windows\SysWOW64\ig4icd32.dll. I've tried looking that up, but get nothing related to my problem.

Any help is appreciated. Also, if anyone knows a work around, I would appreciate it.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: Hapax on August 24, 2020, 11:48:07 pm
If the font fails to load then drawing the text using an invalid font could cause the crash.

You should be testing the return value of "loadFromFile". If it fails, the font should not be used.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on August 25, 2020, 11:17:13 pm
It doesn't fail. I checked that before, I still get the same issue. The font loads fine (at least according, .loadFromFile will execute with a check in place.

To be sure I changed it to:
 
    if (!font.loadFromFile("cour.ttf")){
        std::cout << "Error: font did not load";
    }

No error is thrown.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: FloweyTF on August 26, 2020, 10:43:27 pm
Do you know what causes the error? (Such as invalid pointer, etc)
Title: Re: Just cannot get sf::Text to draw without crash
Post by: fallahn on August 27, 2020, 12:17:40 pm
Try creating the RenderWindow first before you do anything else. It probably initialises OpenGL stuff required by sf::Font.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: Hapax on August 27, 2020, 08:54:47 pm
If creating the render window solves your problem but want to still load resources before the creation of a window, you can create an sf::Context instead that will do all the initialisation that the window does. It's the first time a context is created that does that OpenGL stuff and render window creates a context internally.

As an aside, I was going to suggest just creating the font before the text but that's more for an error when closing the app.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on August 29, 2020, 10:45:11 pm
@fallahn, thanks for the suggestion, but the problem remains alive and well and unchanged if I do that. Don't suppose you have another suggestion :'(

@Hapax, I moved the text creation to after the font, still same problem. Getting really frustrated. It's such a stupid problem, but quite unsolveable.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on August 29, 2020, 11:28:56 pm
Do you know what causes the error? (Such as invalid pointer, etc)

I really don't. No information is given.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on September 01, 2020, 10:41:35 pm
Doesn't look like anyone has any solutions. Does anyone know a workaround? Maybe some third party software to make text draw to an sfml window? Or is there a way to strong arm it through SFML's code?
Title: Re: Just cannot get sf::Text to draw without crash
Post by: fallahn on September 02, 2020, 12:38:37 am
All I can suggest is go back to basics. Check your environment. Make sure you don't have more than one or an old version of sfml installed. Check your headers match the installation of the library and haven't been copied from a different installation. Ensure that debug libraries are linked to the debug builds and release libraries are linked to the release build. If all else fails try a different compiler, like clang or msvc. At least that way you should be able to eliminate some possibilities.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: binary1248 on September 02, 2020, 01:28:19 am
Did you try running any of the SFML examples? If I had to guess they will crash too because this is looking like another case of buggy Intel OpenGL driver.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on September 05, 2020, 10:33:31 pm
All I can suggest is go back to basics. Check your environment. Make sure you don't have more than one or an old version of sfml installed. Check your headers match the installation of the library and haven't been copied from a different installation. Ensure that debug libraries are linked to the debug builds and release libraries are linked to the release build. If all else fails try a different compiler, like clang or msvc. At least that way you should be able to eliminate some possibilities.

I have done all of the above except try a different compiler. I'll try that. But, like I said in the original post, I installed SFML on my laptop AND my desktop. It seems unlikely that I made the same setup mistake twice, spaced quite far apart.

Did you try running any of the SFML examples? If I had to guess they will crash too because this is looking like another case of buggy Intel OpenGL driver.

Yeah, they crash
Title: Re: Just cannot get sf::Text to draw without crash
Post by: eXpl0it3r on September 16, 2020, 07:48:21 am
Go to the Intel's website and find the latest driver for your integrated graphics chip and install that.
Title: Re: Just cannot get sf::Text to draw without crash
Post by: ThatOneGuyThatDoesStuff on September 17, 2020, 09:45:33 pm
Go to the Intel's website and find the latest driver for your integrated graphics chip and install that.

I'll try that, but like I said, I also tried it on my new windows 10 PC and get the exact same issue, so I have my doubts it will work. If it does though, you have my eternal gratitude.