From 90afdcd7bb9d8d3dc50d35bc770eeb4793f379d5 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Wed, 25 Jan 2023 12:55:15 -0500 Subject: [PATCH] Buffer events with `playMSF` --- src/Game/Utils.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Game/Utils.hs b/src/Game/Utils.hs index ba267b2..443e29a 100644 --- a/src/Game/Utils.hs +++ b/src/Game/Utils.hs @@ -10,10 +10,10 @@ import Graphics.Gloss.Interface.IO.Game -- | Display an MSF outputting a picture value as a window using Gloss. -- 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 -- `react` doesn't allow inputs or outputs, so we have to use IORefs - evRef <- newIORef Nothing + evRef <- newIORef [] picRef <- newIORef blank 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. handleInput e _ = do - writeIORef evRef (Just e) + modifyIORef evRef (e :) -- A function to step the world one iteration. stepWorld _ _ = do react handle - writeIORef evRef Nothing + writeIORef evRef [] playIO display color freq () toPic handleInput stepWorld -- * 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 msf = arr (,()) >>> Control.Monad.Trans.MSF.Reader.runReaderS (runWriterS msf) >>> arr fst +runDrawerS :: Monad m => MSF (DrawerT m) () () -> MSF m [Event] Picture +runDrawerS msf = arr (,()) >>> runReaderS (runWriterS msf) >>> arr fst draw :: Monad m => MSF (DrawerT m) Picture () draw = arrM tell