Skip to content

Commit 80fdf84

Browse files
committed
Make 'linear' dependency optional
Via 'nolinear' flag.
1 parent 5ffb121 commit 80fdf84

File tree

3 files changed

+102
-79
lines changed

3 files changed

+102
-79
lines changed

sdl2.cabal

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ flag examples
4444
description: Build examples
4545
default: False
4646

47+
flag nolinear
48+
description: Do not depend on 'linear' library
49+
default: False
50+
manual: True
51+
4752
library
4853
ghc-options: -Wall
4954

@@ -86,6 +91,7 @@ library
8691
other-modules:
8792
Data.Bitmask
8893
SDL.Internal.Numbered
94+
SDL.Internal.Vect
8995
SDL.Exception
9096

9197
hs-source-dirs:
@@ -116,6 +122,12 @@ library
116122
transformers >= 0.2 && < 0.6,
117123
vector >= 0.10.9.0 && < 0.12
118124

125+
if flag(nolinear)
126+
cpp-options: -Dnolinear
127+
else
128+
build-depends:
129+
linear >= 1.10.1.2 && < 1.21
130+
119131
default-language:
120132
Haskell2010
121133

src/SDL/Internal/Vect.hs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
module SDL.Internal.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

src/SDL/Vect.hs

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,11 @@
1-
{-# LANGUAGE ScopedTypeVariables #-}
1+
{-# LANGUAGE CPP #-}
22
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

Comments
 (0)