affection/src/Affection.hs

205 lines
5.8 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
2021-01-03 17:21:12 +00:00
{-# LANGUAGE Strict #-}
2020-05-03 23:36:12 +00:00
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
2020-05-04 03:17:44 +00:00
{-# LANGUAGE TypeApplications #-}
2016-03-25 08:41:22 +00:00
module Affection
( withAffection
2017-09-09 14:47:24 +00:00
, get
2017-11-04 17:35:09 +00:00
, put
2018-01-12 21:08:23 +00:00
, liftIO
2016-12-08 17:22:29 +00:00
, module A
2016-03-25 08:41:22 +00:00
) where
2017-10-03 10:47:18 +00:00
import SDL (($=))
import qualified SDL
2018-06-16 17:34:42 +00:00
import qualified SDL.Raw.Video as SDL (glSetAttribute)
import qualified SDL.Raw.Enum as SDL
2020-10-15 16:49:04 +00:00
import qualified Graphics.Rendering.OpenGL as GL (clear, flush, ClearBuffer(..))
import qualified Graphics.GL as GLRaw
import Foreign.Marshal.Array
import qualified Data.ByteString as B
2022-07-09 21:26:37 +00:00
import Data.String (fromString)
2020-10-15 16:49:04 +00:00
2016-11-02 00:14:53 +00:00
import System.Clock
import Control.Monad.Loops
2018-06-03 00:41:49 +00:00
import Control.Monad.State.Strict
2022-07-10 02:18:22 +00:00
import Control.Monad.Trans.Resource
2020-10-15 16:49:04 +00:00
-- internal imports
2016-12-08 17:22:29 +00:00
import Affection.Types as A
2020-05-03 23:36:12 +00:00
import Affection.Class as A
2017-03-05 15:39:37 +00:00
import Affection.StateMachine as A
2017-09-07 04:23:01 +00:00
import Affection.Util as A
2017-11-27 01:18:23 +00:00
import Affection.MessageBus as A
2017-12-13 14:19:53 +00:00
import Affection.Subsystems as A
import Affection.Logging as A
2016-03-25 15:58:46 +00:00
2016-11-06 04:02:06 +00:00
-- | Main function which bootstraps everything else.
withAffection
2020-05-04 03:17:44 +00:00
:: forall us. (Affectionate us)
2020-05-03 23:36:12 +00:00
=> AffectionConfig us -- ^ Configuration of the Game and its engine.
2016-11-06 04:02:06 +00:00
-> IO ()
2022-07-10 02:18:22 +00:00
withAffection AffectionConfig{..} = runResourceT $ do
liftIO $ logIO Debug "Affection starting"
liftIO $ logIO Debug "Initializing SDL"
2022-07-10 02:18:22 +00:00
2017-02-23 21:54:26 +00:00
-- intialiaze SDL
case initComponents of
All ->
SDL.initializeAll
Only is ->
SDL.initialize is
2022-07-10 02:18:22 +00:00
2017-02-23 21:54:26 +00:00
-- give SDL render quality
SDL.HintRenderScaleQuality SDL.$= SDL.ScaleLinear
2022-07-10 02:18:22 +00:00
2017-02-23 21:54:26 +00:00
-- just checking…
2022-07-10 02:18:22 +00:00
renderQuality <- SDL.get SDL.HintRenderScaleQuality
when (renderQuality /= SDL.ScaleLinear) $
liftIO $ logIO Warn "Linear texture filtering not enabled!"
2022-07-09 21:26:37 +00:00
void $ liftIO (logIO Debug . fromString . show <$> (SDL.version :: IO (Integer, Integer, Integer)))
2022-07-10 02:18:22 +00:00
2017-02-23 21:54:26 +00:00
-- construct window
2019-01-30 11:00:28 +00:00
liftIO $ logIO Debug "Creating Window(s)"
2022-07-10 02:18:22 +00:00
windows <-
mapM
(\(_, sdlWindowConfig, mode) -> do
(windowKey, window) <-
allocate
(SDL.createWindow windowTitle sdlWindowConfig)
(\window -> do
logIO Debug "Destroying Window"
SDL.destroyWindow window
)
return $ AffectionWindow window windowKey mode
)
windowConfigs
-- Show windows
mapM_ (SDL.showWindow . awWindow) windows
-- set modes of windows
mapM_ (\(AffectionWindow window _ mode) -> SDL.setWindowMode window mode) windows
-- Make GL context shareable
void $ SDL.glSetAttribute SDL.SDL_GL_SHARE_WITH_CURRENT_CONTEXT 1
-- Create OpenGL contexts
contexts <-
2019-01-30 11:00:28 +00:00
mapM
2022-07-10 02:18:22 +00:00
(\(AffectionWindow window _ _) -> do
(contextKey, context) <-
allocate
(SDL.glCreateContext window)
(\context -> do
logIO Debug "Destroying context"
SDL.glDeleteContext context
)
return $ AffectionContext context contextKey
)
windows
2020-10-16 04:06:39 +00:00
-- sync updates with monitor
2018-09-25 05:02:33 +00:00
-- SDL.swapInterval $= SDL.SynchronizedUpdates -- <- causes Problems with windows
2022-07-10 02:18:22 +00:00
2020-10-15 16:49:04 +00:00
-- print current used GL Version
2022-07-10 02:18:22 +00:00
version <- liftIO $ peekArray0 (0 :: Word8) =<< GLRaw.glGetString GLRaw.GL_VERSION
liftIO $ print (B.pack version)
2017-11-27 01:18:23 +00:00
-- get current time
2022-07-10 02:18:22 +00:00
liftIO $ logIO Debug "Getting Time"
execTime <- liftIO $ getTime Monotonic
liftIO $ logIO Debug "Loading initial data container"
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- construct game data object from provided Affectionate instance
2022-07-10 02:18:22 +00:00
(gameDataKey, gameData) <-
allocate
(liftIO $ loadState @us)
(liftIO . cleanUp)
2020-05-04 03:26:49 +00:00
-- build state container
2020-05-03 23:36:12 +00:00
let initContainer = AffectionData
{ drawWindows = windows
, glContext = contexts
, elapsedTime = 0
, deltaTime = 0
, sysTime = execTime
, pausedTime = False
}
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- initialize and run state
2022-07-10 02:18:22 +00:00
void $ liftIO $ runAffection initContainer
2016-11-04 15:06:16 +00:00
(do
liftIO $ logIO Debug "Running Pre-Loop stage"
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- run preLoop function from Affectionate
preLoop gameData
liftIO $ logIO Debug "Starting Loop"
2020-05-04 03:26:49 +00:00
whileM_ (hasNextStep gameData)
(do
-- get state
ad <- get
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- Measure time difference form last run
now <- liftIO $ getTime Monotonic
let lastTime = sysTime ad
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- compute dt and update elapsedTime
2021-01-03 17:21:12 +00:00
let dt = fromIntegral
2020-05-04 03:26:49 +00:00
(toNanoSecs $ diffTimeSpec lastTime now) / (10 ^ (9 :: Int))
2021-01-03 17:21:12 +00:00
ne = elapsedTime ad + dt
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- update state data object with new time values
put $ ad
{ elapsedTime = ne
, deltaTime = dt
}
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- poll events
2022-05-13 14:13:12 +00:00
liftIO SDL.pumpEvents
2020-05-04 03:26:49 +00:00
evs <- preHandleEvents =<< liftIO SDL.pollEvents
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- handle events
handleEvents gameData evs
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- execute user defined update loop
unless (pausedTime ad) (update gameData dt)
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- clear GL buffer >> execute user defined draw loop >> flush GL buffer
liftIO $ GL.clear [GL.ColorBuffer, GL.DepthBuffer, GL.StencilBuffer]
draw gameData
liftIO GL.flush
2022-07-10 02:18:22 +00:00
2020-05-04 03:26:49 +00:00
-- actual displaying of newly drawn frame
2022-07-10 02:18:22 +00:00
mapM_ (SDL.glSwapWindow . awWindow) windows
2020-05-04 11:40:57 +00:00
-- save new time
2020-05-04 06:23:03 +00:00
ad3 <- get
when (sysTime ad == sysTime ad3) (
put ad3
{ sysTime = now
}
)
2020-05-04 03:26:49 +00:00
)
)
2022-07-10 02:18:22 +00:00
-- Cleanup works
liftIO $ logIO Debug "Loop ended. Cleaning"
2022-07-10 02:18:22 +00:00
release gameDataKey
-- mapM_ (SDL.glDeleteContext . snd) contexts
-- mapM_ (SDL.destroyWindow . (\(_,y,_) -> y)) windows
2018-09-25 05:02:33 +00:00
-- SDL.quit -- <- This causes segfaults depending on hardware
liftIO $ logIO Debug "This is the end"
2022-07-10 02:18:22 +00:00
runAffection
:: AffectionData
-> AffectionState AffectionData ResIO a
-> IO (a, AffectionData)
runAffection initialState a = runResourceT $ runStateT (A.runState a) initialState