2016-12-11 11:11:58 +00:00
|
|
|
{-# 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
|
2016-12-11 11:11:58 +00:00
|
|
|
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)
|
2016-12-11 11:11:58 +00:00
|
|
|
|
|
|
|
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 ()
|
2016-12-11 11:11:58 +00:00
|
|
|
, drawLoop = draw
|
|
|
|
, updateLoop = update
|
|
|
|
, loadState = load
|
|
|
|
, cleanUp = clean
|
|
|
|
}
|
|
|
|
withAffection conf
|
|
|
|
|
|
|
|
data UserData = UserData
|
|
|
|
{ nodeGraph :: M.Map String G.GeglNode
|
|
|
|
, foreground :: G.GeglBuffer
|
|
|
|
, coordinates :: Maybe (Int, Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 03:28:57 +00:00
|
|
|
buffer@(G.GeglBuffer b) <- 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-21 03:28:57 +00:00
|
|
|
bufPtr <- new b
|
|
|
|
sink <- G.gegl_node_new_child root $ G.Operation "gegl:buffer-sink"
|
|
|
|
[ G.Property "buffer" $ G.PropertyPointer $ castPtr bufPtr
|
2016-12-11 11:11:58 +00:00
|
|
|
]
|
2016-12-21 03:28:57 +00:00
|
|
|
traceM "buffer-sink"
|
|
|
|
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
|
|
|
|
traceM "nop"
|
|
|
|
G.gegl_node_link_many [checkerboard, over, sink]
|
|
|
|
G.gegl_node_connect_to nop "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)
|
|
|
|
, ("nop" , nop)
|
2016-12-11 11:11:58 +00:00
|
|
|
]
|
|
|
|
traceM "loading complete"
|
|
|
|
return $ UserData
|
|
|
|
{ nodeGraph = myMap
|
|
|
|
, foreground = buffer
|
|
|
|
, coordinates = Nothing
|
|
|
|
}
|
|
|
|
|
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
|
2016-12-11 19:24:16 +00:00
|
|
|
|
2016-12-11 11:11:58 +00:00
|
|
|
draw :: Affection UserData ()
|
|
|
|
draw = do
|
|
|
|
traceM "drawing"
|
2016-12-12 01:11:31 +00:00
|
|
|
ad <- get
|
|
|
|
UserData{..} <- getAffection
|
|
|
|
SDL.V2 (CInt rw) (CInt rh) <- SDL.surfaceDimensions $ drawSurface ad
|
2016-12-11 11:11:58 +00:00
|
|
|
let (w, h) = (fromIntegral rw, fromIntegral rh)
|
2016-12-11 16:38:03 +00:00
|
|
|
liftIO $ clearArea foreground (GeglRectangle 0 0 w h)
|
|
|
|
maybe (return ()) (\(x, y) ->
|
2016-12-21 03:28:57 +00:00
|
|
|
drawRect'
|
|
|
|
(nodeGraph M.! "nop")
|
2016-12-11 19:24:16 +00:00
|
|
|
(G.RGBA 1 0 0 0.5)
|
2016-12-21 03:28:57 +00:00
|
|
|
(Fill)
|
2016-12-11 16:38:03 +00:00
|
|
|
(G.GeglRectangle (x - 10) (y - 10) 20 20)
|
2016-12-21 03:28:57 +00:00
|
|
|
foreground
|
2016-12-11 11:11:58 +00:00
|
|
|
) coordinates
|
|
|
|
|
|
|
|
update :: Double -> AffectionState (AffectionData UserData) IO ()
|
|
|
|
update sec = do
|
|
|
|
traceM "updating"
|
2016-12-12 01:11:31 +00:00
|
|
|
ad <- get
|
|
|
|
ud <- getAffection
|
2016-12-11 11:11:58 +00:00
|
|
|
traceM $ (show $ 1 / sec) ++ " FPS"
|
|
|
|
ev <- liftIO $ SDL.pollEvent
|
|
|
|
maybe (return ()) (\e ->
|
|
|
|
case SDL.eventPayload e of
|
|
|
|
SDL.MouseMotionEvent dat -> do
|
|
|
|
let (SDL.P (SDL.V2 x y)) = SDL.mouseMotionEventPos dat
|
2016-12-12 01:11:31 +00:00
|
|
|
putAffection $ ud
|
|
|
|
{ coordinates = Just (fromIntegral x, fromIntegral y)
|
2016-12-11 11:11:58 +00:00
|
|
|
}
|
|
|
|
SDL.WindowClosedEvent _ -> do
|
|
|
|
traceM "seeya!"
|
|
|
|
put $ ad
|
|
|
|
{ quitEvent = True
|
|
|
|
}
|
|
|
|
_ ->
|
|
|
|
return ()
|
|
|
|
) ev
|
|
|
|
|
|
|
|
clean :: UserData -> IO ()
|
|
|
|
clean _ = return ()
|