From: Daniel W. <dm...@lu...> - 2012-04-22 20:24:09
|
diffing dir... Sun Apr 22 20:52:06 BST 2012 Daniel Wagner <da...@wa...> * add a surface type for raw data, plus a demo program showing how to use this with SDL (code supplied by Eli Frey) Ignore-this: 254fd58bd2322a9fecb5449741d7a697 hunk ./cairo/Graphics/Rendering/Cairo.hs 200 + , withImageSurfaceForData hunk ./cairo/Graphics/Rendering/Cairo.hs 204 + , createImageSurfaceForData hunk ./cairo/Graphics/Rendering/Cairo.hs 1688 +-- | Like 'withImageSurface' but creating a surface to target external +-- data pointed to by 'PixelData'. +-- +withImageSurfaceForData :: + PixelData -- ^ pointer to pixel data + -> Format -- ^ format of pixels in the surface to create + -> Int -- ^ width of the surface, in pixels + -> Int -- ^ height of the surface, in pixels + -> Int -- ^ size of stride between rows in the surface to create + -> (Surface -> IO a) -- ^ an action that may use the surface. The surface is + -- only valid within this action + -> IO a +withImageSurfaceForData pixels format width height stride f = + bracket (Internal.imageSurfaceCreateForData pixels format width height stride) + (\surface -> do status <- Internal.surfaceStatus surface + Internal.surfaceDestroy surface + unless (status == StatusSuccess) $ + Internal.statusToString status >>= fail) + (\surface -> f surface) + +-- | Like 'createImageSurface' but creating a surface to target external +-- data pointed to by 'PixelData'. +-- +createImageSurfaceForData :: + PixelData -- ^ pointer to pixel data + -> Format -- ^ format of pixels in the surface to create + -> Int -- ^ width of the surface, in pixels + -> Int -- ^ height of the surface, in pixels + -> Int -- ^ size of stride between rows in the surface to create + -> IO Surface +createImageSurfaceForData pixels format width height stride = do + surface <- Internal.imageSurfaceCreateForData pixels format width height stride + Internal.manageSurface surface + return surface + hunk ./cairo/Graphics/Rendering/Cairo/Internal/Surfaces/Image.chs 24 +{#fun image_surface_create_for_data as imageSurfaceCreateForData + { id `Ptr CUChar' + , cFromEnum `Format' + , `Int' + , `Int' + , `Int' + } -> `Surface' mkSurface*#} + hunk ./cairo/Graphics/Rendering/Cairo/Types.chs 17 - Matrix(Matrix), MatrixPtr + PixelData + , Matrix(Matrix), MatrixPtr hunk ./cairo/Graphics/Rendering/Cairo/Types.chs 67 +type PixelData = Ptr CUChar + addfile ./cairo/demo/CairoSDL.hs hunk ./cairo/demo/CairoSDL.hs 1 +import qualified Graphics.UI.SDL as SDL +import Graphics.Rendering.Cairo +import Foreign.Ptr ( castPtr ) + +demo1 :: Render () +demo1 = do + setSourceRGB 0 0 0 + moveTo 100 100 + lineTo 500 500 + stroke + +demo2 :: Render () +demo2 = do + setSourceRGB 0 0 0 + moveTo 500 100 + lineTo 100 500 + stroke + +main = SDL.withInit [ SDL.InitVideo ] $ do + screen <- SDL.setVideoMode 600 600 32 [ SDL.SWSurface ] + + SDL.fillRect screen Nothing (SDL.Pixel maxBound) + + pixels <- fmap castPtr $ SDL.surfaceGetPixels screen + + canvas <- createImageSurfaceForData pixels FormatRGB24 600 600 (600 * 4) + renderWith canvas demo1 + + withImageSurfaceForData pixels FormatRGB24 600 600 (600 * 4) $ \canvas -> + renderWith canvas demo2 + + SDL.flip screen + + idle + where + idle = do + e <- SDL.waitEvent + case e of + SDL.Quit -> return () + otherwise -> idle hunk ./cairo/demo/Makefile 2 -PROGS = drawing drawing2 starandring text clock graph -SOURCES = Drawing.hs Drawing2.hs StarAndRing.hs Text.hs Clock.hs Graph.hs +PROGS = drawing drawing2 starandring text clock graph sdldrawing +SOURCES = Drawing.hs Drawing2.hs StarAndRing.hs Text.hs Clock.hs Graph.hs CairoSDL.hs hunk ./cairo/demo/Makefile 25 +sdldrawing : CairoSDL.hs + $(HC_RULE) + |