{-# LINE 1 "src/SFML/Window/VideoMode.hsc" #-}
{-# LANGUAGE CPP, ForeignFunctionInterface #-}
{-# LINE 2 "src/SFML/Window/VideoMode.hsc" #-}
module SFML.Window.VideoMode
(
    VideoMode(..)
,   getDesktopMode
,   getFullscreenModes
,   isValid
)
where


import Control.Applicative ((<$>), (<*>), (*>), liftA2)
import Foreign.C.Types
import Foreign.Ptr
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (peekArray)
import Foreign.Marshal.Utils (with)
import Foreign.Storable


{-# LINE 21 "src/SFML/Window/VideoMode.hsc" #-}


sizeInt = (4)
{-# LINE 24 "src/SFML/Window/VideoMode.hsc" #-}


data VideoMode = VideoMode
    { windowWidth  :: Int -- ^ Video mode width, in pixels
    , windowHeight :: Int -- ^ Video mode height, in pixels
    , windowBPP    :: Int -- ^ Video mode pixel depth, in bits per pixels
    }
    deriving (Show)


instance Storable VideoMode where
    sizeOf _ = 3*sizeInt
    alignment _ = alignment (undefined :: CInt)

    peek ptr = VideoMode
        <$> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 0) ptr :: IO CUInt)
{-# LINE 40 "src/SFML/Window/VideoMode.hsc" #-}
        <*> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 4) ptr :: IO CUInt)
{-# LINE 41 "src/SFML/Window/VideoMode.hsc" #-}
        <*> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 8) ptr :: IO CUInt)
{-# LINE 42 "src/SFML/Window/VideoMode.hsc" #-}

    poke ptr (VideoMode w h b) = do
        (\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr (fromIntegral w :: CUInt)
{-# LINE 45 "src/SFML/Window/VideoMode.hsc" #-}
        (\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr (fromIntegral h :: CUInt)
{-# LINE 46 "src/SFML/Window/VideoMode.hsc" #-}
        (\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr (fromIntegral b :: CUInt)
{-# LINE 47 "src/SFML/Window/VideoMode.hsc" #-}


-- | Get the current desktop video mode
getDesktopMode = alloca $ liftA2 (*>) sfVideoMode_getDesktopMode_helper peek

foreign import ccall unsafe "sfVideoMode_getDesktopMode_helper"
    sfVideoMode_getDesktopMode_helper :: Ptr VideoMode -> IO ()

--CSFML_WINDOW_API sfVideoMode sfVideoMode_getDesktopMode(void);


-- | Retrieve all the video modes supported in fullscreen mode
--
-- When creating a fullscreen window, the video mode is restricted
-- to be compatible with what the graphics driver and monitor
-- support.

-- This function returns the complete list of all video
-- modes that can be used in fullscreen mode.
--
-- The returned array is sorted from best to worst, so that
-- the first element will always give the best mode (higher
-- width, height and bits-per-pixel).
getFullscreenModes :: IO [VideoMode]
getFullscreenModes = do
    alloca $ \countPtr -> do
    ptrVM <- sfVideoMode_getFullscreenModes countPtr
    count <- peek countPtr
    peekArray (fromIntegral count) ptrVM

foreign import ccall unsafe "sfVideoMode_getFullscreenModes"
    sfVideoMode_getFullscreenModes :: Ptr CUInt -> IO (Ptr VideoMode)

--CSFML_WINDOW_API const sfVideoMode* sfVideoMode_getFullscreenModes(size_t* Count);


-- | Tell whether or not a video mode is valid
--
-- The validity of video modes is only relevant when using
-- fullscreen windows; otherwise any video mode can be used
-- with no restriction.
isValid :: VideoMode -> IO Bool
isValid vm = with vm $ \ptrVm -> sfVideoMode_isValid_helper ptrVm >>= return . (/=0)

foreign import ccall unsafe "sfVideoMode_isValid_helper"
    sfVideoMode_isValid_helper :: Ptr VideoMode -> IO CChar

--CSFML_WINDOW_API sfBool sfVideoMode_isValid(sfVideoMode mode);