affection/examples/example02.hs

153 lines
4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
import Affection
import qualified SDL
import qualified GEGL as G
import qualified BABL as B
import qualified Data.Map.Strict as M
2017-03-21 11:04:56 +00:00
import qualified Control.Monad.Parallel as MP
import Debug.Trace
main :: IO ()
main = do
2017-03-20 04:24:30 +00:00
conf <- return AffectionConfig
{ initComponents = All
, windowTitle = "Affection: example00"
, windowConfig = SDL.defaultWindow
2016-12-21 03:28:57 +00:00
, preLoop = return ()
, eventLoop = handle
, updateLoop = update
, drawLoop = draw
, loadState = load
, cleanUp = clean
}
withAffection conf
data UserData = UserData
2017-03-21 11:04:56 +00:00
{ nodeGraph :: M.Map String G.GeglNode
, actors :: M.Map String (Actor String)
, foreground :: G.GeglBuffer
, lastTick :: Double
, updateActors :: [Actor String]
}
load :: SDL.Surface -> IO UserData
2016-12-11 19:35:18 +00:00
load _ = do
traceM "loading"
root <- G.gegl_node_new
traceM "new root node"
2017-03-16 23:20:00 +00:00
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $
props $ do
2017-03-18 12:50:34 +00:00
prop "color1" $ G.RGBA 0.4 0.4 0.4 1
prop "color2" $ G.RGBA 0.6 0.6 0.6 1
traceM "checkerboard"
over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over"
2016-12-26 13:19:03 +00:00
buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<<
B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
2017-03-16 23:20:00 +00:00
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $
props $
prop "buffer" buffer
2016-12-21 03:28:57 +00:00
traceM "buffer-sink"
2017-03-18 16:38:26 +00:00
rectProps <- return $
2017-03-16 23:20:00 +00:00
props $ do
2017-03-18 12:50:34 +00:00
prop "x" (0::Double)
prop "y" (0::Double)
prop "width" (20::Double)
prop "height" (20::Double)
prop "color" $ G.RGBA 1 0 0 0.5
2017-03-18 16:38:26 +00:00
rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle" rectProps
2017-03-16 22:24:34 +00:00
traceM "rect"
2017-03-16 23:20:00 +00:00
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $
props $ do
2017-03-18 12:50:34 +00:00
prop "width" (800::Double)
prop "height" (600::Double)
G.gegl_node_link_many [checkerboard, over, crop, sink]
2017-03-16 22:24:34 +00:00
_ <- G.gegl_node_connect_to rect "output" over "aux"
2017-03-21 11:04:56 +00:00
rectActor <- return $ Actor (map
(\p -> ActorProperty
(G.propertyName p)
(G.propertyValue p)
(Just "rect")
)rectProps
) (M.singleton "rect" rect)
traceM "connections made"
myMap <- return $ M.fromList
[ ("root" , root)
, ("over" , over)
, ("background" , checkerboard)
2016-12-21 03:28:57 +00:00
, ("sink" , sink)
2017-03-16 22:24:34 +00:00
, ("rect" , rect)
, ("crop" , crop)
]
traceM "loading complete"
2017-03-18 16:38:26 +00:00
actorMap <- return $ M.fromList
[ ("rect", rectActor)
]
2017-03-20 04:24:30 +00:00
return UserData
2017-03-21 11:04:56 +00:00
{ nodeGraph = myMap
, actors = actorMap
, foreground = buffer
, lastTick = 0
, updateActors = []
}
2016-12-21 03:28:57 +00:00
-- drawInit :: Affection UserData ()
-- drawInit = do
-- UserData{..} <- getAffection
2017-03-16 22:24:34 +00:00
-- present (GeglRectangle 0 0 800 600) foreground True
draw :: Affection UserData ()
draw = do
2017-03-21 11:04:56 +00:00
ud@UserData{..} <- getAffection
MP.mapM_ applyProperties updateActors
putAffection ud
{ updateActors = []
}
2017-03-16 19:52:45 +00:00
process (nodeGraph M.! "sink")
2017-03-16 22:24:34 +00:00
present (GeglRectangle 0 0 800 600) foreground True
update :: Affection UserData ()
update = do
traceM "updating"
2017-03-20 04:24:30 +00:00
tick <- getElapsedTime
ud <- getAffection
putAffection $ ud { lastTick = tick }
let dt = tick - lastTick ud
2017-03-16 22:24:34 +00:00
return ()
2017-03-20 04:24:30 +00:00
traceM $ show (1 / dt) ++ " FPS"
2017-03-18 13:12:42 +00:00
handle :: SDL.EventPayload -> Affection UserData ()
handle (SDL.MouseMotionEvent dat) = do
let (SDL.P (SDL.V2 x y)) = SDL.mouseMotionEventPos dat
2016-12-12 01:11:31 +00:00
ud <- getAffection
2017-03-18 16:38:26 +00:00
nmap <- return $ M.adjust
2017-03-21 11:12:46 +00:00
(updateProperties $ props $ do
prop "x" (fromIntegral (x - 10) :: Double)
prop "y" (fromIntegral (y - 10) :: Double)
2017-03-18 16:38:26 +00:00
)
"rect"
(actors ud)
putAffection ud
2017-03-21 11:04:56 +00:00
{ actors = nmap
, updateActors = (actors ud M.! "rect") : []
2017-03-18 16:38:26 +00:00
}
-- liftIO $ G.gegl_node_set (nodeGraph ud M.! "rect") $ G.Operation "" $
-- props $ do
-- prop "x" (fromIntegral (x - 10) :: Double)
-- prop "y" $ (fromIntegral (y - 10) :: Double)
handle (SDL.WindowClosedEvent _) = do
traceM "seeya!"
quit
handle _ =
return ()
clean :: UserData -> IO ()
clean _ = return ()