aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-01 08:00:43 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-01 08:00:43 +0000
commit8d41ee397d3c159d3843b911e3dc6095700d3e5e (patch)
tree34b352f94d45087ed2d33ad1e9beee68444fcaa2 /src
parentac0ba74d4a1cbd9140cbe12a582a7ef4857d9b1f (diff)
downloadspace-plat-hs-8d41ee397d3c159d3843b911e3dc6095700d3e5e.tar.gz
space-plat-hs-8d41ee397d3c159d3843b911e3dc6095700d3e5e.zip
Pass an optional offset to the viewport
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs2
-rw-r--r--src/Game/Map.hs11
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.