Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » drawing sprite sheet frames ?

This thread is locked; no one can reply to it. rss feed Print
drawing sprite sheet frames ?
Mueble
Member #15,827
December 2014

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
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

Go to: