Gamedev Framework (gf)  0.3.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 #include "Transform.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  class BareTexture;
38  class Shader;
39 
40  /**
41  * @ingroup graphics
42  * @brief Define the states used for drawing to a RenderTarget
43  *
44  * There are four global states that can be applied to
45  * the drawn objects:
46  *
47  * - the blending mode: how pixels of the object are blended with the background
48  * - the transform matrix: how the object is positioned/rotated/scaled
49  * - the texture: what image is mapped to the object
50  * - the shader: what custom effect is applied to the object
51  *
52  * High-level objects such as sprites or text force some of
53  * these states when they are drawn. For example, a sprite
54  * will set its own texture, so that you don't have to care
55  * about it when drawing the sprite.
56  *
57  * The transform is a special case: sprites, texts and shapes
58  * (and it's a good idea to do it with your own drawable classes
59  * too) combine their transform with the one that is passed in the
60  * gf::RenderStates structure. So that you can use a "global" transform
61  * on top of each object's transform.
62  *
63  * Most objects, especially high-level drawables, can be drawn
64  * directly without defining render states explicitly -- the
65  * default set of states is ok in most cases.
66  *
67  * ~~~{.cc}
68  * window.draw(sprite);
69  * ~~~
70  *
71  * When you're inside the `draw()` function of a drawable
72  * object (inherited from gf::Drawable), you can
73  * either pass the render states unmodified, or change
74  * some of them.
75  *
76  * For example, a transformable object will combine the
77  * current transform with its own transform. A sprite will
78  * set its texture. Etc.
79  *
80  * @sa gf::RenderTarget, gf::Drawable
81  */
83  BlendMode mode = BlendAlpha; ///< The blending mode
84  Matrix3f transform = identityTransform(); ///< The transform matrix
85  const BareTexture *texture = nullptr; ///< The texture
86  Shader *shader = nullptr; ///< The shader
87  float lineWidth = 0.0f; ///< The line width
88  };
89 
90  /**
91  * @ingroup graphics
92  * @relates RenderStates
93  * @brief Check render states equality
94  *
95  * Two render states are equals if their blend mode, their transform
96  * matrix, their texture and their shader are the same.
97  */
98  inline
99  bool operator==(const RenderStates& lhs, const RenderStates& rhs) {
100  return lhs.mode == rhs.mode && lhs.transform == rhs.transform && lhs.texture == rhs.texture && lhs.shader == rhs.shader;
101  }
102 
103 #ifndef DOXYGEN_SHOULD_SKIP_THIS
104 }
105 #endif
106 }
107 
108 #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:83
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
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:84
Blending modes for drawing.
Definition: Blend.h:132
const BareTexture * texture
The texture.
Definition: RenderStates.h:85
float lineWidth
The line width.
Definition: RenderStates.h:87
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:99
Shader * shader
The shader.
Definition: RenderStates.h:86