fumieval / free-game

The free game engine
http://hackage.haskell.org/package/free-game
BSD 3-Clause "New" or "Revised" License
64 stars 15 forks source link

Bitmap stitching #37

Open Fuuzetsu opened 9 years ago

Fuuzetsu commented 9 years ago

We can split bitmaps with cropBitmap but there is no way to stitch bitmaps back together. For example if we want to stitch up some text from few bitmaps, we have to do logic to make sure all bitmaps are rendered at right offsets: it would be much easier to join up the bitmaps into one and render it at single point.

Considering

cropBitmap :: Bitmap -> (Int, Int) -> (Int, Int) -> Bitmap  

then maybe the type signature could be something like

stitchBitmap ∷ Bitmap -- ^ Original bitmap
             → Bitmap -- ^ bitmap to stitch
             → (Int, Int) -- ^ Point at which to stitch
             → Bitmap

could work. Everywhere that bitmaps don't overlay would simply be 0 opacity. Same if the point to stitch at is outside the original bitmap size. If we take the point to be the center of where the new bitmap would be spliced in, we could join two bitmaps by sides like this:

-- Stitch two bitmaps together side by side.
sideBySide ∷ Bitmap → Bitmap → Bitmap
sideBySide o n =
  let (w, h) = bitmapSize n & both %~ (round . (/ (2 ∷ Double)) . fromIntegral)
      (ow, _oh) = bitmapSize o
  in stitchBitmap o n (w + ow, h)

which should result in new bitmap with size (w + ow * 2, max (h * 2) _oh).

Would such a function be possible? You can pick whatever implementation you want of course, the above is just an example.