pituicat/examples/example00.hs

118 lines
3 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
2016-12-26 13:06:51 +00:00
import Control.Monad (when)
2016-11-04 15:06:16 +00:00
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 ()
, eventLoop = handle
2016-11-04 15:06:16 +00:00
, updateLoop = update
, drawLoop = draw
2016-11-04 15:06:16 +00:00
, 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
}
2016-11-04 15:06:16 +00:00
2017-03-23 03:27:57 +00:00
load :: IO UserData
load = do
2016-11-04 15:06:16 +00:00
traceM "loading"
root <- G.gegl_node_new
traceM "new root node"
2017-03-16 22:51:41 +00:00
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $
props $ do
prop "color1" $ G.RGBA 0.4 0.4 0.4 1
prop "color2" $ G.RGBA 0.6 0.6 0.6 1
2016-11-04 15:06:16 +00:00
traceM "checkerboard"
2016-11-04 23:41:49 +00:00
over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over"
2017-03-16 22:51:41 +00:00
text <- G.gegl_node_new_child root $ G.textOperation $
props $ do
prop "string" ("Hello world!"::String)
prop "color" $ G.RGBA 0 0 1 0.5
prop "size" (40::Int)
2016-11-04 23:41:49 +00:00
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
}
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
handle :: SDL.EventPayload -> Affection UserData ()
handle = const $ return ()
2017-03-23 03:29:48 +00:00
update :: Double -> Affection UserData ()
update sec = do
2016-11-04 15:06:16 +00:00
traceM "updating"
2016-12-26 13:06:51 +00:00
ad <- get
ud@UserData{..} <- getAffection
2017-03-23 03:29:48 +00:00
-- sec <- getDelta
2016-12-26 13:06:51 +00:00
traceM $ (show $ 1 / sec) ++ " FPS"
when (elapsedTime ad > 5) $
2016-11-04 18:05:43 +00:00
put $ ad
{ quitEvent = True
}
2016-11-13 12:46:23 +00:00
clean :: UserData -> IO ()
clean _ = return ()