fix: better handling of indirect rendering

This commit is contained in:
Kazhnuz 2021-09-03 11:44:17 +02:00
parent 07400ce991
commit 6c55a070fe
10 changed files with 64 additions and 21 deletions

View file

@ -138,6 +138,7 @@ end
-- Draw the whole game -- Draw the whole game
function CoreSystem:draw() function CoreSystem:draw()
self.scenemanager:redraw()
self.scenemanager:draw() self.scenemanager:draw()
end end

View file

@ -127,4 +127,8 @@ function SceneManager:draw()
self.controller.screen:cease() self.controller.screen:cease()
end end
function SceneManager:redraw()
self.currentScene:redraw()
end
return SceneManager return SceneManager

View file

@ -20,9 +20,6 @@ end
function CanvasElement:updateElement(dt) function CanvasElement:updateElement(dt)
CanvasElement.super.updateElement(self, dt) CanvasElement.super.updateElement(self, dt)
if (self.canvas.needRedraw or self.canvas.isAnimated) then
self:redraw()
end
end end
function CanvasElement:getCanvasDimensions() function CanvasElement:getCanvasDimensions()
@ -30,22 +27,28 @@ function CanvasElement:getCanvasDimensions()
end end
function CanvasElement:redraw() 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)) function CanvasElement:generateTexture()
love.graphics.setCanvas(canvas) local w, h = self:getDimensions()
self:drawTexture() local canvas = love.graphics.newCanvas(w + (self.canvas.padding*2), h + (self.canvas.padding*2))
self.canvas.needRedraw = false love.graphics.setCanvas(canvas)
love.graphics.setCanvas()
if (self.canvas.isAnimated) then self:drawTexture()
self.canvas.texture = canvas self.canvas.needRedraw = false
else love.graphics.setCanvas()
local imageData = canvas:newImageData() if (self.canvas.isAnimated) then
self.canvas.texture = love.graphics.newImage(imageData) self.canvas.texture = canvas
canvas:release() else
imageData:release() local imageData = canvas:newImageData()
end self.canvas.texture = love.graphics.newImage(imageData)
canvas:release()
imageData:release()
end
end end
function CanvasElement:drawTexture() function CanvasElement:drawTexture()

View file

@ -110,6 +110,10 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the menu and its content -- Draw the menu and its content
function GuiElement:redraw()
end
function GuiElement:drawElement() function GuiElement:drawElement()
self:draw() self:draw()
end end

View file

@ -162,6 +162,12 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the menu and its content -- Draw the menu and its content
function Gui:redraw()
for _, element in pairs(self.elements) do
element:redraw()
end
end
function Gui:drawTop() function Gui:drawTop()
for _, element in ipairs(self:getVisibleElement(true)) do for _, element in ipairs(self:getVisibleElement(true)) do
element:drawElement() element:drawElement()

View file

@ -185,5 +185,12 @@ function MenuModel:moveCursor(relative)
page:moveCursor(relative) page:moveCursor(relative)
end end
-- DRAW
-- Draw widget
function MenuModel:redraw()
local page = self:getCurrentPage()
page:redraw()
end
return MenuModel return MenuModel

View file

@ -189,5 +189,11 @@ function Page:moveCursor(relative)
self:moveCursorAbsolute(self.selected + relative) self:moveCursorAbsolute(self.selected + relative)
end end
-- DRAW
function Page:redraw()
for _, widget in ipairs(self.widgets) do
widget:redraw()
end
end
return Page return Page

View file

@ -169,6 +169,11 @@ function Menu:drawWidgetBackground(x, y, w, h)
end end
function Menu:redraw()
self.widget:redraw()
Menu.super.redraw(self)
end
-- WIDGET FUNCTIONS -- WIDGET FUNCTIONS
-- Handle widgets of the functions -- Handle widgets of the functions

View file

@ -73,7 +73,13 @@ function BaseWidget:register()
self.menu:addWidget(self) self.menu:addWidget(self)
end 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) self.width, self.height = self.menu:getWidgetSize(self.id)
local canvas = love.graphics.newCanvas(self.width, self.height) local canvas = love.graphics.newCanvas(self.width, self.height)
@ -134,9 +140,6 @@ end
-- Update the widget -- Update the widget
function BaseWidget:update(dt) function BaseWidget:update(dt)
if (self.canvas.needRedraw) then
self:redrawCanvas()
end
-- N/A -- N/A
end end

View file

@ -149,6 +149,10 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the scene and its content -- Draw the scene and its content
function Scene:redraw()
self.gui:redraw()
end
function Scene:drawScene() function Scene:drawScene()
self:drawStart() self:drawStart()
self:drawWorld() self:drawWorld()