4 import Control
.Concurrent
8 import Graphics
.UI
.SDL
.Time
11 lastRef
:: IORef Word32
,
12 errRef
:: IORef Word32
,
16 mkClock
:: Int -> IO Clock
18 now
<- getTicks
>>= newIORef
20 return (Clock now err
(fromIntegral q
))
22 readNewClockTicks
:: Clock
-> IO Int
23 readNewClockTicks clock
@(Clock lastRef errRef quantum
) = do
25 last <- readIORef lastRef
26 err
<- readIORef errRef
27 let delta
= if now
>= last then now
- last else maxBound - last + now
28 let (steps
, err
') = (delta
+err
) `
divMod` quantum
29 let delay
= (quantum
- err
') * 1000
32 writeIORef lastRef now
33 writeIORef errRef err
'
34 return (fromIntegral steps
)
36 -- threadDelay (fromIntegral delay)
38 readNewClockTicks clock