Is there a function to save all graphic data such as the background color, the blend mode, the current canvas, the color, the color mask etc.. and then reset it ?
Something like love.graphics.push() and love.graphics.pop() but instead of saving the rotation, scale, shear and translate it'd save these:
BackgroundColor
BlendMode
Canvas
Color
ColorMask
DefaultFilter
Font
LineJoin
LineStyle
LineWidth
PointSize
PointStyle
RenderTarget
Scissor
Shader
Stencil
I want this because I want to make a safe environment for graphics, basically I'd save all graphics options ( push() ) do a bunch of stuff after and then reset all options ( pop() ).
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping
local function get(...)
arg={...}
storer[#storer+1]={}
local c=1
for i,v in ipairs(arg) do
storer[#storer][c] = v or 'nil'
c=c+1
end
end
local function set(i)
local returner = {}
local c=1
if #storer[1] > 0 then
for i,v in ipairs(storer[i]) do
returner[c]=v
c=c+1
end
else
returner[1] = nil
end
return returner[1],returner[2],returner[3],returner[4]
end
local storer ={}
function push()
get(love.graphics.getBackgroundColor())
get(love.graphics.getBlendMode())
get(love.graphics.getCanvas())
get(love.graphics.getColor())
get(love.graphics.getColorMask())
get(love.graphics.getDefaultFilter())
get(love.graphics.getFont())
get(love.graphics.getLineStyle())
get(love.graphics.getLineWidth())
get(love.graphics.getPointSize())
get(love.graphics.getPointStyle())
get(love.graphics.getScissor())
get(love.graphics.getShader())
end
function pop()
local c=1
love.graphics.setBackgroundColor(set(c))
c=c+1
love.graphics.setBlendMode(set(c))
c=c+1
love.graphics.setCanvas(set(c))
c=c+1
love.graphics.setColor(set(c))
c=c+1
love.graphics.setColorMask(set(c))
c=c+1
love.graphics.setDefaultFilter(set(c))
c=c+1
love.graphics.setFont(set(c))
c=c+1
love.graphics.setLineStyle(set(c))
c=c+1
love.graphics.setLineWidth(set(c))
c=c+1
love.graphics.setPointSize(set(c))
c=c+1
love.graphics.setPointStyle(set(c))
c=c+1
love.graphics.setScissor(set(c))
c=c+1
love.graphics.setShader(set(c))
c=c+1
end
function love.draw()
love.graphics.setBackgroundColor(255,255,255,255)
--BG is white
push()
love.graphics.reset()
--BG is black
pop()
--BG is white
end
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping
There isn't, I'm afraid. And after messing around with the HTML5 Canvas API (which has context.save() and context.restore()), it's a really nice feature that would be great to have.
local function graphicsPushPopGenerator()
local states = {}
local graphicsPush = function()
local currentState = {
color = {love.graphics.getColor()}, -- must be table
lineWidth = love.graphics.getLineWidth(),
pointSize = love.graphics.getPointSize()
}
table.insert(states, currentState)
end
local graphicsPop = function()
if #states > 0 then
local currentState = table.remove(states)
love.graphics.setColor(currentState.color)
love.graphics.setLineWidth(currentState.lineWidth)
love.graphics.setPointSize(currentState.pointSize)
else
error("Cannot pop initial graphics state")
end
end
return graphicsPush, graphicsPop
end
You've bumped a 10 year old thread. love.graphics.push("all") was added to love 9 years ago and does what the OP asks, you should use it instead of that code.
You've bumped a 10 year old thread. love.graphics.push("all") was added to love 9 years ago and does what the OP asks, you should use it instead of that code.
Almost 10 years. But we read the wiki graphics.pop yesterday and there was a problem: no information about the color stacking, but now I understand why.
Thanks for update 9 year ago!