Skip to content

Commit beedd8b

Browse files
afwlehmannpolarina
authored andcommitted
Add pixelFormatToMasks and masksToPixelFormat to SDL.Video.Renderer (#94)
1 parent 72121b3 commit beedd8b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/SDL/Video/Renderer.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ module SDL.Video.Renderer
7878
, formatPalette
7979
, mapRGB
8080
, setPaletteColors
81+
, pixelFormatToMasks
82+
, masksToPixelFormat
8183

8284
-- * Textures
8385
, Texture
@@ -1193,3 +1195,26 @@ rendererLogicalSize (Renderer r) = makeStateVar renderGetLogicalSize renderSetLo
11931195
-- See @<https://wiki.libsdl.org/SDL_RenderTargetSupported SDL_RenderTargetSupported>@ for C documentation.
11941196
renderTargetSupported :: (MonadIO m) => Renderer -> m Bool
11951197
renderTargetSupported (Renderer r) = Raw.renderTargetSupported r
1198+
1199+
-- | Convert the given the enumerated pixel format to a bpp value and RGBA masks.
1200+
--
1201+
-- See @<https://wiki.libsdl.org/SDL_PixelFormatEnumToMasks SDL_PixelFormatEnumToMasks>@ for C documentation.
1202+
pixelFormatToMasks :: (MonadIO m) => PixelFormat -> m (CInt, V4 Word32)
1203+
pixelFormatToMasks pf = liftIO $
1204+
alloca $ \bpp ->
1205+
alloca $ \r ->
1206+
alloca $ \g ->
1207+
alloca $ \b ->
1208+
alloca $ \a -> do
1209+
throwIf_ not "SDL.Video.pixelFormatEnumToMasks" "SDL_PixelFormatEnumToMasks" $
1210+
Raw.pixelFormatEnumToMasks (toNumber pf) bpp r g b a
1211+
wrap <$> peek bpp <*> peek r <*> peek g <*> peek b <*> peek a
1212+
where
1213+
wrap bpp r g b a = (bpp, V4 r g b a)
1214+
1215+
-- | Convert a bpp value and RGBA masks to an enumerated pixel format.
1216+
--
1217+
-- See @<https://wiki.libsdl.org/SDL_MasksToPixelFormatEnum SDL_MasksToPixelFormatEnum>@ for C documentation.
1218+
masksToPixelFormat :: (MonadIO m) => CInt -> V4 Word32 -> m PixelFormat
1219+
masksToPixelFormat bpp (V4 r g b a) = liftIO $
1220+
fromNumber <$> Raw.masksToPixelFormatEnum bpp r g b a

0 commit comments

Comments
 (0)