3

I'm trying to convert a bunch of images to textures using SDL. So far, I know its possible to do everything manually:

//Load front alpha texture
    if (!gModulatedTexture.loadFromFile("14_animated_sprites_and_vsync/text2.png"))
    {
        printf("Failed to load front texture!\n");
        success = false;
    }
    else
.....

However, I have quite a few images I want to load so what I'm looking for is a way to automate the process. I want to put all my images into a single folder, and then do something like this:

i=0
while (there are still images to load) {
     textureBank[i] = current image
     i++
}

I wast thinking there might be some easy way to just read in the file path of all the files in a directory, but I haven't been able to find a way to do that.

Any suggestions?

Adam
  • 8,752
  • 12
  • 54
  • 96
  • 1
    Have a look here: http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c –  Dec 31 '13 at 17:14
  • Note that SDL 2 still does not come with any way to do this natively, as others have suggested it seems like you will have to find your own cross platform option. The topic has [come up in discussion](https://discourse.libsdl.org/t/filesystem-routines/9660) several times but there isn't any functionality like this in SDL right now. – jrh Nov 27 '20 at 15:08

2 Answers2

1

Since you are using SDL, I’ll assume you want to be cross-platform. The boost::filesystem library can do this.

Take a look at their directory iteration example.

Although it’s part of a 3rd-party library, boost::filesystem is proposed for inclusion in a future C++ standard, TR2, so it’s worth the effort to learn. It should eventually be the standard C++ way to work with files and directories.

Nate
  • 18,752
  • 8
  • 48
  • 54
1

You don't need to use any 3rd-party library like boost, just call the following function (for Windows OS). After this, you will get all file paths within given folder in vector<string>.

#include <Windows.h>
// folder must end with "/", e.g. "D:/images/"
vector<string> get_all_files_full_path_within_folder(string folder)
{
    vector<string> names;
    char search_path[200];
    sprintf(search_path, "%s*.*", folder.c_str());
    WIN32_FIND_DATA fd; 
    HANDLE hFind = ::FindFirstFile(search_path, &fd); 
    if(hFind != INVALID_HANDLE_VALUE) 
    { 
        do 
        { 
            // read all (real) files in current folder, delete '!' read other 2 default folder . and ..
            if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) 
            {
                names.push_back(folder+fd.cFileName);
            }
        }while(::FindNextFile(hFind, &fd)); 
        ::FindClose(hFind); 
    } 
    return names;
}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • @Nabla Yes, I will add this constrain. Thanks. – herohuyongtao Dec 31 '13 at 17:26
  • @herohuyongtao thanks for the help! I'm having some trouble though, the function you provided keeps returning an empty vector. This is how I call it in my program: std::vector directories = getDirectories("14_animated_sprites_and_vsync/backgrounds"); (I renamed the method). – Adam Jan 01 '14 at 04:11
  • @herohuyongtao I'm sure its just some silly mistake, but i can't figure it out. – Adam Jan 01 '14 at 04:11
  • @herohuyongtao When I watch it using the debugger, it seems to find the directory ok but never gets past the ! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) check – Adam Jan 01 '14 at 04:14
  • @Adam Weird. I just run it successfully on my PC. Make sure folder `14_animated_sprites_and_vsync` is in under your project folder. – herohuyongtao Jan 01 '14 at 04:17
  • @herohuyongtao Are there any restrictions on the types of files that can be processed? I'm using png's. – Adam Jan 01 '14 at 04:25
  • @Adam I just realized that maybe your path is too long for `search_path[200]`, try to replace it by `search_path[500]` and test again. Hope it helps. – herohuyongtao Jan 01 '14 at 04:26
  • @Adam No, it has no restrictions. All files can be traversed 'cause the code search `*.*`. – herohuyongtao Jan 01 '14 at 04:28
  • @herohuyongtao hmmm, i know that its finding the folder correctly. when i put in a bogus file path it doesn't get past the (hFind != INVALID_HANDLE_VALUE) check. – Adam Jan 01 '14 at 04:30
  • @Adam Try to comment this line `if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )` to see how it goes. – herohuyongtao Jan 01 '14 at 04:34
  • @herohuyongtao Oh geez, as usual it was something silly. The probelm was that since backgrounds is a directory I needed to include the final / ! Thank so much for you help. – Adam Jan 01 '14 at 04:45
  • @Adam Ooooooh, glad to here making it through. – herohuyongtao Jan 01 '14 at 04:47
  • @herohuyongtao ah, actually there is still a problem getting past the if (!(fd.dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY)) check. if i remove the check it works fine, except that the first two filepaths read into the vector are . and .. respectively. After those two the real files get read in though. – Adam Jan 01 '14 at 04:48
  • @Adam Yes, it you remove the `if`-line. It will get your these two extra fake files. Actually, this is how the windows organize the file system. Just add this line to get the real files. – herohuyongtao Jan 01 '14 at 04:50