Pillar/letterbox instead of stretching, if window size has different aspect ratio

This commit is contained in:
Juhani Krekelä 2019-06-30 10:49:11 +03:00
parent 428a07b94f
commit 5b5a72a780
1 changed files with 47 additions and 33 deletions

View File

@ -702,90 +702,104 @@ end
-- Drawing
-- ------------------------------------------------------------------
function drawCavern()
local x_scale = love.graphics.getWidth() / cavern.width
local y_scale = love.graphics.getHeight() / cavern.height
local screen_width = love.graphics.getWidth()
local screen_height = love.graphics.getHeight()
local width_scale = screen_width / cavern.width
local height_scale = screen_height / cavern.height
local scale = math.min(width_scale, height_scale)
local x_offset = (screen_width - cavern.width * scale) / 2
local y_offset = (screen_height - cavern.height * scale) / 2
for tile_x, list in ipairs(visibility_map) do
local x = (tile_x - 1) * x_scale
local x = (tile_x - 1) * scale + x_offset
for tile_y, visible in ipairs(list) do
local y = (tile_y - 1) * y_scale
local y = (tile_y - 1) * scale + y_offset
if visible or debug then
tile = cavern[tile_x][tile_y]
if tile == tiletypes.empty then
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
elseif tile == tiletypes.wall then
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
elseif tile == tiletypes.orb then
love.graphics.setColor(0.5, 0.3, 0.3)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
love.graphics.setColor(0, 1, 0)
love.graphics.ellipse('fill', x + 0.5 * x_scale, y + 0.5 * y_scale, 0.7 * x_scale/2, 0.7 * y_scale/2)
love.graphics.ellipse('fill', x + 0.5 * scale, y + 0.5 * scale, 0.7 * scale/2, 0.7 * scale/2)
elseif tile == tiletypes.slime then
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
love.graphics.setColor(1, 1, 0)
love.graphics.ellipse('fill', x + 0.5 * x_scale, y + 0.5 * y_scale, 0.8 * x_scale/2, 0.8 * y_scale/2)
love.graphics.rectangle('fill', x + 0.1 * x_scale , y + 0.5 * y_scale, 0.8 * x_scale, 0.8 * y_scale/2)
love.graphics.ellipse('fill', x + 0.5 * scale, y + 0.5 * scale, 0.8 * scale/2, 0.8 * scale/2)
love.graphics.rectangle('fill', x + 0.1 * scale , y + 0.5 * scale, 0.8 * scale, 0.8 * scale/2)
elseif tile == tiletypes.unknown then
love.graphics.setColor(1, 0.5, 0.5)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
else
print("Impossible tile!!!")
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
end
else
tile = remembered_cavern[tile_x][tile_y]
if tile == tiletypes.empty then
love.graphics.setColor(0.2, 0.2, 0.2)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
elseif tile == tiletypes.wall then
love.graphics.setColor(0.9, 0.9, 0.9)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
elseif tile == tiletypes.orb then
love.graphics.setColor(0.4, 0.4, 0.4)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
love.graphics.setColor(0.2, 0.7, 0.2)
love.graphics.ellipse('fill', x + 0.5 * x_scale, y + 0.5 * y_scale, 0.7 * x_scale/2, 0.7 * y_scale/2)
love.graphics.ellipse('fill', x + 0.5 * scale, y + 0.5 * scale, 0.7 * scale/2, 0.7 * scale/2)
elseif tile == tiletypes.slime then
love.graphics.setColor(0.2, 0.2, 0.2)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
love.graphics.setColor(0.7, 0.7, 0.2)
love.graphics.ellipse('fill', x + 0.5 * x_scale, y + 0.5 * y_scale, 0.8 * x_scale/2, 0.8 * y_scale/2)
love.graphics.rectangle('fill', x + 0.1 * x_scale , y + 0.5 * y_scale, 0.8 * x_scale, 0.8 * y_scale/2)
love.graphics.ellipse('fill', x + 0.5 * scale, y + 0.5 * scale, 0.8 * scale/2, 0.8 * scale/2)
love.graphics.rectangle('fill', x + 0.1 * scale , y + 0.5 * scale, 0.8 * scale, 0.8 * scale/2)
else
love.graphics.setColor(0.7, 0.5, 0.5)
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
love.graphics.rectangle('fill', x, y, scale, scale)
end
end
if debug and tile_x == player_x and tile_y == player_y then
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('fill', x + 0.5 * x_scale, y + 0.5 * y_scale, x_scale/2, y_scale/2)
love.graphics.rectangle('fill', x + 0.5 * scale, y + 0.5 * scale, scale/2, scale/2)
end
end
end
end
function drawPlayer()
local x_scale = love.graphics.getWidth() / cavern.width
local y_scale = love.graphics.getHeight() / cavern.height
local screen_width = love.graphics.getWidth()
local screen_height = love.graphics.getHeight()
local player_head_center_x = (player_x - 1) * x_scale + 0.5 * x_scale
local player_head_center_y = (player_y - 1) * y_scale + 0.5 * y_scale
local width_scale = screen_width / cavern.width
local height_scale = screen_height / cavern.height
local scale = math.min(width_scale, height_scale)
local x_offset = (screen_width - cavern.width * scale) / 2
local y_offset = (screen_height - cavern.height * scale) / 2
local player_head_center_x = (player_x - 1) * scale + x_offset + 0.5 * scale
local player_head_center_y = (player_y - 1) * scale + y_offset + 0.5 * scale
love.graphics.setColor(0.6, 0.3, 0.2)
love.graphics.ellipse('fill', player_head_center_x, player_head_center_y, x_scale/2, y_scale/2)
love.graphics.ellipse('fill', player_head_center_x, player_head_center_y, scale/2, scale/2)
local player_body_x, player_body_y = getBodyLocation()
local player_body_x = (player_body_x - 1) * x_scale
local player_body_y = (player_body_y - 1) * y_scale
local player_body_x = (player_body_x - 1) * scale + x_offset
local player_body_y = (player_body_y - 1) * scale + y_offset
love.graphics.setColor(0, 0, 1)
love.graphics.rectangle('fill', player_body_x, player_body_y, x_scale, y_scale)
love.graphics.rectangle('fill', player_body_x, player_body_y, scale, scale)
end
function drawWin()
@ -798,11 +812,11 @@ function drawWin()
local height_scale = screen_height / img_height
local scale = math.min(width_scale, height_scale)
local left_border = (screen_width - img_width * scale) / 2
local up_border = (screen_height - img_height * scale) / 2
local x_offset = (screen_width - img_width * scale) / 2
local y_offset = (screen_height - img_height * scale) / 2
love.graphics.setColor(1, 1, 1)
love.graphics.draw(win_image, left_border, up_border, 0, scale, scale)
love.graphics.draw(win_image, x_offset, y_offset, 0, scale, scale)
end
function drawConfig()