Bitmap array
Ceagon Xylas

Now I know 24,467 people have asked how to do this or have been shown how to do this -- but when I finally can't remember how to, the Allegro search completely fails me. How on earth do you load into a BITMAP* array?!

BITMAP *sprite_g[255];

main() {
  sprite_g[0]=load_pcx("file.pcx",NULL);
  sprite_g[1]=load_pcx("file.pcx",NULL);

  draw_sprite(buffer,sprite_g[0],0,0);
  draw_sprite(buffer,sprite_g[1],320,0);

  for(int i=0; i<255; i++) destroy_bitmap(sprite_g<i>);
}

This is crashing for me every time. Checked my file paths, they're correct. I can get this to work:

  for(int i=0; i<255; i++) {
    sprite_g<i>=create_bitmap(20,20);
    clear_to_color(sprite_g<i>,makecol(255,255,255));
  }
  draw_sprite(buffer,sprite_g[0],0,0);

But whenever I load the file into the array it's not workin'.

miran

1. Initialize bitmaps to 0 before loading.
2. Check if 0 before drawing.
3. Check if 0 before destroying.
4. Set to 0 after destroying.

Note: #3 may not be necessary.

ReyBrujo

If your code, you load two bitmaps, all the other positions have trash since they aren't initialized. At the end of the program you try to destroy 256 bitmaps, but only two have been allocated. Probably when i = 2 destroy_bitmap crashes.

(Plus miran's suggestions, of course)

Ceagon Xylas

It's crashing on the draw_sprite() function almost certainly. The reason I say that is because it runs fine until I press 'a' in this program:

//declare bitmaps

main() {
  while(!end_program) {
    if(key[KEY_A]) draw_sprite(buffer,sprite_g[0],0,0);
    //blit the buffer
  }
  //destroy_bitmap for loop
}

I'll try what you said, miran.

[edit]
Yay! I solved it! Thanks guys! =P
Basically, yeah, just had to initialize the entire array.

miran

Yay!

A J
Quote:

It's crashing on the draw_sprite() function almost certainly.

ALMOST What do you mean almost, dont you know ?

Do you know how to use a debugger ?

CGamesPlay

More importantly, initialize allegro!

Thread #584064. Printed from Allegro.cc