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

Author Topic: Loading all png files from a folder?  (Read 3326 times)

0 Members and 1 Guest are viewing this topic.

mercender

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Loading all png files from a folder?
« on: February 01, 2017, 03:51:20 pm »
Hi,
My application (c++/sfml) needs a lot of separate images. I have a folder of 17 png images, with potentially more to be added. I want my program to load all the images from a folder in my app directory into a vector in chronological order (1,2 etc). I have only ever loaded the images into a texture data type individually, which means going into the code every time one is added. (I am using this program to visualize a bunch of images). I plan on adding up to 40 or more, so this is not very intuitive. The code already handles each texture.

What's the best way to implement this? I am having a hard time finding solutions on the internet.

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Loading all png files from a folder?
« Reply #1 on: February 01, 2017, 05:08:14 pm »
Why not use a textures.txt file or something?
Just edit the file whenever you want to change textures.  It'll remove a platform-dependent filesystem API usage.
Kinda like this?

texture1.png
someothertexture.png
somethingelse.png
...

Then in your code:

std::string filename{ "" };
std::ifstream file("path_to_textures.txt");

while (file >> filename) {
    my_textures.push_back({});
    if (!my_textures.back().loadFromFile(filename))
        report error
}

mercender

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Loading all png files from a folder?
« Reply #2 on: February 01, 2017, 05:11:21 pm »
Why not use a textures.txt file or something?
Just edit the file whenever you want to change textures.  It'll remove a platform-dependent filesystem API usage.
Kinda like this?

texture1.png
someothertexture.png
somethingelse.png
...

Then in your code:

std::string filename{ "" };
std::ifstream file("path_to_textures.txt");

while (file >> filename) {
    my_textures.push_back({});
    if (!my_textures.back().loadFromFile(filename))
        report error
}

Cool solution, although it still requires some effort from the user. I think it's worth it though. Will try when I get the chance, many thanks!

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Loading all png files from a folder?
« Reply #3 on: February 01, 2017, 05:36:29 pm »
If your compiler supports C++17 filesystem functions are now part of the standard (which you can see here). Slightly older compilers may also provide access via the experimental namespace, and they are all available as part of boost, upon which the std version is based. You can use file_time_type to sort the files chronologically.

 

anything