-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.UI.SDL.General
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-- Various small functions which makes the binding process easier.
-----------------------------------------------------------------------------
module Graphics.UI.SDL.Utilities where

import Foreign (Bits((.|.), (.&.)))
import Foreign.C (CInt)

import Prelude hiding (Enum(..))

class Enum a b | a -> b where
  succ :: a -> a
  pred :: a -> a
  toEnum :: b -> a
  fromEnum :: a -> b
  enumFromTo :: a -> a -> [a]



intToBool :: Int -> IO Int -> IO Bool
intToBool :: Int -> IO Int -> IO Bool
intToBool err :: Int
err action :: IO Int
action
    = (Int -> Bool) -> IO Int -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int
errInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/=) IO Int
action

toBitmask :: (Enum a b,Bits b,Num b) => [a] -> b
toBitmask :: [a] -> b
toBitmask = (b -> b -> b) -> b -> [b] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr b -> b -> b
forall a. Bits a => a -> a -> a
(.|.) 0 ([b] -> b) -> ([a] -> [b]) -> [a] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
forall a b. Enum a b => a -> b
fromEnum

fromBitmask :: (Bounded a,Enum a b,Bits b,Num b) => b -> [a]
fromBitmask :: b -> [a]
fromBitmask mask :: b
mask = (a -> [a] -> [a]) -> [a] -> [a] -> [a]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> [a] -> [a]
forall a. Enum a b => a -> [a] -> [a]
worker [] [a]
lst
    where lst :: [a]
lst = a -> a -> [a]
forall a b. Enum a b => a -> a -> [a]
enumFromTo a
forall a. Bounded a => a
minBound a
forall a. Bounded a => a
maxBound
          worker :: a -> [a] -> [a]
worker v :: a
v
              = if (b
mask b -> b -> b
forall a. Bits a => a -> a -> a
.&. a -> b
forall a b. Enum a b => a -> b
fromEnum a
v) b -> b -> Bool
forall a. Eq a => a -> a -> Bool
/= 0
                   then (:) a
v
                   else [a] -> [a]
forall a. a -> a
id
{-
toBitmaskW :: (UnsignedEnum a) => [a] -> Word32
toBitmaskW = foldr (.|.) 0 . map fromEnumW

fromBitmaskW :: (Bounded a,UnsignedEnum a) => Word32 -> [a]
fromBitmaskW mask = foldr worker [] lst
    where lst = enumFromToW minBound maxBound
          worker v
              = if (mask .&. fromEnumW v) /= 0
                   then (:) v
                   else id

-}

fromCInt :: Num a => CInt -> a
fromCInt :: CInt -> a
fromCInt = CInt -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral

toCInt :: Int -> CInt
toCInt :: Int -> CInt
toCInt = Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral