From 6c55a070fecbbb20789ebbf96a0e25a35fdfd86b Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 3 Sep 2021 11:44:17 +0200 Subject: [PATCH] fix: better handling of indirect rendering --- sonic-radiance.love/birb/core/init.lua | 1 + .../birb/core/scenemanager.lua | 4 ++ .../birb/modules/gui/elements/canvas.lua | 37 ++++++++++--------- .../birb/modules/gui/elements/parent.lua | 4 ++ sonic-radiance.love/birb/modules/gui/init.lua | 6 +++ .../birb/modules/gui/menus/model/init.lua | 7 ++++ .../birb/modules/gui/menus/model/page.lua | 6 +++ .../birb/modules/gui/menus/parent.lua | 5 +++ .../birb/modules/gui/menus/widgets/base.lua | 11 ++++-- sonic-radiance.love/birb/modules/scenes.lua | 4 ++ 10 files changed, 64 insertions(+), 21 deletions(-) diff --git a/sonic-radiance.love/birb/core/init.lua b/sonic-radiance.love/birb/core/init.lua index f88d879..c618bd0 100644 --- a/sonic-radiance.love/birb/core/init.lua +++ b/sonic-radiance.love/birb/core/init.lua @@ -138,6 +138,7 @@ end -- Draw the whole game function CoreSystem:draw() + self.scenemanager:redraw() self.scenemanager:draw() end diff --git a/sonic-radiance.love/birb/core/scenemanager.lua b/sonic-radiance.love/birb/core/scenemanager.lua index e613ea9..6344b02 100644 --- a/sonic-radiance.love/birb/core/scenemanager.lua +++ b/sonic-radiance.love/birb/core/scenemanager.lua @@ -127,4 +127,8 @@ function SceneManager:draw() self.controller.screen:cease() end +function SceneManager:redraw() + self.currentScene:redraw() +end + return SceneManager diff --git a/sonic-radiance.love/birb/modules/gui/elements/canvas.lua b/sonic-radiance.love/birb/modules/gui/elements/canvas.lua index f3b043c..54d782f 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/canvas.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/canvas.lua @@ -20,9 +20,6 @@ end function CanvasElement:updateElement(dt) CanvasElement.super.updateElement(self, dt) - if (self.canvas.needRedraw or self.canvas.isAnimated) then - self:redraw() - end end function CanvasElement:getCanvasDimensions() @@ -30,22 +27,28 @@ function CanvasElement:getCanvasDimensions() end function CanvasElement:redraw() - local w, h = self:getDimensions() + if (self.canvas.needRedraw or self.canvas.isAnimated) then + self:generateTexture() + end +end - local canvas = love.graphics.newCanvas(w + (self.canvas.padding*2), h + (self.canvas.padding*2)) - love.graphics.setCanvas(canvas) +function CanvasElement:generateTexture() + local w, h = self:getDimensions() - self:drawTexture() - self.canvas.needRedraw = false - love.graphics.setCanvas() - if (self.canvas.isAnimated) then - self.canvas.texture = canvas - else - local imageData = canvas:newImageData() - self.canvas.texture = love.graphics.newImage(imageData) - canvas:release() - imageData:release() - end + local canvas = love.graphics.newCanvas(w + (self.canvas.padding*2), h + (self.canvas.padding*2)) + love.graphics.setCanvas(canvas) + + self:drawTexture() + self.canvas.needRedraw = false + love.graphics.setCanvas() + if (self.canvas.isAnimated) then + self.canvas.texture = canvas + else + local imageData = canvas:newImageData() + self.canvas.texture = love.graphics.newImage(imageData) + canvas:release() + imageData:release() + end end function CanvasElement:drawTexture() diff --git a/sonic-radiance.love/birb/modules/gui/elements/parent.lua b/sonic-radiance.love/birb/modules/gui/elements/parent.lua index f2dcad3..ae7ac02 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/parent.lua @@ -110,6 +110,10 @@ end -- DRAW FUNCTIONS -- Draw the menu and its content +function GuiElement:redraw() + +end + function GuiElement:drawElement() self:draw() end diff --git a/sonic-radiance.love/birb/modules/gui/init.lua b/sonic-radiance.love/birb/modules/gui/init.lua index 7f6fbe5..1a9bbf1 100644 --- a/sonic-radiance.love/birb/modules/gui/init.lua +++ b/sonic-radiance.love/birb/modules/gui/init.lua @@ -162,6 +162,12 @@ end -- DRAW FUNCTIONS -- Draw the menu and its content +function Gui:redraw() + for _, element in pairs(self.elements) do + element:redraw() + end +end + function Gui:drawTop() for _, element in ipairs(self:getVisibleElement(true)) do element:drawElement() diff --git a/sonic-radiance.love/birb/modules/gui/menus/model/init.lua b/sonic-radiance.love/birb/modules/gui/menus/model/init.lua index e6979a1..39e8b53 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/model/init.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/model/init.lua @@ -185,5 +185,12 @@ function MenuModel:moveCursor(relative) page:moveCursor(relative) end +-- DRAW +-- Draw widget +function MenuModel:redraw() + local page = self:getCurrentPage() + page:redraw() +end + return MenuModel diff --git a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua index 326b76f..d99c471 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua @@ -189,5 +189,11 @@ function Page:moveCursor(relative) self:moveCursorAbsolute(self.selected + relative) end +-- DRAW +function Page:redraw() + for _, widget in ipairs(self.widgets) do + widget:redraw() + end +end return Page diff --git a/sonic-radiance.love/birb/modules/gui/menus/parent.lua b/sonic-radiance.love/birb/modules/gui/menus/parent.lua index 51e24c7..c5bfbd5 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/parent.lua @@ -169,6 +169,11 @@ function Menu:drawWidgetBackground(x, y, w, h) end +function Menu:redraw() + self.widget:redraw() + Menu.super.redraw(self) +end + -- WIDGET FUNCTIONS -- Handle widgets of the functions diff --git a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua index 9caee47..ca106f6 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua @@ -73,7 +73,13 @@ function BaseWidget:register() self.menu:addWidget(self) end -function BaseWidget:redrawCanvas() +function BaseWidget:redraw() + if (self.canvas.needRedraw) then + self:generateTexture() + end +end + +function BaseWidget:generateTexture() self.width, self.height = self.menu:getWidgetSize(self.id) local canvas = love.graphics.newCanvas(self.width, self.height) @@ -134,9 +140,6 @@ end -- Update the widget function BaseWidget:update(dt) - if (self.canvas.needRedraw) then - self:redrawCanvas() - end -- N/A end diff --git a/sonic-radiance.love/birb/modules/scenes.lua b/sonic-radiance.love/birb/modules/scenes.lua index d2c467b..141da1e 100644 --- a/sonic-radiance.love/birb/modules/scenes.lua +++ b/sonic-radiance.love/birb/modules/scenes.lua @@ -149,6 +149,10 @@ end -- DRAW FUNCTIONS -- Draw the scene and its content +function Scene:redraw() + self.gui:redraw() +end + function Scene:drawScene() self:drawStart() self:drawWorld()