Skip to content

Commit 12eacae

Browse files
authored
add bindings for renderCopyExF, FRect, FPoint (#228)
1 parent a05c588 commit 12eacae

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/SDL/Raw/Types.hsc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ module SDL.Raw.Types (
6666
Palette(..),
6767
PixelFormat(..),
6868
Point(..),
69+
FPoint(..),
6970
Rect(..),
71+
FRect(..),
7072
RendererInfo(..),
7173
RWops(..),
7274
Surface(..),
@@ -1332,6 +1334,22 @@ instance Storable Point where
13321334
(#poke SDL_Point, x) ptr x
13331335
(#poke SDL_Point, y) ptr y
13341336

1337+
data FPoint = FPoint
1338+
{ fPointX :: !CFloat
1339+
, fPointY :: !CFloat
1340+
} deriving (Eq, Show, Typeable)
1341+
1342+
instance Storable FPoint where
1343+
sizeOf _ = (#size SDL_FPoint)
1344+
alignment = sizeOf
1345+
peek ptr = do
1346+
x <- (#peek SDL_FPoint, x) ptr
1347+
y <- (#peek SDL_FPoint, y) ptr
1348+
return $! FPoint x y
1349+
poke ptr (FPoint x y) = do
1350+
(#poke SDL_FPoint, x) ptr x
1351+
(#poke SDL_FPoint, y) ptr y
1352+
13351353
data Rect = Rect
13361354
{ rectX :: !CInt
13371355
, rectY :: !CInt
@@ -1354,6 +1372,28 @@ instance Storable Rect where
13541372
(#poke SDL_Rect, w) ptr w
13551373
(#poke SDL_Rect, h) ptr h
13561374

1375+
data FRect = FRect
1376+
{ fRectX :: !CFloat
1377+
, fRectY :: !CFloat
1378+
, fRectW :: !CFloat
1379+
, fRectH :: !CFloat
1380+
} deriving (Eq, Show, Typeable)
1381+
1382+
instance Storable FRect where
1383+
sizeOf _ = (#size SDL_FRect)
1384+
alignment = sizeOf
1385+
peek ptr = do
1386+
x <- (#peek SDL_FRect, x) ptr
1387+
y <- (#peek SDL_FRect, y) ptr
1388+
w <- (#peek SDL_FRect, w) ptr
1389+
h <- (#peek SDL_FRect, h) ptr
1390+
return $! FRect x y w h
1391+
poke ptr (FRect x y w h) = do
1392+
(#poke SDL_FRect, x) ptr x
1393+
(#poke SDL_FRect, y) ptr y
1394+
(#poke SDL_FRect, w) ptr w
1395+
(#poke SDL_FRect, h) ptr h
1396+
13571397
data RendererInfo = RendererInfo
13581398
{ rendererInfoName :: !CString
13591399
, rendererInfoFlags :: !Word32

src/SDL/Raw/Video.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module SDL.Raw.Video (
106106
renderClear,
107107
renderCopy,
108108
renderCopyEx,
109+
renderCopyExF,
109110
renderDrawLine,
110111
renderDrawLines,
111112
renderDrawPoint,
@@ -323,6 +324,7 @@ foreign import ccall "SDL.h SDL_QueryTexture" queryTextureFFI :: Texture -> Ptr
323324
foreign import ccall "SDL.h SDL_RenderClear" renderClearFFI :: Renderer -> IO CInt
324325
foreign import ccall "SDL.h SDL_RenderCopy" renderCopyFFI :: Renderer -> Texture -> Ptr Rect -> Ptr Rect -> IO CInt
325326
foreign import ccall "SDL.h SDL_RenderCopyEx" renderCopyExFFI :: Renderer -> Texture -> Ptr Rect -> Ptr Rect -> CDouble -> Ptr Point -> RendererFlip -> IO CInt
327+
foreign import ccall "SDL.h SDL_RenderCopyExF" renderCopyExFFFI :: Renderer -> Texture -> Ptr Rect -> Ptr FRect -> CDouble -> Ptr FPoint -> RendererFlip -> IO CInt
326328
foreign import ccall "SDL.h SDL_RenderDrawLine" renderDrawLineFFI :: Renderer -> CInt -> CInt -> CInt -> CInt -> IO CInt
327329
foreign import ccall "SDL.h SDL_RenderDrawLines" renderDrawLinesFFI :: Renderer -> Ptr Point -> CInt -> IO CInt
328330
foreign import ccall "SDL.h SDL_RenderDrawPoint" renderDrawPointFFI :: Renderer -> CInt -> CInt -> IO CInt
@@ -833,6 +835,10 @@ renderCopyEx :: MonadIO m => Renderer -> Texture -> Ptr Rect -> Ptr Rect -> CDou
833835
renderCopyEx v1 v2 v3 v4 v5 v6 v7 = liftIO $ renderCopyExFFI v1 v2 v3 v4 v5 v6 v7
834836
{-# INLINE renderCopyEx #-}
835837

838+
renderCopyExF :: MonadIO m => Renderer -> Texture -> Ptr Rect -> Ptr FRect -> CDouble -> Ptr FPoint -> RendererFlip -> m CInt
839+
renderCopyExF v1 v2 v3 v4 v5 v6 v7 = liftIO $ renderCopyExFFFI v1 v2 v3 v4 v5 v6 v7
840+
{-# INLINE renderCopyExF #-}
841+
836842
renderDrawLine :: MonadIO m => Renderer -> CInt -> CInt -> CInt -> CInt -> m CInt
837843
renderDrawLine v1 v2 v3 v4 v5 = liftIO $ renderDrawLineFFI v1 v2 v3 v4 v5
838844
{-# INLINE renderDrawLine #-}

src/SDL/Video/Renderer.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module SDL.Video.Renderer
2323
, clear
2424
, copy
2525
, copyEx
26+
, copyExF
2627
, drawLine
2728
, drawLines
2829
, drawPoint
@@ -764,6 +765,27 @@ copyEx (Renderer r) (Texture t) srcRect dstRect theta center flips =
764765
V2 x y -> (if x then Raw.SDL_FLIP_HORIZONTAL else 0) .|.
765766
(if y then Raw.SDL_FLIP_VERTICAL else 0))
766767

768+
-- | Copy a portion of the texture to the current rendering target, optionally rotating it by angle around the given center and also flipping it top-bottom and/or left-right.
769+
copyExF :: MonadIO m
770+
=> Renderer -- ^ The rendering context
771+
-> Texture -- ^ The source texture
772+
-> Maybe (Rectangle CInt) -- ^ The source rectangle to copy, or 'Nothing' for the whole texture
773+
-> Maybe (Rectangle CFloat) -- ^ The destination rectangle to copy to, or 'Nothing' for the whole rendering target. The texture will be stretched to fill the given rectangle.
774+
-> CDouble -- ^ The angle of rotation in degrees. The rotation will be performed clockwise.
775+
-> Maybe (Point V2 CFloat) -- ^ The point indicating the center of the rotation, or 'Nothing' to rotate around the center of the destination rectangle
776+
-> V2 Bool -- ^ Whether to flip the texture on the X and/or Y axis
777+
-> m ()
778+
copyExF (Renderer r) (Texture t) srcRect dstRect theta center flips =
779+
liftIO $
780+
throwIfNeg_ "SDL.Video.copyExF" "SDL_RenderCopyExF" $
781+
maybeWith with srcRect $ \src ->
782+
maybeWith with dstRect $ \dst ->
783+
maybeWith with center $ \c ->
784+
Raw.renderCopyExF r t (castPtr src) (castPtr dst) theta (castPtr c)
785+
(case flips of
786+
V2 x y -> (if x then Raw.SDL_FLIP_HORIZONTAL else 0) .|.
787+
(if y then Raw.SDL_FLIP_VERTICAL else 0))
788+
767789
-- | Draw a line on the current rendering target.
768790
--
769791
-- See @<https://wiki.libsdl.org/SDL_RenderDrawLine SDL_RenderDrawLine>@ for C documentation.

0 commit comments

Comments
 (0)