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-03-25 10:43:31 +00:00
|
|
|
, withWindow
|
|
|
|
, withDefaultWindow
|
|
|
|
, delaySec
|
2016-03-26 02:50:39 +00:00
|
|
|
, module Affection.Render
|
|
|
|
, module Affection.Types
|
2016-03-25 08:41:22 +00:00
|
|
|
) where
|
|
|
|
|
2016-10-31 22:47:16 +00:00
|
|
|
import qualified SDL
|
|
|
|
import qualified GEGL as G
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Maybe
|
|
|
|
|
|
|
|
import System.TimeIt (timeItT)
|
|
|
|
|
|
|
|
import Control.Monad.Loops
|
|
|
|
import Control.Monad.State
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Concurrent.MVar
|
2016-03-25 10:43:31 +00:00
|
|
|
|
2016-03-25 15:58:46 +00:00
|
|
|
import Affection.Render
|
2016-10-31 22:47:16 +00:00
|
|
|
import Affection.Types as Types
|
2016-03-25 15:58:46 +00:00
|
|
|
|
2016-10-31 22:47:16 +00:00
|
|
|
withAffection :: AffectionConfig us -> IO ()
|
|
|
|
withAffection conf@AffectionConfig{..} = do
|
|
|
|
case initComponents of
|
|
|
|
All ->
|
|
|
|
SDL.initializeAll
|
|
|
|
Only is ->
|
|
|
|
SDL.initialize is
|
|
|
|
execTime <- newMVar 0
|
|
|
|
window <- SDL.createWindow windowTitle windowConfig
|
|
|
|
surface <- SDL.getWindowSurface window
|
|
|
|
let initContainer = AffectionData conf False userData window surface
|
|
|
|
state <- newMVar initContainer
|
|
|
|
(res, nState) <- runStateT (
|
|
|
|
whileM_ (do
|
|
|
|
current <- get
|
|
|
|
return $ Types.quitEvent current
|
|
|
|
)
|
|
|
|
$ do
|
|
|
|
lastTime <- liftIO $ fromMaybe 0 <$> tryReadMVar execTime
|
|
|
|
(dTime, _) <- liftIO $ timeItT <$> drawLoop
|
|
|
|
(uTime, _) <- liftIO $ timeItT <$> updateLoop lastTime
|
|
|
|
liftIO $ putMVar execTime $ lastTime + dTime + uTime
|
|
|
|
) initContainer
|
|
|
|
SDL.quit
|
2016-03-25 10:43:31 +00:00
|
|
|
|
2016-10-31 22:47:16 +00:00
|
|
|
withWindow :: Monad m => T.Text -> WindowConfig -> SDL.RendererConfig -> RenderT m a -> IO ()
|
2016-03-25 15:58:46 +00:00
|
|
|
withWindow title wconf rconf ops = do
|
2016-10-31 22:47:16 +00:00
|
|
|
window <- SDL.createWindow title wconf
|
2016-10-16 15:01:31 +00:00
|
|
|
-- I don't need a renderer here, i need a surface
|
2016-10-31 22:47:16 +00:00
|
|
|
renderer <- SDL.createRenderer window (-1) rconf
|
|
|
|
surface <- SDL.getWindowSurface window
|
|
|
|
G.gegl_init
|
2016-10-16 15:01:31 +00:00
|
|
|
-- I think I need some AffectionT or someting similar here and not a RenderT
|
|
|
|
-- from SDL.
|
2016-03-25 15:58:46 +00:00
|
|
|
inRender renderer $ ops
|
2016-10-31 22:47:16 +00:00
|
|
|
G.gegl_exit
|
|
|
|
SDL.destroyWindow window
|
2016-03-25 10:43:31 +00:00
|
|
|
|
2016-10-31 22:47:16 +00:00
|
|
|
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
|
|
|
|
|
|
|
delaySec :: Int -> IO ()
|
2016-10-31 22:47:16 +00:00
|
|
|
delaySec dur = SDL.delay (fromIntegral $ dur * 1000)
|