File: Utilities.hs

package info (click to toggle)
haskell-sdl 0.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 376 kB
  • ctags: 3
  • sloc: haskell: 200; ansic: 18; makefile: 13
file content (61 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-----------------------------------------------------------------------------
-- |
-- 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 err action
    = fmap (err/=) action

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

fromBitmask :: (Bounded a,Enum a b,Bits b,Num b) => b -> [a]
fromBitmask mask = foldr worker [] lst
    where lst = enumFromTo minBound maxBound
          worker v
              = if (mask .&. fromEnum v) /= 0
                   then (:) v
                   else 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 = fromIntegral

toCInt :: Int -> CInt
toCInt = fromIntegral