{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module Graphics.Gloss.Accelerate.Raster.Array (
module Graphics.Gloss.Accelerate.Data.Point,
module Data.Array.Accelerate.Data.Colour.RGBA,
Render, Display(..),
animateArrayWith,
animateArrayIOWith,
playArrayWith,
playArrayIOWith,
makePicture,
) where
import Graphics.Gloss.Accelerate.Render
import Graphics.Gloss.Accelerate.Data.Point
import Graphics.Gloss.Accelerate.Data.Picture
import Data.Array.Accelerate.Data.Colour.RGBA
import Prelude as P
import Graphics.Gloss.Data.Display ( Display(..) )
import Graphics.Gloss.Data.Picture ( Picture(..) )
import Graphics.Gloss.Interface.IO.Animate as G ( animateFixedIO, black )
import Graphics.Gloss.Interface.Pure.Game as G ( Event, play )
import Graphics.Gloss.Interface.IO.Game as G ( playIO )
import Data.Array.Accelerate as A
animateArrayWith
:: Render
-> Display
-> (Int, Int)
-> (Exp Float -> Acc (Array DIM2 Colour))
-> IO ()
animateArrayWith render display (zoomX, zoomY) makeArray
| zoomX P.< 1 P.|| zoomY P.< 1
= error "Graphics.Gloss.Raster: invalid pixel scalar factor"
| otherwise
= let picture = makePicture render zoomX zoomY (makeArray . the)
. fromList Z
. return
in
#if MIN_VERSION_gloss(1,10,0)
animateFixedIO display G.black (return . picture) (\_ -> return ())
#else
animateFixedIO display G.black (return . picture)
#endif
animateArrayIOWith
:: Arrays world
=> Render
-> Display
-> (Int, Int)
-> (Float -> IO world)
-> (Acc world -> Acc (Array DIM2 Colour))
-> IO ()
animateArrayIOWith render display (zoomX, zoomY) makeWorld makeArray
| zoomX P.< 1 P.|| zoomY P.< 1
= error "Graphics.Gloss.Raster: invalid pixel scalar factor"
| otherwise
= let picture = fmap (makePicture render zoomX zoomY makeArray)
. makeWorld
in
#if MIN_VERSION_gloss(1,10,0)
animateFixedIO display G.black picture (\_ -> return ())
#else
animateFixedIO display G.black picture
#endif
playArrayWith
:: Arrays world
=> Render
-> Display
-> (Int, Int)
-> Int
-> state
-> (state -> world)
-> (Acc world -> Acc (Array DIM2 Colour))
-> (Event -> state -> state)
-> (Float -> state -> state)
-> IO ()
playArrayWith render display (zoomX, zoomY) stepRate
initState makeWorld makeArray handleEvent stepState
| zoomX P.< 1 P.|| zoomY P.< 1
= error "Graphics.Gloss.Raster: invalid pixel scalar factor"
| otherwise
= let picture = makePicture render zoomX zoomY makeArray
. makeWorld
in
play display G.black stepRate initState picture handleEvent stepState
playArrayIOWith
:: Arrays world
=> Render
-> Display
-> (Int, Int)
-> Int
-> state
-> (state -> IO world)
-> (Acc world -> Acc (Array DIM2 Colour))
-> (Event -> state -> IO state)
-> (Float -> state -> IO state)
-> IO ()
playArrayIOWith render display (zoomX, zoomY) stepRate
initState makeWorld makeArray handleEvent stepState
| zoomX P.< 1 P.|| zoomY P.< 1
= error "Graphics.Gloss.Raster: invalid pixel scalar factor"
| otherwise
= let picture = fmap (makePicture render zoomX zoomY makeArray)
. makeWorld
in
G.playIO display G.black stepRate initState picture handleEvent stepState
makePicture
:: Arrays world
=> Render
-> Int
-> Int
-> (Acc world -> Acc (Array DIM2 Colour))
-> (world -> Picture)
makePicture render zoomX zoomY makeArray
= let
pixels = render (A.map (packRGBA . opaque) . makeArray)
picture world = bitmapOfArray (pixels world) False
in
Scale (P.fromIntegral zoomX) (P.fromIntegral zoomY) . picture