Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
BufferedGeometry.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 #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  * If the geometry has no source texture, a `nullptr` pointer is returned.
119  * The returned pointer is const, which means that you can't
120  * modify the texture when you retrieve it with this function.
121  *
122  * @return Pointer to the geometry's texture
123  *
124  * @sa setTexture()
125  */
126  const BareTexture *getTexture() const {
127  return m_texture;
128  }
129 
130  /**
131  * @brief Unset the source texture of the geometry
132  *
133  * After a call to this function, the geometry has no source texture.
134  *
135  * @sa setTexture()
136  */
137  void unsetTexture() {
138  m_texture = nullptr;
139  }
140 
141  /**
142  * @brief Set the local bounds of the geometry
143  *
144  * There is no way to compute the bounds of the object with the vertex
145  * buffer. So you have to set it directly with this function when you
146  * create the geometry.
147  *
148  * @param bounds The local bounds of the object
149  */
150  void setLocalBounds(const RectF& bounds) {
151  m_bounds = bounds;
152  }
153 
154  /**
155  * @brief Get the local bounding rectangle of the entity
156  *
157  * The returned rectangle is in local coordinates, which means
158  * that it ignores the transformations (translation, rotation,
159  * scale, ...) that are applied to the entity.
160  * In other words, this function returns the bounds of the
161  * entity in the entity's coordinate system.
162  *
163  * @return Local bounding rectangle of the entity
164  */
165  RectF getLocalBounds() const {
166  return m_bounds;
167  }
168 
169  /**
170  * @brief Set the anchor origin of the entity
171  *
172  * Compute the origin of the entity based on the local bounds and
173  * the specified anchor. Internally, this function calls
174  * `Transformable::setOrigin()`.
175  *
176  * @param anchor The anchor of the entity
177  * @sa getLocalBounds(), Transformable::setOrigin()
178  */
179  void setAnchor(Anchor anchor);
180 
181  virtual void draw(RenderTarget& target, RenderStates states) override;
182 
183  private:
184  const VertexBuffer *m_buffer;
185  const VertexBuffer *m_outlineBuffer;
186  const BareTexture *m_texture;
187  RectF m_bounds;
188  };
189 
190 
191 #ifndef DOXYGEN_SHOULD_SKIP_THIS
192 }
193 #endif
194 }
195 
196 #endif // GF_BUFFERED_GEOMETRY_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:113
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:65
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:150
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
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: BufferedGeometry.h:165
const BareTexture * getTexture() const
Get the source texture of the geometry.
Definition: BufferedGeometry.h:126
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
Definition: Action.h:34
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
Anchor
The origin anchor of the transformable object.
Definition: Transformable.h:45
#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:137