Storing and returning a love.graphics state

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
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Storing and returning a love.graphics state

Post by TechnoCat »

Is there something similar to love.graphics.push/pop to cover this?

Code: Select all

  local blendMode = love.graphics.getBlendMode()
  local r,g,b,a = love.graphics.getColor()
  local colorMode = love.graphics.getColorMode()
  local font = love.graphics.getFont()
  local lineStyle = love.graphics.getLineStyle()
  local lineWidth = love.graphics.getLineWidth()
  local lineStipple = love.graphics.getLineStipple()
  --DO STUFF
  love.graphics.setBlendMode(blendMode)
  love.graphics.setColor(r,g,b,a)
  love.graphics.setColorMode(colorMode)
  love.graphics.setFont(font)
  love.graphics.setLine(lineWidth, lineStyle)
  love.graphics.setLineStipple(lineStipple)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Storing and returning a love.graphics state

Post by vrld »

Not natively, but it can be added lua-side:

Code: Select all

local stack = {}
function love.graphics.pushState()
    -- get state
    local state = {}
    state.blendMode   = love.graphics.getBlendMode()
    state.color       = {love.graphics.getColor()}
    state.colorMode   = love.graphics.getColorMode()
    state.font        = love.graphics.getFont() or 12
    state.lineStyle   = love.graphics.getLineStyle()
    state.lineWidth   = love.graphics.getLineWidth()
    state.lineStipple = love.graphics.getLineStipple() and {love.graphics.getLineStipple()} or {1,255}

    -- push state
    stack[#stack+1] = state
end

function love.graphics.popState()
    -- restore state
    local state = stack[#stack]
    love.graphics.setBlendMode( state.blendMode )
    love.graphics.setColor( unpack(state.color) )
    love.graphics.setColorMode( state.colorMode )
    love.graphics.setFont( state.font )
    love.graphics.setLine( state.lineWidth, state.lineStyle )
    love.graphics.setLineStipple( unpack(state.lineStipple) )

    -- pop state
    stack[#stack] = nil
end
Put that either on top of main.lua or (better) into a separate file which you then require in main.lua

Edit: Account for possible nil-ness of love.graphics.getFont() and love.graphics.getLineStipple(). See TechnoCat's post
Last edited by vrld on Tue Nov 09, 2010 2:41 pm, edited 1 time in total.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Storing and returning a love.graphics state

Post by zac352 »

I should write a library just for this.
I should also finish that library I made for sharing _G throughout threads. :P
Hello, I am not dead.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Storing and returning a love.graphics state

Post by TechnoCat »

Anyone curious about doing this.
adding this to love.load before pushing and popping the state

Code: Select all

if love.graphics.getFont()==nil then
  love.graphics.setFont(love.graphics.newFont(12))
end
if love.graphics.getLineStipple()==nil then
  love.graphics.setLineStipple(1111111111111111, 1)
end
is a good idea.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Storing and returning a love.graphics state

Post by vrld »

I wasn't aware of that love.graphics.getFont() and love.graphics.getLineStipple() could be nil (possible bug there?).
The pushState function now accounts for that by setting default values. This will be done only once, so don''t worry this will take up too much runtime.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Storing and returning a love.graphics state

Post by TechnoCat »

Code: Select all

love.graphics.getLineStipple() and {love.graphics.getLineStipple()} or {1,255}
This doesn't work.

I wrote this unsatisfying code:

Code: Select all

local lineStipple = love.graphics.getLineStipple()
love.graphics.setLineStipple(lineStipple or unpack({1,255}))
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Storing and returning a love.graphics state

Post by vrld »

TechnoCat wrote:This doesn't work.
Strange, it worked for me with both 0.6.2 and 0.7. What version are you using?
TechnoCat wrote:

Code: Select all

local lineStipple = love.graphics.getLineStipple()
love.graphics.setLineStipple(lineStipple or unpack({1,255}))
This does only set the pattern, but not the repeat parameter ;)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Storing and returning a love.graphics state

Post by TechnoCat »

vrld wrote:Strange, it worked for me with both 0.6.2 and 0.7. What version are you using?
0.7.0 Ubuntu from bartbe's unstable ppa.

EDIT: Scissor also has default as nil.
EDIT2: that line stipple code is wrong too.
Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests