Skip to content

Commit 2ecfbec

Browse files
committed
Merge branch 'cloudhead-master' into master-next
2 parents 5bb09e1 + 6da586f commit 2ecfbec

File tree

10 files changed

+152
-12
lines changed

10 files changed

+152
-12
lines changed

sdl2.cabal

Lines changed: 14 additions & 2 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 no-linear
48+
description: Do not depend on 'linear' library
49+
default: False
50+
manual: True
51+
4752
library
4853
ghc-options: -Wall
4954

@@ -61,11 +66,13 @@ library
6166
SDL.Input.Mouse
6267
SDL.Power
6368
SDL.Time
69+
SDL.Vect
6470
SDL.Video
6571
SDL.Video.OpenGL
6672
SDL.Video.Renderer
6773

6874
SDL.Internal.Types
75+
SDL.Internal.Vect
6976

7077
SDL.Raw
7178
SDL.Raw.Audio
@@ -110,12 +117,17 @@ library
110117
base >= 4.7 && < 5,
111118
bytestring >= 0.10.4.0 && < 0.11,
112119
exceptions >= 0.4 && < 0.9,
113-
linear >= 1.10.1.2 && < 1.21,
114120
StateVar >= 1.1.0.0 && < 1.2,
115121
text >= 1.1.0.0 && < 1.3,
116122
transformers >= 0.2 && < 0.6,
117123
vector >= 0.10.9.0 && < 0.12
118124

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

@@ -388,7 +400,7 @@ executable twinklebear-lesson-05
388400

389401
executable audio-example
390402
if flag(examples)
391-
build-depends: base >= 4.7 && < 5, lens >= 4.4.0.2 && < 4.15, linear >= 1.10.1.2 && < 1.21, sdl2, vector
403+
build-depends: base >= 4.7 && < 5, sdl2, vector
392404
else
393405
buildable: False
394406

src/SDL.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module SDL
2323
, module SDL.Input
2424
, module SDL.Power
2525
, module SDL.Time
26+
, module SDL.Vect
2627
, module SDL.Video
2728

2829
-- * Working with State Variables
@@ -45,6 +46,7 @@ import SDL.Init
4546
import SDL.Input
4647
import SDL.Power
4748
import SDL.Time
49+
import SDL.Vect
4850
import SDL.Video
4951

5052
{- $gettingStarted

src/SDL/Event.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ import Data.Typeable
8080
import Foreign
8181
import Foreign.C
8282
import GHC.Generics (Generic)
83-
import Linear
84-
import Linear.Affine (Point(P))
83+
import SDL.Vect
8584
import SDL.Input.Keyboard
8685
import SDL.Input.Mouse
8786
import SDL.Internal.Numbered

src/SDL/Input/Joystick.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Foreign.C.Types
3030
import Foreign.Marshal.Alloc
3131
import Foreign.Storable
3232
import GHC.Generics (Generic)
33-
import Linear
33+
import SDL.Vect
3434
import SDL.Exception
3535
import SDL.Internal.Types
3636
import qualified Data.ByteString as BS

src/SDL/Input/Mouse.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ import Foreign.Marshal.Alloc
5252
import Foreign.Ptr
5353
import Foreign.Storable
5454
import GHC.Generics (Generic)
55-
import Linear
56-
import Linear.Affine
55+
import SDL.Vect
5756
import SDL.Exception
5857
import SDL.Internal.Numbered
5958
import SDL.Internal.Types (Window(Window))

src/SDL/Internal/Vect.hs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3+
4+
-- 2-D, 3-D and 4-D Vectors.
5+
-- The interface is compatible with that of the 'linear' package.
6+
module SDL.Internal.Vect
7+
( V2(..)
8+
, V3(..)
9+
, V4(..)
10+
, Point(..)
11+
) where
12+
13+
-- From the 'linear' package, (c) Edward Kmett.
14+
15+
import Control.Applicative
16+
import Foreign.Storable
17+
import Foreign.Ptr (castPtr)
18+
19+
-- | A handy wrapper to help distinguish points from vectors at the
20+
-- type level.
21+
newtype Point f a = P (f a)
22+
deriving (Show, Read, Ord, Eq, Functor, Applicative, Num, Storable)
23+
24+
-- | A 2-dimensional vector
25+
--
26+
-- >>> pure 1 :: V2 Int
27+
-- V2 1 1
28+
--
29+
-- >>> V2 1 2 + V2 3 4
30+
-- V2 4 6
31+
--
32+
-- >>> V2 1 2 * V2 3 4
33+
-- V2 3 8
34+
--
35+
-- >>> sum (V2 1 2)
36+
-- 3
37+
data V2 a = V2 !a !a
38+
deriving (Show, Read, Ord, Eq)
39+
40+
-- | A 3-dimensional vector
41+
data V3 a = V3 !a !a !a
42+
deriving (Show, Read, Ord, Eq)
43+
44+
-- | A 4-dimensional vector
45+
data V4 a = V4 !a !a !a !a
46+
deriving (Show, Read, Ord, Eq)
47+
48+
instance Functor V2 where
49+
fmap f (V2 a b) = V2 (f a) (f b)
50+
{-# INLINE fmap #-}
51+
a <$ _ = V2 a a
52+
{-# INLINE (<$) #-}
53+
54+
instance Functor V3 where
55+
fmap f (V3 a b c) = V3 (f a) (f b) (f c)
56+
{-# INLINE fmap #-}
57+
a <$ _ = V3 a a a
58+
{-# INLINE (<$) #-}
59+
60+
instance Functor V4 where
61+
fmap f (V4 a b c d) = V4 (f a) (f b) (f c) (f d)
62+
{-# INLINE fmap #-}
63+
a <$ _ = V4 a a a a
64+
{-# INLINE (<$) #-}
65+
66+
instance Applicative V2 where
67+
pure a = V2 a a
68+
V2 a b <*> V2 d e = V2 (a d) (b e)
69+
70+
instance Storable a => Storable (V4 a) where
71+
sizeOf _ = 4 * sizeOf (undefined::a)
72+
{-# INLINE sizeOf #-}
73+
alignment _ = alignment (undefined::a)
74+
{-# INLINE alignment #-}
75+
poke ptr (V4 x y z w) = do poke ptr' x
76+
pokeElemOff ptr' 1 y
77+
pokeElemOff ptr' 2 z
78+
pokeElemOff ptr' 3 w
79+
where ptr' = castPtr ptr
80+
{-# INLINE poke #-}
81+
peek ptr = V4 <$> peek ptr' <*> peekElemOff ptr' 1
82+
<*> peekElemOff ptr' 2 <*> peekElemOff ptr' 3
83+
where ptr' = castPtr ptr
84+
{-# INLINE peek #-}
85+
86+
instance Storable a => Storable (V2 a) where
87+
sizeOf _ = 2 * sizeOf (undefined::a)
88+
{-# INLINE sizeOf #-}
89+
alignment _ = alignment (undefined::a)
90+
{-# INLINE alignment #-}
91+
poke ptr (V2 x y) = poke ptr' x >> pokeElemOff ptr' 1 y
92+
where ptr' = castPtr ptr
93+
{-# INLINE poke #-}
94+
peek ptr = V2 <$> peek ptr' <*> peekElemOff ptr' 1
95+
where ptr' = castPtr ptr
96+
{-# INLINE peek #-}
97+
98+
instance Num a => Num (V2 a) where
99+
(+) = liftA2 (+)
100+
(-) = liftA2 (-)
101+
(*) = liftA2 (*)
102+
negate = fmap negate
103+
abs = fmap abs
104+
signum = fmap signum
105+
fromInteger = pure . fromInteger

src/SDL/Vect.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
-- | SDL's vector representation.
4+
--
5+
-- By default, re-exports the vector types from the 'linear' package, but this can be changed via the @-no-linear@
6+
-- build flag to export SDL's internal vector types from "SDL.Internal.Vect".
7+
-- This is useful if one does not want to incur the 'lens' dependency.
8+
module SDL.Vect
9+
( module Vect
10+
11+
-- * Vectors
12+
, V2 (..)
13+
, V3 (..)
14+
, V4 (..)
15+
16+
-- * Point
17+
, Point (..)
18+
) where
19+
20+
#if defined(nolinear)
21+
import SDL.Internal.Vect as Vect
22+
#else
23+
import Linear as Vect
24+
import Linear.Affine as Vect
25+
#endif

src/SDL/Video.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ import Data.Typeable
8787
import Foreign hiding (void, throwIfNull, throwIfNeg, throwIfNeg_)
8888
import Foreign.C
8989
import GHC.Generics (Generic)
90-
import Linear
91-
import Linear.Affine (Point(P))
90+
import SDL.Vect
9291
import SDL.Exception
9392
import SDL.Internal.Numbered
9493
import SDL.Internal.Types

src/SDL/Video/OpenGL.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Data.Typeable
3636
import Foreign hiding (void, throwIfNull, throwIfNeg, throwIfNeg_)
3737
import Foreign.C.Types
3838
import GHC.Generics (Generic)
39-
import Linear
39+
import SDL.Vect
4040
import SDL.Exception
4141
import SDL.Internal.Numbered
4242
import SDL.Internal.Types

src/SDL/Video/Renderer.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ import Foreign.Marshal.Utils
131131
import Foreign.Ptr
132132
import Foreign.Storable
133133
import GHC.Generics (Generic)
134-
import Linear
135-
import Linear.Affine (Point(P))
136134
import Prelude hiding (foldr)
135+
import SDL.Vect
137136
import SDL.Exception
138137
import SDL.Internal.Numbered
139138
import SDL.Internal.Types

0 commit comments

Comments
 (0)