Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
BufferedGeometry.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 #ifndef GF_BUFFERED_GEOMETRY_H
22 #define GF_BUFFERED_GEOMETRY_H
23 
24 #include "Portability.h"
25 #include "Rect.h"
26 #include "Texture.h"
27 #include "Transformable.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  struct RenderStates;
35  class RenderTarget;
36  class VertexBuffer;
37 
38  /**
39  * @ingroup graphics
40  * @brief A drawable for buffers
41  *
42  * A buffered geometry is a lightweight object to draw vertex buffers. It
43  * can handle two vertex buffers, one for the main object and one for the
44  * outline when it exists. The outline is drawn first and the main object
45  * second.
46  *
47  * In addition to the geometry, a buffered geometry can store a texture if
48  * needed because a vertex buffer has no notion of texture.
49  *
50  * Finally, a buffered geometry can remember the local bounds of the
51  * original object, so that it can be used for setting an anchor.
52  *
53  * @sa gf::VertexBuffer
54  */
56  public:
57  /**
58  * @brief Default constructor
59  */
61 
62  /**
63  * @brief Constructor with a buffer
64  *
65  * @param buffer A vertex buffer
66  */
67  BufferedGeometry(const VertexBuffer& buffer);
68 
69  /**
70  * @brief Constructor with a buffer and an outline buffer
71  *
72  * @param buffer A vertex buffer
73  * @param outlineBuffer An outline buffer
74  * @sa setBuffer(), setOutlineBuffer()
75  */
76  BufferedGeometry(const VertexBuffer& buffer, const VertexBuffer& outlineBuffer);
77 
78  /**
79  * @brief Set the vertex buffer
80  *
81  * @param buffer A vertex buffer
82  * @sa setOutlineBuffer()
83  */
84  void setBuffer(const VertexBuffer& buffer) {
85  m_buffer = &buffer;
86  }
87 
88  /**
89  * @brief Set the outline buffer, if any
90  *
91  * @param outlineBuffer An outline buffer
92  * @sa setBuffer()
93  */
94  void setOutlineBuffer(const VertexBuffer& outlineBuffer) {
95  m_outlineBuffer = &outlineBuffer;
96  }
97 
98  /**
99  * @brief Change the source texture of the geometry
100  *
101  * The texture must exist as long as the geometry uses it. Indeed, the
102  * geometry doesn't store its own copy of the texture, but rather keeps
103  * a pointer to the one that you passed to this function.
104  * If the source texture is destroyed and the geometry tries to
105  * use it, the behavior is undefined.
106  *
107  * @param texture The texture
108  *
109  * @sa getTexture()
110  */
111  void setTexture(const BareTexture& texture) {
112  m_texture = &texture;
113  }
114 
115  /**
116  * @brief Get the source texture of the geometry
117  *
118  * The returned reference is const, which means that you can't
119  * modify the texture when you retrieve it with this function.
120  *
121  * @return Reference to the geometry's texture
122  *
123  * @sa setTexture()
124  */
125  const BareTexture& getTexture() const {
126  return *m_texture;
127  }
128 
129  /**
130  * @brief Check if a texture is set
131  *
132  * @returns True if a texture is already set
133  *
134  * @sa setTexture(), getTexture()
135  */
136  bool hasTexture() const {
137  return m_texture != nullptr;
138  }
139 
140  /**
141  * @brief Unset the source texture of the geometry
142  *
143  * After a call to this function, the geometry has no source texture.
144  *
145  * @sa setTexture()
146  */
147  void unsetTexture() {
148  m_texture = nullptr;
149  }
150 
151  /**
152  * @brief Set the local bounds of the geometry
153  *
154  * There is no way to compute the bounds of the object with the vertex
155  * buffer. So you have to set it directly with this function when you
156  * create the geometry.
157  *
158  * @param bounds The local bounds of the object
159  */
160  void setLocalBounds(const RectF& bounds) {
161  m_bounds = bounds;
162  }
163 
164  /**
165  * @brief Get the local bounding rectangle of the entity
166  *
167  * The returned rectangle is in local coordinates, which means
168  * that it ignores the transformations (translation, rotation,
169  * scale, ...) that are applied to the entity.
170  * In other words, this function returns the bounds of the
171  * entity in the entity's coordinate system.
172  *
173  * @return Local bounding rectangle of the entity
174  */
175  RectF getLocalBounds() const {
176  return m_bounds;
177  }
178 
179  /**
180  * @brief Set the anchor origin of the entity
181  *
182  * Compute the origin of the entity based on the local bounds and
183  * the specified anchor. Internally, this function calls
184  * `Transformable::setOrigin()`.
185  *
186  * @param anchor The anchor of the entity
187  * @sa getLocalBounds(), Transformable::setOrigin()
188  */
189  void setAnchor(Anchor anchor);
190 
191  virtual void draw(RenderTarget& target, RenderStates states) override;
192 
193  private:
194  const VertexBuffer *m_buffer;
195  const VertexBuffer *m_outlineBuffer;
196  const BareTexture *m_texture;
197  RectF m_bounds;
198  };
199 
200 
201 #ifndef DOXYGEN_SHOULD_SKIP_THIS
202 }
203 #endif
204 }
205 
206 #endif // GF_BUFFERED_GEOMETRY_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
BufferedGeometry(const VertexBuffer &buffer, const VertexBuffer &outlineBuffer)
Constructor with a buffer and an outline buffer.
BufferedGeometry()
Default constructor.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setLocalBounds(const RectF &bounds)
Set the local bounds of the geometry.
Definition: BufferedGeometry.h:160
Data in the graphics memory.
Definition: VertexBuffer.h:70
void setTexture(const BareTexture &texture)
Change the source texture of the geometry.
Definition: BufferedGeometry.h:111
bool hasTexture() const
Check if a texture is set.
Definition: BufferedGeometry.h:136
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
The namespace for gf classes.
Definition: Action.h:34
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: BufferedGeometry.h:175
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:67
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
BufferedGeometry(const VertexBuffer &buffer)
Constructor with a buffer.
A drawable for buffers.
Definition: BufferedGeometry.h:55
void setOutlineBuffer(const VertexBuffer &outlineBuffer)
Set the outline buffer, if any.
Definition: BufferedGeometry.h:94
const BareTexture & getTexture() const
Get the source texture of the geometry.
Definition: BufferedGeometry.h:125
Anchor
An anchor of a box.
Definition: Anchor.h:41
#define GF_API
Definition: Portability.h:35
void setBuffer(const VertexBuffer &buffer)
Set the vertex buffer.
Definition: BufferedGeometry.h:84
void unsetTexture()
Unset the source texture of the geometry.
Definition: BufferedGeometry.h:147