2016-12-13 10:08:49 +00:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2017-02-25 16:23:54 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
2016-12-13 10:08:49 +00:00
|
|
|
|
|
|
|
import Affection
|
|
|
|
import qualified SDL
|
|
|
|
import qualified GEGL as G
|
|
|
|
import qualified BABL as B
|
2016-12-24 07:27:09 +00:00
|
|
|
|
2016-12-13 10:08:49 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
|
2016-12-24 07:27:09 +00:00
|
|
|
import Control.Monad (when)
|
|
|
|
|
2016-12-13 10:08:49 +00:00
|
|
|
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
|
2017-03-16 19:12:41 +00:00
|
|
|
, eventLoop = handle
|
2016-12-13 10:08:49 +00:00
|
|
|
, updateLoop = update
|
2017-03-16 19:12:41 +00:00
|
|
|
, drawLoop = draw
|
2016-12-13 10:08:49 +00:00
|
|
|
, loadState = load
|
|
|
|
, cleanUp = clean
|
|
|
|
}
|
|
|
|
withAffection conf
|
|
|
|
|
|
|
|
data UserData = UserData
|
|
|
|
{ nodeGraph :: M.Map String G.GeglNode
|
|
|
|
, foreground :: G.GeglBuffer
|
|
|
|
, partsys :: ParticleSystem
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:27:57 +00:00
|
|
|
load :: IO UserData
|
|
|
|
load = do
|
2016-12-13 10:08:49 +00:00
|
|
|
traceM "loading"
|
|
|
|
root <- G.gegl_node_new
|
|
|
|
traceM "new root node"
|
2017-03-20 04:24:30 +00:00
|
|
|
rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle" $
|
|
|
|
props $ do
|
|
|
|
prop "x" (0 :: Double)
|
|
|
|
prop "y" (0 :: Double)
|
|
|
|
prop "width" (800 :: Double)
|
|
|
|
prop "height" (600 :: Double)
|
|
|
|
prop "color" $ G.RGB 0 0 0
|
|
|
|
traceM "rect"
|
2016-12-13 10:08:49 +00:00
|
|
|
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-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"
|
2016-12-26 13:06:51 +00:00
|
|
|
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
|
2016-12-23 13:18:39 +00:00
|
|
|
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"
|
2017-03-20 04:24:30 +00:00
|
|
|
G.gegl_node_link_many [rect, over, crop, sink]
|
2016-12-23 13:18:39 +00:00
|
|
|
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)
|
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-25 07:14:51 +00:00
|
|
|
, partsys = ParticleSystem (ParticleStorage Nothing []) nop buffer
|
2016-12-13 10:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drawInit :: Affection UserData ()
|
2017-03-20 04:24:30 +00:00
|
|
|
drawInit = return ()
|
|
|
|
-- drawInit = do
|
|
|
|
-- UserData{..} <- getAffection
|
|
|
|
-- present (GeglRectangle 0 0 800 600) foreground 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
|
2017-03-20 04:24:30 +00:00
|
|
|
drawParticleSystem partsys partDraw
|
2017-03-16 19:52:45 +00:00
|
|
|
process $ nodeGraph M.! "sink"
|
2017-03-20 04:24:30 +00:00
|
|
|
present
|
|
|
|
(G.GeglRectangle 0 0 800 600)
|
|
|
|
foreground
|
|
|
|
True
|
2016-12-13 10:08:49 +00:00
|
|
|
|
2017-03-16 19:12:41 +00:00
|
|
|
update :: Affection UserData ()
|
|
|
|
update = do
|
2016-12-13 10:08:49 +00:00
|
|
|
traceM "updating"
|
|
|
|
ad <- get
|
|
|
|
ud <- getAffection
|
2017-03-16 19:12:41 +00:00
|
|
|
delta <- getDelta
|
|
|
|
traceM $ (show $ 1 / delta) ++ " FPS"
|
|
|
|
ud2 <- getAffection
|
2017-03-20 04:24:30 +00:00
|
|
|
!nps <- updateParticleSystem (partsys ud2) delta partUpd
|
2017-03-16 19:12:41 +00:00
|
|
|
putAffection $ ud2 { partsys = nps }
|
|
|
|
|
|
|
|
handle (SDL.MouseMotionEvent dat) =
|
2017-03-20 04:24:30 +00:00
|
|
|
when (SDL.ButtonLeft `elem` SDL.mouseMotionEventState dat)
|
|
|
|
$ do
|
|
|
|
ad <- get
|
|
|
|
ud <- getAffection
|
|
|
|
let (SDL.P (SDL.V2 x y)) = SDL.mouseMotionEventPos dat
|
|
|
|
vx <- liftIO $ randomRIO (-20, 20)
|
|
|
|
vy <- liftIO $ randomRIO (-20, 20)
|
|
|
|
life <- liftIO $ randomRIO (1, 5)
|
|
|
|
r <- liftIO $ randomRIO (0,1)
|
|
|
|
g <- liftIO $ randomRIO (0,1)
|
|
|
|
b <- liftIO $ randomRIO (0,1)
|
|
|
|
tempRoot <- liftIO $ G.gegl_node_new
|
|
|
|
tempOver <- liftIO $ G.gegl_node_new_child tempRoot
|
|
|
|
G.defaultOverOperation
|
|
|
|
tempRect <- liftIO $ G.gegl_node_new_child tempRoot $ G.Operation
|
|
|
|
"gegl:rectangle"
|
|
|
|
[ G.Property "x" $ G.PropertyDouble $ fromIntegral x - 10
|
|
|
|
, G.Property "y" $ G.PropertyDouble $ fromIntegral y - 10
|
|
|
|
, G.Property "width" $ G.PropertyDouble 20
|
|
|
|
, G.Property "height" $ G.PropertyDouble 20
|
|
|
|
, G.Property "color" $ G.PropertyColor $ (G.RGBA r g b 0.5)
|
|
|
|
]
|
|
|
|
liftIO $ G.gegl_node_connect_to tempRect "output" tempOver "aux"
|
|
|
|
-- traceM $ "position is: " ++ show x ++ " " ++ show y
|
|
|
|
-- traceM $ "velocity is: " ++ show vx ++ " " ++ show vy
|
|
|
|
ips <- insertParticle (partsys ud) $
|
|
|
|
Particle
|
|
|
|
{ particleTimeToLive = life
|
|
|
|
, particleCreation = elapsedTime ad
|
|
|
|
, particlePosition = (fromIntegral x, fromIntegral y)
|
|
|
|
, particleRotation = Rad 0
|
|
|
|
, particleVelocity = (vx, vy)
|
|
|
|
, particlePitchRate = Rad 0
|
|
|
|
, particleRootNode = tempRoot
|
|
|
|
, particleNodeGraph = M.fromList
|
|
|
|
[ ("root", tempRoot)
|
|
|
|
, ("over", tempOver)
|
|
|
|
, ("rect", tempRect)
|
|
|
|
]
|
|
|
|
, particleStackCont = tempOver
|
|
|
|
, particleDrawFlange = tempOver
|
|
|
|
}
|
|
|
|
putAffection $ ud
|
|
|
|
{ partsys = ips
|
|
|
|
}
|
|
|
|
-- when (not $ null $ psParts $ partsys ud) $
|
|
|
|
-- liftIO $ G.gegl_node_link
|
|
|
|
-- tempOver
|
|
|
|
-- (particleStackCont $ head $ psParts $ partsys ud)
|
2017-03-16 19:12:41 +00:00
|
|
|
|
|
|
|
handle (SDL.WindowClosedEvent _) = do
|
|
|
|
traceM "seeya!"
|
|
|
|
quit
|
|
|
|
|
|
|
|
handle _ =
|
|
|
|
return ()
|
2016-12-13 10:08:49 +00:00
|
|
|
|
|
|
|
clean :: UserData -> IO ()
|
2017-03-20 04:24:30 +00:00
|
|
|
clean ud = do
|
|
|
|
G.gegl_node_drop (nodeGraph ud M.! "root")
|
2016-12-13 10:08:49 +00:00
|
|
|
|
2016-12-24 00:13:00 +00:00
|
|
|
partUpd :: Double -> Particle -> Affection UserData Particle
|
2017-02-25 16:23:54 +00:00
|
|
|
partUpd sec p = do
|
|
|
|
let !newX = (fst $ particlePosition p) + sec * (fromIntegral $ fst $ particleVelocity p)
|
|
|
|
!newY = (snd $ particlePosition p) + sec * (fromIntegral $ snd $ particleVelocity p)
|
|
|
|
liftIO $ G.gegl_node_set (particleNodeGraph p M.! "rect") $ G.Operation "gegl:rectangle"
|
2016-12-24 00:13:00 +00:00
|
|
|
[ G.Property "x" $ G.PropertyDouble $ newX - 10
|
|
|
|
, G.Property "y" $ G.PropertyDouble $ newY - 10
|
|
|
|
]
|
2017-02-25 16:23:54 +00:00
|
|
|
let !np = p {particlePosition = (newX, newY)}
|
|
|
|
return np
|
2016-12-13 10:08:49 +00:00
|
|
|
|
|
|
|
partDraw :: G.GeglBuffer -> G.GeglNode -> Particle -> Affection UserData ()
|
2017-03-20 04:24:02 +00:00
|
|
|
partDraw _ _ _ = return ()
|
|
|
|
-- partDraw buf node Particle{..} = do
|
|
|
|
-- present
|
|
|
|
-- (G.GeglRectangle (floor $ fst particlePosition - 10) (floor $ snd particlePosition - 10) 20 20)
|
|
|
|
-- buf
|
|
|
|
-- False
|
|
|
|
-- -- ud <- getAffection
|
|
|
|
-- -- drawRect'
|
|
|
|
-- -- particleDrawFlange
|
|
|
|
-- -- (G.RGBA 1 0 0 0.5)
|
|
|
|
-- -- (Fill)
|
|
|
|
-- -- (G.GeglRectangle ((floor $ fst particlePosition) - 10) ((floor $ snd particlePosition) -10) 20 20)
|
|
|
|
-- -- buf
|