Last active
December 18, 2015 13:49
-
-
Save schell/5793058 to your computer and use it in GitHub Desktop.
Re-inversion of control. Part 1.
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
| module Main where | |
| import Graphics.UI.GLFW | |
| import Control.Monad.Cont | |
| import System.Exit ( exitSuccess ) | |
| main :: IO () | |
| main = do | |
| putStrLn "Running glfw-cont-test." | |
| True <- initialize | |
| True <- openWindow defaultDisplayOptions | |
| -- Run the computation. | |
| flip runContT return $ forever $ do | |
| -- This yield ends execution until the continuation is called by | |
| -- the key callback. Until then swapBuffers will give you seizures. | |
| (key, isPressed) <- yieldInput | |
| -- Continuation was called and now the computation completes and | |
| -- runs again due to $ forever | |
| mapM_ liftIO [ putStrLn $ showKeyPress key isPressed | |
| , when (isPressed && key == KeyEsc) $ void exitSuccess ] | |
| return () | |
| setWindowCloseCallback exitSuccess | |
| forever swapBuffers | |
| showKeyPress :: Key -> Bool -> String | |
| showKeyPress k b = show k ++ if b | |
| then " is pressed." | |
| else " is not pressed." | |
| yieldInput :: ContT () IO (Key, Bool) | |
| yieldInput = ContT $ \f -> do | |
| liftIO $ putStrLn "Setting key callback." | |
| -- Curry the args of the callback to (key, isPressed) and call the | |
| -- continuation. | |
| setKeyCallback $ curry f | |
Author
Author
The next step is to have yieldInput return the list of currently pressed keys.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The comments are more questions that declarations, since I'm not sure that what I've stated is correct.