affection/examples/example01.hs
2017-03-23 04:31:10 +01:00

110 lines
2.9 KiB
Haskell

{-# LANGUAGE RecordWildCards #-}
import Affection
import qualified SDL
import qualified SDL.Raw as Raw
import qualified GEGL as G
import qualified BABL as B
import qualified Data.Map.Strict as M
import Control.Monad (when)
import Foreign.Storable (peek)
import Foreign.C.Types
import Debug.Trace
-- main :: IO ()
-- main = withAllAffection $
-- withDefaultWindow "test" $ do
-- changeColor $ RGBA 255 255 255 255
-- clear
-- present
-- liftIO $ delaySec 2
main :: IO ()
main = do
conf <- return $ AffectionConfig
{ initComponents = All
, windowTitle = "Affection: example00"
, windowConfig = SDL.defaultWindow
, preLoop = return ()
, eventLoop = const $ return ()
, updateLoop = update
, drawLoop = draw
, loadState = load
, cleanUp = clean
, canvasSize = Nothing
}
withAffection conf
data UserData = UserData
{ nodeGraph :: M.Map String G.GeglNode
, foreground :: G.GeglBuffer
-- , lastTick :: Double
}
load :: IO UserData
load = do
traceM "loading"
root <- G.gegl_node_new
traceM "new root node"
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $
props $ do
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"
buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<<
B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $
props $
prop "buffer" buffer
traceM "buffer-source"
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
traceM "nop"
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $
props $ do
prop "width" (800::Double)
prop "height" (600.0::Double)
G.gegl_node_link_many [checkerboard, over, crop, sink]
G.gegl_node_connect_to nop "output" over "aux"
traceM "connections made"
myMap <- return $ M.fromList
[ ("root" , root)
, ("over" , over)
, ("background" , checkerboard)
, ("sink" , sink)
, ("nop" , nop)
]
let roi = G.GeglRectangle 0 0 20 20
traceM "loading complete"
return $ UserData
{ nodeGraph = myMap
, foreground = buffer
-- , lastTick = 0
}
draw :: Affection UserData ()
draw = do
traceM "drawing"
UserData{..} <- getAffection
drawRect (nodeGraph M.! "nop") (G.RGB 1 0 0) Fill (G.GeglRectangle 10 10 500 500) foreground
process $ nodeGraph M.! "sink"
render Nothing Nothing
update :: Double -> Affection UserData ()
update dt = do
traceM "updating"
ud <- getAffection
-- let last = lastTick ud
-- tick <- getTick
-- putAffection $ ud { lastTick = tick }
traceM $ (show $ 1 / dt) ++ " FPS"
elapsed <- getElapsedTime
when (elapsed > 20) $
quit
clean :: UserData -> IO ()
clean _ = return ()