Write gloss-dunai compatibility layer

This commit is contained in:
Kiana Sheibani 2023-01-24 13:23:15 -05:00
parent d4799c3dc4
commit c4065b4b1b
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 56 additions and 4 deletions

View file

@ -1,4 +0,0 @@
module Main where
main :: IO ()
main = undefined

14
src/Main.hs Normal file
View file

@ -0,0 +1,14 @@
{-# LANGUAGE Arrows #-}
module Main where
import Data.MonadicStreamFunction
import Graphics.Gloss
import Utils
mainSF :: MSF Drawer () ()
mainSF = proc () -> do
draw -< color white $ circleSolid 40
main :: IO ()
main = playMSF FullScreen black 60 (runDrawerS mainSF)

42
src/Utils.hs Normal file
View file

@ -0,0 +1,42 @@
module Utils where
import Control.Monad.Trans.MSF.Reader
import Control.Monad.Trans.MSF.Writer
import Data.IORef
import Data.MonadicStreamFunction
import Data.MonadicStreamFunction.ReactHandle
import Graphics.Gloss
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 freq msf = do
-- `react` doesn't allow inputs or outputs, so we have to use IORefs
evRef <- newIORef Nothing
picRef <- newIORef blank
handle <- reactInit (constM (readIORef evRef) >>> msf >>> arrM (writeIORef picRef))
let -- An action to convert the world to a picture.
toPic _ = readIORef picRef
-- A function to handle input events.
handleInput e _ = do
writeIORef evRef (Just e)
react handle
-- A function to step the world one iteration.
stepWorld _ _ = do
writeIORef evRef Nothing
react handle
playIO display color freq () toPic handleInput stepWorld
type Drawer = WriterT Picture (ReaderT (Maybe Event) IO)
runDrawerS :: MSF Drawer () () -> MSF IO (Maybe Event) Picture
runDrawerS msf = arr (,()) >>> runReaderS (runWriterS msf) >>> arr fst
draw :: MSF Drawer Picture ()
draw = arrM tell