Last active
December 18, 2015 13:49
-
-
Save schell/5793215 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
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 | |
-- Make sure GLFW quits for us if we close the window. | |
setWindowCloseCallback exitSuccess | |
forever $ do | |
-- Run the computation. | |
k <- flip runContT return $ 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 (Just key) | |
stepAndRender k | |
stepAndRender :: Maybe Key -> IO () | |
stepAndRender mLastKey = | |
case mLastKey of | |
Just key -> do | |
putStrLn $ "Last key pressed was: "++show key | |
swapBuffers | |
Nothing -> do | |
putStrLn "No key has been pressed." | |
swapBuffers | |
showKeyPress :: Key -> Bool -> String | |
showKeyPress k b = show k ++ if b | |
then " is pressed." | |
else " is not pressed." | |
yieldInput :: ContT (Maybe Key) 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 | |
{- | |
Main.hs|49 col 14 error| Couldn't match type `()' with `Maybe Key' | |
|| Expected type: ContT (Maybe Key) IO (Key, Bool) | |
|| Actual type: ContT () IO (Key, Bool) | |
|| In the expression: | |
|| ContT | |
|| $ \ f | |
|| -> do { liftIO $ putStrLn "Setting key callback."; | |
|| setKeyCallback $ curry f } | |
|| In an equation for `yieldInput': | |
|| yieldInput | |
|| = ContT | |
|| $ \ f | |
|| -> do { liftIO $ putStrLn "Setting key callback."; | |
|| setKeyCallback $ curry f } | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So - how do we use re-inversion of control to pass the last passed key into stepAndRender?