-- State.hs module Graphics.GD.State where import Control.Monad.State.Lazy (State(..),modify,execState) import qualified Graphics.GD as GD data GDCmd = SetPixel GD.Point GD.Color data GD' = GD' { gdCmds :: [GDCmd] } type GD a = State GD' a consCmd :: GDCmd -> GD () consCmd cmd = modify $ \gd -> gd { gdCmds = cmd : (gdCmds gd) } setPixel :: GD.Point -> GD.Color -> GD () setPixel = (consCmd .) . SetPixel newImage :: GD.Size -> GD () -> IO GD.Image newImage size f = do im <- GD.newImage size mapM_ (flip runCmd $ im) $ gdCmds $ execState f $ GD' [] return im runCmd :: GDCmd -> GD.Image -> IO () runCmd (SetPixel pt c) = GD.setPixel pt c