module SFML.System.Vector2
(
Vec2i(..)
, Vec2u(..)
, Vec2f(..)
)
where
import Control.Applicative ((<$>), (<*>))
import Data.Word
import Foreign.C.Types
import Foreign.Storable
sizeFloat = (4)
sizeInt = (4)
data Vec2i = Vec2i !Int !Int deriving Show
instance Storable Vec2i where
sizeOf _ = 2*sizeInt
alignment _ = alignment (undefined :: CInt)
peek ptr = Vec2i
<$> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 0) ptr :: IO CInt)
<*> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 4) ptr :: IO CInt)
poke ptr (Vec2i x y) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr (fromIntegral x :: CInt)
(\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr (fromIntegral y :: CInt)
instance Num Vec2i where
Vec2i ax ay + Vec2i bx by = Vec2i (ax + bx) (ay + by)
Vec2i ax ay Vec2i bx by = Vec2i (ax bx) (ay by)
Vec2i ax ay * Vec2i bx by = Vec2i (ax * bx) (ay * by)
abs (Vec2i ax ay) = Vec2i (abs ax) (abs ay)
signum (Vec2i ax ay) = Vec2i (signum ax) (signum ay)
fromInteger i = Vec2i i' i' where i' = fromInteger i
data Vec2u = Vec2u !Word !Word deriving Show
instance Storable Vec2u where
sizeOf _ = 2*sizeInt
alignment _ = alignment (undefined :: CUInt)
peek ptr = Vec2u
<$> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 0) ptr :: IO CUInt)
<*> fmap fromIntegral ((\hsc_ptr -> peekByteOff hsc_ptr 4) ptr :: IO CUInt)
poke ptr (Vec2u x y) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr (fromIntegral x :: CUInt)
(\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr (fromIntegral y :: CUInt)
instance Num Vec2u where
Vec2u ax ay + Vec2u bx by = Vec2u (ax + bx) (ay + by)
Vec2u ax ay Vec2u bx by = Vec2u (ax bx) (ay by)
Vec2u ax ay * Vec2u bx by = Vec2u (ax * bx) (ay * by)
abs (Vec2u ax ay) = Vec2u (abs ax) (abs ay)
signum (Vec2u ax ay) = Vec2u (signum ax) (signum ay)
fromInteger i = Vec2u i' i' where i' = fromInteger i
data Vec2f = Vec2f !Float !Float deriving Show
instance Storable Vec2f where
sizeOf _ = 2*sizeFloat
alignment _ = alignment (undefined :: CFloat)
peek ptr = Vec2f
<$> fmap realToFrac ((\hsc_ptr -> peekByteOff hsc_ptr 0) ptr :: IO CFloat)
<*> fmap realToFrac ((\hsc_ptr -> peekByteOff hsc_ptr 4) ptr :: IO CFloat)
poke ptr (Vec2f x y) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr (realToFrac x :: CFloat)
(\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr (realToFrac y :: CFloat)
instance Num Vec2f where
Vec2f ax ay + Vec2f bx by = Vec2f (ax + bx) (ay + by)
Vec2f ax ay Vec2f bx by = Vec2f (ax bx) (ay by)
Vec2f ax ay * Vec2f bx by = Vec2f (ax * bx) (ay * by)
abs (Vec2f ax ay) = Vec2f (abs ax) (abs ay)
signum (Vec2f ax ay) = Vec2f (signum ax) (signum ay)
fromInteger i = Vec2f i' i' where i' = fromInteger i
instance Fractional Vec2f where
Vec2f ax ay / Vec2f bx by = Vec2f (ax / bx) (ay / by)
fromRational r = Vec2f r' r' where r' = fromRational r