Reuse bitmap memory for different sized bitmaps?

Hello,

I was wondering if there is a way to have bitmaps reuse the same preallocated memory rather than constantly reallocating via newBitmap/freeBitmap.

I know there is loadIntoBitmap, but that doesn't do what I need. My situation is: every time I load a level in my game I generate one big bitmap containing the image of my level's floor, and I generate this image procedurally (so I don't load an image from file). Each level has a different size, but I know the max allowed size, so to avoid memory fragmentation I would like to:

  • preallocate a chunk of memory large enough to store the largest possible bitmap
  • every time I load a level, clear the memory and use it to store that level floor's bitmap

I guess I could do it by allocating one large bitmap and then using clear to draw into it, but I'm not sure if that is going to give me the worst possible performance when drawing it (is drawBitmap going to always draw the full-sized image even though most pixels are transparents? I don't see how to avoid that).

So ideally I would need something like LCDBitmap *newBitmapIntoMemory(void *mem, int mem_size, int width, int height, LCDColor fill) or, alternatively, a way to set width/height of an existing bitmap without reallocating.

Is there a better way to go about this?