Candle v1.0
2D lighting for SFML
|
Object to manage ambient light and fog. More...
#include <LightingArea.hpp>
Inherits sf::Transformable, and sf::Drawable.
Public Types | |
enum | Mode { FOG , AMBIENT } |
Operation modes for a LightingArea. More... | |
Public Member Functions | |
LightingArea (Mode mode, const sf::Vector2f &position, const sf::Vector2f &size) | |
Constructor. More... | |
LightingArea (Mode mode, const sf::Texture *texture, sf::IntRect rect=sf::IntRect()) | |
Constructor. More... | |
sf::FloatRect | getLocalBounds () const |
Get the local bounding rectangle of the area. More... | |
sf::FloatRect | getGlobalBounds () const |
Get the global bounding rectangle of the area. More... | |
void | setAreaColor (sf::Color color) |
Set color of the fog/light. More... | |
sf::Color | getAreaColor () const |
Get color of the fog/light. More... | |
void | setAreaOpacity (float opacity) |
Set the opacity of the fog/light. More... | |
float | getAreaOpacity () const |
Get the opacity of the fog/light. More... | |
void | setAreaTexture (const sf::Texture *texture, sf::IntRect rect=sf::IntRect()) |
Set the texture of the fog/light. More... | |
const sf::Texture * | getAreaTexture () const |
Get the texture of the fog/light. More... | |
void | setTextureRect (const sf::IntRect &rect) |
Set the rectangle of the used sub-section of the texture. More... | |
sf::IntRect | getTextureRect () const |
Get the rectangle of the used sub-section of the texture. More... | |
void | setMode (Mode mode) |
Set the lighting mode. More... | |
Mode | getMode () const |
Set the lighting mode. More... | |
void | clear () |
Updates and restores the color and the texture. More... | |
void | draw (const LightSource &light) |
In FOG mode, makes visible the area illuminated by the light. More... | |
void | display () |
Calls display on the sf::RenderTexture. More... | |
Private Member Functions | |
void | draw (sf::RenderTarget &, sf::RenderStates) const override |
Draw the object to the target. | |
Object to manage ambient light and fog.
A LightingArea is a wrapper class to a sf::RenderTexture that provides the required functions to use it as a mask for the light or a layer of extra lighting. This behaviour is specified through the operation mode of the area.
No LightingArea | FOG mode (Color black, medium opacity) | AMBIENT mode (Color yellow, low opacity) |
It can be used with a plain color or with another sf::Texture as base. If this is the case, such texture must exist and be managed externally during the life of the LightingArea. All changes made to the color, opacity or texture of the area require a call to clear and display to make effect.
As the sf::RenderTexture may be a heavy resource to be creating repeteadly, there are only two moments in which you are able to do so. The first one and most common is upon construction, where you can specify the size of the area, which will also be the size of the sf::RenderTexture. The second one is upon the assignment of a texture (with setAreaTexture), on which the area is created again to match the size of the new texture.
There are two things to note about this:
Operation modes for a LightingArea.
Enumerator | |
---|---|
FOG | In this mode, the area behaves like a mask through which it is only possible to see by drawing light on it. |
AMBIENT | Use the area as an extra layer of light. |
candle::LightingArea::LightingArea | ( | Mode | mode, |
const sf::Vector2f & | position, | ||
const sf::Vector2f & | size | ||
) |
Constructor.
Constructs a LightingArea with plain color and specifies the initial position and the size of the created sf::RenderTexture.
mode | |
position | |
size |
candle::LightingArea::LightingArea | ( | Mode | mode, |
const sf::Texture * | texture, | ||
sf::IntRect | rect = sf::IntRect() |
||
) |
Constructor.
Constructs a LightArea from a texture, in position {0, 0}. As an optional parameter, you can pass the rectangle of the texture that delimits the subsection of the texture to use.
mode | |
texture | |
rect |
sf::FloatRect candle::LightingArea::getLocalBounds | ( | ) | const |
Get the local bounding rectangle of the area.
The rectangle returned bounds the area before any transformations.
sf::FloatRect candle::LightingArea::getGlobalBounds | ( | ) | const |
Get the global bounding rectangle of the area.
The rectangle returned bounds the area with the transformation already applied.
void candle::LightingArea::setAreaColor | ( | sf::Color | color | ) |
Set color of the fog/light.
If the area has no texture, the plain color is used in the next calls to clear. Otherwise, the texture is multiplied by the color. In both cases, the alpha value of the color is preserved.
The default color is sf::Color::White.
color |
sf::Color candle::LightingArea::getAreaColor | ( | ) | const |
Get color of the fog/light.
void candle::LightingArea::setAreaOpacity | ( | float | opacity | ) |
Set the opacity of the fog/light.
The opacity is a value multiplied to the alpha value before any use of the color, to ease the separate manipulation.
opacity |
float candle::LightingArea::getAreaOpacity | ( | ) | const |
Get the opacity of the fog/light.
void candle::LightingArea::setAreaTexture | ( | const sf::Texture * | texture, |
sf::IntRect | rect = sf::IntRect() |
||
) |
Set the texture of the fog/light.
texture | Pointer to the new texture. Pass a null pointer to just unset the texture. |
rect | Optional rectangle to call setTextureRect. If none is specified, the whole texture is used. |
const sf::Texture * candle::LightingArea::getAreaTexture | ( | ) | const |
Get the texture of the fog/light.
void candle::LightingArea::setTextureRect | ( | const sf::IntRect & | rect | ) |
Set the rectangle of the used sub-section of the texture.
Note that this function won't adjust the size of the area to fit the new rectangle.
rect |
sf::IntRect candle::LightingArea::getTextureRect | ( | ) | const |
Get the rectangle of the used sub-section of the texture.
void candle::LightingArea::setMode | ( | Mode | mode | ) |
Mode candle::LightingArea::getMode | ( | ) | const |
void candle::LightingArea::clear | ( | ) |
Updates and restores the color and the texture.
In FOG mode, it restores the covered areas.
void candle::LightingArea::draw | ( | const LightSource & | light | ) |
In FOG mode, makes visible the area illuminated by the light.
In FOG mode with opacity greater than zero, this function. is necessary to keep the lighting coherent. In AMBIENT mode, this function has no effect.
light |
void candle::LightingArea::display | ( | ) |
Calls display on the sf::RenderTexture.
Updates the changes made since the last call to clear.