@@ -1,72 +0,0 @@
1−
2−module FrameRate where
3−
4−import Maybe (mapMaybe)
5−import JavaScript
6−import Keyboard.Raw
7−import Input
8−
9−(drop,dFPS) = dropDown
10− [ ( "30", 30)
11− , ( "4", 4)
12− , ( "10", 10)
13− , ( "60", 60)
14− , ("100", 100) ]
15−
16−desiredFPS = lift castIntToJSNumber dFPS
17−foreign export jsevent "desiredFPS"
18− desiredFPS :: Signal JSNumber
19−
20−
21−foreign import jsevent "trigger" (castIntToJSNumber 0)
22− jsTime :: Signal JSNumber
23−
24−time = lift castJSNumberToFloat jsTime
25−
26−
27−-- Calculate FPS based on incoming times.
28−
29−getFPS ts =
30− let len = length ts in
31− if len > 1 then (len-1) / (head ts - last ts) else 0
32−fps = lift (truncate . getFPS) (foldp (\t ts -> take 10 (t:ts)) [0] time)
33−
34−
35−-- Use time and keyboard input to determine the
36−-- position of the black square.
37−
38−addVecs (x1,y1) (x2,y2) = (x1+x2, y1+y2)
39−scaleBy s (x,y) = (x*s, y*s)
40−
41−addKey k (x,y) =
42− if k < 37 || k > 40 then (x,y) else
43− if k `mod` 2 == 1 then (x + k - 38, y)
44− else (x, y + k - 39)
45−
46−velocity = lift (foldl addKey (0,0)) keysDown
47−
48−timestep = lift snd $ foldp (\t1 (t0,d) -> (t1, t1-t0)) (0,0) time
49−
50−delta = dropIf (\(x,y) -> x == 0 && y == 0) (0,0) . sampleOn timestep $
51− lift (scaleBy 100) (lift2 scaleBy timestep velocity)
52−
53−position = foldp addVecs (0,0) delta
54−
55−
56−-- Display moving square and FPS on screen.
57−
58−screen pos actual =
59− flow down [ collage 400 400 [ outlined black (rect 40 40 pos) ]
60− , plainText "Move the square around with the arrow keys."
61− , plainText $ "Actual frames per second: " ++ show actual
62− , plainText "Desired frames per second: " `beside` drop
63− ]
64−
65−
66−game = lift2 screen position fps
67−
68−done = lift (\_ -> castBoolToJSBool True) game
69−foreign export jsevent "finished"
70− done :: Signal JSBool
71−
72−main = game