Skip to content

Commit

Permalink
Add resizing of the window on desktop OS
Browse files Browse the repository at this point in the history
  • Loading branch information
aglitchman committed Mar 1, 2022
1 parent d46f376 commit 231f8cf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,39 @@ function M.load_scene(self, id)
end, nil, error)
end

local SYSTEM_NAME = sys.get_sys_info().system_name

function M.resize_window()
if SYSTEM_NAME ~= "Windows" and SYSTEM_NAME ~= "Darwin" and SYSTEM_NAME ~= "Linux" then
return
end
assert(defos, "`defos` is required")

local displays = defos.get_displays()
local current_display_id = defos.get_current_display_id()
local screen_width = 1920 -- displays[current_display_id].bounds.width
local screen_height = 1080 -- displays[current_display_id].bounds.height

local project_width = tonumber(sys.get_config("display.width", 1920))
local project_height = tonumber(sys.get_config("display.height", 1080))

-- Resize the window. The code doesn't respect HiDPi screens.
local x, y, w, h = defos.get_view_size()
w = project_width
h = project_height
print("w x h -> ", w, h, screen_width, screen_height)
while screen_height * 0.95 <= h or screen_width * 0.95 <= w do
w = w * 0.75
h = h * 0.75
print("w x h -> ", w, h, screen_width, screen_height)
end
defos.set_view_size(x, y, w, h)

-- Center the window
local x, y, w, h = defos.get_window_size()
x = math.floor((screen_width - w) / 2)
y = math.floor((screen_height - h) / 2)
defos.set_window_size(x, y, w, h)
end

return M
3 changes: 3 additions & 0 deletions examples/main.script
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function init(self)
main.setup_error_handling()
window.set_listener(main.window_callback)

-- Resize game window on desktop platforms
main.resize_window()

-- Set Pointer Lock's virtual cursor invisible by default
pointer.cursor_visible = false

Expand Down

0 comments on commit 231f8cf

Please sign in to comment.