1+ {-# LANGUAGE BangPatterns #-}
12{-# LANGUAGE OverloadedStrings #-}
23
34-- | Test that space usage is low and avoid GCs where possible.
45
56module Main where
67
8+ import Foreign.C (CInt)
79import SDL
10+ import SDL.Vect
811import Weigh
912
1013-- | Main entry point.
1114main :: IO ()
1215main =
16+
17+
1318 mainWith
1419 (do setColumns [Case, Allocated, GCs]
20+ -- sequence_
21+ -- [ validateAction
22+ -- ("pollEvent " ++ show i)
23+ -- pollEventTest
24+ -- i
25+ -- (\weight ->
26+ -- if weightGCs weight > 0
27+ -- then Just "Non-zero number of garbage collections!"
28+ -- else if weightAllocatedBytes weight > 2000
29+ -- then Just
30+ -- "Allocated >2KB! Allocations should be constant."
31+ -- else Nothing)
32+ -- | i <- [1, 10, 100, 1000, 10000]
33+ -- ]
34+ -- sequence_
35+ -- [ validateAction
36+ -- ("pollEvent + clear " ++ show i)
37+ -- pollEventClearTest
38+ -- i
39+ -- (\weight ->
40+ -- if weightGCs weight > 0
41+ -- then Just "Non-zero number of garbage collections!"
42+ -- else if weightAllocatedBytes weight > 3000
43+ -- then Just
44+ -- "Allocated >3KB! Allocations should be constant."
45+ -- else Nothing)
46+ -- | i <- [1, 10, 100, 1000, 10000]
47+ -- ]
48+ -- sequence_
49+ -- [ validateAction
50+ -- ("pollEvent + present " ++ show i)
51+ -- pollEventPresentTest
52+ -- i
53+ -- (\weight ->
54+ -- if weightGCs weight > 0
55+ -- then Just "Non-zero number of garbage collections!"
56+ -- else if weightAllocatedBytes weight > 4000
57+ -- then Just
58+ -- "Allocated >4KB! Allocations should be constant."
59+ -- else Nothing)
60+ -- | i <- [1, 10, 100, 1000]
61+ -- ]
62+ -- sequence_
63+ -- [ validateAction
64+ -- ("pollEvent + drawColor " ++ show i)
65+ -- pollEventDrawColorTest
66+ -- i
67+ -- (\weight ->
68+ -- if weightGCs weight > 0
69+ -- then Just "Non-zero number of garbage collections!"
70+ -- else if weightAllocatedBytes weight > 4000
71+ -- then Just
72+ -- "Allocated >KB! Allocations should be constant."
73+ -- else Nothing)
74+ -- | i <- [1, 10, 100, 1000, 2000]
75+ -- ]
76+ -- sequence_
77+ -- [ validateAction
78+ -- ("pollEvent + drawRect " ++ show i)
79+ -- pollEventDrawRectTest
80+ -- i
81+ -- (\weight ->
82+ -- if weightGCs weight > 0
83+ -- then Just "Non-zero number of garbage collections!"
84+ -- else if weightAllocatedBytes weight > 4000
85+ -- then Just
86+ -- "Allocated >4KB! Allocations should be constant."
87+ -- else Nothing)
88+ -- | i <- [1, 10, 100, 1000]
89+ -- ]
1590 sequence_
1691 [ validateAction
17- ("pollEvent " ++ show i)
18- pollEventTest
19- i
20- (\weight ->
21- if weightGCs weight > 0
22- then Just "Non-zero number of garbage collections!"
23- else if weightAllocatedBytes weight > 2000
24- then Just
25- "Allocated >2KB! Allocations should be constant."
26- else Nothing)
27- | i <- [1, 10, 100, 1000, 10000]
28- ]
29- sequence_
30- [ validateAction
31- ("pollEvent + clear " ++ show i)
32- pollEventClearTest
33- i
34- (\weight ->
35- if weightGCs weight > 0
36- then Just "Non-zero number of garbage collections!"
37- else if weightAllocatedBytes weight > 3000
38- then Just
39- "Allocated >3KB! Allocations should be constant."
40- else Nothing)
41- | i <- [1, 10, 100, 1000, 10000]
42- ]
43- sequence_
44- [ validateAction
45- ("pollEvent + present " ++ show i)
46- pollEventPresentTest
92+ ("animated rect " ++ show i)
93+ pollEventAnimRectTest
4794 i
4895 (\weight ->
4996 if weightGCs weight > 0
@@ -52,36 +99,8 @@ main =
5299 then Just
53100 "Allocated >4KB! Allocations should be constant."
54101 else Nothing)
55- | i <- [1, 10, 100, 1000]
56- ]
57- sequence_
58- [ validateAction
59- ("pollEvent + drawColor " ++ show i)
60- pollEventDrawColorTest
61- i
62- (\weight ->
63- if weightGCs weight > 0
64- then Just "Non-zero number of garbage collections!"
65- else if weightAllocatedBytes weight > 4000
66- then Just
67- "Allocated >KB! Allocations should be constant."
68- else Nothing)
69102 | i <- [1, 10, 100, 1000, 2000]
70103 ]
71- sequence_
72- [ validateAction
73- ("pollEvent + drawRect " ++ show i)
74- pollEventDrawRectTest
75- i
76- (\weight ->
77- if weightGCs weight > 0
78- then Just "Non-zero number of garbage collections!"
79- else if weightAllocatedBytes weight > 4000
80- then Just
81- "Allocated >4KB! Allocations should be constant."
82- else Nothing)
83- | i <- [1, 10, 100, 1000]
84- ]
85104 )
86105
87106-- | Test that merely polling does not allocate or engage the GC.
@@ -161,3 +180,52 @@ pollEventDrawRectTest iters = do
161180 present renderer
162181 go (i - 1)
163182 go iters
183+
184+ --------------------------------------------------------------------------------
185+ -- Animated rect test
186+
187+ data State = State
188+ { stateI :: !CInt
189+ , stateV :: !(V2 CInt)
190+ , stateP :: !(V2 CInt)
191+ }
192+
193+ -- | Animate a rectangle on the screen for n iterations.
194+ pollEventAnimRectTest :: CInt -> IO ()
195+ pollEventAnimRectTest iters = do
196+ initializeAll
197+ window <-
198+ createWindow
199+ "pollEventAnimRectTest"
200+ defaultWindow {windowInitialSize = defaultWindowSize}
201+ renderer <- createRenderer window (-1) defaultRenderer
202+ let go :: State -> IO ()
203+ go !(State 0 _ _) = pure ()
204+ go !(State i (V2 xv yv) p@(V2 x y)) = do
205+ _ <- pollEvent
206+ rendererDrawColor renderer $= V4 40 40 40 255
207+ clear renderer
208+ rendererDrawColor renderer $= V4 255 255 255 255
209+ let xv'
210+ | x + w > mw = -xv
211+ | x < 0 = -xv
212+ | otherwise = xv
213+ yv'
214+ | y + h > mh = -yv
215+ | y < 0 = -yv
216+ | otherwise = yv
217+ v' = V2 xv' yv'
218+ p' = p + v'
219+ fillRect renderer (Just (Rectangle (P p') (V2 w h)))
220+ present renderer
221+ go (State (i - 1) v' p')
222+ go (State iters (V2 2 1) (V2 0 0))
223+ where
224+ defaultWindowSize :: V2 CInt
225+ defaultWindowSize = V2 800 600
226+ mw :: CInt
227+ mh :: CInt
228+ V2 mw mh = defaultWindowSize
229+ w :: CInt
230+ h :: CInt
231+ (w, h) = (100, 100)
0 commit comments