pituicat/examples/example00.hs

112 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 ()
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
}
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!"
2016-12-24 00:10:18 +00:00
, G.Property "color" $ G.PropertyColor $ G.RGBA 0 0 1 0.5
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
}
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
2017-02-17 16:15:06 +00:00
update :: Double -> [SDL.Event] -> 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
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 ()