Last active
August 29, 2015 14:24
-
-
Save lotz84/ae28bf1b4701ca8a7f5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.IORef | |
data Scene = Scene { keyboardHandler :: Char -> IO () | |
, stepHandler :: IO () | |
, drawHandler :: IO () | |
} | |
mkTitle :: IORef Scene -> IO Scene | |
mkTitle transit = do | |
x <- newIORef 0 | |
return $ Scene { keyboardHandler = \key -> if key == 'e' -- Enter | |
then do | |
let game = mkGame x transit | |
writeIORef transit game | |
else return () | |
, stepHandler = modifyIORef x (+1) | |
, drawHandler = print =<< readIORef x -- Graphics.drawCircle(x, h) | |
} | |
mkGame :: IORef Int -> IORef Scene -> Scene | |
mkGame x transit = do | |
Scene { keyboardHandler = \key -> if key == 'c' -- ESC | |
then do | |
title <- mkTitle transit | |
writeIORef transit title | |
else return () | |
, stepHandler = modifyIORef x (subtract 1) | |
, drawHandler = print =<< readIORef x -- Graphics.drawCircle(x, h) | |
} | |
main = do | |
let dummy = Scene (const (return ())) (return ()) (return ()) | |
sceneRef <- newIORef dummy | |
title <- mkTitle sceneRef | |
writeIORef sceneRef title | |
loop sceneRef | |
where | |
loop sceneRef = do | |
-- c <- getChar | |
scene <- readIORef sceneRef | |
-- keyboardHandler scene c | |
stepHandler scene | |
drawHandler scene | |
loop sceneRef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment