SFML community forums

Help => Graphics => Topic started by: obscurelyme on September 07, 2021, 06:06:45 pm

Title: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 07, 2021, 06:06:45 pm
Hello,

I'm looking for some advice on how to use sf::Text object. Specifically what is causing this crash to occur from Freetype library. Right now, any attempts to draw sf::Text results in a crash. I have included a link to my source code below as well as screenshots that showcase my setup after a build/crash. I am running 64-bit Windows with vcpkg as my main source of pulling in dependencies. I'm more than certain I am using debug libraries for a DEBUG build as vs code cmake tools sets by default.

My Source Code
https://github.com/obscurelyme/hello-sfml/blob/ed15963b38b896e414fab1b811ac5c8c7918e92d/main.cpp#L53-L73 (https://github.com/obscurelyme/hello-sfml)

Code Snippet:

// Load fonts
    std::string executableDirectory = std::filesystem::weakly_canonical(std::filesystem::path(argv[0])).parent_path().string();
    std::string relativeFontFile = "/assets/fonts/Roboto/Roboto-Regular.ttf";
    std::string fontFilePath = executableDirectory.append(relativeFontFile);
    sf::Font font;
    if (!font.loadFromFile(fontFilePath))
    {
        spdlog::debug("Could not load Roboto-Regular.ttf from directory {}", fontFilePath);
    }
    else
    {
        spdlog::debug("Successfully loaded {} font-family.", font.getInfo().family);
    }

    sf::Text text{"Hello", font, 48};
    text.setFillColor(sf::Color::Red);
    text.setStyle(sf::Text::Regular);
    text.setPosition(200, 200);
    ...
    while(running) {
       sf::Event event;
       while(window.pollEvent(event)) {
           // handle event[s] ...
       }

       window.clear(sf::Color::Blue);

        window.draw(text); // program crashes when this is called.

        window.display();
    }
 

(https://raw.githubusercontent.com/obscurelyme/hello-sfml/master/doc/debug-libs-screenshot.PNG)

Here is the screenshot of the crash in debugger
(https://raw.githubusercontent.com/obscurelyme/hello-sfml/master/doc/stack-screenshot.PNG)
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 07, 2021, 09:03:24 pm
Update:

I pulled down my source and compiled on OSX Big Sur v11.5.2 and it seems to work as intended. So right now I'm unsure if this is a platform issue or perhaps Mac is compiling with static libs vs on Windows I'm getting dynamic libs.

Here is the screenshot on OSX attached.

Would still like to get to the bottom on why this code doesn't work on x64 Windows 10.
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: eXpl0it3r on September 08, 2021, 09:56:44 am
As vcpkg pulls in the freetype version provided by vcpkg, it might be that there's some incompatibility or otherwise different build. You could try to use the freetype library provided by SFML in the extlibs directory.
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 09, 2021, 11:32:12 pm
Thanks for the advice. I'll give it a go configuring CMake to pull in freetype from SFML. More to come.
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 10, 2021, 01:09:23 am
Yikes, so vcpkg does have some in-development version control over what dependencies are to be installed. The lowest version of freetype that anyone can specify using vcpkg is 2.9.x and SFML uses 2.5.5 So Looks like vcpkg could be a non-starter for SFML type projects.
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: texus on September 10, 2021, 07:56:27 am
Have you tested a FreeType version on Windows other than 2.11?

There is a freetype bug that causes a crash in af_face_globals_get_metrics (where your callstack also points to) that only occurs on Windows (with libraries build with Visual Studio) and only with FreeType 2.11. https://gitlab.freedesktop.org/freetype/freetype/-/issues/1092

Downgrading to any version lower than that fixes that issue. FreeType versions are backwards compatible, I would expect SFML to work fine with FreeType 2.9 or 2.10
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 11, 2021, 04:55:19 pm
I just updated my vcpkg.json to use Freetype 2.10 and it works! Many thanks.
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: theo on September 16, 2021, 10:43:15 am
May I ask how you did this? For me it won't work.
I edited the vcpkg.json in the ports\sfml dir to
(click to show/hide)

I got the value of builtin-baseline by doing
git rev-parse HEAD

This didn't do anything for me: it still grabbed freetype version 2.11.0.

Then I edited vcpkg\versions\baseline.json and changed the baseline of freetype to 2.10.0 and I edited the vcpkg.json of freetype to also take 2.10.0:
"version-semver": "2.10.0",

Now it SAYS that it takes 2.10.0. And vcpkg list says 2.10.0 for freetype. But the dll version is STILL 2.11.0 and drawing an sf::Text STILL crashes in freetype.

Maybe the easier way is to compile sfml with cmake and copy the freetype dll into the vcpkg\bin dir
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: theo on September 16, 2021, 11:07:14 am
stupid mistake: in vcpkg.json:

"overrides":[{"name":"sfml", "version":"2.10.0"}] -> "overrides":[{"name":"freetype", "version":"2.10.0"}]

But it doesn't make a difference, it still builds freetype 2.11.0
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: theo on September 16, 2021, 01:38:22 pm
Got it working, though my method is a bit hacky.


Stackoverflow says that you have to edit header files. This is not necessary anymore. Just choose dll in the solution properties.

Hopefully they fix that bug soon. I do wonder however how they could miss this with any kind of testing on windows (not an entirely insignificant platform).
Title: Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
Post by: obscurelyme on September 19, 2021, 08:15:32 pm
I have a branch that leverages freetype 2.10. You can take a look here. There isn't any modification needed just leverage the extensions to handle the heavy lifting.

It's important to remember that you need to have VCPKG_FEATURE_FLAGS set to "versions" as an environment variable in order for vcpkg versions to work.

https://github.com/obscurelyme/Semi-Pro-Cpp/tree/feature/sfml-freetype

Let me know if you have any questions.