2016-12-11 11:11:58 +00:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
|
|
|
import Affection
|
|
|
|
import qualified SDL
|
|
|
|
import qualified GEGL as G
|
|
|
|
import qualified BABL as B
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
|
|
|
|
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 ()
|
2017-03-16 19:12:41 +00:00
|
|
|
, eventLoop = handle
|
2016-12-11 11:11:58 +00:00
|
|
|
, updateLoop = update
|
2017-03-16 19:12:41 +00:00
|
|
|
, drawLoop = draw
|
2016-12-11 11:11:58 +00:00
|
|
|
, loadState = load
|
|
|
|
, cleanUp = clean
|
|
|
|
}
|
|
|
|
withAffection conf
|
|
|
|
|
|
|
|
data UserData = UserData
|
|
|
|
{ nodeGraph :: M.Map String G.GeglNode
|
|
|
|
, foreground :: G.GeglBuffer
|
2017-03-16 19:12:41 +00:00
|
|
|
, lastTick :: Double
|
2016-12-11 11:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
load :: SDL.Surface -> IO UserData
|
2016-12-11 19:35:18 +00:00
|
|
|
load _ = do
|
2016-12-11 11:11:58 +00:00
|
|
|
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) =<<
|
2016-12-11 11:11:58 +00:00
|
|
|
B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
|
2016-12-23 13:18:39 +00:00
|
|
|
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer"
|
|
|
|
[ G.Property "buffer" $ G.PropertyBuffer buffer
|
2016-12-11 11:11:58 +00:00
|
|
|
]
|
2016-12-21 03:28:57 +00:00
|
|
|
traceM "buffer-sink"
|
2017-03-16 22:24:34 +00:00
|
|
|
rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle"
|
|
|
|
[ G.Property "x" $ G.PropertyDouble 0
|
|
|
|
, G.Property "y" $ G.PropertyDouble 0
|
|
|
|
, G.Property "width" $ G.PropertyDouble 20
|
|
|
|
, G.Property "height" $ G.PropertyDouble 20
|
|
|
|
, G.Property "color" $ G.PropertyColor $ G.RGBA 1 0 0 0.5
|
|
|
|
]
|
|
|
|
traceM "rect"
|
2016-12-23 13:18:39 +00:00
|
|
|
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]
|
2017-03-16 22:24:34 +00:00
|
|
|
_ <- G.gegl_node_connect_to rect "output" over "aux"
|
2016-12-11 11:11:58 +00:00
|
|
|
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)
|
2016-12-23 13:18:39 +00:00
|
|
|
, ("crop" , crop)
|
2016-12-11 11:11:58 +00:00
|
|
|
]
|
|
|
|
traceM "loading complete"
|
|
|
|
return $ UserData
|
|
|
|
{ nodeGraph = myMap
|
|
|
|
, foreground = buffer
|
2017-03-16 19:12:41 +00:00
|
|
|
, lastTick = 0
|
2016-12-11 11:11:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
2016-12-11 19:24:16 +00:00
|
|
|
|
2016-12-11 11:11:58 +00:00
|
|
|
draw :: Affection UserData ()
|
|
|
|
draw = do
|
2016-12-12 01:11:31 +00:00
|
|
|
UserData{..} <- getAffection
|
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
|
2016-12-11 11:11:58 +00:00
|
|
|
|
2017-03-16 19:12:41 +00:00
|
|
|
update :: Affection UserData ()
|
|
|
|
update = do
|
2016-12-11 11:11:58 +00:00
|
|
|
traceM "updating"
|
2017-03-16 19:12:41 +00:00
|
|
|
|
|
|
|
tick <- getTick
|
|
|
|
ud <- getAffection
|
|
|
|
putAffection $ ud { lastTick = tick }
|
|
|
|
|
|
|
|
let dt = tick - lastTick ud
|
2017-03-16 22:24:34 +00:00
|
|
|
return ()
|
|
|
|
-- traceM $ (show $ 1 / dt) ++ " FPS"
|
2017-03-16 19:12:41 +00:00
|
|
|
|
|
|
|
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-16 22:24:34 +00:00
|
|
|
liftIO $ G.gegl_node_set (nodeGraph ud M.! "rect") $ G.Operation ""
|
|
|
|
[ G.Property "x" $ G.PropertyDouble $ fromIntegral (x - 10)
|
|
|
|
, G.Property "y" $ G.PropertyDouble $ fromIntegral (y - 10)
|
|
|
|
]
|
|
|
|
liftIO $ traceIO $ show x ++ " " ++ show y
|
2017-03-16 19:12:41 +00:00
|
|
|
|
|
|
|
handle (SDL.WindowClosedEvent _) = do
|
|
|
|
traceM "seeya!"
|
|
|
|
quit
|
|
|
|
|
|
|
|
handle _ =
|
|
|
|
return ()
|
2016-12-11 11:11:58 +00:00
|
|
|
|
|
|
|
clean :: UserData -> IO ()
|
|
|
|
clean _ = return ()
|