vulkan-tutorial/examples/example04.hs

223 lines
6.1 KiB
Haskell
Raw Normal View History

2016-12-21 03:33:28 +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 Data.List as L
import Data.Maybe (fromJust)
import Control.Monad (when)
import qualified Control.Monad.Parallel as MP
2016-12-21 03:33:28 +00:00
2017-03-23 03:34:04 +00:00
import Foreign.C.Types
2016-12-21 03:33:28 +00:00
import Debug.Trace
main :: IO ()
main = do
conf <- return AffectionConfig
2016-12-21 03:33:28 +00:00
{ initComponents = All
, windowTitle = "Affection: example00"
, windowConfig = SDL.defaultWindow
2017-03-23 03:31:10 +00:00
, canvasSize = Just (3289, 600)
2016-12-21 03:33:28 +00:00
, preLoop = return ()
, eventLoop = handle
2016-12-21 03:33:28 +00:00
, updateLoop = update
, drawLoop = draw
2016-12-21 03:33:28 +00:00
, loadState = load
, cleanUp = clean
}
withAffection conf
data UserData = UserData
{ nodeGraph :: M.Map String G.GeglNode
, actors :: M.Map String (Actor String)
, foreground :: G.GeglBuffer
, updateActors :: [Actor String]
2017-03-28 22:43:59 +00:00
, keysDown :: [SDL.Keycode]
2017-03-24 20:58:53 +00:00
, cameraX :: Double
2016-12-21 03:33:28 +00:00
}
2017-03-23 03:27:57 +00:00
load :: IO UserData
load = do
2016-12-21 03:33:28 +00:00
traceM "loading"
root <- G.gegl_node_new
traceM "new root node"
let bgProps = props $ prop "path" ("examples/example04/panorama.jpg" :: String)
bg <- G.gegl_node_new_child root $ G.Operation "gegl:jpg-load" bgProps
bgScaleProps <- return $ props $ do
prop "y" (600 :: Double)
prop "sampler" (fromEnum G.GeglSamplerCubic)
bgScale <- G.gegl_node_new_child root $
G.Operation "gegl:scale-size-keepaspect" bgScaleProps
bgTransProps <- return $ props $ do
prop "x" (0 :: Double)
prop "y" (0 :: Double)
prop "sampler" (fromEnum G.GeglSamplerCubic)
bgTrans <- G.gegl_node_new_child root $ G.Operation "gegl:translate" bgTransProps
traceM "background"
2016-12-21 03:33:28 +00:00
over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over"
2017-03-23 03:34:04 +00:00
buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 3289 600) =<<
B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $
props $
prop "buffer" buffer
traceM "buffer-sink"
rectProps <- return $ props $ do
prop "x" (50 :: Double)
prop "y" (290 :: Double)
prop "width" (20::Double)
prop "height" (20::Double)
prop "color" $ G.RGBA 1 0 0 0.5
rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle" rectProps
traceM "rect"
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $
props $ do
2017-03-23 03:34:04 +00:00
prop "width" (3289 :: Double)
prop "height" (600 :: Double)
G.gegl_node_link_many [bg, bgTrans, bgScale, over, crop, sink]
_ <- G.gegl_node_connect_to rect "output" over "aux"
rectActor <- return $ Actor (map
(\p -> ActorProperty
(G.propertyName p)
(G.propertyValue p)
(Just "rect")
)rectProps
) (M.singleton "rect" rect)
bgActor <- return $ Actor (map
(\p -> ActorProperty
(G.propertyName p)
(G.propertyValue p)
(Just "bg")
)bgProps ++
map (\p -> ActorProperty
(G.propertyName p)
(G.propertyValue p)
(Just "scale")
) bgScaleProps ++
map (\p -> ActorProperty
(G.propertyName p)
(G.propertyValue p)
(Just "trans")
) bgTransProps
) (M.fromList
[ ("bg", bg)
, ("trans", bgTrans)
, ("scale", bgScale)
])
2016-12-21 03:33:28 +00:00
traceM "connections made"
myMap <- return $ M.fromList
[ ("root" , root)
, ("over" , over)
, ("background" , bg)
2016-12-21 03:33:28 +00:00
, ("sink" , sink)
, ("rect" , rect)
, ("crop" , crop)
2016-12-21 03:33:28 +00:00
]
traceM "loading complete"
actorMap <- return $ M.fromList
[ ("rect", rectActor)
, ("background", bgActor)
]
return UserData
{ nodeGraph = myMap
, actors = actorMap
, foreground = buffer
, updateActors = []
2017-03-28 22:43:59 +00:00
, keysDown = []
2017-03-23 03:34:04 +00:00
, cameraX = 0
2016-12-21 03:33:28 +00:00
}
2017-03-23 03:34:04 +00:00
drawInit :: Affection UserData ()
drawInit = do
ad <- get
ud <- getAffection
process (nodeGraph ud M.! "sink")
2017-03-28 22:44:36 +00:00
present (GeglRectangle (round $ cameraX ud) 0 800 600) (foreground ud) True
render Nothing Nothing
2016-12-21 03:33:28 +00:00
draw :: Affection UserData ()
draw = do
ud@UserData{..} <- getAffection
MP.mapM_ applyProperties updateActors
putAffection ud
{ updateActors = []
}
process (nodeGraph M.! "sink")
2017-03-24 20:58:53 +00:00
present (GeglRectangle (round cameraX) 0 800 600) foreground True
2017-03-23 03:34:04 +00:00
render
2017-03-24 20:58:53 +00:00
(Just $ G.GeglRectangle (round cameraX) 0 800 600)
2017-03-23 03:34:04 +00:00
Nothing
2017-03-23 03:34:04 +00:00
update :: Double -> Affection UserData ()
update dt = do
traceM "updating"
2016-12-21 03:33:28 +00:00
ud <- getAffection
traceM $ show (1 / dt) ++ " FPS"
2017-03-28 22:43:59 +00:00
-- traceM $ show $ keysDown ud
2017-03-28 22:43:59 +00:00
mapM_ (\code -> do
2017-03-23 03:34:04 +00:00
let vel = 400 -- velocity in Pixels per second
2017-03-24 20:58:53 +00:00
leg = vel * dt
(G.PropertyDouble xpos) <-
return $ apValue $ fromJust $ L.find (\a -> "x" == apName a) $
actorProperties $ actors ud M.! "rect"
2017-03-23 03:34:04 +00:00
nmap <- return $ M.adjust
2017-03-24 20:58:53 +00:00
(updateProperties $ props $ prop "x" $ xpos +
2017-03-23 03:34:04 +00:00
case code of
SDL.KeycodeLeft ->
2017-03-24 20:58:53 +00:00
if xpos - leg > 0
then (- leg)
else 0
2017-03-23 03:34:04 +00:00
SDL.KeycodeRight ->
2017-03-24 20:58:53 +00:00
if xpos + leg < 3269
then leg
else 0
_ -> 0
2017-03-23 03:34:04 +00:00
)
"rect"
(actors ud)
2017-03-24 20:58:53 +00:00
let {
2017-03-28 22:44:36 +00:00
offset | xpos - cameraX ud > 750 && cameraX ud + leg < 2489 && code == SDL.KeycodeRight =
leg
| xpos - cameraX ud < 50 && cameraX ud - leg > 0 && code == SDL.KeycodeLeft =
(-leg)
2017-03-24 20:58:53 +00:00
| otherwise = 0
}
2017-03-23 03:34:04 +00:00
putAffection ud
{ actors = nmap
, updateActors = (nmap M.! "rect") : updateActors ud
2017-03-24 20:58:53 +00:00
, cameraX = cameraX ud + offset
2017-03-23 03:34:04 +00:00
}
2017-03-28 22:43:59 +00:00
) (keysDown ud)
2017-03-23 03:34:04 +00:00
handle :: SDL.EventPayload -> Affection UserData ()
handle (SDL.KeyboardEvent dat) =
when (not (SDL.keyboardEventRepeat dat)) $ do
ud <- getAffection
if (SDL.keyboardEventKeyMotion dat == SDL.Pressed)
then
putAffection ud
2017-03-28 22:43:59 +00:00
{ keysDown =
SDL.keysymKeycode (SDL.keyboardEventKeysym dat) : keysDown ud
}
2017-03-23 03:34:04 +00:00
else
putAffection ud
2017-03-28 22:43:59 +00:00
{ keysDown =
delete (SDL.keysymKeycode $ SDL.keyboardEventKeysym dat) (keysDown ud)
}
handle (SDL.WindowClosedEvent _) = do
traceM "seeya!"
quit
handle _ =
return ()
2016-12-21 03:33:28 +00:00
clean :: UserData -> IO ()
clean _ = return ()