From f0507125e06e3e8939f259e9468d797e9a8e668e Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 1 Mar 2023 20:02:14 +0000 Subject: Dropped ReaderT as is not really needed --- game.cabal | 1 - src/Game.hs | 56 ++++++++++++++++++++++++++------------------------------ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/game.cabal b/game.cabal index d7e890f..9852415 100644 --- a/game.cabal +++ b/game.cabal @@ -34,7 +34,6 @@ library Game.Utils build-depends: base - , mtl , text >= 1.1.0.0 && < 2.1 , vector>=0.10.9.0 && <0.14 , containers diff --git a/src/Game.hs b/src/Game.hs index 537b889..d8ce8b6 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -1,6 +1,6 @@ module Game (main) where -import Control.Monad.Reader +import Control.Monad import Data.IORef import Data.Maybe (fromMaybe) import Data.Text (pack) @@ -92,8 +92,7 @@ main = do } hud <- H.mkHud sprites state entities <- newIORef =<< E.mkEntities sprites map' controls state - runReaderT - gameLoop + gameLoop Env { window = window, renderer = renderer, @@ -111,9 +110,8 @@ main = do SDL.destroyWindow window SDL.quit -toggleFullscreen :: ReaderT Env IO () -toggleFullscreen = do - env <- ask +toggleFullscreen :: Env -> IO () +toggleFullscreen env = do let fullscreen = env.fullscreen renderRect = env.renderRect renderer = env.renderer @@ -136,9 +134,8 @@ toggleFullscreen = do rh = gameHeight * scale in renderRect $= newRenderRect -gameLoop :: ReaderT Env IO () -gameLoop = do - env <- ask +gameLoop :: Env -> IO () +gameLoop env = do let renderer = env.renderer canvas = env.canvas renderRect = env.renderRect @@ -148,7 +145,7 @@ gameLoop = do events <- map SDL.eventPayload <$> SDL.pollEvents -- F11 for fullscreen / windowed - when (fromMaybe False $ U.isPressed SDL.KeycodeF11 events) toggleFullscreen + when (fromMaybe False $ U.isPressed SDL.KeycodeF11 events) $ toggleFullscreen env -- ESC or close the window to quit let quit = fromMaybe False (U.isPressed SDL.KeycodeEscape events) || SDL.QuitEvent `elem` events @@ -159,11 +156,10 @@ gameLoop = do SDL.rendererRenderTarget renderer $= Just canvas SDL.clear renderer - void $ liftIO $ do - state <- readIORef stateRef - when (state.gameOverDelay > 1) $ stateRef $= state {GS.gameOverDelay = state.gameOverDelay - 1} - when (state.gameOverDelay /= 1) $ playLoop env - when (state.gameOverDelay == 1) $ gameOverLoop env + state <- readIORef stateRef + when (state.gameOverDelay > 1) $ stateRef $= state {GS.gameOverDelay = state.gameOverDelay - 1} + when (state.gameOverDelay /= 1) playLoop + when (state.gameOverDelay == 1) gameOverLoop SDL.rendererRenderTarget renderer $= Nothing SDL.clear renderer @@ -172,10 +168,10 @@ gameLoop = do SDL.present renderer - gameLoop + gameLoop env where - playLoop :: Env -> IO () - playLoop env = do + playLoop :: IO () + playLoop = do let renderer = env.renderer map' = env.map entities = env.entities @@ -188,18 +184,18 @@ gameLoop = do let (px, py) = E.playerPosition updated -- render map and entities - void $ do - -- set the SDL viewport - viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height)) - M.render renderer map' viewport - E.renderVisible renderer updated viewport - - -- reset viewport to draw the HUD - SDL.rendererViewport renderer $= Nothing - H.render renderer hud - - gameOverLoop :: Env -> IO () - gameOverLoop env = do + + -- set the SDL viewport + viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height)) + M.render renderer map' viewport + E.renderVisible renderer updated viewport + + -- reset viewport to draw the HUD + SDL.rendererViewport renderer $= Nothing + H.render renderer hud + + gameOverLoop :: IO () + gameOverLoop = do let renderer = env.renderer sprites = env.sprites hud = env.hud -- cgit v1.2.3