module Graphics.UI.SDL.MPEG (
    module Graphics.UI.SDL.MPEG,
    module Graphics.UI.SDL.MPEG.Types,
    module Graphics.UI.SDL.MPEG.General
) where
import Foreign
import Foreign.C.String
import Graphics.UI.SDL
import Graphics.UI.SDL.MPEG.Types
import Graphics.UI.SDL.MPEG.General

-- | Create a new MPEG object from an MPEG file.
load :: FilePath -> IO MPEG
load fn = do
    mpeg <- withCString fn $ \cstr -> _SMPEG_new cstr nullPtr 0
    mkFinalizedMPEG mpeg

-- | Create a new MPEG object from a file descriptor.
loadFd :: Int -> IO MPEG
loadFd fd = do
    mpeg <- _SMPEG_new_descr fd nullPtr 0
    mkFinalizedMPEG mpeg

-- | Create a new MPEG object from a byte string.
loadData :: CStringLen -> IO MPEG
loadData (cstr, len) = do
    mpeg <- _SMPEG_new_data (castPtr cstr) len nullPtr 0
    mkFinalizedMPEG mpeg

-- | Create a new MPEG object from a RWops structure.
loadRWops :: RWops -> IO MPEG
loadRWops rwops = withForeignPtr rwops $ \rptr -> do
    mpeg <- _SMPEG_new_rwops rptr nullPtr 0
    mkFinalizedMPEG mpeg

-- | Set the SDL surface and the position the MPEG object is to be painted on.
setSurface :: MPEG -> Surface -> Int -> Int -> IO ()
setSurface mpeg surface x y = withForeignPtr mpeg $ \mptr -> do
    withForeignPtr surface $ \sptr -> _SMPEG_setdisplay mptr sptr nullPtr nullFunPtr
    _SMPEG_move mptr x y

-- | Set the region of the video to be shown.
setDisplayRegion :: MPEG -> Rect -> IO ()
setDisplayRegion mpeg (Rect x y w h) = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_setdisplayregion mptr x y w h

-- | Render a particular frame in the MPEG video
renderFrame :: MPEG -> Int -> IO ()
renderFrame mpeg i = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_renderFrame mptr i

setScale :: MPEG -> Int -> IO ()
setScale mpeg scale = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_scale mptr scale

setScaleXY :: MPEG -> Int -> Int -> IO ()
setScaleXY mpeg x y = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_scaleXY mptr x y

enableAudio, enableVideo, enableLoop :: MPEG -> Bool -> IO ()
enableAudio mpeg bool = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_enableaudio mptr (fromEnum bool)
enableVideo mpeg bool = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_enablevideo mptr (fromEnum bool)
enableLoop mpeg bool = withForeignPtr mpeg $ \mptr -> do
    _SMPEG_loop mptr (fromEnum bool)

play, pause, stop, rewind :: MPEG -> IO ()
play   = (`withForeignPtr` _SMPEG_play)
pause  = (`withForeignPtr` _SMPEG_pause)
stop   = (`withForeignPtr` _SMPEG_stop)
rewind = (`withForeignPtr` _SMPEG_rewind)