0

Rather than loading everything upon startup, is it possible to load only the resources you need upon startup and load each individual resource only when you need to use it?

How can this be done?

I have tried tinkering around with C++/SFML but I get errors when I try this. I really want to not have to load everything at once on startup because it makes my program display a white screen for 30 seconds on startup when I would prefer that it be much faster.

For clarification, i'm referring to sf::Texture , sf::Sprite, sf::Music, etc.

UPDATE: Here's some code

#include "SFML/Audio.hpp"
#include "SFML/Graphics.hpp"
#include <iostream>


void loadResources();

int main()
{


//create the main window
sf::RenderWindow window(sf::VideoMode(720, 500), "Sky Ocean");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);

//View
sf::View view1(sf::FloatRect(200, 200, 300, 200));
view1.setSize(sf::Vector2f(window.getSize().x, window.getSize().y));
view1.setCenter(sf::Vector2f(view1.getSize().x / 2, view1.getSize().y / 2));
window.setView(view1);

//load resources
loadResources();

//npc object
class npc npc1;
npc1.sprite.setTexture(textureNPC);

class npc npc2;
npc2.sprite.setTexture(textureNPC2);

class npc npc3;
npc3.sprite.setTexture(textureNPC3);

class npc npc4;
npc4.sprite.setTexture(textureNPC4);

As you can see, I call the function before setting textures, but I still get an error.

void loadResources()
{
//load npc texture
sf::Texture textureNPC;
if (!textureNPC.loadFromFile("images/npc.png"))
    return EXIT_FAILURE;

sf::Texture textureNPC2;
if (!textureNPC2.loadFromFile("images/npc2.png"))
    return EXIT_FAILURE;

sf::Texture textureNPC3;
if (!textureNPC3.loadFromFile("images/npc3.png"))
    return EXIT_FAILURE;

sf::Texture textureNPC4;
if (!textureNPC4.loadFromFile("images/npc4.png"))
    return EXIT_FAILURE;
}

I get the "undeclared identifier" error. How can I make this work?

1 Answers1

0

Your problem is that you declare textureNPC in the loading function. Once that's over, they go out of scope and don't exist anymore. You should learn the basics before learning a library like SFML. To fix the issue, restructure things so that they're in the same scope. You can do it via class/struct, or by passing references/pointers to the function.

Notes:

You can display something fun instead of a white screen - that's essentialy your loading screen.

Do note that Direct3D (DirectX renderer) might need the context to be re-created when you recompile shaders. I think SFML should handle that, but it might have side-effects.

struct Resources
{
    sf::Texture textureNPC;
    sf::Texture textureNPC2;
    sf::Texture textureNPC3;
    sf::Texture textureNPC4;

    Resources()
    {
        //load npc texture
        if (!textureNPC.loadFromFile("images/npc.png"))
        return EXIT_FAILURE;

        if (!textureNPC2.loadFromFile("images/npc2.png"))
            return EXIT_FAILURE;

        if (!textureNPC3.loadFromFile("images/npc3.png"))
            return EXIT_FAILURE;

        if (!textureNPC4.loadFromFile("images/npc4.png"))
            return EXIT_FAILURE;
    }
}
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48