push and pop for graphics ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

push and pop for graphics ?

Post by Ranguna259 »

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:

Code: Select all

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 :D

Check out my twitter.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: push and pop for graphics ?

Post by bartbes »

love.graphics.reset resets it to the initial values, if that helps.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: push and pop for graphics ?

Post by Ranguna259 »

It's not quite that what I want, that reset everything to the default values what I want is to reset to a previously saved state.
Exemple of reset

Code: Select all

love.graphics.getBackgroundColor() --0,0,0,255
love.graphics.setBackgroundColor(255,255,255,255)
love.graphics.getBackgroundColor() --255,255,255,255
love.graphics.reset()
love.graphics.getBackgroundColor() --0,0,0,255
What I want:

Code: Select all

love.graphics.getBackgroundColor() --0,0,0,255
love.graphics.setBackgroundColor(255,255,255,255)
love.graphics.getBackgroundColor() --255,255,255,255
push()
love.graphics.setBackgroundColor(255,0,0,255)
love.graphics.getBackgroundColor() --255,0,0,255
pop()
love.graphics.getBackgroundColor() --255,255,255,255
love.graphics.reset()
love.graphics.getBackgroundColor() --0,0,0,255
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: push and pop for graphics ?

Post by Ranguna259 »

Ugly way:

Code: Select all

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
Exemple use:

Code: Select all

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 :D

Check out my twitter.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: push and pop for graphics ?

Post by MarekkPie »

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.
womple
Prole
Posts: 1
Joined: Sun Sep 21, 2014 1:49 am

Re: push and pop for graphics ?

Post by womple »

Based on Ranguna259's answer, here's maybe a less ugly way :)

Code: Select all

local stack = {}
function pushGraphics()

	--Store current config
	local config = {}
	
    config.backgroundColor = { love.graphics.getBackgroundColor() }
    config.blendMode = { love.graphics.getBlendMode() }
    config.canvas = { love.graphics.getCanvas() }
    config.color = { love.graphics.getColor() }
    config.colorMask = { love.graphics.getColorMask() }
    config.defaultFilter = { love.graphics.getDefaultFilter() }
    config.font = { love.graphics.getFont() }
    config.lineStyle = { love.graphics.getLineStyle() }
    config.lineWidth = { love.graphics.getLineWidth() }
    config.pointSize = { love.graphics.getPointSize() }
    config.pointStyle = { love.graphics.getPointStyle() }
    config.scissor = { love.graphics.getScissor() }
    config.shader = { love.graphics.getShader() }
		
	--Push onto stack
	table.insert(stack, config)
end
function popGraphics()
   --Pop top of stack
   local config = table.remove(stack)
   
   --Restore configuration
   love.graphics.setBackgroundColor(unpack(config.backgroundColor))
   love.graphics.setBlendMode(unpack(config.blendMode))
   love.graphics.setCanvas(unpack(config.canvas))
   love.graphics.setColor(unpack(config.color))
   love.graphics.setColorMask(unpack(config.colorMask))
   love.graphics.setDefaultFilter(unpack(config.defaultFilter))
   love.graphics.setFont(unpack(config.font))
   love.graphics.setLineStyle(unpack(config.lineStyle))
   love.graphics.setLineWidth(unpack(config.lineWidth))
   love.graphics.setPointSize(unpack(config.pointSize))
   love.graphics.setPointStyle(unpack(config.pointStyle))
   love.graphics.setScissor(unpack(config.scissor))
   love.graphics.setShader(unpack(config.shader))
 
end
Same usage as above (except I've renamed mine to pushGraphics() and popGraphics())
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: push and pop for graphics ?

Post by darkfrei »

Code: Select all

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
Usage:

Code: Select all

local graphicsPush, graphicsPop = graphicsPushPopGenerator()

function love.draw ()
	love.graphics.setColor (1,1,1)
	love.graphics.rectangle ('fill', 50,50,50,50)
	graphicsPush ()
		love.graphics.setColor (0,0,1)
		love.graphics.rectangle ('fill', 150,50,50,50)
	graphicsPop ()
	
	love.graphics.rectangle ('fill', 250,50,50,50)
end
2024-03-12T18_40_50-Untitled.png
2024-03-12T18_40_50-Untitled.png (5.52 KiB) Viewed 3458 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: push and pop for graphics ?

Post by slime »

darkfrei wrote: Tue Mar 12, 2024 5:41 pm

Code: Select all

[...]
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.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: push and pop for graphics ?

Post by darkfrei »

slime wrote: Tue Mar 12, 2024 10:14 pm
darkfrei wrote: Tue Mar 12, 2024 5:41 pm

Code: Select all

[...]
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!
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests