Loading several images of arbitrary name

I’m wanting to use SDL_LoadBMP(fileName), or SDL_LoadImage once I get
it, to load a bunch of images. The whole idea is to load an entire photo
album worth of images from a single directory so that I can texture map
them into a virtual photo album.

I know how to load a single file that I know the name of, but I can’t
hard-code names in because the album should be dynamic.

Ideas?

Cheers,
Glenn

If you have string containing the path of the directory then you can
use an OS-specific function to read the contents of the directory.
Then for each file in that directory you’ll want to call
SDL_LoadBMP().

If you’re in Unix/Linux you can use the scandir() function by
including dirent.h. If you’re using Windows then you probably already
have included windows.h so you can use FindFirstFile() and
FindNextFile().

ErikOn 7/21/05, Glenn McCord <clari_player at paradise.net.nz> wrote:

I’m wanting to use SDL_LoadBMP(fileName), or SDL_LoadImage once I get
it, to load a bunch of images. The whole idea is to load an entire photo
album worth of images from a single directory so that I can texture map
them into a virtual photo album.

I know how to load a single file that I know the name of, but I can’t
hard-code names in because the album should be dynamic.

Ideas?

Cheers,
Glenn


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

#include <stdlib.h>
#include <io.h>
#include <SDL.h>

SDL_Surface **photos = NULL;
struct _finddata_t find;
int handle, ret = 1, num_photos = 1;

photos = (SDL_Surface **)malloc(sizeof(SDL_Surface *) * num_photos);
if(!photos) exit(1);

for(handle = _findfirst("*.bmp",&find); handle != -1 && ret != -1; ret
= _findnext(handle,&find))
{
if(!ret)
{
photos = (SDL_Surface **)realloc(photos,sizeof(SDL_Surface *) * num_photos);
if(!photos) exit(2);
}
photos[num_photos - 1] = SDL_LoadBMP(find.name);
num_photos++;
}
if(handle != -1) _findclose(handle);

//…

int i;
for(i = 0; i < num_photos; i++) SDL_FreeSurface(photos[i]);
free(photos);------------------------

This is untested, but it looks right to my eye. Once you have
SDL_Image going, you’ll need to have multiple loops checking for image
types, or else search for . and do a strcmp() on the last 4
characters of the filename to see if it’s a correct image type.

Josh

On 7/22/05, Glenn McCord <clari_player at paradise.net.nz> wrote:

I’m wanting to use SDL_LoadBMP(fileName), or SDL_LoadImage once I get
it, to load a bunch of images. The whole idea is to load an entire photo
album worth of images from a single directory so that I can texture map
them into a virtual photo album.

I know how to load a single file that I know the name of, but I can’t
hard-code names in because the album should be dynamic.

Ideas?

Cheers,
Glenn


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thanks for the help. I’ll definitely look into this FindFirstFile().

Glenn

Josh Matthews wrote:>#include <stdlib.h>

#include <io.h>
#include <SDL.h>

SDL_Surface **photos = NULL;
struct _finddata_t find;
int handle, ret = 1, num_photos = 1;

photos = (SDL_Surface **)malloc(sizeof(SDL_Surface *) * num_photos);
if(!photos) exit(1);

for(handle = _findfirst("*.bmp",&find); handle != -1 && ret != -1; ret
= _findnext(handle,&find))
{
if(!ret)
{
photos = (SDL_Surface **)realloc(photos,sizeof(SDL_Surface *) * num_photos);
if(!photos) exit(2);
}
photos[num_photos - 1] = SDL_LoadBMP(find.name);
num_photos++;
}
if(handle != -1) _findclose(handle);

//…

int i;
for(i = 0; i < num_photos; i++) SDL_FreeSurface(photos[i]);
free(photos);


This is untested, but it looks right to my eye. Once you have
SDL_Image going, you’ll need to have multiple loops checking for image
types, or else search for . and do a strcmp() on the last 4
characters of the filename to see if it’s a correct image type.

Josh

On 7/22/05, Glenn McCord <clari_player at paradise.net.nz> wrote:

I’m wanting to use SDL_LoadBMP(fileName), or SDL_LoadImage once I get
it, to load a bunch of images. The whole idea is to load an entire photo
album worth of images from a single directory so that I can texture map
them into a virtual photo album.

I know how to load a single file that I know the name of, but I can’t
hard-code names in because the album should be dynamic.

Ideas?

Cheers,
Glenn


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl