module Affection
( withAffection
, getAffection
, putAffection
, delaySec
, get
, put
, module A
) where
import qualified SDL
import qualified SDL.Internal.Numbered as SDL (toNumber)
import qualified SDL.Raw as Raw
import qualified GEGL as G
import Data.Maybe
import Data.IORef
import System.Clock
import Control.Monad.Loops
import Control.Monad.State
import Foreign.C.Types (CInt(..))
import Foreign.Storable (peek)
import Debug.Trace
import Affection.Types as A
import Affection.Draw as A
import Affection.Particle as A
import qualified BABL as B
withAffection
:: AffectionConfig us
-> IO ()
withAffection AffectionConfig{..} = do
case initComponents of
All ->
SDL.initializeAll
Only is ->
SDL.initialize is
G.gegl_init
SDL.HintRenderScaleQuality SDL.$= SDL.ScaleLinear
do
renderQuality <- SDL.get SDL.HintRenderScaleQuality
when (renderQuality /= SDL.ScaleLinear) $
putStrLn "Warning: Linear texture filtering not enabled!"
execTime <- newIORef =<< getTime Monotonic
window <- SDL.createWindow windowTitle windowConfig
SDL.showWindow window
renderer <- SDL.createRenderer
window
(1)
SDL.defaultRenderer
surface <- SDL.createRGBSurface
(SDL.windowInitialSize windowConfig)
SDL.ABGR8888
let (SDL.Surface ptr _) = surface
pixelFormat <- liftIO $ peek . Raw.surfaceFormat =<< peek ptr
SDL.V2 (CInt rw) (CInt rh) <- liftIO $ SDL.surfaceDimensions surface
let (w, h) = (fromIntegral rw, fromIntegral rh)
stride = (fromIntegral $ Raw.pixelFormatBytesPerPixel pixelFormat) * w
pixels <- SDL.surfacePixels $ surface
let bablFormat = B.PixelFormat B.RGBA B.CFu8
cpp = B.babl_components_per_pixel bablFormat
format <- B.babl_format bablFormat
initContainer <- (\x -> AffectionData
{ quitEvent = False
, userState = x
, drawWindow = window
, windowRenderer = renderer
, drawSurface = surface
, drawFormat = format
, drawPixels = pixels
, drawDimensions = (w, h)
, drawStride = stride
, drawCPP = cpp
, drawStack = []
, elapsedTime = 0
}) <$> loadState surface
(_, nState) <- runStateT ( A.runState $ do
preLoop
whileM_ (do
current <- get
return $ not $ A.quitEvent current
)
(do
now <- liftIO $ getTime Monotonic
lastTime <- liftIO $ readIORef execTime
ad <- get
mapM_ (invalidateDrawRequest (drawPixels ad) (drawStride ad) (drawCPP ad)) (drawStack ad)
SDL.clear renderer
let dt = (fromIntegral $ toNanoSecs $ diffTimeSpec lastTime now) / (fromIntegral 10 ^ 9)
put $ ad
{ drawStack = []
, elapsedTime = elapsedTime ad + dt
}
evs <- preHandleEvents =<< liftIO SDL.pollEvents
updateLoop dt evs
drawLoop
ad2 <- get
clear <- catMaybes <$>
mapM (handleDrawRequest (drawPixels ad) (drawStride ad) (drawCPP ad)) (drawStack ad2)
put $ ad2
{ drawStack = clear }
texture <- SDL.createTextureFromSurface (windowRenderer ad2) (drawSurface ad2)
SDL.copy (windowRenderer ad2) texture Nothing Nothing
SDL.present (windowRenderer ad2)
SDL.destroyTexture texture
liftIO $ writeIORef execTime $ now
)
) initContainer
G.gegl_exit
cleanUp $ userState nState
SDL.destroyWindow window
SDL.quit
getSurfaces :: SDL.Window -> IO (SDL.Surface, SDL.Surface)
getSurfaces window = do
oldSurf@(SDL.Surface ptr _) <- SDL.getWindowSurface window
rawSurfacePtr <- Raw.convertSurfaceFormat ptr (SDL.toNumber SDL.ABGR8888) 0
let surface = (flip SDL.Surface Nothing) rawSurfacePtr
return (oldSurf, surface)
preHandleEvents :: [SDL.Event] -> Affection us [SDL.Event]
preHandleEvents evs =
mapM handle evs
where
handle e =
case SDL.eventPayload e of
SDL.WindowMovedEvent _ -> do
liftIO $ traceIO "I was moved"
return e
_ ->
return e
getAffection :: Affection us us
getAffection = do
ad <- get
return $ userState ad
putAffection
:: us
-> Affection us ()
putAffection us = do
ad <- get
put $ ad
{ userState = us }
delaySec
:: Int
-> IO ()
delaySec dur = SDL.delay (fromIntegral $ dur * 1000)