Candle v1.0
2D lighting for SFML
candle::LightingArea Class Reference

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.
 

Detailed Description

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.


LightingArea in FOG mode with texture

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:

  1. To change the size of a LightingArea that has already been constructed, without changing the base texture, you have to scale it as you would do with any other sf::Transformable.
  2. If you change the texture of the area, its size might also be modified. So, if you want to change the texture and the size, you must change the texture first and scale it after that.

Member Enumeration Documentation

◆ Mode

Operation modes for a LightingArea.

See also
setMode, getMode
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.

Constructor & Destructor Documentation

◆ LightingArea() [1/2]

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.

Parameters
mode
position
size

◆ LightingArea() [2/2]

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.

Parameters
mode
texture
rect

Member Function Documentation

◆ getLocalBounds()

sf::FloatRect candle::LightingArea::getLocalBounds ( ) const

Get the local bounding rectangle of the area.

The rectangle returned bounds the area before any transformations.

Returns
Local bounding rectangle of the area.

◆ getGlobalBounds()

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.

Returns
Global bounding rectangle of the area.

◆ setAreaColor()

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.

Parameters
color
See also
getAreaColor, setAreaOpacity

◆ getAreaColor()

sf::Color candle::LightingArea::getAreaColor ( ) const

Get color of the fog/light.

Returns
The plain color of the fog/light.
See also
setAreaColor, setAreaOpacity

◆ setAreaOpacity()

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.

Parameters
opacity
See also
getAreaOpacity, setAreaColor

◆ getAreaOpacity()

float candle::LightingArea::getAreaOpacity ( ) const

Get the opacity of the fog/light.

Returns
The opacity of the fog/light.
See also
setAreaOpacity getAreaColor

◆ setAreaTexture()

void candle::LightingArea::setAreaTexture ( const sf::Texture *  texture,
sf::IntRect  rect = sf::IntRect() 
)

Set the texture of the fog/light.

Parameters
texturePointer to the new texture. Pass a null pointer to just unset the texture.
rectOptional rectangle to call setTextureRect. If none is specified, the whole texture is used.
See also
getAreaTexture

◆ getAreaTexture()

const sf::Texture * candle::LightingArea::getAreaTexture ( ) const

Get the texture of the fog/light.

Returns
Pointer to the texture of the fog/light.
See also
getAreaTexture

◆ setTextureRect()

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.

Parameters
rect
See also
getTextureRect

◆ getTextureRect()

sf::IntRect candle::LightingArea::getTextureRect ( ) const

Get the rectangle of the used sub-section of the texture.

Returns
The rectangle of the used sub-section of the texture.
See also
setTextureRect

◆ setMode()

void candle::LightingArea::setMode ( Mode  mode)

Set the lighting mode.

Parameters
mode
See also
LightingArea::Mode, setMode

◆ getMode()

Mode candle::LightingArea::getMode ( ) const

Set the lighting mode.

Returns
The lighting mode.
See also
LightingArea::Mode, setMode

◆ clear()

void candle::LightingArea::clear ( )

Updates and restores the color and the texture.

In FOG mode, it restores the covered areas.

◆ draw()

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.

Parameters
light

◆ display()

void candle::LightingArea::display ( )

Calls display on the sf::RenderTexture.

Updates the changes made since the last call to clear.