diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-03-01 08:00:43 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-03-01 08:00:43 +0000 |
commit | 8d41ee397d3c159d3843b911e3dc6095700d3e5e (patch) | |
tree | 34b352f94d45087ed2d33ad1e9beee68444fcaa2 /src/Game | |
parent | ac0ba74d4a1cbd9140cbe12a582a7ef4857d9b1f (diff) | |
download | space-plat-hs-8d41ee397d3c159d3843b911e3dc6095700d3e5e.tar.gz space-plat-hs-8d41ee397d3c159d3843b911e3dc6095700d3e5e.zip |
Pass an optional offset to the viewport
Diffstat (limited to 'src/Game')
-rw-r--r-- | src/Game/Map.hs | 11 |
1 files changed, 6 insertions, 5 deletions
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. |