File: Space.hs

package info (click to toggle)
haskell-sdl2 2.5.5.0-2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 17,348 kB
  • sloc: haskell: 10,160; ansic: 102; makefile: 5
file content (337 lines) | stat: -rw-r--r-- 10,866 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Test that space usage is low and avoid GCs where possible.

module Main where

import qualified Data.Vector.Storable as SV
import qualified Data.Vector.Storable.Mutable as SVM
import           Foreign.C (CInt)
import           Foreign.Ptr
import           Foreign.Storable
import           SDL
import           Weigh

-- | Main entry point.
main :: IO ()
main =
  mainWith
    (do setColumns [Case, Allocated, GCs]
        wgroup
          "pollEvent"
          (sequence_
             [ validateAction
               ("pollEvent " ++ show i)
               pollEventTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 2000
                           then Just
                                  "Allocated >2KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000, 10000]
             ])
        wgroup
          "pollEvent+clear"
          (sequence_
             [ validateAction
               ("pollEvent + clear " ++ show i)
               pollEventClearTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 4000
                           then Just
                                  "Allocated >4KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000, 10000]
             ])
        wgroup
          "pollEvent+present"
          (sequence_
             [ validateAction
               ("pollEvent + present " ++ show i)
               pollEventPresentTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 4000
                           then Just
                                  "Allocated >4KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000]
             ])
        wgroup
          "pollEvent+drawColor"
          (sequence_
             [ validateAction
               ("pollEvent + drawColor " ++ show i)
               pollEventDrawColorTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 4000
                           then Just
                                  "Allocated >KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000, 2000]
             ])
        wgroup
          "pollEvent+drawRect"
          (sequence_
             [ validateAction
               ("pollEvent + drawRect " ++ show i)
               pollEventDrawRectTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 4000
                           then Just
                                  "Allocated >4KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000]
             ])
        wgroup
          "animated rect"
          (sequence_
             [ validateAction
               ("animated rect " ++ show i)
               pollEventAnimRectTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 4000
                           then Just
                                  "Allocated >4KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000, 2000]
             ])
        wgroup
          "animated rects"
          (sequence_
             [ validateAction
               ("animated rects " ++ show i)
               pollEventAnimRectsTest
               i
               (\weight ->
                  if weightGCs weight > 0
                    then Just "Non-zero number of garbage collections!"
                    else if weightAllocatedBytes weight > 5000
                           then Just
                                  "Allocated >4KB! Allocations should be constant."
                           else Nothing)
             | i <- [1, 10, 100, 1000, 2000, 3000]
             ]))

-- | Test that merely polling does not allocate or engage the GC.
-- <https://github.com/haskell-game/sdl2/issues/178>
pollEventTest :: Int -> IO ()
pollEventTest iters = do
  initializeAll
  let go :: Int -> IO ()
      go 0 = pure ()
      go i = do
        _ <- pollEvent
        go (i - 1)
  go iters

-- | Test that merely polling and clearing the screen does not
-- allocate or engage the GC.
pollEventClearTest :: Int -> IO ()
pollEventClearTest iters = do
  initializeAll
  window <- createWindow "pollEventClearTest" defaultWindow
  renderer <- createRenderer window (-1) defaultRenderer
  let go :: Int -> IO ()
      go 0 = pure ()
      go i = do
        _ <- pollEvent
        clear renderer
        go (i - 1)
  go iters

-- | Test that merely polling and presenting does not allocate or
-- engage the GC.
pollEventPresentTest :: Int -> IO ()
pollEventPresentTest iters = do
  initializeAll
  window <- createWindow "pollEventPresentTest" defaultWindow
  renderer <- createRenderer window (-1) defaultRenderer
  let go :: Int -> IO ()
      go 0 = pure ()
      go i = do
        _ <- pollEvent
        clear renderer
        present renderer
        go (i - 1)
  go iters

-- | Test that merely polling and drawColoring does not allocate or
-- engage the GC.
pollEventDrawColorTest :: Int -> IO ()
pollEventDrawColorTest iters = do
  initializeAll
  window <- createWindow "pollEventDrawColorTest" defaultWindow
  renderer <- createRenderer window (-1) defaultRenderer
  let go :: Int -> IO ()
      go 0 = pure ()
      go i = do
        _ <- pollEvent
        rendererDrawColor renderer $= V4 0 0 255 255
        clear renderer
        present renderer
        go (i - 1)
  go iters

-- | Draw a rectangle on screen.
pollEventDrawRectTest :: Int -> IO ()
pollEventDrawRectTest iters = do
  initializeAll
  window <- createWindow "pollEventDrawRectTest" defaultWindow
  renderer <- createRenderer window (-1) defaultRenderer
  let go :: Int -> IO ()
      go 0 = pure ()
      go i = do
        _ <- pollEvent
        rendererDrawColor renderer $= V4 40 40 40 255
        clear renderer
        rendererDrawColor renderer $= V4 255 255 255 255
        fillRect renderer (Just (Rectangle (P (V2 40 40)) (V2 80 80)))
        present renderer
        go (i - 1)
  go iters

--------------------------------------------------------------------------------
-- Animated rect test

data State = State
  { stateI :: !CInt
  , stateV :: !(V2 CInt)
  , stateP :: !(V2 CInt)
  }

-- | Animate a rectangle on the screen for n iterations.
pollEventAnimRectTest :: CInt -> IO ()
pollEventAnimRectTest iters = do
  initializeAll
  window <-
    createWindow
      "pollEventAnimRectTest"
      defaultWindow {windowInitialSize = defaultWindowSize}
  renderer <- createRenderer window (-1) defaultRenderer
  let go ::  State -> IO ()
      go !(State 0 _ _) = pure ()
      go !(State i (V2 xv yv) p@(V2 x y)) = do
        _ <- pollEvent
        rendererDrawColor renderer $= V4 40 40 40 255
        clear renderer
        rendererDrawColor renderer $= V4 255 255 255 255
        let xv'
              | x + w > mw = -xv
              | x < 0 = -xv
              | otherwise = xv
            yv'
              | y + h > mh = -yv
              | y < 0 = -yv
              | otherwise = yv
            v' = V2 xv' yv'
            p' = p + v'
        fillRect renderer (Just (Rectangle (P p') (V2 w h)))
        present renderer
        go (State (i - 1) v' p')
  go (State iters (V2 2 1) (V2 0 0))
  where
    defaultWindowSize :: V2 CInt
    defaultWindowSize = V2 800 600
    mw :: CInt
    mh :: CInt
    V2 mw mh = defaultWindowSize
    w :: CInt
    h :: CInt
    (w, h) = (100, 100)

--------------------------------------------------------------------------------
-- Animated rects test

data Square = Square
  {  squareV :: !(V2 CInt)
   , squareP :: !(V2 CInt)
  }

instance Storable Square where
  sizeOf _ = sizeOf (undefined :: V2 CInt) * 2
  alignment _ = 1
  poke  ptr (Square x y) = do
    poke (castPtr ptr) x
    poke (plusPtr ptr (sizeOf x)) y
  peek ptr = do
    x <- peek (castPtr ptr)
    y <- peek (plusPtr ptr (sizeOf x))
    pure (Square x y)

-- | Animate a rectangle on the screen for n iterations.
pollEventAnimRectsTest :: CInt -> IO ()
pollEventAnimRectsTest iters = do
  initializeAll
  window <-
    createWindow
      "pollEventAnimRectsTest"
      defaultWindow {windowInitialSize = defaultWindowSize}
  renderer <- createRenderer window (-1) defaultRenderer
  squares <-
    SV.unsafeThaw
      (SV.fromList
         [ Square (V2 2 1) (V2 0 0)
         , Square (V2 3 2) (V2 300 200)
         , Square (V2 1 1) (V2 100 500)
         , Square (V2 1 1) (V2 400 100)
         , Square (V2 1 2) (V2 200 400)
         , Square (V2 2 1) (V2 250 0)
         , Square (V2 1 2) (V2 300 500)
         , Square (V2 1 2) (V2 230 100)
         , Square (V2 1 1) (V2 200 490)
         ])
  let go :: CInt -> IO ()
      go !0 = pure ()
      go !i = do
        _ <- pollEvent
        rendererDrawColor renderer $= V4 40 40 40 255
        clear renderer
        rendererDrawColor renderer $= V4 255 255 255 255
        let animateSquare si = do
              Square (V2 xv yv) p@(V2 x y) <- SVM.read squares si
              let xv'
                    | x + w > mw = -xv
                    | x < 0 = -xv
                    | otherwise = xv
                  yv'
                    | y + h > mh = -yv
                    | y < 0 = -yv
                    | otherwise = yv
                  v' = V2 xv' yv'
                  p' = p + v'
              SVM.write squares si (Square v' p')
              fillRect renderer (Just (Rectangle (P p') (V2 w h)))
        let loop 0 = pure ()
            loop si = animateSquare si>>loop (si-1)
        loop (SVM.length squares - 1)
        present renderer
        go (i - 1)
  go iters
  where
    defaultWindowSize :: V2 CInt
    defaultWindowSize = V2 800 600
    mw :: CInt
    mh :: CInt
    V2 mw mh = defaultWindowSize
    w :: CInt
    h :: CInt
    (w, h) = (100, 100)