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

Author Topic: I can't load my game resources into SFML with CMake  (Read 2127 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
I can't load my game resources into SFML with CMake
« on: December 24, 2021, 07:01:34 pm »
I am programming a little game in C ++ using Visual Studio Code with CMake as the Build System. So far there hadn't been any problem with accessing resources, but since I decided to tidy up my project by organizing it in directories, my GetTexture, GetSoundBuffer and GetFont functions are unable to load images from the Resources folder.

Obviously I know that when saving the files in different directories, I had to update the paths. So what at first was "Resources / image.png" became "../../../Resources/image.png", (which did work for all #include directives) but when I run the game I only see the black screen and console showing me messages like Failed to load image "../../../Resources/image.png". Reason: Unable to open file.

I've tried over and over again to rearrange the project but every time I compile this happens. I don't know much about CMake and I don't know if the problem is with my CMakeLists.txt file or with my previously mentioned functions, which I doubt as they worked perfectly before.


sf::Texture& Game::GetTexture(std::string _fileName)
    auto iter = textures.find(_fileName);
    if (iter != textures.end())
        return *iter->second;
    TexturePtr texture = std::make_shared<sf::Texture>();
    textures[_fileName] = texture;
    return *texture;

sf::SoundBuffer& Game::GetSoundBuffer(std::string _fileName)
     auto iter = sounds.find(_fileName);
     if (iter != sounds.end())
         return *iter->second;
     SoundBufferPtr sound = std::make_shared<sf::SoundBuffer>();
     sound->loadFromFile(_fileName);      sounds[_fileName] = sound;
    return *sound;


sf::Font& Game::GetFont(std::string _fileName)
    auto iter = fonts.find(_fileName);
    if (iter != fonts.end())
        return *iter->second;

    FontPtr font = std::make_shared<sf::Font>();
    fonts[_fileName] = font;
    return *font;


cmake_minimum_required(VERSION 3.18)
set(PROJECT_NAME "MyGame")

set(SFML_DIR "${CMAKE_CURRENT_LIST_DIR}/libs/SFML-2.5.1/lib/cmake/SFML")

file(GLOB ALL_REQUIRED_DLL "libs/required_dlls/*.dll")



add_executable(${PROJECT_NAME} ${SOURCE_FILES})

find_package(SFML 2.5.1 COMPONENTS system window graphics network audio REQUIRED)
target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics sfml-window sfml-system)

The organization of the project is as follows:

    (Cmake build stuff) 

    (All engine codes, no problem here)

              (All .h files)
              (All .cpp files, here is where i call GetTexture, GetSoundBuffer 
              and GetFont)

      (All the images, sounds and fonts) 


To all this, it is also worth mentioning that I am using Linux.


  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: I can't load my game resources into SFML with CMake
« Reply #1 on: January 01, 2022, 09:23:37 pm »
If I understand you correctly, SFML is unable to access the resources in your program. I have a workaround in mind. Although I am not sure if it would work or not. Just try it and let me know.

Suppose you have all your resources in a folder called 'media' which is situated in cmake source path. Then simply add this line in your CMakeLists.txt.
# add this line

Now you should be able to access the resources from anywhere of your programs if you have done the other cmake parts correctly. For example you should be able to load your resources like this:

Tell me if it worked.
(But how come you are using linux? I see you linking dlls)
« Last Edit: January 01, 2022, 09:26:56 pm by metal_in_my_veins »


  • SFML Team
  • Hero Member
  • *****
  • Posts: 10226
    • View Profile
    • development blog
    • Email
Re: I can't load my game resources into SFML with CMake
« Reply #2 on: January 03, 2022, 01:13:09 pm »
Resource loading is completely independent of your build system. The only thing that does matter with relative paths, is what the working directory is set to when executing the application, that will ultimately be the "root".

The simplest thing is to use the install() instructions in CMake and put the resource directory and executable next to each other, than always build the install target. That way both executable and resources are copied to the correct location, now all you need to make sure is that when you run the application, that the working directory is set to the same location as the executable that is being executed.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/