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

Author Topic: Function for loading multiple textures?  (Read 1110 times)

0 Members and 1 Guest are viewing this topic.

FrankHamill

  • Newbie
  • *
  • Posts: 3
    • View Profile
Function for loading multiple textures?
« 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.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Function for loading multiple textures?
« Reply #1 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 if you have a lot of files.

 

anything