forked from mlesniak/game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.hs
92 lines (76 loc) · 2.29 KB
/
Statistics.hs
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
-- | Display information about current and past FPS.
--
-- By proving an additional parameter to drawFPS, user-defined strings can be
-- shown in the FPS window, too. Currently the maximum FPS is fixed to 60 as a
-- constant.
module Statistics (
FPS
, newFPS
, drawFPS
, toggleFPS
) where
import Control.Monad
import Data.IORef
import Graphics.UI.GLUT hiding (position)
import Unsafe.Coerce (unsafeCoerce)
import Window
data FPS = FPS {
fpsTime :: IORef Double
, fpsShown :: IORef Bool
, fpsLast :: IORef [GLdouble]
}
-- | Creates a new measuring object.
newFPS :: IO FPS
newFPS = do
t <- newIORef =<< getTime
s <- newIORef True
l <- newIORef []
return FPS {
fpsTime = t
, fpsShown = s
, fpsLast = l
}
-- | Shows the fps (if toggled) in a graph.
--
-- The additional string in add is shown directly after the FPS.
drawFPS :: FPS -> Maybe String -> IO ()
drawFPS (FPS t s fpss) add = do
-- Should we display the graph at all?
toShow <- readIORef s
when toShow $ do
-- Calculate difference between runs and update list of differences.
told <- readIORef t
tcur <- getTime
let tdiff = tcur - told
writeIORef t tcur
runs <- readIORef fpss
let f' = take 59 runs
writeIORef fpss $ unsafeCoerce (1/tdiff) : f'
preservingMatrix $ do
-- Move to upper left corner.
translate $ Vector3 (0.02 :: GLdouble) 0.89 0.0
Graphics.UI.GLUT.scale 0.5 0.1 (0.1 :: GLdouble)
-- Draw black border.
color $ Color3 0 0 (0 :: GLdouble)
renderPrimitive LineLoop $ do
toVertex (0.0, 1 :: Double)
toVertex (1.0, 1 :: Double)
toVertex (1.0, 0 :: Double)
toVertex (0.0, 0 :: Double)
-- Show values.
values <- readIORef fpss
unless (null values) $ do
let steps = 59.0
dsteps = 1.0 / steps
color $ Color3 1 0 (0 :: GLdouble)
renderPrimitive LineStrip $
forM_ (zip [1.0,(1.0-dsteps)..0] values) $ \(x,y) -> do
let b = (x, min (y/steps) 0.99)
toVertex b
-- Display textual information
color $ Color3 0 0 (0 :: GLdouble)
let str = take 5 (show (head values)) ++ maybe [] (" " ++) add
text (0.04, 0.75) [str]
-- | Toggles display of fps information.
toggleFPS :: FPS -> IO ()
toggleFPS f = modifyIORef (fpsShown f) not