module Graphics.UI.SDL.RWOps
( fromFile
, tryFromFile
, free
, with
, mkFinalizedRW
) where
import Foreign (Ptr, FunPtr,
#if defined(__GLASGOW_HASKELL__)
finalizeForeignPtr,
#endif
maybePeek, newForeignPtr)
import Foreign.C (withCString, CString)
import Control.Exception (bracket)
import Graphics.UI.SDL.Types (RWops, RWopsStruct)
import Graphics.UI.SDL.General (unwrapMaybe)
with :: FilePath -> String -> (RWops -> IO a) -> IO a
with path mode action
= bracket (fromFile path mode)
(free)
action
foreign import ccall unsafe "SDL_RWFromFile" rwFromFile :: CString -> CString -> IO (Ptr RWopsStruct)
tryFromFile :: FilePath -> String -> IO (Maybe RWops)
tryFromFile filepath mode
= withCString filepath $ \cPath ->
withCString mode $ \cMode ->
rwFromFile cPath cMode >>= maybePeek mkFinalizedRW
fromFile :: FilePath -> String -> IO RWops
fromFile filepath mode = unwrapMaybe "SDL_RWFromFile" (tryFromFile filepath mode)
foreign import ccall unsafe "&SDL_FreeRW" rwFreeFinal :: FunPtr (Ptr RWopsStruct -> IO ())
mkFinalizedRW :: Ptr RWopsStruct -> IO RWops
mkFinalizedRW = newForeignPtr rwFreeFinal
free :: RWops -> IO ()
free =
#if defined(__GLASGOW_HASKELL__)
finalizeForeignPtr
#else
const (return ())
#endif