|
How to initialize multiple(over 200) Allegro bitmaps in a for loop? |
say_what!
Member #14,535
September 2012
|
How would I initialize Allegro bitmaps(287 to be exact) in a for loop? //not all code ALLEGRO_BITMAP *image[287] = NULL; for (num = 0; num < = 287; num++) |
J-Gamer
Member #12,491
January 2011
|
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());
" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
Arthur Kalliokoski
Second in Command
February 2005
|
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]);
They all watch too much MSNBC... they get ideas. |
J-Gamer
Member #12,491
January 2011
|
You do have to check if the image loaded, but he wanted to know how to get those file-names correct. " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
Arthur Kalliokoski
Second in Command
February 2005
|
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}
They all watch too much MSNBC... they get ideas. |
Neil Roy
Member #2,229
April 2002
|
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."); shut_down(); exit(1); } 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).
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); --- |
say_what!
Member #14,535
September 2012
|
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. |
wootrop
Member #14,516
August 2012
|
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()); }
|
|