| 1 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 2 |
{-# OPTIONS -O #-} |
| 3 |
|
| 4 |
-- Example of an drawing graphics onto a canvas. |
| 5 |
import Graphics.UI.Gtk |
| 6 |
|
| 7 |
import Control.Applicative |
| 8 |
import Prelude |
| 9 |
import Data.IORef |
| 10 |
import Graphics.Rendering.Cairo |
| 11 |
import Foreign (allocaArray) |
| 12 |
import Graphics.Rendering.Cairo.Types (PixelData) |
| 13 |
import Foreign.Storable (Storable(..)) |
| 14 |
import Foreign.C (CUChar) |
| 15 |
|
| 16 |
|
| 17 |
main = do |
| 18 |
initGUI |
| 19 |
dia <- dialogNew |
| 20 |
dialogAddButton dia stockOk ResponseOk |
| 21 |
contain <- castToBox <$> dialogGetContentArea dia |
| 22 |
canvas <- drawingAreaNew |
| 23 |
let w = 256 |
| 24 |
h = 256 |
| 25 |
chan = 4 |
| 26 |
row = w * chan |
| 27 |
stride = row |
| 28 |
widgetSetSizeRequest canvas 256 256 |
| 29 |
|
| 30 |
-- create the Pixbuf |
| 31 |
allocaArray (w * h * chan) $ \ pbData -> do |
| 32 |
|
| 33 |
-- draw into the Pixbuf |
| 34 |
doFromTo 0 (h-1) $ \y -> |
| 35 |
doFromTo 0 (w-1) $ \x -> do |
| 36 |
pokeByteOff pbData (2+x*chan+y*row) (fromIntegral x :: CUChar) |
| 37 |
pokeByteOff pbData (1+x*chan+y*row) (fromIntegral y :: CUChar) |
| 38 |
pokeByteOff pbData (0+x*chan+y*row) (0 :: CUChar) |
| 39 |
|
| 40 |
-- a function to update the Pixbuf |
| 41 |
blueRef <- newIORef (0 :: CUChar) |
| 42 |
dirRef <- newIORef True |
| 43 |
let updateBlue = do |
| 44 |
blue <- readIORef blueRef |
| 45 |
-- print blue |
| 46 |
doFromTo 0 (h-1) $ \y -> |
| 47 |
doFromTo 0 (w-1) $ \x -> |
| 48 |
pokeByteOff pbData (0+x*chan+y*row) blue -- unchecked indexing |
| 49 |
|
| 50 |
-- arrange for the canvas to be redrawn now that we've changed |
| 51 |
-- the Pixbuf |
| 52 |
widgetQueueDraw canvas |
| 53 |
|
| 54 |
-- update the blue state ready for next time |
| 55 |
dir <- readIORef dirRef |
| 56 |
let diff = 1 |
| 57 |
let blue' = if dir then blue+diff else blue-diff |
| 58 |
if dir then |
| 59 |
if blue<=maxBound-diff then writeIORef blueRef blue' else |
| 60 |
writeIORef blueRef maxBound >> modifyIORef dirRef not |
| 61 |
else |
| 62 |
if blue>=minBound+diff then writeIORef blueRef blue' else |
| 63 |
writeIORef blueRef minBound >> modifyIORef dirRef not |
| 64 |
return True |
| 65 |
|
| 66 |
idleAdd updateBlue priorityLow |
| 67 |
canvas `on` draw $ updateCanvas pbData w h stride |
| 68 |
boxPackStart contain canvas PackGrow 0 |
| 69 |
widgetShow canvas |
| 70 |
dialogRun dia |
| 71 |
return () |
| 72 |
|
| 73 |
updateCanvas :: PixelData -> Int -> Int -> Int -> Render () |
| 74 |
updateCanvas pb w h stride = do |
| 75 |
s <- liftIO $ createImageSurfaceForData pb FormatRGB24 w h stride |
| 76 |
setSourceSurface s 0 0 |
| 77 |
paint |
| 78 |
|
| 79 |
-- GHC is much better at opimising loops like this: |
| 80 |
-- |
| 81 |
-- > doFromTo 0 255 $ \y -> |
| 82 |
-- > doFromTo 0 255 $ \x -> do ... |
| 83 |
-- |
| 84 |
-- Than it is at optimising loops like this: |
| 85 |
-- |
| 86 |
-- > sequence_ [ do ... |
| 87 |
-- > | x <- [0..255] |
| 88 |
-- > , y <- [0..255] ] |
| 89 |
-- |
| 90 |
-- The first kind of loop runs significantly faster (with GHC 6.2 and 6.4) |
| 91 |
|
| 92 |
{-# INLINE doFromTo #-} |
| 93 |
-- do the action for [from..to], ie it's inclusive. |
| 94 |
doFromTo :: Int -> Int -> (Int -> IO ()) -> IO () |
| 95 |
doFromTo from to action = |
| 96 |
let loop n | n > to = return () |
| 97 |
| otherwise = do action n |
| 98 |
loop (n+1) |
| 99 |
in loop from |