Skip to content

Instantly share code, notes, and snippets.

@schell
Last active December 18, 2015 13:49
Show Gist options
  • Select an option

  • Save schell/5793058 to your computer and use it in GitHub Desktop.

Select an option

Save schell/5793058 to your computer and use it in GitHub Desktop.
Re-inversion of control. Part 1.
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
@schell

schell commented Jun 16, 2013

Copy link
Copy Markdown
Author

The comments are more questions that declarations, since I'm not sure that what I've stated is correct.

@schell

schell commented Jun 16, 2013

Copy link
Copy Markdown
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