drawing sprite sheet frames ?
Mueble

BITMAP *grabframe(BITMAP *source,int startx,int starty,int width,int height,int frame,int columns)
{
BITMAP *temp = create_bitmap(width,height);
int x = startx + (frame %columns)* width;
int y = starty + (frame /columns) * height;
blit(source, temp, x,y, 0, 0,width,height);
return temp;
}

Hi guys I'm using this function to fill an array of bitmap which then I can use for drawing frames.Is there a drawback from doing this?

It seems intuitive to me but the majority of other methods that I've seen use the sprite sheet and then calculate the position of a given frame in order to draw it. What would be the advantage of doing that?

Thomas Fjellstrom

If you call your function a lot, you'll be creating a ton of new bitmaps. It's a lot of churn. One option is to just draw out of it directly as you mentioned, or create a sub bitmap instead. And hopefully you're storing them some place, and not calling that function over and over in your drawing code :D

Edgar Reynaldo

As Thomas said, you may want to use sub-bitmaps. It reduces your code to :

BITMAP *grabframe(BITMAP *source,int startx,int starty,int width,int height,int frame,int columns)
{
  int x = startx + (frame %columns)* width;
  int y = starty + (frame /columns) * height;
  BITMAP* frame = create_sub_bitmap(source , x , y , width , height);
  return frame;
}

This allows you to keep all your frames on the same sprite sheet, which can then be used as an atlas. You draw everything from this sheet in one batch if possible, using al_hold_bitmap_drawing.

Thomas Fjellstrom

This allows you to keep all your frames on the same sprite sheet, which can then be used as an atlas. You draw everything from this sheet in one batch if possible, using al_hold_bitmap_drawing.

Keep in mind that the OPs code is Allegro 4 which doesn't have held drawing.

Edgar Reynaldo

I need to pay more attention.... I knew it was allegro 4, but I gave A5 advice too. Whoops. Sorry bout that. ;)

Thread #615117. Printed from Allegro.cc