I needed this feature to port TacoShell [1] to LÖVE. You can take a look at it in the demo, if you want. It uses
my own class system and a couple helper utilities, but nothing really esoteric.
Here follows a poorly-summarized description.
(So far I've only done the demo and some prototyping, so I
haven't documented much.) Some of this may be overkill for your needs, but is probably worth mentioning
to describe the code.
In the Scripts folder, these are the relevant files:
- Utility/LoveClasses.lua
This just extends the LÖVE functions that create objects, marking the new objects so I can later ask what type they are.
- Class/Game/Graphics/GraphicBase.lua
Where all the magic is, via the
WithProperties() method.
Thus far, these important methods are available:
SetColorPolicy(),
SetColorKeep(),
SetBlendModePolicy(),
SetBlendModeKeep(),
SetColorModePolicy(),
SetColorModeKeep().
By default, all the "keep" features are false. You just pass
true or
false to these. If
true, you want your change
(
setColor(),
setBlendMode(),
setColorMode()) to stick around; otherwise it gets undone after drawing.
You can pass a properties table (see the draw call below) through
WithProperties(), and it will look for the following
fields:
color,
blend_mode,
color_mode. If present,
color can be either a LÖVE color or a string that
is the name of a registered color (
"default",
"current", and several others, including your own custom ones; see
Graphics.lua);
blend_mode and
color_mode can be the appropriate LÖVE constants.
If one of these keys is NOT present, the relevant policy is used to get the current value. The default policies are
"default" for color, and
"normal" for both blend and color mode.
The color policies are:
-
"default": Uses the registered color,
"default"
-
"current": Uses the color last set by
setColor()
The blend mode policies are:
-
"current": Uses the mode last set by
setBlendMode()
-
"normal": Uses normal blending mode
-
"additive": Uses additive blending mode
The color mode policies are:
-
"current": Uses the mode last set by
setColorMode()
-
"normal": Uses normal color mode
-
"modulate": Uses modulate color mode
- Class/Game/Graphics/GraphicElement.lua
Wrapper for either an Image or Animation (passed via method
SetObject() or the constructor); derives from
GraphicBase. You then call this wrapper to draw it, passing the draw region and an optional properties table,
which has the colors and modes and such. I've found it more flexible to keep the properties table separate
from the wrapper, but YMMV.
Initialization:
Code: Select all
local image = love.graphics.newImage("OttersAndPonies.png")
background = class.New("GraphicElement", image, true) -- Last parameter corrects for centering
background_props = { color = "blue", blend_mode = "modulate" }
In
draw():
Code: Select all
background(0, 0, love.graphics.getWidth(), love.graphics.getHeight(), background_props)
- Class/Game/Graphics/Font.lua
Nothing in particular, just another example like GraphicElement.
- Subsystems/Graphics.lua
Color registration, and some primitive-drawing operations built on top of this feature. Note that in this case
the base object is out of reach, so you can't switch its policies or keep flags; it's only used to respond to the
properties table argument.
[1] - A
slightly updated demo can be found
here. The updates include the rectangle primitive functions
using this feature in
Graphics.lua.
*Massive post saved for later documentation*