@@ -6,17 +6,20 @@ import Foreign.JavaScript
66import Signal.Keyboard.Raw
77import Signal.Input
88
9−
10−foreign import jsevent "trigger" (castIntToJSNumber 0)
11− jsTime :: Signal JSNumber
12−
13−foreign export jsevent "finished"
14− done :: Signal JSBool
15−
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
1617foreign export jsevent "desiredFPS"
1718 desiredFPS :: Signal JSNumber
1819
19−desiredFPS = constant (castIntToJSNumber 30)
20+
21+foreign import jsevent "trigger" (castIntToJSNumber 0)
22+ jsTime :: Signal JSNumber
2023
2124time = lift castJSNumberToFloat jsTime
2225
@@ -26,7 +29,6 @@ time = lift castJSNumberToFloat jsTime
2629getFPS ts =
2730 let len = length ts in
2831 if len > 1 then (len-1) / (head ts - last ts) else 0
29−truncate n = castJSNumberToInt (castFloatToJSNumber $ 100 * n) / 100
3032fps = lift (truncate . getFPS) (foldp (\t ts -> take 10 (t:ts)) [0] time)
3133
3234
@@ -45,7 +47,7 @@ velocity = lift (foldl addKey (0,0)) keysDown
4547
4648timestep = lift snd $ foldp (\t1 (t0,d) -> (t1, t1-t0)) (0,0) time
4749
48−delta = dropIf (\(x,y) -> x == 0 && y == 0) (0,0) $
50+delta = dropIf (\(x,y) -> x == 0 && y == 0) (0,0) . sampleOn timestep $
4951 lift (scaleBy 100) (lift2 scaleBy timestep velocity)
5052
5153position = foldp addVecs (0,0) delta
@@ -53,15 +55,18 @@ position = foldp addVecs (0,0) delta
5355
5456-- Display moving square and FPS on screen.
5557
56−screen pos actual desired =
58+screen pos actual =
5759 flow down [ collage 400 400 [ outlined black (rect 40 40 pos) ]
5860 , plainText "Move the square around with the arrow keys."
5961 , text $ toText "Actual frames per second: " ++ show actual
60− , text $ toText "Desired frames per second: " ++ show desired
62+ , plainText "Desired frames per second: " `beside` drop
6163 ]
6264
6365
64−game = lift3 screen position fps desiredFPS
66+game = lift2 screen position fps
67+
6568done = lift (\_ -> castBoolToJSBool True) game
69+foreign export jsevent "finished"
70+ done :: Signal JSBool
6671
6772main = game