From 2f7d313ab9c3410b94c49a2f6bc33504b13574ab Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Jul 2019 10:04:06 +0200 Subject: [PATCH] fix(camera): fix tearing and heavy pixelisation in zoom mode Currently, the canvas was created at render size and elements where resized inside. Work a bit differenlty, by creating a canvas that have the view "true" size (meaning the scaled size), and draw everything at 1:1 size inside. Then, when drawing the view canvas, resize the whole canvas at the multiplication of both scaling. --- gamecore/modules/world/camera/init.lua | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index a6f9576..add1031 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -164,8 +164,8 @@ function CameraSystem:attachView(id) -- de position de camera pour afficher la carte par rapport à ces infos tx, ty = self:getViewCoordinate(id) scale = self:getViewScale(id) or 1 - tx = math.floor(tx) * -1 * scale - ty = math.floor(ty) * -1 * scale + tx = math.floor(tx) * -1 + ty = math.floor(ty) * -1 local w, h = core.screen:getDimensions() end @@ -173,25 +173,30 @@ function CameraSystem:attachView(id) love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) - love.graphics.scale(view.scale, view.scale) end end function CameraSystem:detachView(id) if (id ~= nil) then local view = self:getView(id) + love.graphics.rectangle("fill", view.x-8, view.y-8, 16, 16) love.graphics.pop() love.graphics.setCanvas(self.current_canvas) local tx, ty = self:getOnScreenViewCoordinate(id) - local scale = core.screen:getScale() + local scale = core.screen:getScale() * view.scale + love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) love.graphics.scale(scale, scale) love.graphics.draw(view.canvas) + + local unscale = 1 / view.scale + love.graphics.scale(unscale, unscale) self:drawHUD(id) + love.graphics.pop() end end @@ -247,6 +252,16 @@ function CameraSystem:getOnScreenViewCenter(id) return core.screen:getScreenCoordinate(viewx, viewy) end +function CameraSystem:setViewScale(id, scale) + local view = self:getView(id) + + view.scale = scale + + local _, _, w, h = self:getViewCoordinate(id) + + view.canvas = love.graphics.newCanvas(math.ceil(w), math.ceil(h)) +end + function CameraSystem:getViewScale(id) local view = self:getView(id) @@ -337,7 +352,7 @@ function CameraSystem:followAllActors(id) local scalex = (maxX-minX)/ww local scaley = (maxY-minY)/hh scale = math.max(scale, scalex, scaley) - view.scale = 1/scale + self:setViewScale(id, 1/scale) self.world:resizeMap(ww * 3, hh * 3) end