vulkan-tutorial/src/Affection.hs

184 lines
5.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
2016-03-25 08:41:22 +00:00
module Affection
( withAffection
2016-12-12 01:10:30 +00:00
, getAffection
, putAffection
-- , withWindow
-- , withDefaultWindow
2016-03-25 10:43:31 +00:00
, delaySec
2016-11-04 15:06:16 +00:00
, get
, put
2016-12-08 17:22:29 +00:00
, module A
2016-03-25 08:41:22 +00:00
) where
import qualified SDL
2016-12-08 17:22:29 +00:00
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
2016-11-02 00:14:53 +00:00
import System.Clock
import Control.Monad.Loops
import Control.Monad.State
2016-03-25 10:43:31 +00:00
import Foreign.C.Types (CInt(..))
import Foreign.Storable (peek)
2017-02-17 16:15:06 +00:00
import Debug.Trace
2016-12-08 17:22:29 +00:00
import Affection.Types as A
import Affection.Draw as A
import Affection.Particle as A
2016-03-25 15:58:46 +00:00
import qualified BABL as B
2016-11-06 04:02:06 +00:00
-- | Main function which bootstraps everything else.
withAffection
:: AffectionConfig us -- ^ Configuration of the Game and its engine.
-> IO ()
2016-11-08 04:15:44 +00:00
withAffection AffectionConfig{..} = do
case initComponents of
All ->
SDL.initializeAll
Only is ->
SDL.initialize is
2016-11-04 15:06:16 +00:00
G.gegl_init
execTime <- newIORef =<< getTime Monotonic
window <- SDL.createWindow windowTitle windowConfig
2017-02-17 16:15:06 +00:00
-- let surface = (flip SDL.Surface Nothing) rawSurfacePtr
(oldSurf, surface) <- getSurfaces window
let (SDL.Surface ptr _) = surface
rawSurfacePtr <- Raw.convertSurfaceFormat ptr (SDL.toNumber SDL.ABGR8888) 0
pixelFormat <- liftIO $ peek . Raw.surfaceFormat =<< peek rawSurfacePtr
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
2017-02-17 16:15:06 +00:00
let bablFormat = B.PixelFormat B.RGBA B.CFu8
cpp = B.babl_components_per_pixel bablFormat
format <- B.babl_format bablFormat
2016-12-20 23:16:21 +00:00
initContainer <- (\x -> AffectionData
2016-11-04 15:06:16 +00:00
{ quitEvent = False
, userState = x
2016-11-02 00:14:53 +00:00
, drawWindow = window
2017-02-17 16:15:06 +00:00
, windowSurface = oldSurf
2016-11-02 00:14:53 +00:00
, drawSurface = surface
2016-12-21 03:28:57 +00:00
, drawFormat = format
, drawPixels = pixels
, drawDimensions = (w, h)
, drawStride = stride
, drawCPP = cpp
, drawStack = []
2016-12-25 07:14:51 +00:00
, elapsedTime = 0
2016-12-20 23:16:21 +00:00
}) <$> loadState surface
(_, nState) <- runStateT ( A.runState $ do
preLoop
pad <- get
liftIO $ SDL.surfaceBlit (drawSurface pad) Nothing (windowSurface pad) Nothing
whileM_ (do
current <- get
2016-12-08 17:22:29 +00:00
return $ not $ A.quitEvent current
)
2016-11-04 15:06:16 +00:00
(do
2016-12-20 03:14:57 +00:00
-- Measure time difference form last run
2016-11-02 00:14:53 +00:00
now <- liftIO $ getTime Monotonic
lastTime <- liftIO $ readIORef execTime
2016-12-20 03:14:57 +00:00
-- get state
ad <- get
2016-12-20 03:14:57 +00:00
-- clean draw requests from last run
mapM_ (invalidateDrawRequest (drawPixels ad) (drawStride ad) (drawCPP ad)) (drawStack ad)
2016-12-25 07:14:51 +00:00
-- compute dt and update elapsedTime
let dt = (fromIntegral $ toNanoSecs $ diffTimeSpec lastTime now) / (fromIntegral 10 ^ 9)
put $ ad
2016-12-25 07:14:51 +00:00
{ drawStack = []
, elapsedTime = elapsedTime ad + dt
}
2017-02-17 16:15:06 +00:00
-- poll events
evs <- preHandleEvents =<< liftIO SDL.pollEvents
2016-12-20 03:14:57 +00:00
-- execute user defined update loop
2017-02-17 16:15:06 +00:00
updateLoop dt evs
-- execute user defined draw loop
drawLoop
2016-12-20 03:14:57 +00:00
-- handle all new draw requests
2016-12-20 04:27:35 +00:00
ad2 <- get
clear <- catMaybes <$>
mapM (handleDrawRequest (drawPixels ad) (drawStride ad) (drawCPP ad)) (drawStack ad2)
2016-12-20 03:14:57 +00:00
-- save all draw requests to clear in next run
2016-12-20 04:27:35 +00:00
put $ ad2
{ drawStack = clear }
2016-12-20 03:14:57 +00:00
-- blit surface and update window
2017-02-17 16:15:06 +00:00
liftIO $ SDL.surfaceBlit
(drawSurface ad2)
Nothing
(windowSurface ad2)
Nothing
2016-12-20 04:27:35 +00:00
liftIO $ SDL.updateWindowSurface $ drawWindow ad2
2016-12-20 03:14:57 +00:00
-- save new time
liftIO $ writeIORef execTime $ now
2016-11-04 15:06:16 +00:00
)
) initContainer
2016-11-04 15:06:16 +00:00
G.gegl_exit
cleanUp $ userState nState
SDL.quit
2016-03-25 10:43:31 +00:00
2017-02-17 16:15:06 +00:00
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 =
2017-02-20 19:22:01 +00:00
mapM handle evs
2017-02-17 16:15:06 +00:00
where
handle e =
case SDL.eventPayload e of
SDL.WindowMovedEvent _ -> do
liftIO $ traceIO "I was moved"
putNewSurface
2017-02-20 19:22:01 +00:00
return e
2017-02-17 16:15:06 +00:00
_ ->
2017-02-20 19:22:01 +00:00
return e
2017-02-17 16:15:06 +00:00
putNewSurface = do
ad <- get
(oldSurf, surface) <- liftIO $ getSurfaces $ drawWindow ad
pixels <- SDL.surfacePixels $ surface
SDL.V2 (CInt rw) (CInt rh) <- liftIO $ SDL.surfaceDimensions surface
let (SDL.Surface ptr _) = surface
rawSurfacePtr <- Raw.convertSurfaceFormat ptr (SDL.toNumber SDL.ABGR8888) 0
pixelFormat <- liftIO $ peek . Raw.surfaceFormat =<< peek rawSurfacePtr
let (w, h) = (fromIntegral rw, fromIntegral rh)
stride = fromIntegral (Raw.pixelFormatBytesPerPixel pixelFormat) * w
2017-02-17 16:15:06 +00:00
put ad
{ windowSurface = oldSurf
, drawSurface = surface
, drawPixels = pixels
, drawDimensions = (w, h)
, drawStride = stride
2017-02-17 16:15:06 +00:00
}
2016-12-12 01:10:30 +00:00
-- | Return the userstate to the user
getAffection :: Affection us us
getAffection = do
ad <- get
return $ userState ad
-- | Put altered user state back
putAffection
:: us -- User state
-> Affection us ()
putAffection us = do
ad <- get
put $ ad
{ userState = us }
2016-03-25 10:43:31 +00:00
2016-11-06 04:02:06 +00:00
-- | block a thread for a specified amount of time
delaySec
:: Int -- ^ Number of seconds
-> IO ()
delaySec dur = SDL.delay (fromIntegral $ dur * 1000)