affection/examples/example02.hs

128 lines
3.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
import Affection
import qualified SDL
import qualified GEGL as G
2016-12-21 03:28:57 +00:00
import qualified GEGL.FFI.Buffer as G
import qualified BABL as B
import qualified Data.Map.Strict as M
import Foreign.C.Types
2016-12-21 03:28:57 +00:00
import Foreign.Marshal.Utils (new)
import Foreign.Ptr (castPtr)
import Debug.Trace
main :: IO ()
main = do
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
{ nodeGraph :: M.Map String G.GeglNode
, foreground :: G.GeglBuffer
, coordinates :: Maybe (Int, Int)
, lastTick :: Double
}
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"
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation
[ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1
, G.Property "color2" $ G.PropertyColor $ 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)
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer"
[ G.Property "buffer" $ G.PropertyBuffer buffer
]
2016-12-21 03:28:57 +00:00
traceM "buffer-sink"
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
traceM "nop"
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop"
[ G.Property "width" $ G.PropertyDouble 800
, G.Property "height" $ G.PropertyDouble 600
]
G.gegl_node_link_many [checkerboard, over, crop, sink]
2016-12-21 03:28:57 +00:00
G.gegl_node_connect_to nop "output" over "aux"
traceM "connections made"
myMap <- return $ M.fromList
[ ("root" , root)
, ("over" , over)
, ("background" , checkerboard)
2016-12-21 03:28:57 +00:00
, ("sink" , sink)
, ("nop" , nop)
, ("crop" , crop)
]
traceM "loading complete"
return $ UserData
{ nodeGraph = myMap
, foreground = buffer
, coordinates = Nothing
, lastTick = 0
}
2016-12-21 03:28:57 +00:00
-- drawInit :: Affection UserData ()
-- drawInit = do
-- UserData{..} <- getAffection
-- present (nodeGraph M.! "over") foreground (GeglRectangle 0 0 800 600) True
draw :: Affection UserData ()
draw = do
traceM "drawing"
2016-12-12 01:11:31 +00:00
UserData{..} <- getAffection
2017-03-16 19:21:04 +00:00
clear foreground
maybe (return ()) (\(x, y) ->
drawRect
2016-12-21 03:28:57 +00:00
(nodeGraph M.! "nop")
(G.RGBA 1 0 0 0.5)
2016-12-21 03:28:57 +00:00
(Fill)
(G.GeglRectangle (x - 10) (y - 10) 20 20)
2016-12-21 03:28:57 +00:00
foreground
) coordinates
liftIO $ G.gegl_node_process $ nodeGraph M.! "sink"
update :: Affection UserData ()
update = do
traceM "updating"
tick <- getTick
ud <- getAffection
putAffection $ ud { lastTick = tick }
let dt = tick - lastTick ud
traceM $ (show $ 1 / dt) ++ " FPS"
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
putAffection $ ud
{ coordinates = Just (fromIntegral x, fromIntegral y)
}
handle (SDL.WindowClosedEvent _) = do
traceM "seeya!"
quit
handle _ =
return ()
clean :: UserData -> IO ()
clean _ = return ()