Skip to content

add bindings for renderCopyExF, FRect, FPoint #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/SDL/Raw/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ module SDL.Raw.Types (
Palette(..),
PixelFormat(..),
Point(..),
FPoint(..),
Rect(..),
FRect(..),
RendererInfo(..),
RWops(..),
Surface(..),
Expand Down Expand Up @@ -1332,6 +1334,22 @@ instance Storable Point where
(#poke SDL_Point, x) ptr x
(#poke SDL_Point, y) ptr y

data FPoint = FPoint
{ fPointX :: !CFloat
, fPointY :: !CFloat
} deriving (Eq, Show, Typeable)

instance Storable FPoint where
sizeOf _ = (#size SDL_FPoint)
alignment = sizeOf
peek ptr = do
x <- (#peek SDL_FPoint, x) ptr
y <- (#peek SDL_FPoint, y) ptr
return $! FPoint x y
poke ptr (FPoint x y) = do
(#poke SDL_FPoint, x) ptr x
(#poke SDL_FPoint, y) ptr y

data Rect = Rect
{ rectX :: !CInt
, rectY :: !CInt
Expand All @@ -1354,6 +1372,28 @@ instance Storable Rect where
(#poke SDL_Rect, w) ptr w
(#poke SDL_Rect, h) ptr h

data FRect = FRect
{ fRectX :: !CFloat
, fRectY :: !CFloat
, fRectW :: !CFloat
, fRectH :: !CFloat
} deriving (Eq, Show, Typeable)

instance Storable FRect where
sizeOf _ = (#size SDL_FRect)
alignment = sizeOf
peek ptr = do
x <- (#peek SDL_FRect, x) ptr
y <- (#peek SDL_FRect, y) ptr
w <- (#peek SDL_FRect, w) ptr
h <- (#peek SDL_FRect, h) ptr
return $! FRect x y w h
poke ptr (FRect x y w h) = do
(#poke SDL_FRect, x) ptr x
(#poke SDL_FRect, y) ptr y
(#poke SDL_FRect, w) ptr w
(#poke SDL_FRect, h) ptr h

data RendererInfo = RendererInfo
{ rendererInfoName :: !CString
, rendererInfoFlags :: !Word32
Expand Down
6 changes: 6 additions & 0 deletions src/SDL/Raw/Video.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module SDL.Raw.Video (
renderClear,
renderCopy,
renderCopyEx,
renderCopyExF,
renderDrawLine,
renderDrawLines,
renderDrawPoint,
Expand Down Expand Up @@ -323,6 +324,7 @@ foreign import ccall "SDL.h SDL_QueryTexture" queryTextureFFI :: Texture -> Ptr
foreign import ccall "SDL.h SDL_RenderClear" renderClearFFI :: Renderer -> IO CInt
foreign import ccall "SDL.h SDL_RenderCopy" renderCopyFFI :: Renderer -> Texture -> Ptr Rect -> Ptr Rect -> IO CInt
foreign import ccall "SDL.h SDL_RenderCopyEx" renderCopyExFFI :: Renderer -> Texture -> Ptr Rect -> Ptr Rect -> CDouble -> Ptr Point -> RendererFlip -> IO CInt
foreign import ccall "SDL.h SDL_RenderCopyExF" renderCopyExFFFI :: Renderer -> Texture -> Ptr Rect -> Ptr FRect -> CDouble -> Ptr FPoint -> RendererFlip -> IO CInt
foreign import ccall "SDL.h SDL_RenderDrawLine" renderDrawLineFFI :: Renderer -> CInt -> CInt -> CInt -> CInt -> IO CInt
foreign import ccall "SDL.h SDL_RenderDrawLines" renderDrawLinesFFI :: Renderer -> Ptr Point -> CInt -> IO CInt
foreign import ccall "SDL.h SDL_RenderDrawPoint" renderDrawPointFFI :: Renderer -> CInt -> CInt -> IO CInt
Expand Down Expand Up @@ -833,6 +835,10 @@ renderCopyEx :: MonadIO m => Renderer -> Texture -> Ptr Rect -> Ptr Rect -> CDou
renderCopyEx v1 v2 v3 v4 v5 v6 v7 = liftIO $ renderCopyExFFI v1 v2 v3 v4 v5 v6 v7
{-# INLINE renderCopyEx #-}

renderCopyExF :: MonadIO m => Renderer -> Texture -> Ptr Rect -> Ptr FRect -> CDouble -> Ptr FPoint -> RendererFlip -> m CInt
renderCopyExF v1 v2 v3 v4 v5 v6 v7 = liftIO $ renderCopyExFFFI v1 v2 v3 v4 v5 v6 v7
{-# INLINE renderCopyExF #-}

renderDrawLine :: MonadIO m => Renderer -> CInt -> CInt -> CInt -> CInt -> m CInt
renderDrawLine v1 v2 v3 v4 v5 = liftIO $ renderDrawLineFFI v1 v2 v3 v4 v5
{-# INLINE renderDrawLine #-}
Expand Down
22 changes: 22 additions & 0 deletions src/SDL/Video/Renderer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module SDL.Video.Renderer
, clear
, copy
, copyEx
, copyExF
, drawLine
, drawLines
, drawPoint
Expand Down Expand Up @@ -764,6 +765,27 @@ copyEx (Renderer r) (Texture t) srcRect dstRect theta center flips =
V2 x y -> (if x then Raw.SDL_FLIP_HORIZONTAL else 0) .|.
(if y then Raw.SDL_FLIP_VERTICAL else 0))

-- | 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.
copyExF :: MonadIO m
=> Renderer -- ^ The rendering context
-> Texture -- ^ The source texture
-> Maybe (Rectangle CInt) -- ^ The source rectangle to copy, or 'Nothing' for the whole texture
-> 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.
-> CDouble -- ^ The angle of rotation in degrees. The rotation will be performed clockwise.
-> Maybe (Point V2 CFloat) -- ^ The point indicating the center of the rotation, or 'Nothing' to rotate around the center of the destination rectangle
-> V2 Bool -- ^ Whether to flip the texture on the X and/or Y axis
-> m ()
copyExF (Renderer r) (Texture t) srcRect dstRect theta center flips =
liftIO $
throwIfNeg_ "SDL.Video.copyExF" "SDL_RenderCopyExF" $
maybeWith with srcRect $ \src ->
maybeWith with dstRect $ \dst ->
maybeWith with center $ \c ->
Raw.renderCopyExF r t (castPtr src) (castPtr dst) theta (castPtr c)
(case flips of
V2 x y -> (if x then Raw.SDL_FLIP_HORIZONTAL else 0) .|.
(if y then Raw.SDL_FLIP_VERTICAL else 0))

-- | Draw a line on the current rendering target.
--
-- See @<https://wiki.libsdl.org/SDL_RenderDrawLine SDL_RenderDrawLine>@ for C documentation.
Expand Down