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

Author Topic: Issues with loading resources  (Read 644 times)

0 Members and 1 Guest are viewing this topic.

Xscreade

  • Newbie
  • *
  • Posts: 2
    • View Profile
Issues with loading resources
« on: November 18, 2022, 12:25:17 pm »
EDIT: ISSUE FIXED

We were instanciating things in static wich were called before the resources could be loaded




Hello, me and a friend are trying to recreate a game we made in C sometime ago. However we are stuck at the resource loading.
We have tried many different things and it always crashes. The issue seems that the resources are lost when the end of the scope is reached but we can't figure out why. We are new to c++ so we are really getting a hard time with that.
We are both compiling on linux (me on Fedora and him on ubuntu).

We have a main Game class in wich everything is stored like the renderer class, the scenes etc and the resource manager is a currently a singleton but we had tried earlier with a static manager with no success.
The thing is that we are getting inconsistent results, depending on wich machine the resources do load fine, and sometimes it crashes with this error:

RPG_MASTERCLASS: src/class/AssetManager.cpp:91: sf::Font& AssetManager::getFont(FontID): Assertion `found != m_fonts.end()' failed.
Abandon (core dumped)

AssetManager.hpp:
#pragma once

#include "libs.hpp"
#include "enum/Assets.hpp"

class AssetManager
{
    private:
        static AssetManager s_instance;
        AssetManager();
        map<TextureID, unique_ptr<Texture>> m_textures;
        map<SoundID, unique_ptr<SoundBuffer>> m_sounds;
        map<FontID, unique_ptr<Font>> m_fonts;

    public:
        static AssetManager& getInstance();
        void loadTexture(TextureID identifier, string filename);
        Texture& getTexture(TextureID identifier);

        void loadSound(SoundID identifier, string filename);
        SoundBuffer& getSound(SoundID identifier);

        void loadFont(FontID identifier, string filename);
        Font& getFont(FontID identifier);

};


AssetManager.cpp:
#include "class/AssetManager.hpp"

// Loader

AssetManager::AssetManager()
{
    //fonts
    loadFont(DEBUG_FONT, DEBUG_FONT_PATH);
    loadFont(INGAME_FONT, GAME_FONT_PATH);

    loadTexture(B_CONTINUE_TEX, UI_PATH + "continue.png");
    loadTexture(B_LOAD_GAME_TEX,UI_PATH + "load_game.png");
    loadTexture(B_NEW_GAME_TEX, UI_PATH + "new_game.png");
    loadTexture(B_SETG_TEX, UI_PATH + "settings.png");
    loadTexture(B_EXT_TEX, UI_PATH + "exit.png");
    loadTexture(B_BACK_TEX, UI_PATH + "back.png");
    loadTexture(B_APPLY_TEX, UI_PATH + "apply.png");
    loadTexture(B_TICKBOX, UI_PATH + "tickbox.png");

    // entities
    loadTexture(ENTITY_TEXTURE, "asset/texture/entity/entity.png");

    // background
    loadTexture(MM_BG1, BCKGRD_PATH + "background_no_char.jpg");
    loadTexture(MM_BG2, BCKGRD_PATH + "char.png");
    loadTexture(MM_BG3, BCKGRD_PATH + "embers.png");
    loadTexture(MM_BG4, BCKGRD_PATH + "smoke.png");
    loadTexture(MM_BG5, BCKGRD_PATH + "title.png");

    // slider
    loadTexture(SLIDER_IN, UI_PATH + "slider_in.png");
    loadTexture(SLIDER_OUT, UI_PATH + "slider_out.png");
    loadTexture(SLIDER_THINGY, UI_PATH + "slider_thingy.png");


    // sound
    loadSound(CLICK_SOUND , AUDIO_PATH  + "ui/click.ogg");
    loadSound(HOVER_SOUND , AUDIO_PATH  + "ui/hover.ogg");
}

AssetManager AssetManager::s_instance;

AssetManager& AssetManager::getInstance() { return s_instance; }

void AssetManager::loadTexture(TextureID identifier, string filename)
{
    unique_ptr<Texture> texture(new Texture());
    texture.get()->loadFromFile(filename);
    texture.get()->setSmooth(true);
    auto insert = m_textures.insert(make_pair(identifier, move(texture)));
    assert(insert.second);
}

void AssetManager::loadSound(SoundID identifier, string filename)
{
    unique_ptr<SoundBuffer> soundBuffer(new SoundBuffer());
    soundBuffer.get()->loadFromFile(filename);
    auto insert = m_sounds.insert(make_pair(identifier, move(soundBuffer)));
    assert(insert.second);
}


void AssetManager::loadFont(FontID identifier, string filename)
{
    unique_ptr<Font> font(new Font());
    font.get()->loadFromFile(filename);
    auto insert = m_fonts.insert(make_pair(identifier, move(font)));
    assert(insert.second);
}

// Getter

Texture& AssetManager::getTexture(TextureID identifier)
{
    auto found = m_textures.find(identifier);
    assert(found != m_textures.end());
    return *found->second.get();
}

SoundBuffer& AssetManager::getSound(SoundID identifier)
{
    auto found = m_sounds.find(identifier);
    assert(found != m_sounds.end());
    return *found->second.get();
}


Font& AssetManager::getFont(FontID identifier)
{
    auto found = m_fonts.find(identifier);
    assert(found != m_fonts.end());
    return *found->second.get();
}
 

main.cpp
#include "libs.hpp"
#include "class/Game.hpp"
#include "class/AssetManager.hpp"

int main()
{
    Music music;
    music.openFromFile(AUDIO_PATH + "music/music.ogg");

    AssetManager& assetManager = AssetManager::getInstance();

    Game& engine = Game::getInstance();

    engine.setMusic(&music);
    engine.Initialize();
    while (engine.isRunning()) {
        engine.update();
        engine.render();
    }
}
 

Usage exemple:
Level::Level()
{
    m_index = 0;

    m_hasFocus = false;

    m_buttons.push_back(Button(Vector2f(250, 100), Vector2f(SCREEN_SIZE.x * 0.1,
    SCREEN_SIZE.y * 0.95), AssetManager::getInstance().getTexture(B_EXT_TEX), &buttonBackMainMenuFunc));

    m_levelTitle.setFont(AssetManager::getInstance().getFont(INGAME_FONT));
    m_levelTitle.setCharacterSize(100);
    m_levelTitle.setString("Level " + to_string(m_index));
    m_levelTitle.setOrigin(getCenter(m_levelTitle));
    m_levelTitle.setPosition(Vector2f(SCREEN_SIZE.x / 2, SCREEN_SIZE.y / 2));

    for (int i = 0; i < 100; i++)
        addEntity(Vector2f(randomNumber(-1920, 1920 * 2), randomNumber(-1080, 1080 * 2)));
}

We have already watched a few tutorials and exemples of how to implement a resource manager but we always end up with the same problems.
Any help would be much appreciated.
« Last Edit: November 18, 2022, 02:07:13 pm by Xscreade »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Issues with loading resources
« Reply #1 on: November 18, 2022, 02:10:45 pm »
EDIT: ISSUE FIXED

We were instanciating things in static wich were called before the resources could be loaded
Yeah, that can/will cause crashes. SFML resources should never be instantiated in a global (e.g. static) scope, as the initialization and destruction order at a global scope are undefined and SFML handles certain scenarios globally as a sort of "catch-all" cases.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xscreade

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Issues with loading resources
« Reply #2 on: November 18, 2022, 02:21:43 pm »
Thx, thats good to know