|
1 | | -{-# LANGUAGE ScopedTypeVariables #-} |
| 1 | +{-# LANGUAGE CPP #-} |
2 | 2 | module SDL.Vect |
3 | | - ( V2(..) |
4 | | - , V3(..) |
5 | | - , V4(..) |
6 | | - ) where |
7 | | - |
8 | | --- Copied from the 'linear' package by Edward Kmett. |
9 | | - |
10 | | -import Control.Applicative (liftA2) |
11 | | -import Foreign.Storable |
12 | | -import Foreign.Ptr (castPtr) |
13 | | - |
14 | | -data V2 a = V2 !a !a |
15 | | - deriving (Show, Read, Ord, Eq) |
16 | | - |
17 | | -data V3 a = V3 !a !a !a |
18 | | - deriving (Show, Read, Ord, Eq) |
19 | | - |
20 | | -data V4 a = V4 !a !a !a !a |
21 | | - deriving (Show, Read, Ord, Eq) |
22 | | - |
23 | | -instance Functor V2 where |
24 | | - fmap f (V2 a b) = V2 (f a) (f b) |
25 | | - {-# INLINE fmap #-} |
26 | | - a <$ _ = V2 a a |
27 | | - {-# INLINE (<$) #-} |
28 | | - |
29 | | -instance Functor V3 where |
30 | | - fmap f (V3 a b c) = V3 (f a) (f b) (f c) |
31 | | - {-# INLINE fmap #-} |
32 | | - a <$ _ = V3 a a a |
33 | | - {-# INLINE (<$) #-} |
34 | | - |
35 | | -instance Functor V4 where |
36 | | - fmap f (V4 a b c d) = V4 (f a) (f b) (f c) (f d) |
37 | | - {-# INLINE fmap #-} |
38 | | - a <$ _ = V4 a a a a |
39 | | - {-# INLINE (<$) #-} |
40 | | - |
41 | | -instance Applicative V2 where |
42 | | - pure a = V2 a a |
43 | | - V2 a b <*> V2 d e = V2 (a d) (b e) |
44 | | - |
45 | | -instance Storable a => Storable (V4 a) where |
46 | | - sizeOf _ = 4 * sizeOf (undefined::a) |
47 | | - {-# INLINE sizeOf #-} |
48 | | - alignment _ = alignment (undefined::a) |
49 | | - {-# INLINE alignment #-} |
50 | | - poke ptr (V4 x y z w) = do poke ptr' x |
51 | | - pokeElemOff ptr' 1 y |
52 | | - pokeElemOff ptr' 2 z |
53 | | - pokeElemOff ptr' 3 w |
54 | | - where ptr' = castPtr ptr |
55 | | - {-# INLINE poke #-} |
56 | | - peek ptr = V4 <$> peek ptr' <*> peekElemOff ptr' 1 |
57 | | - <*> peekElemOff ptr' 2 <*> peekElemOff ptr' 3 |
58 | | - where ptr' = castPtr ptr |
59 | | - {-# INLINE peek #-} |
60 | | - |
61 | | -instance Storable a => Storable (V2 a) where |
62 | | - sizeOf _ = 2 * sizeOf (undefined::a) |
63 | | - {-# INLINE sizeOf #-} |
64 | | - alignment _ = alignment (undefined::a) |
65 | | - {-# INLINE alignment #-} |
66 | | - poke ptr (V2 x y) = poke ptr' x >> pokeElemOff ptr' 1 y |
67 | | - where ptr' = castPtr ptr |
68 | | - {-# INLINE poke #-} |
69 | | - peek ptr = V2 <$> peek ptr' <*> peekElemOff ptr' 1 |
70 | | - where ptr' = castPtr ptr |
71 | | - {-# INLINE peek #-} |
72 | | - |
73 | | -instance Num a => Num (V2 a) where |
74 | | - (+) = liftA2 (+) |
75 | | - (-) = liftA2 (-) |
76 | | - (*) = liftA2 (*) |
77 | | - negate = fmap negate |
78 | | - abs = fmap abs |
79 | | - signum = fmap signum |
80 | | - fromInteger = pure . fromInteger |
| 3 | + ( module Vect |
| 4 | + ) where |
| 5 | + |
| 6 | +#if defined(nolinear) |
| 7 | +import SDL.Internal.Vect as Vect |
| 8 | +#else |
| 9 | +import Linear as Vect |
| 10 | +import Linear.Affine as Vect |
| 11 | +#endif |
0 commit comments