module Affection
( withAffection
, get
, put
, module A
) where
import SDL (($=))
import qualified SDL
import System.Clock
import Control.Monad.Loops
import Control.Monad.State
import Foreign.C.Types (CInt(..))
import Debug.Trace
import Affection.Types as A
import Affection.StateMachine as A
import Affection.MouseInteractable as A
import Affection.Util as A
import Affection.MessageBus as A
import Affection.Subsystems as A
import Affection.Logging as A
import Graphics.Rendering.OpenGL as GL (clear, flush, ClearBuffer(..))
withAffection
:: AffectionConfig us
-> IO ()
withAffection AffectionConfig{..} = do
liftIO $ logIO Debug "Affection starting"
liftIO $ logIO Debug "Initializing SDL"
case initComponents of
All ->
SDL.initializeAll
Only is ->
SDL.initialize is
SDL.HintRenderScaleQuality SDL.$= SDL.ScaleLinear
do
renderQuality <- SDL.get SDL.HintRenderScaleQuality
when (renderQuality /= SDL.ScaleLinear) $
logIO Warn "Linear texture filtering not enabled!"
liftIO $ logIO Debug "Creating Window"
window <- SDL.createWindow windowTitle windowConfig
SDL.showWindow window
context <- SDL.glCreateContext window
let SDL.V2 (CInt rw) (CInt rh) = SDL.windowInitialSize windowConfig
(w, h) = case canvasSize of
Just (cw, ch) -> (cw, ch)
Nothing -> (fromIntegral rw, fromIntegral rh)
SDL.setWindowMode window initScreenMode
SDL.swapInterval $= SDL.SynchronizedUpdates
liftIO $ logIO Debug "Getting Time"
execTime <- getTime Monotonic
liftIO $ logIO Debug "Loading initial data container"
initContainer <- (\x -> AffectionData
{ quitEvent = False
, userState = x
, drawWindow = window
, glContext = context
, drawDimensions = case canvasSize of
Just (cw, ch) -> (cw, ch)
Nothing -> (w, h)
, screenMode = initScreenMode
, elapsedTime = 0
, deltaTime = 0
, sysTime = execTime
, pausedTime = False
}) <$> loadState
(_, nState) <- runStateT ( A.runState $ do
liftIO $ logIO Debug "Starting Loop"
preLoop
whileM_ (do
current <- get
return $ not $ A.quitEvent current
)
(do
ad <- get
now <- liftIO $ getTime Monotonic
let lastTime = sysTime ad
let !dt = fromIntegral
(toNanoSecs $ diffTimeSpec lastTime now) / (10 ^ (9 :: Int))
!ne = elapsedTime ad + dt
put $ ad
{ elapsedTime = ne
, deltaTime = dt
}
evs <- preHandleEvents =<< liftIO SDL.pollEvents
eventLoop evs
unless (pausedTime ad) (updateLoop dt)
liftIO $ GL.clear [ColorBuffer, DepthBuffer, StencilBuffer]
drawLoop
liftIO flush
ad2 <- get
SDL.glSwapWindow window
ad3 <- get
when (sysTime ad == sysTime ad3) (
put ad3
{ sysTime = now
}
)
)
) initContainer
liftIO $ logIO Debug "Loop ended. Cleaning"
cleanUp $ userState nState
liftIO $ logIO Debug "Destroying Window"
SDL.destroyWindow window
SDL.quit
liftIO $ logIO Debug "This is the end"