affection/src/Affection/Util.hs

67 lines
1.4 KiB
Haskell
Raw Normal View History

2017-09-07 04:23:01 +00:00
module Affection.Util
2017-07-29 19:45:40 +00:00
where
import Affection.Types
import qualified SDL
import System.Clock
import Control.Monad.State
-- Prehandle SDL events in case any window events occur
2017-12-12 12:12:06 +00:00
preHandleEvents :: [SDL.Event] -> Affection us [SDL.EventPayload]
2017-07-29 19:45:40 +00:00
preHandleEvents evs =
return $ map SDL.eventPayload evs
-- | Return the userstate to the user
2017-12-12 12:12:06 +00:00
getAffection :: Affection us us
2017-07-29 19:45:40 +00:00
getAffection = do
ad <- get
return $ userState ad
-- | Put altered user state back
putAffection
:: us -- User state
2017-12-12 12:12:06 +00:00
-> Affection us ()
2017-07-29 19:45:40 +00:00
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.
2017-12-12 12:12:06 +00:00
getElapsedTime :: Affection us Double
2017-07-29 19:45:40 +00:00
getElapsedTime =
elapsedTime <$> get
2017-12-12 12:12:06 +00:00
getDelta :: Affection us Double
2017-07-29 19:45:40 +00:00
getDelta =
deltaTime <$> get
2017-12-12 12:12:06 +00:00
quit :: Affection us ()
2017-07-29 19:45:40 +00:00
quit = do
ad <- get
put $ ad { quitEvent = True }
2017-12-12 12:12:06 +00:00
toggleScreen :: Affection us ()
2017-07-29 19:45:40 +00:00
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
}