affection/examples/example00.hs

120 lines
3.2 KiB
Haskell
Raw Normal View History

2016-11-04 15:06:16 +00:00
{-# LANGUAGE RecordWildCards #-}
2016-05-29 16:01:23 +00:00
import Affection
2016-11-04 15:06:16 +00:00
import qualified SDL
import qualified SDL.Raw as Raw
import qualified GEGL as G
import qualified BABL as B
import qualified Data.Map.Strict as M
import Foreign.Storable (peek)
import Foreign.C.Types (CInt(..))
import Debug.Trace
-- main :: IO ()
-- main = withAllAffection $
-- withDefaultWindow "test" $ do
-- changeColor $ RGBA 255 255 255 255
-- clear
-- present
-- liftIO $ delaySec 2
2016-05-29 16:01:23 +00:00
main :: IO ()
2016-11-04 15:06:16 +00:00
main = do
conf <- return $ AffectionConfig
{ initComponents = All
, windowTitle = "Affection: example00"
, windowConfig = SDL.defaultWindow
2016-12-12 01:10:30 +00:00
, preLoop = return ()
2016-11-04 15:06:16 +00:00
, drawLoop = draw
, updateLoop = update
, loadState = load
2016-11-13 12:46:23 +00:00
, cleanUp = clean
2016-11-04 15:06:16 +00:00
}
withAffection conf
2016-11-04 18:05:43 +00:00
data UserData = UserData
{ nodeGraph :: M.Map String G.GeglNode
, elapsedTime :: Double
}
2016-11-04 15:06:16 +00:00
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"
2016-11-04 23:41:49 +00:00
over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over"
text <- G.gegl_node_new_child root $ G.textOperation
[ G.Property "string" $ G.PropertyString "Hello world!"
, G.Property "color" $ G.PropertyColor $ G.RGBA 0 0 1 0.1
2016-11-04 23:41:49 +00:00
, G.Property "size" $ G.PropertyDouble 40
]
traceM "text"
G.gegl_node_link checkerboard over
G.gegl_node_connect_to text "output" over "aux"
traceM "connections made"
2016-11-04 15:06:16 +00:00
myMap <- return $ M.fromList
[ ("root" , root)
2016-11-04 23:41:49 +00:00
, ("over" , over)
2016-11-04 15:06:16 +00:00
, ("checkerboard", checkerboard)
2016-11-04 23:41:49 +00:00
, ("text" , text)
2016-11-04 15:06:16 +00:00
]
traceM "loading complete"
2016-11-04 18:05:43 +00:00
return $ UserData
{ nodeGraph = myMap
, elapsedTime = 0
}
2016-11-04 15:06:16 +00:00
2016-12-08 17:22:29 +00:00
draw :: Affection UserData ()
2016-11-04 15:06:16 +00:00
draw = do
traceM "drawing"
AffectionData{..} <- get
2016-11-04 18:05:43 +00:00
let UserData{..} = userState
2016-11-04 15:06:16 +00:00
liftIO $ SDL.lockSurface drawSurface
pixels <- liftIO $ SDL.surfacePixels drawSurface
let SDL.Surface rawSurfacePtr _ = drawSurface
rawSurface <- liftIO $ peek rawSurfacePtr
pixelFormat <- liftIO $ peek $ Raw.surfaceFormat rawSurface
2016-11-04 23:41:49 +00:00
format <- liftIO $ (B.babl_format $ B.PixelFormat B.RGBA B.CFu8)
2016-11-04 15:06:16 +00:00
SDL.V2 (CInt rw) (CInt rh) <- SDL.surfaceDimensions drawSurface
let (w, h) = (fromIntegral rw, fromIntegral rh)
liftIO $ G.gegl_node_blit
2016-11-04 23:41:49 +00:00
(nodeGraph M.! "over" :: G.GeglNode)
2016-11-04 15:06:16 +00:00
1
(G.GeglRectangle 0 0 w h)
format
pixels
(fromIntegral (Raw.pixelFormatBytesPerPixel pixelFormat) * w)
[G.GeglBlitDefault]
liftIO $ SDL.unlockSurface drawSurface
liftIO $ SDL.updateWindowSurface drawWindow
2016-12-08 17:22:29 +00:00
update :: Double -> Affection UserData ()
2016-11-04 15:06:16 +00:00
update sec = do
traceM "updating"
2016-11-04 18:05:43 +00:00
-- liftIO $ delaySec 5
ad@AffectionData{..} <- get
let ud@UserData{..} = userState
traceM $ show elapsedTime
if elapsedTime < 5
then
put $ ad
{ userState = ud
{ elapsedTime = elapsedTime + sec
}
}
else
put $ ad
{ quitEvent = True
}
2016-11-13 12:46:23 +00:00
clean :: UserData -> IO ()
clean _ = return ()