pituicat/src/Affection/Util.hs
2017-11-27 23:30:11 +01:00

67 lines
1.4 KiB
Haskell

module Affection.Util
where
import Affection.Types
import qualified SDL
import System.Clock
import Control.Monad.State
-- Prehandle SDL events in case any window events occur
preHandleEvents :: [SDL.Event] -> Affection us msg [SDL.EventPayload]
preHandleEvents evs =
return $ map SDL.eventPayload evs
-- | Return the userstate to the user
getAffection :: Affection us msg us
getAffection = do
ad <- get
return $ userState ad
-- | Put altered user state back
putAffection
:: us -- User state
-> Affection us msg ()
putAffection us = do
ad <- get
put $ ad
{ userState = us }
-- | block a thread for a specified amount of time
delaySec
:: Int -- ^ Number of seconds
-> IO ()
delaySec dur = SDL.delay (fromIntegral $ dur * 1000)
-- | Get time since start but always the same in the current tick.
getElapsedTime :: Affection us msg Double
getElapsedTime =
elapsedTime <$> get
getDelta :: Affection us msg Double
getDelta =
deltaTime <$> get
quit :: Affection us msg ()
quit = do
ad <- get
put $ ad { quitEvent = True }
toggleScreen :: Affection us msg ()
toggleScreen = do
ad <- get
newMode <- case screenMode ad of
SDL.Windowed -> do
SDL.setWindowMode (drawWindow ad) SDL.Fullscreen
return SDL.Fullscreen
SDL.Fullscreen -> do
SDL.setWindowMode (drawWindow ad) SDL.Windowed
return SDL.Windowed
now <- liftIO $ getTime Monotonic
put ad
{ sysTime = now
, screenMode = newMode
}