Skip to content

Instantly share code, notes, and snippets.

@Bigfoot71
Created April 17, 2023 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bigfoot71/c7a79e742fe06cc93abc03ba4a198891 to your computer and use it in GitHub Desktop.
Save Bigfoot71/c7a79e742fe06cc93abc03ba4a198891 to your computer and use it in GitHub Desktop.
Script to handle display resizing in Löve2D
local lg = love.graphics
local Display = {
size = {
w = 0,
h = 0,
ox = 0,
oy = 0
};
scale = {
x = 1, y = 1,
rx = 1, ry = 1
};
ratio = {
value = 0, -- Ratio value: GW/GH
w = 0, h = 0, -- Size of the display in the window
offset_x = 0, -- Display offset (to center the display in the window)
offset_y = 0,
bar_w = 0, -- Bar size
bar_h = 0,
bar_offset_x = 0, -- Bar offset
bar_offset_y = 0,
};
}
function Display:init(keep_ratio)
self.base_width, self.base_height = lg.getDimensions()
self.keep_ratio = keep_ratio
self:resize()
end
-- Render scaled and translate --
function Display:push()
local scale = self.scale
local ratio = self.ratio
lg.push()
lg.translate(ratio.offset_x, ratio.offset_y)
lg.scale(scale.x, scale.y)
end
function Display:pop()
local ratio = self.ratio
if self.keep_ratio then
lg.setColor(0,0,0)
lg.rectangle("fill", -ratio.bar_offset_x, -ratio.bar_offset_y, ratio.bar_offset_x, ratio.bar_h)
lg.rectangle("fill", self.base_width, -ratio.bar_offset_y, ratio.bar_offset_x, ratio.bar_h)
lg.rectangle("fill", 0, -ratio.bar_offset_y, ratio.bar_w, ratio.bar_offset_y)
lg.rectangle("fill", 0, self.base_height, ratio.bar_w, ratio.bar_offset_y)
lg.setColor(1,1,1)
end
lg.pop()
end
function Display:render(drawable, ...)
self:push(); drawable(...); self:pop()
end
-- Render unscaled and "untranslated" --
function Display:unScaledPush()
local scale = self.scale
local ratio = self.ratio
lg.push()
lg.translate(-ratio.offset_x*scale.rx, -ratio.offset_y*scale.rx)
lg.scale(scale.rx, scale.ry)
end
function Display:unScaledPop()
lg.pop()
end
function Display:renderUnScaled(drawable, ...)
self:unScaledPush(); drawable(...); self:unScaledPop()
end
-- Resize function --
function Display:resize()
local size = self.size
local scale = self.scale
local ratio = self.ratio
local GW = self.base_width
local GH = self.base_height
size.w, size.h = lg.getDimensions()
if self.keep_ratio then
-- Calculation related to game display --
local ratio_value = GH/GW
local new_width, new_height = size.w, size.h
if new_width * ratio_value > new_height then
new_width = new_height / ratio_value
else
new_height = new_width * ratio_value
end
scale.x = new_width / GW
scale.y = new_height / GH
-- Calculation related to ratio and bars --
ratio.value = ratio_value
ratio.w, ratio.h = new_width, new_height
ratio.offset_x = (size.w - new_width) / 2
ratio.offset_y = (size.h - new_height) / 2
ratio.bar_w = size.w / scale.x
ratio.bar_h = size.h / scale.y
ratio.bar_offset_x = (ratio.bar_w - GW) / 2
ratio.bar_offset_y = (ratio.bar_h - GH) / 2
else
scale.x = size.w / GW
scale.y = size.h / GH
ratio.value = GW/GH
ratio.w = size.w
ratio.h = size.h
ratio.offset_x = 0
ratio.offset_y = 0
ratio.bar_w = 0
ratio.bar_h = 0
ratio.bar_offset_x = 0
ratio.bar_offset_y = 0
end
size.ox = size.w / 2
size.oy = size.h / 2
scale.rx = 1/scale.x
scale.ry = 1/scale.y
end
function Display:toWorld(x,y)
local s, r = self.scale, self.ratio
return (x - r.offset_x) / s.x,
(y - r.offset_y) / s.y
end
function Display:toScreen(x,y)
local s, r = self.scale, self.ratio
return (x * s.x) + r.offset_x,
(y * s.y) + r.offset_y
end
return Display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment