Is there a quicker way to load multiple pngs from a file in SFML (C++)?

Viewed 57

I've looked on message forums for a good minute, both SFML and StackOverflow, and perhaps I'm searching for the wrong criteria because I can't find an answer and I'm certain this isn't an original question. (I searched 'Loading Multiple Textures'.)

Is there a faster method of loading textures than simply copying and pasting texture.loadFromFile("C:...") for each and every texture? I have all my pngs I would need in a large folder with multiple subsets folders and loading every texture one by one is taking a painstakingly long time. Is there a function that would allow me to sort through the contents of said file and assign each image to a declared texture?

something like (this is not accurate code):


vector <sf::Texture> saved_text = {};

for (int i = 0; i < folder.size(); i++){

sf::texture new_text.loadFromFile("folder");

saved_text.push_back(new_text);

}

Any advice would be helpful, I'm new to SFML.

1 Answers

As has been said in the comments, you should not load all graphic assets at once, but only those currently needed. That rule could be extended to any other asset types (music, sounds etc.) and there are very few if any reasons to not follow it.

As loading external files is costly operation it is wise to keep those already loaded and that could be needed again into some kind of register (a map for example) and reuse them from there.

Reusing the same texture for multiple objects of the same type is always a good idea - for example if there are 10 objects with the same texture in the scene, load the texture once and use it for all 10 objects.

In case there are many small textures that are tightly related to each other (for example frames of a sprite) it is wise to combine them into a single file which is loaded once and then using an index to display only the part of it which is currently needed.

Related