improvement(boxes): only use one texture for boxes instead of two

This commit is contained in:
Kazhnuz 2019-07-19 18:48:15 +02:00
parent afbbf50539
commit 4e366051c4

View file

@ -37,10 +37,8 @@ function Box3D:new(owner, w, h, d)
self.shadowSources = {} self.shadowSources = {}
self.needRedraw = false self.needRedraw = false
self.texture = {} self:setTexture()
self:setTopTexture() self.shadows = love.graphics.newCanvas(self.w, self.h)
self:setBottomTexture()
self.texture.shadows = love.graphics.newCanvas(self.w, self.h)
self:register() self:register()
end end
@ -62,29 +60,33 @@ function Box3D:setSize()
self:regenerate() self:regenerate()
end end
function Box3D:setTopTexture() function Box3D:setTexture()
local canvas = love.graphics.newCanvas(self.w, self.h) local canvas = love.graphics.newCanvas(self.w, self.h + self.d)
love.graphics.setCanvas( canvas ) love.graphics.setCanvas( canvas )
utils.graphics.resetColor() utils.graphics.resetColor()
love.graphics.rectangle("fill", 0, 0, self.w, self.h)
self:drawTextureContent()
love.graphics.setCanvas( ) love.graphics.setCanvas( )
local imagedata = canvas:newImageData() local imagedata = canvas:newImageData()
self.texture.top = love.graphics.newImage( imagedata ) self.texture = love.graphics.newImage( imagedata )
imagedata:release() imagedata:release()
canvas:release() canvas:release()
end end
function Box3D:setBottomTexture() function Box3D:drawTextureContent()
local canvas = love.graphics.newCanvas(self.w, self.d) self:drawTopTexture()
love.graphics.setCanvas( canvas ) self:drawBottomTexture()
love.graphics.setColor(0.9, 0.9, 0.9, 1) end
love.graphics.rectangle("fill", 0, 0, self.w, self.d)
function Box3D:drawTopTexture()
utils.graphics.resetColor() utils.graphics.resetColor()
love.graphics.setCanvas( ) love.graphics.rectangle("fill", 0, 0, self.w, self.h)
local imagedata = canvas:newImageData() end
self.texture.bottom = love.graphics.newImage( imagedata )
imagedata:release() function Box3D:drawBottomTexture()
canvas:release() love.graphics.setColor(0.9, 0.9, 0.9, 1)
love.graphics.rectangle("fill", 0, self.h, self.w, self.d)
end end
function Box3D:setShadowSource(actor, x, y) function Box3D:setShadowSource(actor, x, y)
@ -123,7 +125,7 @@ end
function Box3D:redrawShadowCanvas() function Box3D:redrawShadowCanvas()
if (self.needRedraw) then if (self.needRedraw) then
love.graphics.setCanvas( self.texture.shadows ) love.graphics.setCanvas( self.shadows )
love.graphics.clear() love.graphics.clear()
for i,v in ipairs(self.shadowSources) do for i,v in ipairs(self.shadowSources) do
v.actor:drawShadow(v.x, v.y) v.actor:drawShadow(v.x, v.y)
@ -141,10 +143,9 @@ function Box3D:draw(x, y, z)
love.graphics.rectangle("line", x, (y-z) - (self.d), self.w, self.d + self.h) love.graphics.rectangle("line", x, (y-z) - (self.d), self.w, self.d + self.h)
end end
utils.graphics.resetColor() utils.graphics.resetColor()
love.graphics.draw(self.texture.top, x, (y-z) - (self.d)) love.graphics.draw(self.texture, x, (y-z) - (self.d))
love.graphics.draw(self.texture.bottom, x, (y-z) - (self.d) + (self.h)) if (self.shadows ~= nil) and (#self.shadowSources > 0) then
if (self.texture.shadows ~= nil) and (#self.shadowSources > 0) then love.graphics.draw(self.shadows, x, (y-z) - (self.d))
love.graphics.draw(self.texture.shadows, x, (y-z) - (self.d))
end end
end end