From 6cca604b46884a407ae637387d25ae48e1ac7ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elm=C4=81rs=20=C4=80boli=C5=86=C5=A1?= Date: Thu, 1 Jul 2021 20:30:11 +0300 Subject: [PATCH] canvas assignment fix --- core/atlas.lua | 11 ++++------- core/element.lua | 19 ++++++++++++++++++- core/stack.lua | 3 +++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/core/atlas.lua b/core/atlas.lua index 55e56d7..3939d83 100644 --- a/core/atlas.lua +++ b/core/atlas.lua @@ -57,18 +57,18 @@ function atlases:assign(element) local elW = element.view.w local elH = element.view.h - local canvas, quad, interQuad = self.atlases[canvasID]:assignElement(element) + local canvas, quad = self.atlases[canvasID]:assignElement(element) if not canvas and self.atlases[canvasID].ideal_area < self.atlases[canvasID].taken_area*4 then --print('refragmenting ;3') self.atlases[canvasID]:refragment() - canvas, quad, interQuad = self.atlases[canvasID]:assignElement(element) + canvas, quad = self.atlases[canvasID]:assignElement(element) if not canvas then --print('ran out of space') end else --print('wont refragment', createdAtlas.ideal_area, createdAtlas.taken_area) end - return canvas, quad, interQuad + return canvas, quad, canvasID end function atlases:unassign(element) @@ -142,12 +142,9 @@ function atlas:assignElement(element) if self.users[element] and self.users[element].quad and self.users[element].interQuad then --update by reference owo self.users[element].quad:setViewport((x-1)*BLOCK_SIZE, (y-1)*BLOCK_SIZE, elW, elH) - self.users[element].interQuad:setViewport(0, 0, elW, elH) quad = self.users[element].quad - iquad = self.users[element].interQuad else quad = love.graphics.newQuad((x-1)*BLOCK_SIZE, (y-1)*BLOCK_SIZE, elW, elH, self.w, self.h) - iquad = love.graphics.newQuad(0, 0, elW, elH, sw, sh) end self.users[element] = { @@ -162,7 +159,7 @@ function atlas:assignElement(element) self:markTiles(x, y, tileSizeX, tileSizeY) self.taken_area = self.taken_area + ((tileSizeY*BLOCK_SIZE)*(tileSizeX*BLOCK_SIZE)) - return self.canvas, self.users[element].quad, iquad + return self.canvas, self.users[element].quad else print('failed to allocate :X') return false diff --git a/core/element.lua b/core/element.lua index a0f8727..44ae712 100644 --- a/core/element.lua +++ b/core/element.lua @@ -8,6 +8,8 @@ local helium = require(path .. ".dummy") local context = require(path.. ".core.stack") local scene = require(path.. ".core.scene") +local currentCanvasID + ---@class Element local element = {} element.__index = element @@ -55,6 +57,8 @@ function element:new(param, immediate, w, h, flags) immediate = immediate or false, --Whether this element has a canvas assigned hasCanvas = false, + --Which canvas is it assigned to + currentCanvasIndex = nil, --Current test render passes to be benchmarked testRenderPasses = 20, -- @@ -198,7 +202,7 @@ end local newCanvas, newQuad = love.graphics.newCanvas, love.graphics.newQuad function element:createCanvas() - self.canvas, self.quad = scene.activeScene.atlas:assign(self) + self.canvas, self.quad, self.settings.currentCanvasIndex = scene.activeScene.atlas:assign(self) if not self.canvas then self.settings.failedCanvas = true @@ -292,6 +296,18 @@ function element:externalRender() self.settings.isSetup = true end + local oldCanvasId + if self.settings.hasCanvas then + if self.settings.currentCanvasIndex == currentCanvasID then + --problem lol + self.settings.renderingParentCanvasIndex = currentCanvasID + self:reassignCanvas() + else + oldCanvasId = currentCanvasID + currentCanvasID = self.settings.currentCanvasIndex + end + end + if self.settings.needsRendering then if self.settings.hasCanvas then setCanvas(self.canvas) @@ -329,6 +345,7 @@ function element:externalRender() lg.setScissor() love.graphics.pop() + currentCanvasID = oldCanvasId end function element:externalUpdate() diff --git a/core/stack.lua b/core/stack.lua index e882255..c7be1ff 100644 --- a/core/stack.lua +++ b/core/stack.lua @@ -109,6 +109,9 @@ function context:getCanvasIndex(forCanvas) return self.parentCtx:getCanvasIndex() == 1 and 2 or 1 else if forCanvas then + if self.element.settings.renderingParentCanvasIndex then + return self.element.settings.renderingParentCanvasIndex == 1 and 2 or 1 + end return self.parentCtx:getCanvasIndex() == 1 and 2 or 1 end end