From 8d41ee397d3c159d3843b911e3dc6095700d3e5e Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 1 Mar 2023 08:00:43 +0000 Subject: Pass an optional offset to the viewport --- src/Game.hs | 2 +- src/Game/Map.hs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index d1552d0..537b889 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -190,7 +190,7 @@ gameLoop = do -- render map and entities void $ do -- set the SDL viewport - viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) + 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 diff --git a/src/Game/Map.hs b/src/Game/Map.hs index 5da8133..8445a1b 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -13,7 +13,7 @@ module Game.Map where import Control.Monad -import Data.Maybe (mapMaybe) +import Data.Maybe (fromMaybe, mapMaybe) import qualified Game.Utils as U import SDL (($=)) import qualified SDL @@ -169,8 +169,9 @@ totalBatteries m = length $ filter isBattery (objects m) -- | Set the SDL viewport based on the map and the provided viewport coordinates. -- It returns the viewport to be used by render. -viewport :: SDL.Renderer -> Map -> Int -> Int -> Int -> Int -> IO Viewport -viewport renderer (Map mapData _) vx vy vw vh = do +-- Optionally an offset can be provided. +viewport :: SDL.Renderer -> Map -> Int -> Int -> Int -> Int -> Maybe (Int, Int) -> IO Viewport +viewport renderer (Map mapData _) vx vy vw vh offs = do SDL.rendererViewport renderer $= Just mapRect pure $ Viewport newx newy vw vh where @@ -187,8 +188,8 @@ viewport renderer (Map mapData _) vx vy vw vh = do newx = min (if fx + halfViewportWidth > mapWidth then mapWidth - vw else fx) (mapWidth - vw) newy = min (if fy + halfViewportHeight > mapHeight then mapHeight - vh else fy) (mapHeight - vh) - -- FIXME: hardcoded size of the HUD; perhaps we need to pass x, y offsets to the viewport - mapRect = U.rect (-newx) (16 - newy) (newx + vw) vh + (offsx, offsy) = fromMaybe (0, 0) offs + mapRect = U.rect (offsx - newx) (offsy - newy) (newx + vw) vh -- | Render a map. -- Requires a Viewport from viewport. -- cgit v1.2.3