2016-10-31 22:47:16 +00:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2016-03-25 08:41:22 +00:00
|
|
|
module Affection
|
2016-10-31 22:47:16 +00:00
|
|
|
( withAffection
|
2016-12-12 01:10:30 +00:00
|
|
|
, getAffection
|
|
|
|
, putAffection
|
2016-11-13 12:39:25 +00:00
|
|
|
-- , 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
|
|
|
|
|
2016-10-31 22:47:16 +00:00
|
|
|
import qualified SDL
|
2016-12-08 17:22:29 +00:00
|
|
|
import qualified SDL.Internal.Numbered as SDL (toNumber)
|
2016-11-08 03:31:51 +00:00
|
|
|
import qualified SDL.Raw as Raw
|
2016-10-31 22:47:16 +00:00
|
|
|
import qualified GEGL as G
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Maybe
|
|
|
|
|
2016-11-02 00:14:53 +00:00
|
|
|
import System.Clock
|
2016-10-31 22:47:16 +00:00
|
|
|
|
|
|
|
import Control.Monad.Loops
|
|
|
|
import Control.Monad.State
|
|
|
|
import Control.Concurrent.MVar
|
2016-03-25 10:43:31 +00:00
|
|
|
|
2016-12-11 16:38:03 +00:00
|
|
|
import Foreign.C.Types (CInt(..))
|
|
|
|
import Foreign.Storable (peek)
|
|
|
|
|
2016-12-08 17:22:29 +00:00
|
|
|
import Affection.Types as A
|
|
|
|
import Affection.Draw as A
|
2016-03-25 15:58:46 +00:00
|
|
|
|
2016-12-11 16:38:03 +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
|
2016-10-31 22:47:16 +00:00
|
|
|
case initComponents of
|
|
|
|
All ->
|
|
|
|
SDL.initializeAll
|
|
|
|
Only is ->
|
|
|
|
SDL.initialize is
|
2016-11-04 15:06:16 +00:00
|
|
|
G.gegl_init
|
2016-11-02 00:14:53 +00:00
|
|
|
execTime <- newMVar =<< getTime Monotonic
|
2016-10-31 22:47:16 +00:00
|
|
|
window <- SDL.createWindow windowTitle windowConfig
|
2016-11-08 04:15:44 +00:00
|
|
|
oldSurf@(SDL.Surface ptr _) <- SDL.getWindowSurface window
|
2016-12-11 16:38:03 +00:00
|
|
|
rawSurfacePtr <- Raw.convertSurfaceFormat ptr (SDL.toNumber SDL.ABGR8888) 0
|
|
|
|
let surface = (flip SDL.Surface Nothing) rawSurfacePtr
|
|
|
|
bablFormat = B.PixelFormat B.RGBA B.CFu8
|
|
|
|
pixels <- SDL.surfacePixels surface
|
|
|
|
format <- B.babl_format bablFormat
|
|
|
|
SDL.V2 (CInt rw) (CInt rh) <- SDL.surfaceDimensions surface
|
|
|
|
pixelFormat <- peek . Raw.surfaceFormat =<< peek rawSurfacePtr
|
|
|
|
let (w, h) = (fromIntegral rw, fromIntegral rh)
|
|
|
|
stride = fromIntegral (Raw.pixelFormatBytesPerPixel pixelFormat) * w
|
|
|
|
cpp = B.babl_components_per_pixel bablFormat
|
2016-11-04 15:06:16 +00:00
|
|
|
initContainer <- return . (\x -> AffectionData
|
|
|
|
{ quitEvent = False
|
|
|
|
, userState = x
|
2016-11-02 00:14:53 +00:00
|
|
|
, drawWindow = window
|
|
|
|
, drawSurface = surface
|
2016-12-11 16:38:03 +00:00
|
|
|
, drawStack = []
|
2016-11-04 15:06:16 +00:00
|
|
|
}) =<< loadState surface
|
2016-12-11 16:38:03 +00:00
|
|
|
(_, nState) <- runStateT ( A.runState $ do
|
2016-12-11 19:24:16 +00:00
|
|
|
preLoop
|
2016-10-31 22:47:16 +00:00
|
|
|
whileM_ (do
|
|
|
|
current <- get
|
2016-12-08 17:22:29 +00:00
|
|
|
return $ not $ A.quitEvent current
|
2016-10-31 22:47:16 +00:00
|
|
|
)
|
2016-11-04 15:06:16 +00:00
|
|
|
(do
|
2016-11-02 00:14:53 +00:00
|
|
|
now <- liftIO $ getTime Monotonic
|
|
|
|
lastTime <- liftIO $ fromMaybe now <$> tryReadMVar execTime
|
2016-12-11 16:38:03 +00:00
|
|
|
ad <- get
|
2016-12-11 19:24:16 +00:00
|
|
|
mapM_ (invalidateDrawRequest pixels format stride cpp) $ drawStack ad
|
2016-12-11 16:38:03 +00:00
|
|
|
put $ ad
|
|
|
|
{ drawStack = [] }
|
2016-12-11 19:24:16 +00:00
|
|
|
drawLoop
|
|
|
|
ad <- get
|
|
|
|
clear <- return . catMaybes =<< mapM (handleDrawRequest pixels format stride cpp) (drawStack ad)
|
|
|
|
put $ ad
|
|
|
|
{ drawStack = clear }
|
2016-11-13 12:39:25 +00:00
|
|
|
liftIO $ SDL.surfaceBlit surface Nothing oldSurf Nothing
|
2016-11-02 00:14:53 +00:00
|
|
|
updateLoop $ (fromIntegral $ toNanoSecs $ diffTimeSpec lastTime now) /
|
|
|
|
(fromIntegral 10 ^ 9)
|
2016-11-04 15:06:16 +00:00
|
|
|
_ <- liftIO $ swapMVar execTime $ now
|
|
|
|
return ()
|
|
|
|
)
|
2016-10-31 22:47:16 +00:00
|
|
|
) initContainer
|
2016-11-04 15:06:16 +00:00
|
|
|
G.gegl_exit
|
2016-11-13 12:39:25 +00:00
|
|
|
cleanUp $ userState nState
|
2016-10-31 22:47:16 +00:00
|
|
|
SDL.quit
|
2016-03-25 10:43:31 +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 ()
|
2016-10-31 22:47:16 +00:00
|
|
|
delaySec dur = SDL.delay (fromIntegral $ dur * 1000)
|