Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
RenderStates.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RENDER_STATES_H
25 #define GF_RENDER_STATES_H
26 
27 #include "Blend.h"
28 #include "Matrix.h"
29 #include "Portability.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  class BareTexture;
37  class Shader;
38 
39  /**
40  * @ingroup graphics
41  * @brief Define the states used for drawing to a RenderTarget
42  *
43  * There are four global states that can be applied to
44  * the drawn objects:
45  *
46  * - the blending mode: how pixels of the object are blended with the background
47  * - the transform matrix: how the object is positioned/rotated/scaled
48  * - the texture: what image is mapped to the object
49  * - the shader: what custom effect is applied to the object
50  *
51  * High-level objects such as sprites or text force some of
52  * these states when they are drawn. For example, a sprite
53  * will set its own texture, so that you don't have to care
54  * about it when drawing the sprite.
55  *
56  * The transform is a special case: sprites, texts and shapes
57  * (and it's a good idea to do it with your own drawable classes
58  * too) combine their transform with the one that is passed in the
59  * gf::RenderStates structure. So that you can use a "global" transform
60  * on top of each object's transform.
61  *
62  * Most objects, especially high-level drawables, can be drawn
63  * directly without defining render states explicitly -- the
64  * default set of states is ok in most cases.
65  *
66  * ~~~{.cc}
67  * window.draw(sprite);
68  * ~~~
69  *
70  * When you're inside the `draw()` function of a drawable
71  * object (inherited from gf::Drawable), you can
72  * either pass the render states unmodified, or change
73  * some of them.
74  *
75  * For example, a transformable object will combine the
76  * current transform with its own transform. A sprite will
77  * set its texture. Etc.
78  *
79  * @sa gf::RenderTarget, gf::Drawable
80  */
82  BlendMode mode = BlendAlpha; ///< The blending mode
83  Matrix3f transform = identity<Matrix3f>(); ///< The transform matrix
84  const BareTexture *texture = nullptr; ///< The texture
85  Shader *shader = nullptr; ///< The shader
86  float lineWidth = 0.0f; ///< The line width
87  };
88 
89  /**
90  * @ingroup graphics
91  * @relates RenderStates
92  * @brief Check render states equality
93  *
94  * Two render states are equals if their blend mode, their transform
95  * matrix, their texture and their shader are the same.
96  */
97  GF_API inline
98  bool operator==(const RenderStates& lhs, const RenderStates& rhs) {
99  return lhs.mode == rhs.mode && lhs.transform == rhs.transform && lhs.texture == rhs.texture && lhs.shader == rhs.shader;
100  }
101 
102 #ifndef DOXYGEN_SHOULD_SKIP_THIS
103 }
104 #endif
105 }
106 
107 #endif // GF_RENDER_STATES_H
bool operator==(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Equality operator between two matrices.
Definition: Matrix.h:353
BlendMode mode
The blending mode.
Definition: RenderStates.h:82
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:81
Matrix< float, 3, 3 > Matrix3f
A float square matrix of size 3.
Definition: Matrix.h:326
An OpenGL vertex and/or fragment shader.
Definition: Shader.h:119
Definition: Action.h:34
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:67
Matrix3f transform
The transform matrix.
Definition: RenderStates.h:83
Blending modes for drawing.
Definition: Blend.h:132
const BareTexture * texture
The texture.
Definition: RenderStates.h:84
float lineWidth
The line width.
Definition: RenderStates.h:86
constexpr bool operator==(const BlendMode &lhs, const BlendMode &rhs)
Equality operator.
Definition: Blend.h:273
#define GF_API
Definition: Portability.h:35
bool operator==(const RenderStates &lhs, const RenderStates &rhs)
Check render states equality.
Definition: RenderStates.h:98
Shader * shader
The shader.
Definition: RenderStates.h:85