pituicat/src/Affection.hs

94 lines
2.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
2016-03-25 08:41:22 +00:00
module Affection
( withAffection
-- , withWindow
-- , withDefaultWindow
2016-03-25 10:43:31 +00:00
, delaySec
2016-11-04 15:06:16 +00:00
, get
, put
2016-11-02 00:14:53 +00:00
, module Types
2016-03-25 08:41:22 +00:00
) where
import qualified SDL
import qualified SDL.Raw as Raw
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
import Control.Monad.Loops
import Control.Monad.State
import Control.Concurrent.MVar
2016-03-25 10:43:31 +00:00
import Affection.Types as Types
2016-03-25 15:58:46 +00:00
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
2016-11-02 00:14:53 +00:00
execTime <- newMVar =<< getTime Monotonic
window <- SDL.createWindow windowTitle windowConfig
2016-11-08 04:15:44 +00:00
oldSurf@(SDL.Surface ptr _) <- SDL.getWindowSurface window
2016-11-08 03:34:26 +00:00
surface <- (\x -> return $ SDL.Surface x Nothing) =<<
2016-11-08 04:15:44 +00:00
Raw.convertSurfaceFormat ptr (SDL.toNumber SDL.ABGR8888) 0
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-11-04 15:06:16 +00:00
}) =<< loadState surface
(_, nState) <- runStateT ( Types.runState $
whileM_ (do
current <- get
2016-11-04 15:06:16 +00:00
return $ not $ Types.quitEvent current
)
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
drawLoop
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 ()
)
) 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
-- -- | DEPRECATED!
-- -- Function for bootstraping a window.
-- withWindow :: Monad m => T.Text -> WindowConfig -> SDL.RendererConfig -> RenderT m a -> IO ()
-- withWindow title wconf rconf ops = do
-- window <- SDL.createWindow title wconf
-- -- I don't need a renderer here, i need a surface
-- renderer <- SDL.createRenderer window (-1) rconf
-- surface <- SDL.getWindowSurface window
-- G.gegl_init
-- -- I think I need some AffectionT or someting similar here and not a RenderT
-- -- from SDL.
-- inRender renderer $ ops
-- G.gegl_exit
-- SDL.destroyWindow window
--
-- -- | DEPRECATED!
-- -- Bootstrap a default window.
-- withDefaultWindow :: Monad m => T.Text -> (RenderT m a) -> IO ()
-- withDefaultWindow title ops = withWindow title defaultWindow SDL.defaultRenderer ops
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)