SFML community forums

Help => General => Topic started by: FrankHamill on September 17, 2022, 03:08:15 am

Title: Function for loading multiple textures?
Post by: FrankHamill on September 17, 2022, 03:08:15 am
I've looked on the message boards for a good minute, 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, is there a function that would allow me to sort through the contents and assign each one to a texture?

something like:

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 and the SFML page on loading textures doesn't seem to address my particular issue.
Title: Re: Function for loading multiple textures?
Post by: fallahn on September 18, 2022, 12:37:37 pm
If you're using C++17 the filesystem functions will let you parse files from a given directory. eg

#include <filesystem>
#include <iostream>

for (const auto& file : std::filesystem::directory_iterator("assets/images"))
{
    std::cout << file.path() << std::endl;
}

 

You can do your texture loading with file.path() if it's a suitable image. It might also be worth checking out a resource manager (https://github.com/SFML/SFML/wiki/Source%3A-Smart-ResourceManager) if you have a lot of files.