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/Map.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/Game/Map.hs') 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