Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | Public Attributes | Related Functions | List of all members
gf::BlendMode Struct Reference

Blending modes for drawing. More...

#include <gf/Blend.h>

Public Member Functions

constexpr BlendMode ()
 Default constructor. More...
 
constexpr BlendMode (BlendFactor sourceFactor, BlendFactor destinationFactor, BlendEquation equation=BlendEquation::Add)
 Construct the blend mode given the factors and equation. More...
 
constexpr BlendMode (BlendFactor colorSourceFactor, BlendFactor colorDestinationFactor, BlendEquation colorBlendEquation, BlendFactor alphaSourceFactor, BlendFactor alphaDestinationFactor, BlendEquation alphaBlendEquation)
 Construct the blend mode given the factors and equation. More...
 

Public Attributes

BlendFactor colorSrcFactor
 Source blending factor for the color channels. More...
 
BlendFactor colorDstFactor
 Destination blending factor for the color channels. More...
 
BlendEquation colorEquation
 Blending equation for the color channels. More...
 
BlendFactor alphaSrcFactor
 Source blending factor for the alpha channel. More...
 
BlendFactor alphaDstFactor
 Destination blending factor for the alpha channel. More...
 
BlendEquation alphaEquation
 Blending equation for the alpha channel. More...
 

Related Functions

(Note that these are not member functions.)

constexpr bool operator== (const BlendMode &lhs, const BlendMode &rhs)
 Equality operator. More...
 

Detailed Description

Blending modes for drawing.

gf::BlendMode is a class that represents a blend mode. A blend mode determines how the colors of an object you draw are mixed with the colors that are already in the buffer.

The class is composed of 6 components, each of which has its own public member variable:

The source factor specifies how the pixel you are drawing contributes to the final color. The destination factor specifies how the pixel already drawn in the buffer contributes to the final color.

The color channels RGB (red, green, blue; simply referred to as color) and A (alpha; the transparency) can be treated separately. This separation can be useful for specific blend modes, but most often you won't need it and will simply treat the color as a single unit.

The blend factors and equations correspond to their OpenGL equivalents. In general, the color of the resulting pixel is calculated according to the following formula (src is the color of the source pixel, dst the color of the destination pixel, the other variables correspond to the public members, with the equations being + or - operators):

dst.rgb = colorSrcFactor * src.rgb (colorEquation) colorDstFactor * dst.rgb
dst.a = alphaSrcFactor * src.a (alphaEquation) alphaDstFactor * dst.a

All factors and colors are represented as floating point numbers between 0 and 1. Where necessary, the result is clamped to fit in that range.

The most common blending modes are defined as constants in the gf::PredefinedBlendMonde namespace:

gf::BlendMode alphaBlending = gf::BlendAlpha;
gf::BlendMode additiveBlending = gf::BlendAdd;
gf::BlendMode multiplicativeBlending = gf::BlendMultiply;

A blend mode can be specified every time you draw a gf::Drawable object to a gf::RenderWindow. It is part of the gf::RenderStates compound that is passed to the member function gf::RenderTarget::draw().

See also
gf::BlendFactor, gf::BlendEquation, gf::RenderStates, gf::RenderWindow

Constructor & Destructor Documentation

constexpr gf::BlendMode::BlendMode ( )
inline

Default constructor.

Constructs a blending mode that does alpha blending.

constexpr gf::BlendMode::BlendMode ( BlendFactor  sourceFactor,
BlendFactor  destinationFactor,
BlendEquation  equation = BlendEquation::Add 
)
inline

Construct the blend mode given the factors and equation.

This constructor uses the same factors and equation for both color and alpha components. It also defaults to the Add equation.

Parameters
sourceFactorSpecifies how to compute the source factor for the color and alpha channels.
destinationFactorSpecifies how to compute the destination factor for the color and alpha channels.
equationSpecifies how to combine the source and destination colors and alpha.
constexpr gf::BlendMode::BlendMode ( BlendFactor  colorSourceFactor,
BlendFactor  colorDestinationFactor,
BlendEquation  colorBlendEquation,
BlendFactor  alphaSourceFactor,
BlendFactor  alphaDestinationFactor,
BlendEquation  alphaBlendEquation 
)
inline

Construct the blend mode given the factors and equation.

Parameters
colorSourceFactorSpecifies how to compute the source function for the color channels.
colorDestinationFactorSpecifies how to compute the destination factor for the color channels.
colorBlendEquationSpecifies how to combine the source and destination colors.
alphaSourceFactorSpecifies how to compute the source factor.
alphaDestinationFactorSpecifies how to compute the destination factor.
alphaBlendEquationSpecifies how to combine the source and destination alphas.

Friends And Related Function Documentation

constexpr bool operator== ( const BlendMode lhs,
const BlendMode rhs 
)
related

Equality operator.

Parameters
lhsFirst blend mode
rhsSecond blend mode
Returns
True if the blend modes are the same

Member Data Documentation

BlendFactor gf::BlendMode::alphaDstFactor

Destination blending factor for the alpha channel.

BlendEquation gf::BlendMode::alphaEquation

Blending equation for the alpha channel.

BlendFactor gf::BlendMode::alphaSrcFactor

Source blending factor for the alpha channel.

BlendFactor gf::BlendMode::colorDstFactor

Destination blending factor for the color channels.

BlendEquation gf::BlendMode::colorEquation

Blending equation for the color channels.

BlendFactor gf::BlendMode::colorSrcFactor

Source blending factor for the color channels.