How to initialize multiple(over 200) Allegro bitmaps in a for loop?

How would I initialize Allegro bitmaps(287 to be exact) in a for loop?
The images are in a folder called, "images" and the file name have the syntax,
image-0.jpg image-1.jpg image-2.jpg ... Here is what I have of code, have not tested.

//not all code
int num;

ALLEGRO_BITMAP *image[287] = NULL;
for (num = 0; num <= 287; num++)
image[num] = al_load_bitmap("images/image-"".jpg");
// code needs the number of image

for (num = 0; num < = 287; num++)


You can use a stringstream to get that:

#include <sstream>
std::stringstream s;
s << "image-" << i;
image[i] = al_load_bitmap(s.str().c_str());

Arthur Kalliokoski

I don't know about C++, but in regular C:

1//not all code 2int num; 3//ALLEGRO_BITMAP *image[287] = NULL; <-you can't initialize them all at once like that! 4ALLEGRO_BITMAP *image[287]; 5for (num = 0; num <= 287; num++) 6{ 7 image[num] = NULL; 8 image[num] = al_load_bitmap("images/image-"".jpg"); 9 if(image[num] == NULL) //Always check! Always! 10 { 11 error message blah blah 12 exit(1) 13 } 14} 15 . 16 . 17 . 18// code needs the number of image 19...for (num = 0; num < = 287; num++) 20 al_destroy_bitmap(image[num]);


You do have to check if the image loaded, but he wanted to know how to get those file-names correct.

Arthur Kalliokoski
J-Gamer said:

he wanted to know how to get those file-names correct.

My bad. :-[

1//not all code 2int num; 3//ALLEGRO_BITMAP *image[287] = NULL; <-you can't initialize them all at once like that! 4ALLEGRO_BITMAP *image[287]; 5for (num = 0; num <= 287; num++) 6{ 7 char buff[512]; 8 image[num] = NULL; 9 snprintf(buff,sizeof(buff),"images/image-%d.jpg",num); 10 image[num] = al_load_bitmap(buff); 11 if(image[num] == NULL) //Always check! Always! 12 { 13 error message blah blah 14 exit(1) 15 } 16}


I use this for my pacman game to load in my main character. He is on a sprite sheet which is basically just a group of images, all 50x50 each, 4 wide x 5 high.

   pacman.sheet = al_load_bitmap("Graphics/Pacman.png");
   if(!pacman.sheet) {
      a5_error(AT, display.screen, "Failed to load Pacman bitmaps.");
   for(int y = 0; y < 5; y++) {     // bitmaps are 5 rows
      for(int x = 0; x < 4; x++) {  // by 4 columns
         pacman.bitmap[4*y+x] = al_create_sub_bitmap(pacman.sheet, x*SPRITE_SIZE, y*SPRITE_SIZE,
                                                     SPRITE_SIZE, SPRITE_SIZE);

I have a function called shut_down(); that will destroy any bitmaps that were created. I don't like just exiting my program on error without first calling that to properly destroy anything that may have been created before the error.

Anyhow, this way you load in the graphics all at once, then you can procede to create subbitmaps of each image you want if they're all related (and small enough to fit on a single sheet).

Arthur: how will <code>image[num] = al_load_bitmap("images/image-"".jpg");</code> know what image number to load?!
I see you corrected that. ;)

I have always done it like: (C code, I actually use C99 myself)

1#include<stdio.h> 2 3int i; 4char filename[80]; 5ALLEGRO_BITMAP *image[287]; 6 7/* you have 287 images, so <287, not <=287, it will be 0 to 286 8 unless you have 288 images, 0 to 287? */ 9for(i=0; i<287; i++) { 10 sprintf(filename, "images/image-%d.jpg", i); 11 image[i] = NULL; 12 image[i] = al_load_bitmap(filename); 13 if(image[i] == NULL) { 14 printf("Error loading /"%s/"", filename); 15 shutdown(); /* destroy any bitmaps that were created first */ 16 exit(1); 17 } 18}

Also, when I save images like that, I like to name them with 3 numbers, like IMAGE000.JPG, IMAGE001.JPG ... IMAGE287.JPG, they will list in your directory in order this way... in which case you want to use something like:

sprintf(filename, "images/image%03d.jpg", i);


Wow, a quick replay!!! Thanks, and I got the program going so now program was successful in allocating bitmaps with a for loop to get all 287 bitmaps with correct file names. Since you guys help me I am willing to share you on my project and source code(still needs work though). I am creating a program which flashes images so fast it bypasses the conscious mind and goes to the subconscious mind. I'm using this with books I want to read mostly programming books in pdf converted with imageMagick to jpg. I've decided to call the program al_photoreader. In theory I will be able to bring back this information back to the conscious mind by remembering my dreams. Hopefully somebody is interested.


this is the simplest code i have used for loading 8 audio samples in my program.

ALLEGRO_SAMPLE *sample[8];
  ALLEGRO_SAMPLE_INSTANCE *sample_instance[8];
  char path[30] = "data\\samples\\sample (0).wav";

  for (int i = 0; i < 8; i++)
    // i replace the char 22 of the path, result: sample(i).wav
    path[21] = '0'+(i + 1); // the '0' is just for implicit conversion
    cout << path << endl;
    sample[i] = al_load_sample(path);
    sample_instance[i] = al_create_sample_instance(sample[i]);
    al_set_sample_instance_gain(sample_instance[i], samples_volume);
    al_set_sample_instance_playmode(sample_instance[i], ALLEGRO_PLAYMODE_ONCE);
    al_attach_sample_instance_to_mixer(sample_instance[i], al_get_default_mixer());

Thread #610983. Printed from