2016-12-13 10:08:49 +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 Foreign.C.Types
|
|
|
|
|
|
|
|
import System.Random (randomRIO)
|
|
|
|
|
|
|
|
import Debug.Trace
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
conf <- return $ AffectionConfig
|
|
|
|
{ initComponents = All
|
|
|
|
, windowTitle = "Affection: example00"
|
|
|
|
, windowConfig = SDL.defaultWindow
|
|
|
|
, preLoop = drawInit
|
|
|
|
, drawLoop = draw
|
|
|
|
, updateLoop = update
|
|
|
|
, loadState = load
|
|
|
|
, cleanUp = clean
|
|
|
|
}
|
|
|
|
withAffection conf
|
|
|
|
|
|
|
|
data UserData = UserData
|
|
|
|
{ nodeGraph :: M.Map String G.GeglNode
|
|
|
|
, foreground :: G.GeglBuffer
|
|
|
|
, partsys :: ParticleSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
load :: SDL.Surface -> IO UserData
|
|
|
|
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-23 13:18:39 +00:00
|
|
|
buffer@(G.GeglBuffer b) <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<<
|
2016-12-13 10:08:49 +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-13 10:08:49 +00:00
|
|
|
]
|
2016-12-23 13:18:39 +00:00
|
|
|
traceM "buffer-sink"
|
|
|
|
nop <- G.gegl_node_new_child root $ G.Operation "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
|
|
|
|
]
|
|
|
|
traceM "crop"
|
|
|
|
G.gegl_node_link_many [checkerboard, over, crop, sink]
|
|
|
|
G.gegl_node_connect_to nop "output" over "aux"
|
2016-12-13 10:08:49 +00:00
|
|
|
traceM "connections made"
|
|
|
|
myMap <- return $ M.fromList
|
|
|
|
[ ("root" , root)
|
|
|
|
, ("over" , over)
|
|
|
|
, ("background" , checkerboard)
|
2016-12-23 13:18:39 +00:00
|
|
|
, ("sink" , sink)
|
|
|
|
, ("nop" , nop)
|
|
|
|
, ("crop" , crop)
|
2016-12-13 10:08:49 +00:00
|
|
|
]
|
|
|
|
traceM "loading complete"
|
|
|
|
return $ UserData
|
|
|
|
{ nodeGraph = myMap
|
|
|
|
, foreground = buffer
|
2016-12-23 13:18:39 +00:00
|
|
|
, partsys = ParticleSystem [] nop buffer
|
2016-12-13 10:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drawInit :: Affection UserData ()
|
2016-12-23 13:18:39 +00:00
|
|
|
drawInit = return ()
|
|
|
|
-- UserData{..} <- getAffection
|
|
|
|
-- present (nodeGraph M.! "over") foreground (GeglRectangle 0 0 800 600) True
|
2016-12-13 10:08:49 +00:00
|
|
|
|
|
|
|
draw :: Affection UserData ()
|
|
|
|
draw = do
|
|
|
|
traceM "drawing"
|
2016-12-23 13:18:39 +00:00
|
|
|
UserData{..} <- getAffection
|
|
|
|
liftIO $ G.gegl_node_process $ nodeGraph M.! "sink"
|
2016-12-13 10:08:49 +00:00
|
|
|
-- ad <- get
|
|
|
|
-- ud <- getAffection
|
|
|
|
-- drawParticles partDraw $ particles ud
|
|
|
|
-- SDL.V2 (CInt rw) (CInt rh) <- SDL.surfaceDimensions $ drawSurface ad
|
|
|
|
-- let (w, h) = (fromIntegral rw, fromIntegral rh)
|
|
|
|
-- liftIO $ clearArea (foreground ud) (GeglRectangle 0 0 w h)
|
|
|
|
-- maybe (return ()) (\(x, y) ->
|
|
|
|
-- drawRect
|
|
|
|
-- (foreground ud)
|
|
|
|
-- (nodeGraph ud M.! "over")
|
|
|
|
-- (G.RGBA 1 0 0 0.5)
|
|
|
|
-- (Line 7)
|
|
|
|
-- (G.GeglRectangle (x - 10) (y - 10) 20 20)
|
|
|
|
-- ) $ coordinates ud
|
|
|
|
|
|
|
|
update :: Double -> AffectionState (AffectionData UserData) IO ()
|
|
|
|
update sec = do
|
|
|
|
traceM "updating"
|
|
|
|
ad <- get
|
|
|
|
ud <- getAffection
|
|
|
|
-- let newPart = updateParticles sec partUpd $ particles ud
|
|
|
|
-- putAffection $ ud { particles = newPart }
|
|
|
|
traceM $ (show $ 1 / sec) ++ " FPS"
|
2016-12-18 18:01:53 +00:00
|
|
|
ev <- liftIO $ SDL.pollEvents
|
|
|
|
mapM_ (\e ->
|
2016-12-13 10:08:49 +00:00
|
|
|
case SDL.eventPayload e of
|
2016-12-20 04:27:45 +00:00
|
|
|
SDL.MouseMotionEvent dat ->
|
|
|
|
if SDL.ButtonLeft `elem` SDL.mouseMotionEventState dat
|
|
|
|
then do
|
|
|
|
let (SDL.P (SDL.V2 x y)) = SDL.mouseMotionEventPos dat
|
2016-12-13 10:08:49 +00:00
|
|
|
vx <- liftIO $ randomRIO (-20, 20)
|
|
|
|
vy <- liftIO $ randomRIO (-20, 20)
|
2016-12-18 18:01:53 +00:00
|
|
|
life <- liftIO $ randomRIO (1, 5)
|
2016-12-23 13:18:39 +00:00
|
|
|
tempRoot <- liftIO $ G.gegl_node_new
|
|
|
|
tempOver <- liftIO $ G.gegl_node_new_child tempRoot
|
|
|
|
G.defaultOverOperation
|
|
|
|
tempNop <- liftIO $ G.gegl_node_new_child tempRoot $ G.Operation
|
|
|
|
"gegl:nop" []
|
|
|
|
liftIO $ G.gegl_node_connect_to tempNop "output" tempOver "aux"
|
2016-12-20 05:28:18 +00:00
|
|
|
-- traceM $ "position is: " ++ show x ++ " " ++ show y
|
|
|
|
-- traceM $ "velocity is: " ++ show vx ++ " " ++ show vy
|
2016-12-13 10:08:49 +00:00
|
|
|
putAffection $ ud
|
2016-12-23 13:18:39 +00:00
|
|
|
{ partsys = (partsys ud)
|
2016-12-13 10:08:49 +00:00
|
|
|
{ psParts = (Particle
|
2016-12-18 18:01:53 +00:00
|
|
|
{ particleLife = life
|
2016-12-13 10:08:49 +00:00
|
|
|
, particlePosition = (fromIntegral x, fromIntegral y)
|
2016-12-20 10:29:03 +00:00
|
|
|
, particleRotation = Rad 0
|
2016-12-13 10:08:49 +00:00
|
|
|
, particleVelocity = (vx, vy)
|
2016-12-20 10:29:03 +00:00
|
|
|
, particlePitchRate = Rad 0
|
2016-12-23 13:18:39 +00:00
|
|
|
, particleRootNode = tempRoot
|
|
|
|
, particleNodeGraph = M.fromList
|
|
|
|
[ ("root", tempRoot)
|
|
|
|
, ("over", tempOver)
|
|
|
|
, ("nop" , tempNop)
|
|
|
|
]
|
|
|
|
, particleStackCont = tempOver
|
|
|
|
, particleDrawFlange = tempNop
|
2016-12-13 10:08:49 +00:00
|
|
|
}) : (psParts $ partsys ud)
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 04:27:45 +00:00
|
|
|
else
|
|
|
|
return ()
|
2016-12-13 10:08:49 +00:00
|
|
|
SDL.WindowClosedEvent _ -> do
|
|
|
|
traceM "seeya!"
|
|
|
|
put $ ad
|
|
|
|
{ quitEvent = True
|
|
|
|
}
|
|
|
|
_ ->
|
|
|
|
return ()
|
|
|
|
) ev
|
2016-12-20 05:28:18 +00:00
|
|
|
ud2 <- getAffection
|
|
|
|
nps <- updateParticleSystem (partsys ud2) sec partUpd partDraw
|
|
|
|
putAffection $ ud2 { partsys = nps }
|
2016-12-13 10:08:49 +00:00
|
|
|
|
|
|
|
clean :: UserData -> IO ()
|
|
|
|
clean _ = return ()
|
|
|
|
|
|
|
|
partUpd :: Double -> Particle -> Particle
|
|
|
|
partUpd sec p@Particle{..} =
|
|
|
|
p
|
|
|
|
{ particlePosition = (newX, newY)
|
|
|
|
}
|
|
|
|
where
|
|
|
|
newX = (fst particlePosition) + sec * (fromIntegral $ fst particleVelocity)
|
|
|
|
newY = (snd particlePosition) + sec * (fromIntegral $ snd particleVelocity)
|
|
|
|
|
|
|
|
partDraw :: G.GeglBuffer -> G.GeglNode -> Particle -> Affection UserData ()
|
|
|
|
partDraw buf node Particle{..} = do
|
|
|
|
ud <- getAffection
|
2016-12-23 13:18:39 +00:00
|
|
|
drawRect'
|
|
|
|
particleDrawFlange
|
2016-12-13 10:08:49 +00:00
|
|
|
(G.RGBA 1 0 0 0.5)
|
2016-12-23 13:18:39 +00:00
|
|
|
(Fill)
|
2016-12-13 10:08:49 +00:00
|
|
|
(G.GeglRectangle ((floor $ fst particlePosition) - 10) ((floor $ snd particlePosition) -10) 20 20)
|
2016-12-23 13:18:39 +00:00
|
|
|
buf
|