Buffer events with playMSF
This commit is contained in:
parent
e788ddee39
commit
90afdcd7bb
|
@ -10,10 +10,10 @@ import Graphics.Gloss.Interface.IO.Game
|
||||||
|
|
||||||
-- | Display an MSF outputting a picture value as a window using Gloss.
|
-- | Display an MSF outputting a picture value as a window using Gloss.
|
||||||
-- Note that the MSF is not passed a real-time clock.
|
-- Note that the MSF is not passed a real-time clock.
|
||||||
playMSF :: Display -> Color -> Int -> MSF IO (Maybe Event) Picture -> IO ()
|
playMSF :: Display -> Color -> Int -> MSF IO [Event] Picture -> IO ()
|
||||||
playMSF display color freq msf = do
|
playMSF display color freq msf = do
|
||||||
-- `react` doesn't allow inputs or outputs, so we have to use IORefs
|
-- `react` doesn't allow inputs or outputs, so we have to use IORefs
|
||||||
evRef <- newIORef Nothing
|
evRef <- newIORef []
|
||||||
picRef <- newIORef blank
|
picRef <- newIORef blank
|
||||||
|
|
||||||
handle <- reactInit (constM (readIORef evRef) >>> msf >>> arrM (writeIORef picRef))
|
handle <- reactInit (constM (readIORef evRef) >>> msf >>> arrM (writeIORef picRef))
|
||||||
|
@ -23,21 +23,21 @@ playMSF display color freq msf = do
|
||||||
|
|
||||||
-- A function to handle input events.
|
-- A function to handle input events.
|
||||||
handleInput e _ = do
|
handleInput e _ = do
|
||||||
writeIORef evRef (Just e)
|
modifyIORef evRef (e :)
|
||||||
|
|
||||||
-- A function to step the world one iteration.
|
-- A function to step the world one iteration.
|
||||||
stepWorld _ _ = do
|
stepWorld _ _ = do
|
||||||
react handle
|
react handle
|
||||||
writeIORef evRef Nothing
|
writeIORef evRef []
|
||||||
|
|
||||||
playIO display color freq () toPic handleInput stepWorld
|
playIO display color freq () toPic handleInput stepWorld
|
||||||
|
|
||||||
-- * DrawerT
|
-- * DrawerT
|
||||||
|
|
||||||
type DrawerT m = WriterT Picture (Control.Monad.Trans.MSF.Reader.ReaderT (Maybe Event) m)
|
type DrawerT m = WriterT Picture (ReaderT [Event] m)
|
||||||
|
|
||||||
runDrawerS :: Monad m => MSF (DrawerT m) () () -> MSF m (Maybe Event) Picture
|
runDrawerS :: Monad m => MSF (DrawerT m) () () -> MSF m [Event] Picture
|
||||||
runDrawerS msf = arr (,()) >>> Control.Monad.Trans.MSF.Reader.runReaderS (runWriterS msf) >>> arr fst
|
runDrawerS msf = arr (,()) >>> runReaderS (runWriterS msf) >>> arr fst
|
||||||
|
|
||||||
draw :: Monad m => MSF (DrawerT m) Picture ()
|
draw :: Monad m => MSF (DrawerT m) Picture ()
|
||||||
draw = arrM tell
|
draw = arrM tell
|
||||||
|
|
Loading…
Reference in a new issue