Crop bitmap (or setBitmapData)

I have cases in my game where I'd like to draw some sub-rect of an image; for example, a laser image where I'd like to draw it (rotated) but only to a certain length (the length can change each frame).

As far as I can tell the options are:

  • use the mask image for this, but that seems unnecessarily expensive to fill that in and check it while drawing;
  • use the global clip rect, but that doesn't work for my case because I want to rotate the image so there's no screen-aligned rect to clip to;
  • allocate another image of the clipped size and draw the source image into it (again seems expensive).

Would it be possible to add something (to the C API) like setBitmapClipRect, getSubBitmap or even allow me to create a bitmap with my own data/width/height/rowbytes so I could build this myself?

Thanks!

For drawing cropped bitmaps, you can manually read the bitmap data (playdate->graphics->getBitmapData) and then write it to the target surface (e.g. getFrame()).

You can create and edit bitmaps with these functions:
playdate->graphcs->newBitmap
playdate->graphcs->copyBitmap
playdate->graphcs->getBitmapData (and then edit the data returned in the mask and data fields)
playdate->graphcs->clearBitmap
playdate->graphcs->setBitmapMask

It may also be helpful to know that the .pdi file format is documented here.

Thanks for the reply! In this case I was trying to avoid having to write the rotation code myself, which I think would be required for the solutions where I read the data manually.

1 Like