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

Author Topic: Font Loading. [SOLVED]  (Read 2257 times)

0 Members and 1 Guest are viewing this topic.

o2ciri

  • Newbie
  • *
  • Posts: 4
    • View Profile
Font Loading. [SOLVED]
« on: May 31, 2024, 01:43:25 am »
Hi,

I'm fairly new to SFML, so I might be missing something.
I've gone through a lot of information but nothing has helped.

After I load a font, the program just closes without any message.

I use SFML as a git submodule and build everything with CMake.

CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)

project(Ironforge)

set(CMAKE_CXX_STANDARD 20)

set(BUILD_SHARED_LIBS FALSE)

add_subdirectory(dep/SFML)

add_executable(${PROJECT_NAME}
        src/main.cpp
        src/Application.cpp
        src/State/StateManager.cpp
        src/UI/Button.cpp
        src/UI/MenuTabBar.cpp
        src/WindowManager.cpp
)

target_include_directories(${PROJECT_NAME}
        PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/dep/SFML/include)

set(DEBUG_LIBS
        sfml-graphics-d)

set(RELEASE_LIBS
        sfml-graphics
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_link_libraries(${PROJECT_NAME} PRIVATE ${DEBUG_LIBS})
elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
    target_link_libraries(${PROJECT_NAME} PRIVATE ${RELEASE_LIBS})
endif ()
 

After building the project and checking all dependencies and libraries through VS project settings ALL is correct. see screenshots.

I load the font in a class, which is in separate files

.h

#ifndef APPLICATION_H
#define APPLICATION_H

#include "macro.h"
#include "WindowManager.h"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/VideoMode.hpp>

#include "SFML/Graphics/Font.hpp"

class Application
{
public:
    Application() = delete;
    explicit Application(sf::VideoMode videoMode);
    ~Application();

    DISABLE_COPY_ASSIGN(Application);

    void run();

private:
    void handleEvent();
    void update(sf::Time dt);
    void render();

    sf::Font* font;

    sf::RenderWindow window;
};

#endif // APPLICATION_H
 

.cpp relative only
Application::Application(sf::VideoMode videoMode)
    : window(videoMode, APP_TITLE, sf::Style::None, sf::State::Fullscreen)
{
    WindowManager::getInstance().setWindow(&window);
    *font = sf::Font::loadFromFile("resources/fonts/Cthulhumbus.ttf").value();
}
 

The paths to the fonts are also specified correctly, because if you specify incorrectly in the console displays an error about it.

It's also worth pointing out that apart from fonts, everything loads fine, textures, etc.

Hope someone can help ;)
« Last Edit: May 31, 2024, 05:45:35 pm by o2ciri »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Font Loading.
« Reply #1 on: May 31, 2024, 08:33:06 am »
Have you tried a different font?
Does it crash with an exception, because of the .value() call on the optional?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

o2ciri

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Font Loading.
« Reply #2 on: May 31, 2024, 12:25:00 pm »
Yes, i tried to use many other fonts.

After I changed code this way:
Application::Application(sf::VideoMode videoMode)
    : window(videoMode, APP_TITLE, sf::Style::None, sf::State::Fullscreen)
{
    WindowManager::getInstance().setWindow(&window);
    try
    {
    *font = sf::Font::loadFromFile("resources/fonts/Cthulhumbus.ttf").value();
    }
    catch(std::exception& e)
    {
        std::cerr << e.what();
    }

}
 

I still don't get any message in my console.

Maybe not relevant to the problem, but I'm also getting the error:

.\Ironforge.exe
The requested video mode is not available, switching to a valid mode
  VideoMode: { size: { 2560, 1440 }, bitsPerPixel: 32 }
Failed to change display mode for fullscreen
 

But the font problem, I never got it. I also tried to catch std::runtime_error, but it did nothing.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Font Loading.
« Reply #3 on: May 31, 2024, 12:33:59 pm »
How are you using the font?

.\Ironforge.exe
The requested video mode is not available, switching to a valid mode
  VideoMode: { size: { 2560, 1440 }, bitsPerPixel: 32 }
Failed to change display mode for fullscreen
 

But the font problem, I never got it. I also tried to catch std::runtime_error, but it did nothing.
Is 2560, 1440 a valid resolution for your monitor?

Maybe you're mixing debug and release libraries, which could cause odd side effects due to undefined behavior.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

o2ciri

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Font Loading.
« Reply #4 on: May 31, 2024, 12:52:32 pm »
The font is not used in any way, it just loads and that's it.

Since I said above, the problem with mixing libraries goes away, the screenshots show how the built project looks like in VS2022:

Connected libraries for the build type:

Debug
dep\SFML\lib\Debug\sfml-graphics-s-d.lib;dep\SFML\lib\Debug\sfml-window-s-d.lib;dep\SFML\lib\Debug\sfml-system-s-d.lib;opengl32.lib;winmm.lib;gdi32. lib;D:\Projects\Ironforge\dep\SFML\extlibs\libs-msvc-universal\x64\freetype.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib
 

Release
dep\SFML\lib\Release\SFML\sfml-graphics-s.lib;dep\SFML\lib\Release\sfml-window-s.lib;dep\SFML\lib\Release\sfml-system-s.lib;opengl32.lib;winmm.lib;gdi32. lib;D:\Projects\Ironforge\dep\SFML\extlibs\libs-msvc-universal\x64\freetype.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib
 


Is 2560, 1440 a valid resolution for your monitor? Yeah, I take it this way:

int main()
{
    sf::VideoMode videoMode;
    videoMode.size.x = videoMode.getDesktopMode().size.x;
    videoMode.size.y = videoMode.getDesktopMode().size.y;

    Application application(videoMode);
    application.run();

    return 0;
}
 

Also u can see screenshot :)
« Last Edit: May 31, 2024, 12:57:07 pm by o2ciri »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Font Loading.
« Reply #5 on: May 31, 2024, 03:10:38 pm »
I can't reproduce it.

I've used VS2022 as well and the following CMake script and C++ code.

cmake_minimum_required(VERSION 3.22)
project(SFMLTest CXX)

include(FetchContent)
FetchContent_Declare(SFML
    GIT_REPOSITORY https://github.com/SFML/SFML.git
    GIT_TAG master)
FetchContent_MakeAvailable(SFML)

add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest PRIVATE SFML::Graphics)

#include <SFML/Graphics.hpp>

#include <iostream>

int main()
{
    auto window = sf::RenderWindow{sf::VideoMode{sf::Vector2u{500, 500}}, "SFML Test"};
    window.setVerticalSyncEnabled(true);

    auto font = sf::Font::loadFromFile(R"(C:\Windows\Fonts\Arial.ttf)");
    if (!font.has_value())
    {
        std::cerr << "Can&#39;t load font" << std::endl;
        return -1;
    }

    auto text = sf::Text{ *font, "Hello World" };

    while (window.isOpen())
    {
        while (auto event = window.pollEvent())
        {
            if (event.is<sf::Event::Closed>())
            {
                window.close();
            }
        }

        window.clear();
        window.draw(text);
        window.display();
    }
}

If you run it through a debugger do you see where it crashes?
If you check the Windows Event Log under Application, do you see more information there?
Is you application logic just so that the application closes? ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Font Loading.
« Reply #6 on: May 31, 2024, 05:26:13 pm »
I didn't pay much attention to the specific code, but the culprit might be this: *font

You're dereferencing your pointer, but since you've never initialized it, you'll run into undefined behavior and it can crash.

Check your warning levels of the compiler, there might be a warning shown for this.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

o2ciri

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Font Loading.
« Reply #7 on: May 31, 2024, 05:45:16 pm »
Ok, I tried pasting your code into my main and it worked. Also changing font* in the constructor to sf::Font font also works. Thanks for your help.

I'm also wondering how to initialize the font in the .h file to use it in the .cpp, since it should be initialized in the initializers of the constructor, and writing the loading from the file in the initializers, I'm not sure that's a good idea.