Skip to content

Commit 5ffb121

Browse files
committed
Remove 'linear' package dependency from library
By doing this, we remove the lens dependency too, and a few dozen other packages. The vector functionality needed (which is very little) is in SDL.Vect. There is no reason that a low-level general purpose package such as SDL incur a lens depndency.
1 parent c2c10ea commit 5ffb121

File tree

9 files changed

+131
-53
lines changed

9 files changed

+131
-53
lines changed

sdl2.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ library
6161
SDL.Input.Mouse
6262
SDL.Power
6363
SDL.Time
64+
SDL.Vect
6465
SDL.Video
6566
SDL.Video.OpenGL
6667
SDL.Video.Renderer
@@ -110,7 +111,6 @@ library
110111
base >= 4.7 && < 5,
111112
bytestring >= 0.10.4.0 && < 0.11,
112113
exceptions >= 0.4 && < 0.9,
113-
linear >= 1.10.1.2 && < 1.21,
114114
StateVar >= 1.1.0.0 && < 1.2,
115115
text >= 1.1.0.0 && < 1.3,
116116
transformers >= 0.2 && < 0.6,
@@ -388,7 +388,7 @@ executable twinklebear-lesson-05
388388

389389
executable audio-example
390390
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
391+
build-depends: base >= 4.7 && < 5, sdl2, vector
392392
else
393393
buildable: False
394394

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: 13 additions & 14 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
@@ -170,7 +169,7 @@ data WindowExposedEventData =
170169
data WindowMovedEventData =
171170
WindowMovedEventData {windowMovedEventWindow :: Window
172171
-- ^ The associated 'Window'.
173-
,windowMovedEventPosition :: Point V2 Int32
172+
,windowMovedEventPosition :: V2 Int32
174173
-- ^ The new position of the 'Window'.
175174
}
176175
deriving (Eq,Ord,Generic,Show,Typeable)
@@ -290,7 +289,7 @@ data MouseMotionEventData =
290289
-- ^ The 'MouseDevice' that was moved.
291290
,mouseMotionEventState :: [MouseButton]
292291
-- ^ A collection of 'MouseButton's that are currently held down.
293-
,mouseMotionEventPos :: Point V2 Int32
292+
,mouseMotionEventPos :: V2 Int32
294293
-- ^ The new position of the mouse.
295294
,mouseMotionEventRelMotion :: V2 Int32
296295
-- ^ The relative mouse motion of the mouse.
@@ -309,7 +308,7 @@ data MouseButtonEventData =
309308
-- ^ The button that was pressed or released.
310309
,mouseButtonEventClicks :: Word8
311310
-- ^ The amount of clicks. 1 for a single-click, 2 for a double-click, etc.
312-
,mouseButtonEventPos :: Point V2 Int32
311+
,mouseButtonEventPos :: V2 Int32
313312
-- ^ The coordinates of the mouse click.
314313
}
315314
deriving (Eq,Ord,Generic,Show,Typeable)
@@ -429,7 +428,7 @@ data TouchFingerEventData =
429428
-- ^ The touch device index.
430429
,touchFingerEventFingerID :: Raw.FingerID
431430
-- ^ The finger index.
432-
,touchFingerEventPos :: Point V2 CFloat
431+
,touchFingerEventPos :: V2 CFloat
433432
-- ^ The location of the touch event, normalized between 0 and 1.
434433
,touchFingerEventRelMotion :: V2 CFloat
435434
-- ^ The distance moved, normalized between -1 and 1.
@@ -446,7 +445,7 @@ data MultiGestureEventData =
446445
-- ^ The amount that the fingers rotated during this motion.
447446
,multiGestureEventDDist :: CFloat
448447
-- ^ The amount that the fingers pinched during this motion.
449-
,multiGestureEventPos :: Point V2 CFloat
448+
,multiGestureEventPos :: V2 CFloat
450449
-- ^ The normalized center of the gesture.
451450
,multiGestureEventNumFingers :: Word16
452451
-- ^ The number of fingers used in this gesture.
@@ -463,7 +462,7 @@ data DollarGestureEventData =
463462
-- ^ The number of fingers used to draw the stroke.
464463
,dollarGestureEventError :: CFloat
465464
-- ^ The difference between the gesture template and the actual performed gesture (lower errors correspond to closer matches).
466-
,dollarGestureEventPos :: Point V2 CFloat
465+
,dollarGestureEventPos :: V2 CFloat
467466
-- ^ The normalized center of the gesture.
468467
}
469468
deriving (Eq,Ord,Generic,Show,Typeable)
@@ -514,7 +513,7 @@ convertRaw (Raw.WindowEvent t ts a b c d) =
514513
Raw.SDL_WINDOWEVENT_MOVED ->
515514
WindowMovedEvent
516515
(WindowMovedEventData w'
517-
(P (V2 c d)))
516+
((V2 c d)))
518517
Raw.SDL_WINDOWEVENT_RESIZED ->
519518
WindowResizedEvent
520519
(WindowResizedEventData w'
@@ -583,7 +582,7 @@ convertRaw (Raw.MouseMotionEvent _ ts a b c d e f g) =
583582
(MouseMotionEventData w'
584583
(fromNumber b)
585584
buttons
586-
(P (V2 d e))
585+
((V2 d e))
587586
(V2 f g))))
588587
where mask `test` x =
589588
if mask .&. x /= 0
@@ -609,7 +608,7 @@ convertRaw (Raw.MouseButtonEvent t ts a b c _ e f g) =
609608
(fromNumber b)
610609
button
611610
e
612-
(P (V2 f g)))))
611+
((V2 f g)))))
613612
convertRaw (Raw.MouseWheelEvent _ ts a b c d) =
614613
do w' <- fmap Window (Raw.getWindowFromID a)
615614
return (Event ts
@@ -649,7 +648,7 @@ convertRaw (Raw.TouchFingerEvent _ ts a b c d e f g) =
649648
(TouchFingerEvent
650649
(TouchFingerEventData a
651650
b
652-
(P (V2 c d))
651+
((V2 c d))
653652
(V2 e f)
654653
g)))
655654
convertRaw (Raw.MultiGestureEvent _ ts a b c d e f) =
@@ -658,7 +657,7 @@ convertRaw (Raw.MultiGestureEvent _ ts a b c d e f) =
658657
(MultiGestureEventData a
659658
b
660659
c
661-
(P (V2 d e))
660+
((V2 d e))
662661
f)))
663662
convertRaw (Raw.DollarGestureEvent _ ts a b c d e f) =
664663
return (Event ts
@@ -667,7 +666,7 @@ convertRaw (Raw.DollarGestureEvent _ ts a b c d e f) =
667666
b
668667
c
669668
d
670-
(P (V2 e f)))))
669+
((V2 e f)))))
671670
convertRaw (Raw.DropEvent _ ts a) =
672671
return (Event ts (DropEvent (DropEventData a)))
673672
convertRaw (Raw.ClipboardUpdateEvent _ ts) =

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: 14 additions & 15 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))
@@ -85,7 +84,7 @@ getMouseLocationMode = do
8584
return $ if relativeMode then RelativeLocation else AbsoluteLocation
8685

8786
-- | Return proper mouse location depending on mouse mode
88-
getModalMouseLocation :: MonadIO m => m (LocationMode, Point V2 CInt)
87+
getModalMouseLocation :: MonadIO m => m (LocationMode, V2 CInt)
8988
getModalMouseLocation = do
9089
mode <- getMouseLocationMode
9190
location <- case mode of
@@ -106,7 +105,7 @@ getRelativeMouseMode :: MonadIO m => m Bool
106105
getRelativeMouseMode = Raw.getRelativeMouseMode
107106

108107
--deprecated
109-
getMouseLocation :: MonadIO m => m (Point V2 CInt)
108+
getMouseLocation :: MonadIO m => m (V2 CInt)
110109
{-# DEPRECATED getMouseLocation "Use getAbsoluteMouseLocation instead, or getModalMouseLocation to match future behavior." #-}
111110
getMouseLocation = getAbsoluteMouseLocation
112111

@@ -139,9 +138,9 @@ data WarpMouseOrigin
139138
deriving (Data, Eq, Generic, Ord, Show, Typeable)
140139

141140
-- | Move the current location of a mouse pointer. The 'WarpMouseOrigin' specifies the origin for the given warp coordinates.
142-
warpMouse :: MonadIO m => WarpMouseOrigin -> Point V2 CInt -> m ()
143-
warpMouse (WarpInWindow (Window w)) (P (V2 x y)) = Raw.warpMouseInWindow w x y
144-
warpMouse WarpCurrentFocus (P (V2 x y)) = Raw.warpMouseInWindow nullPtr x y
141+
warpMouse :: MonadIO m => WarpMouseOrigin -> V2 CInt -> m ()
142+
warpMouse (WarpInWindow (Window w)) ((V2 x y)) = Raw.warpMouseInWindow w x y
143+
warpMouse WarpCurrentFocus ((V2 x y)) = Raw.warpMouseInWindow nullPtr x y
145144

146145
-- | Get or set whether the cursor is currently visible.
147146
--
@@ -160,20 +159,20 @@ cursorVisible = makeStateVar getCursorVisible setCursorVisible
160159
getCursorVisible = (== 1) <$> Raw.showCursor (-1)
161160

162161
-- | Retrieve the current location of the mouse, relative to the currently focused window.
163-
getAbsoluteMouseLocation :: MonadIO m => m (Point V2 CInt)
162+
getAbsoluteMouseLocation :: MonadIO m => m (V2 CInt)
164163
getAbsoluteMouseLocation = liftIO $
165164
alloca $ \x ->
166165
alloca $ \y -> do
167166
_ <- Raw.getMouseState x y -- We don't deal with button states here
168-
P <$> (V2 <$> peek x <*> peek y)
167+
(V2 <$> peek x <*> peek y)
169168

170169
-- | Retrieve mouse motion
171-
getRelativeMouseLocation :: MonadIO m => m (Point V2 CInt)
170+
getRelativeMouseLocation :: MonadIO m => m (V2 CInt)
172171
getRelativeMouseLocation = liftIO $
173172
alloca $ \x ->
174173
alloca $ \y -> do
175174
_ <- Raw.getRelativeMouseState x y
176-
P <$> (V2 <$> peek x <*> peek y)
175+
(V2 <$> peek x <*> peek y)
177176

178177

179178
-- | Retrieve a mapping of which buttons are currently held down.
@@ -217,9 +216,9 @@ createCursor :: MonadIO m
217216
=> V.Vector Bool -- ^ Whether this part of the cursor is black. Use 'False' for white and 'True' for black.
218217
-> V.Vector Bool -- ^ Whether or not pixels are visible. Use 'True' for visible and 'False' for transparent.
219218
-> V2 CInt -- ^ The width and height of the cursor.
220-
-> Point V2 CInt -- ^ The X- and Y-axis location of the upper left corner of the cursor relative to the actual mouse position
219+
-> V2 CInt -- ^ The X- and Y-axis location of the upper left corner of the cursor relative to the actual mouse position
221220
-> m Cursor
222-
createCursor dta msk (V2 w h) (P (V2 hx hy)) =
221+
createCursor dta msk (V2 w h) ((V2 hx hy)) =
223222
liftIO . fmap Cursor $
224223
throwIfNull "SDL.Input.Mouse.createCursor" "SDL_createCursor" $
225224
V.unsafeWith (V.map (bool 0 1) dta) $ \unsafeDta ->
@@ -237,9 +236,9 @@ freeCursor = Raw.freeCursor . unwrapCursor
237236
-- See @<https://wiki.libsdl.org/SDL_CreateColorCursor SDL_CreateColorCursor>@ for C documentation.
238237
createColorCursor :: MonadIO m
239238
=> Surface
240-
-> Point V2 CInt -- ^ The location of the cursor hot spot
239+
-> V2 CInt -- ^ The location of the cursor hot spot
241240
-> m Cursor
242-
createColorCursor (Surface surfPtr _) (P (V2 hx hy)) =
241+
createColorCursor (Surface surfPtr _) ((V2 hx hy)) =
243242
liftIO . fmap Cursor $
244243
throwIfNull "SDL.Input.Mouse.createColorCursor" "SDL_createColorCursor" $
245244
Raw.createColorCursor surfPtr hx hy

src/SDL/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.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

0 commit comments

Comments
 (0)