Coneone
Coneone

Reputation: 322

Modify bitmap after being drawn but before onDraw() finishes

I'm creating a home made game engine for Android and I'm having some problems with the reuse of a Bitmap canvas:

Visible game objects receive the main Canvas in each frame so they can draw themselves with a static bitmap or with a sprite. So far everything works fine.

The sprite consists in two bitmaps. First contains the complete sequence of drawings, and the second is dynamically drawn through a Canvas(bitmap). The result bitmap and its canvas are created in the sprite constructor to avoid reallocations in the draw loop.

I reuse this sprites for each object that needs the same sequence of drawings. This sprites also have a function that allows to obtain a bitmap containing a different position of the drawings sequence. So, if the object wants the sprite in another position calls its function to get the Bitmap and draws to the main canvas.

So the problem is that all objects using the same sprite end up drawing the same image.

If I create a new result bitmap and a new canvas in the returning function all works fine:

currentBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
currentBmpCanvas = new Canvas(currentBmp);

I assume that all bitmaps are not drawn until onDraw() is finished, so is there some way to solve this without reallocating the bitmap in each loop?

Thanks!

Upvotes: 1

Views: 486

Answers (1)

Coneone
Coneone

Reputation: 322

After some hours thinking, I opted to implement a simple solution that didn't cross my mind before.

Like the object game receives the main canvas, why not do the same with the sprite?

So basicly this is the code for the game object:

sprite.smashDelayedCanvas(canvas, focusLocation);

And for the sprite:

canvas.drawBitmap(scaledImage, delayedIni, dest, null);

Upvotes: 1

Related Questions