Functions to wrap love.graphics calls to preserve settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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