-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCallbacks.hs
35 lines (28 loc) · 977 Bytes
/
Callbacks.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
module Haskeroids.Callbacks (
renderViewport,
handleKeyboard,
logicTick) where
import Data.IORef
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Haskeroids.Render (LineRenderable(..))
import Haskeroids.Tick
import Haskeroids.Keyboard
type KeyboardRef = IORef Keyboard
-- | Render the viewport using the given renderable and swap buffers
renderViewport :: LineRenderable r => r -> IO ()
renderViewport r = do
clear [ColorBuffer]
render r
swapBuffers
-- | Periodical logic tick
logicTick :: (LineRenderable t, Tickable t) => KeyboardRef -> t -> IO ()
logicTick kb t = do
keys <- readIORef kb
let newTickable = tick keys t
displayCallback $= renderViewport newTickable
addTimerCallback 33 $ logicTick kb newTickable
postRedisplay Nothing
-- | Update the Keyboard state according to the event
handleKeyboard :: KeyboardRef -> KeyboardMouseCallback
handleKeyboard kb k ks _ _ = modifyIORef kb (handleKeyEvent k ks)