From f1de3a2d228883b5824f780d473547bd516ca7e7 Mon Sep 17 00:00:00 2001 From: Nate Boxer Date: Mon, 7 Apr 2025 09:41:08 -0700 Subject: [PATCH 1/2] Added useSafeArea (optional, use window safe area for mobile target instead of getDimensions()), safe_offset_x, safe_offset_y to state. WIP - this correctly sizes the viewport but does not center it corr ectly or use the safe offset values. --- shove.lua | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/shove.lua b/shove.lua index b34310a..eb40160 100644 --- a/shove.lua +++ b/shove.lua @@ -17,6 +17,9 @@ ---@field resizeCallback function Callback function for window resize ---@field inDrawMode boolean Whether we're currently in drawing mode ---@field specialLayerUsage table Tracking for special layer usage +---@field useSafeArea boolean Use mobile safe area for screen resolution +---@field safe_offset_x number Horizontal safe offset +---@field safe_offset_y number Vertical safe offset -- Internal state variables local state = { -- Settings @@ -61,7 +64,10 @@ local state = { shaderRegistry = { nextId = 1, shaders = setmetatable({}, {__mode = "k"}) -- Weak keys to allow shader garbage collection - } + }, + useSafeArea = false, + safe_offset_x = 0, + safe_offset_y = 0 } ---@class ShoveLayerSystem @@ -854,6 +860,7 @@ local shove = { ---@field fitMethod? "aspect"|"pixel"|"stretch"|"none" Scaling method ---@field renderMode? "direct"|"layer" Rendering approach ---@field scalingFilter? "nearest"|"linear" Scaling filter for textures + ---@field useSafeArea? boolean Use mobile safe area --- Initialize the resolution system ---@param width number Viewport width @@ -897,6 +904,13 @@ local shove = { settingsTable.scalingFilter ~= "none" then error("shove.setResolution: scalingFilter must be 'nearest', 'linear', or 'none'", 2) end + + -- Validate useSafeArea + if settingsTable.useSafeArea ~= nil and + settingsTable.useSafeArea ~= true and + settingsTable.useSafeArea ~= false then + error("shove.setResolutions: useSafeArea must be true or false", 2) + end end -- Clear previous state @@ -908,7 +922,6 @@ local shove = { state.viewport_width = width state.viewport_height = height - state.screen_width, state.screen_height = love.graphics.getDimensions() if settingsTable then state.fitMethod = settingsTable.fitMethod or "aspect" @@ -918,10 +931,18 @@ local shove = { else state.scalingFilter = state.fitMethod == "pixel" and "nearest" or "linear" end + state.useSafeArea = settingsTable.useSafeArea or false else state.fitMethod = "aspect" state.renderMode = "direct" state.scalingFilter = "linear" + state.useSafeArea = false + end + + if state.useSafeArea then + state.safe_offset_x, safe_offset_y, state.screen_width, state.screen_height = love.window.getSafeArea() + else + state.screen_width, state.screen_height = love.graphics.getDimensions() end calculateTransforms() @@ -979,7 +1000,12 @@ local shove = { if success then -- Only call resize if we're already initialized if state.viewport_width > 0 and state.viewport_height > 0 then - local actualWidth, actualHeight = love.graphics.getDimensions() + local actualWidth, actualHeight + if state.useSafeArea then + _, _, actualWidth, actualHeight = love.window.getSafeArea() + else + actualWidth, actualHeight = love.graphics.getDimensions() + end shove.resize(actualWidth, actualHeight) end end @@ -1010,7 +1036,12 @@ local shove = { if success then -- Get the actual dimensions (might differ from requested) - local actualWidth, actualHeight = love.graphics.getDimensions() + local actualWidth, actualHeight + if state.useSafeArea then + _, _, actualWidth, actualHeight = love.window.getSafeArea() + else + actualWidth, actualHeight = love.graphics.getDimensions() + end shove.resize(actualWidth, actualHeight) end @@ -1785,6 +1816,10 @@ local shove = { state.screen_width = width state.screen_height = height + if state.useSafeArea then + state.safe_offset_x, state.safe_offset_y, _, _ = love.window.getSafeArea() + end + calculateTransforms() -- Call resize callback if it exists if type(state.resizeCallback) == "function" then From ea6407f849a21872e62d47995aa3178a5336144d Mon Sep 17 00:00:00 2001 From: Nate Boxer Date: Tue, 8 Apr 2025 15:22:58 -0700 Subject: [PATCH 2/2] removed safe area from get actual dimensions and now it seems to work (mostly) --- shove.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shove.lua b/shove.lua index eb40160..0019592 100644 --- a/shove.lua +++ b/shove.lua @@ -1002,8 +1002,8 @@ local shove = { if state.viewport_width > 0 and state.viewport_height > 0 then local actualWidth, actualHeight if state.useSafeArea then - _, _, actualWidth, actualHeight = love.window.getSafeArea() - else + --_, _, actualWidth, actualHeight = love.window.getSafeArea() + --else actualWidth, actualHeight = love.graphics.getDimensions() end shove.resize(actualWidth, actualHeight) @@ -1038,8 +1038,8 @@ local shove = { -- Get the actual dimensions (might differ from requested) local actualWidth, actualHeight if state.useSafeArea then - _, _, actualWidth, actualHeight = love.window.getSafeArea() - else + --_, _, actualWidth, actualHeight = love.window.getSafeArea() + --else actualWidth, actualHeight = love.graphics.getDimensions() end shove.resize(actualWidth, actualHeight)