module HGamer3D.Bindings.CEGUI.Utils where
import Foreign
import Foreign.Ptr
import Foreign.C
import HGamer3D.Data.HG3DClass
import HGamer3D.Data.Vector
import HGamer3D.Data.Colour
import HGamer3D.Data.Angle
import Control.Monad (liftM)
withCStringLenIntConv :: Num n => String -> ((CString, n) -> IO a) -> IO a
withCStringLenIntConv s f = withCStringLen s $ \(p, n) -> f (p, fromIntegral n)
peekCStringLenIntConv :: Integral n => (CString, n) -> IO String
peekCStringLenIntConv (s, n) = peekCStringLen (s, fromIntegral n)
withIntConv :: (Storable b, Integral a, Integral b)
=> a -> (Ptr b -> IO c) -> IO c
withIntConv = with . fromIntegral
withFloatConv :: (Storable b, RealFloat a, RealFloat b)
=> a -> (Ptr b -> IO c) -> IO c
withFloatConv = with . realToFrac
peekIntConv :: (Storable a, Integral a, Integral b)
=> Ptr a -> IO b
peekIntConv = liftM fromIntegral . peek
peekFloatConv :: (Storable a, RealFloat a, RealFloat b)
=> Ptr a -> IO b
peekFloatConv = liftM realToFrac . peek
withCUString b f = withCWString b (f . castPtr)
peekCUString = peekCWString . castPtr
alloc64k = allocaBytes (1024 * 64)
cIntFromEnum :: Enum a => a -> CInt
cIntFromEnum = cIntConv . fromEnum
cIntToEnum :: Enum a => CInt -> a
cIntToEnum = toEnum . cIntConv
withBoolUtil :: (Integral a, Storable a) => Bool -> (Ptr a -> IO b) -> IO b
withBoolUtil = with . fromBool
peekBoolUtil :: (Integral a, Storable a) => Ptr a -> IO Bool
peekBoolUtil = liftM toBool . peek
withEnumUtil :: (Enum a, Integral b, Storable b) => a -> (Ptr b -> IO c) -> IO c
withEnumUtil = with . cFromEnum
peekEnumUtil :: (Enum a, Integral b, Storable b) => Ptr b -> IO a
peekEnumUtil = liftM cToEnum . peek
cIntConv :: (Integral a, Integral b) => a -> b
cIntConv = fromIntegral
cFloatConv :: (RealFloat a, RealFloat b) => a -> b
cFloatConv = realToFrac
cToEnum :: (Integral i, Enum e) => i -> e
cToEnum = toEnum . fromIntegral
cFromEnum :: (Enum e, Integral i) => e -> i
cFromEnum = fromIntegral . fromEnum