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?