-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.UI.SDL.Rotozoomer
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-----------------------------------------------------------------------------
module Graphics.UI.SDL.Rotozoomer where

import Foreign
import Foreign.C

import Graphics.UI.SDL.Video
import Graphics.UI.SDL.General
import Graphics.UI.SDL.Types

finalizeWhenNotNull :: String -> Ptr SurfaceStruct -> IO Surface
finalizeWhenNotNull :: String -> Ptr SurfaceStruct -> IO Surface
finalizeWhenNotNull String
errMsg Ptr SurfaceStruct
image
    = if Ptr SurfaceStruct
image Ptr SurfaceStruct -> Ptr SurfaceStruct -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr SurfaceStruct
forall a. Ptr a
nullPtr
         then String -> IO Surface
forall a. String -> IO a
failWithError String
errMsg
         else Ptr SurfaceStruct -> IO Surface
mkFinalizedSurface Ptr SurfaceStruct
image



-- SDL_Surface * rotozoomSurface (SDL_Surface *src, double angle, double zoom, int smooth);
foreign import ccall unsafe "rotozoomSurface" sdlRotozoom
    :: Ptr SurfaceStruct -> Double -> Double -> Int -> IO (Ptr SurfaceStruct)
rotozoom :: Surface -> Double -> Double -> Bool -> IO Surface
rotozoom :: Surface -> Double -> Double -> Bool -> IO Surface
rotozoom Surface
src Double
angle Double
zoom Bool
smooth
    = Surface -> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr Surface
src ((Ptr SurfaceStruct -> IO Surface) -> IO Surface)
-> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall a b. (a -> b) -> a -> b
$ \Ptr SurfaceStruct
imgSrc ->
      Ptr SurfaceStruct
-> Double -> Double -> Int -> IO (Ptr SurfaceStruct)
sdlRotozoom Ptr SurfaceStruct
imgSrc Double
angle Double
zoom (Bool -> Int
forall a. Num a => Bool -> a
fromBool Bool
smooth) IO (Ptr SurfaceStruct)
-> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Ptr SurfaceStruct -> IO Surface
finalizeWhenNotNull String
"rotozoomSurface"

{-
-- SDL_Surface * rotozoomSurfaceXY (SDL_Surface *src, double angle, double zoomx, double zoomy, int smooth);
foreign import ccall unsafe "rotozoomSurfaceXY" sdlRotozoomXY
    :: Ptr SurfaceStruct -> Double -> Double -> Double -> Int -> IO (Ptr SurfaceStruct)
rotozoomXY :: Surface -> Double -> Double -> Double -> Bool -> IO Surface
rotozoomXY src angle zoomx zoomy smooth
    = withForeignPtr src $ \imgSrc ->
      sdlRotozoomXY imgSrc angle zoomx zoomy (fromBool smooth) >>= finalizeWhenNotNull "rotozoomSurfaceXY"
-}

-- SDL_Surface * zoomSurface (SDL_Surface *src, double zoomx, double zoomy, int smooth);
foreign import ccall unsafe "zoomSurface" sdlZoom
    :: Ptr SurfaceStruct -> Double -> Double -> Int -> IO (Ptr SurfaceStruct)
zoom :: Surface -> Double -> Double -> Bool -> IO Surface
zoom :: Surface -> Double -> Double -> Bool -> IO Surface
zoom Surface
src Double
zoomx Double
zoomy Bool
smooth
    = Surface -> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr Surface
src ((Ptr SurfaceStruct -> IO Surface) -> IO Surface)
-> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall a b. (a -> b) -> a -> b
$ \Ptr SurfaceStruct
imgSrc ->
      Ptr SurfaceStruct
-> Double -> Double -> Int -> IO (Ptr SurfaceStruct)
sdlZoom Ptr SurfaceStruct
imgSrc Double
zoomx Double
zoomy (Bool -> Int
forall a. Num a => Bool -> a
fromBool Bool
smooth) IO (Ptr SurfaceStruct)
-> (Ptr SurfaceStruct -> IO Surface) -> IO Surface
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Ptr SurfaceStruct -> IO Surface
finalizeWhenNotNull String
"zoomSurface"