import Graphics.UI.Gtk hiding (eventRegion) import Graphics.UI.Gtk.Gdk.Events import Graphics.UI.Gtk.Gdk.GC import Ray import Data.Array import Basics(Color(..)) import Foreign.C.Types import Foreign.Marshal.Array import Foreign.Marshal.Alloc import Foreign(Ptr) import Data.Word import Vectors import Data.IORef import Bitmaps import System.IO.Unsafe import Scenes import Data.Array.MArray import Data.Array.Base ( unsafeWrite ) main = do timeRef <- newIORef (0 :: Scal) initGUI putStrLn "GTK inited" print $ sum $ elems $ skyBitmap dia <- dialogNew dialogAddButton dia stockOk ResponseOk contain <- dialogGetUpper dia canvas <- drawingAreaNew canvas `onSizeRequest` return (Requisition 400 200) pb <- pixbufNew ColorspaceRgb False 8 400 200 pbData <- (pixbufGetPixels pb :: IO (PixbufData Int Word8)) row <- pixbufGetRowstride pb chan <- pixbufGetNChannels pb bits <- pixbufGetBitsPerSample pb putStrLn ("bytes per row: "++show row++", channels per pixel: "++show chan++ ", bits per sample: "++show bits) let updateFrame = do time <- updateTime timeRef putStrLn $ "updateCanvas @" ++ show time let frame = Ray.rt time sequence_ [unsafeWrite pbData (0+x*chan+y*row) (mkCol $ colR $ frame!(y,x)) >> unsafeWrite pbData (1+x*chan+y*row) (mkCol $ colG $ frame!(y,x)) >> unsafeWrite pbData (2+x*chan+y*row) (mkCol $ colB $ frame!(y,x)) | x <- [0..399], y <- [0..199] ] widgetQueueDraw canvas return True canvas `widgetCreateLayout` "Hello World." idleAdd updateFrame priorityLow canvas `onExpose` updateCanvas canvas pb boxPackStartDefaults contain canvas widgetShow canvas dialogRun dia return () updateCanvas :: DrawingArea -> Pixbuf -> Event -> IO Bool updateCanvas canvas pb Expose { eventRegion = region } = do win <- widgetGetDrawWindow canvas gc <- gcNew win (width,height) <- widgetGetSize canvas rects <- regionGetRectangles region (flip mapM_) rects $ \(Rectangle x y w h) -> do drawPixbuf win gc pb x y x y (-1) (-1) RgbDitherNone 0 0 return True updateTime :: IORef(Scal) -> IO (Scal) updateTime timeRef = do t <- readIORef timeRef writeIORef timeRef (t + 0.2) return t mkCol x = clamp $ round $ (255 * x) where clamp x | x < 0 = 0 | x > 255 = 255 | otherwise = x