affection/examples/example01.hs

111 lines
2.9 KiB
Haskell
Raw Normal View History

2016-11-18 15:27:47 +00:00
{-# LANGUAGE RecordWildCards #-}
2016-05-29 16:01:23 +00:00
import Affection
2016-11-18 15:27:47 +00:00
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
2016-12-26 13:06:51 +00:00
import Control.Monad (when)
2016-11-18 15:27:47 +00:00
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
2016-05-29 16:01:23 +00:00
main :: IO ()
2016-11-18 15:27:47 +00:00
main = do
conf <- return $ AffectionConfig
{ initComponents = All
, windowTitle = "Affection: example00"
, windowConfig = SDL.defaultWindow
2016-12-12 01:10:30 +00:00
, preLoop = return ()
, eventLoop = const $ return ()
2016-11-18 15:27:47 +00:00
, updateLoop = update
, drawLoop = draw
2016-11-18 15:27:47 +00:00
, loadState = load
, cleanUp = clean
2017-03-23 03:31:10 +00:00
, canvasSize = Nothing
2016-11-18 15:27:47 +00:00
}
withAffection conf
data UserData = UserData
{ nodeGraph :: M.Map String G.GeglNode
, foreground :: G.GeglBuffer
2017-03-20 04:24:30 +00:00
-- , lastTick :: Double
2016-11-18 15:27:47 +00:00
}
2017-03-23 03:27:57 +00:00
load :: IO UserData
load = do
2016-11-18 15:27:47 +00:00
traceM "loading"
root <- G.gegl_node_new
traceM "new root node"
2017-03-16 22:51:41 +00:00
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
2016-11-18 15:27:47 +00:00
traceM "checkerboard"
over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over"
2016-12-21 03:28:57 +00:00
buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<<
2016-11-18 15:27:47 +00:00
B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
2017-03-16 22:51:41 +00:00
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $
props $
prop "buffer" buffer
2016-11-18 15:27:47 +00:00
traceM "buffer-source"
2016-12-26 13:06:51 +00:00
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
traceM "nop"
2017-03-16 22:51:41 +00:00
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $
props $ do
prop "width" (800::Double)
prop "height" (600.0::Double)
2016-12-26 13:06:51 +00:00
G.gegl_node_link_many [checkerboard, over, crop, sink]
G.gegl_node_connect_to nop "output" over "aux"
2016-11-18 15:27:47 +00:00
traceM "connections made"
myMap <- return $ M.fromList
[ ("root" , root)
, ("over" , over)
, ("background" , checkerboard)
2016-12-26 13:06:51 +00:00
, ("sink" , sink)
, ("nop" , nop)
2016-11-18 15:27:47 +00:00
]
let roi = G.GeglRectangle 0 0 20 20
traceM "loading complete"
return $ UserData
{ nodeGraph = myMap
, foreground = buffer
2017-03-20 04:24:30 +00:00
-- , lastTick = 0
2016-11-18 15:27:47 +00:00
}
2016-12-08 17:22:29 +00:00
draw :: Affection UserData ()
2016-11-18 15:27:47 +00:00
draw = do
traceM "drawing"
2016-12-20 10:29:03 +00:00
UserData{..} <- getAffection
drawRect (nodeGraph M.! "nop") (G.RGB 1 0 0) Fill (G.GeglRectangle 10 10 500 500) foreground
2017-03-16 19:52:45 +00:00
process $ nodeGraph M.! "sink"
2017-03-23 03:29:48 +00:00
render Nothing Nothing
2016-11-18 15:27:47 +00:00
2017-03-23 03:29:48 +00:00
update :: Double -> Affection UserData ()
update dt = do
2016-11-18 15:27:47 +00:00
traceM "updating"
ud <- getAffection
2017-03-20 04:24:30 +00:00
-- let last = lastTick ud
-- tick <- getTick
-- putAffection $ ud { lastTick = tick }
traceM $ (show $ 1 / dt) ++ " FPS"
2017-03-20 04:24:30 +00:00
elapsed <- getElapsedTime
when (elapsed > 20) $
quit
2016-11-18 15:27:47 +00:00
clean :: UserData -> IO ()
clean _ = return ()