Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Last active August 29, 2015 14:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SpotlightKid/5685c2ec3ba43e673527 to your computer and use it in GitHub Desktop.
Functions to wrap love.graphics calls to preserve settings
--- Functions to wrap love.graphics calls to preserve settings like color,
-- line style, filters etc.
-- getLineJoin() throws an error if no line join mode is set in love 0.9.1
if not pcall(love.graphics.getLineJoin) then
love.graphics.setLineJoin('miter')
end
-- these are for love 0.9.1
local ALL_SETTINGS = {
'BackgroundColor',
'BlendMode',
'Canvas',
'Color',
'ColorMask',
'DefaultFilter',
'Font',
'LineJoin',
'LineStyle',
'LineWidth',
'PointSize',
'PointStyle',
'Scissor',
'Shader'
}
local function getGraphicsState(settings)
settings = settings or ALL_SETTINGS
local state = {}
for i = 1, #settings do
local name = settings[i]
local f = love.graphics['get' .. name]
if f then state[name] = {f()} end
end
return state
end
local function setGraphicsState(state)
for name, val in pairs(state) do
local f = love.graphics['set' .. name]
if f then f(unpack(val)) end
end
end
local function withGraphicsState(f, state, settings)
local oldstate = getGraphicsState(settings)
if state then setGraphicsState(state) end
local status, result = pcall(f)
setGraphicsState(oldstate)
return status, result
end
return {
getGraphicsState = getGraphicsState,
setGraphicsState = setGraphicsState,
withGraphicsState = withGraphicsState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment