Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
SpriteBatch.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_SPRITE_BATCH_H
22 #define GF_SPRITE_BATCH_H
23 
24 #include <cstddef>
25 #include <array>
26 
27 #include "Portability.h"
28 #include "RenderStates.h"
29 #include "Vertex.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  class RenderTarget;
37  class Sprite;
38  class Texture;
39 
40  /**
41  * @ingroup graphics
42  * @brief A sprite batch
43  *
44  * A sprite batch is responsible for minimizing the number of draw calls by
45  * concatenating the different calls for sprites.
46  *
47  * Before using a sprite batch, you have to call `begin()`, then call
48  * `draw()` for each sprite you want to draw, and finally call `end()`.
49  *
50  * ~~~{.cc}
51  * gf::SpriteBatch batch(renderer);
52  *
53  * batch.begin();
54  * batch.draw(sprite1);
55  * batch.draw(sprite2);
56  * batch.draw(sprite3);
57  * batch.end();
58  * ~~~
59  *
60  * @sa gf::Sprite
61  */
63  public:
64  /**
65  * @brief Constructor
66  *
67  * @param target A render target where the sprites will be drawn
68  */
69  SpriteBatch(RenderTarget& target);
70 
71  /**
72  * @brief Begin the batch
73  */
74  void begin();
75 
76  /**
77  * @brief Add a sprite to the batch
78  *
79  * You must call `begin()` before calling this function, and `end()` once
80  * you have finished to draw the sprites.
81  *
82  * @param sprite The sprite to draw
83  * @param states The render states
84  */
85  void draw(Sprite& sprite, const RenderStates& states = RenderStates());
86 
87  /**
88  * @brief Add a raw texture to the batch
89  *
90  * You must call `begin()` before calling this function, and `end()` once
91  * you have finished to draw the sprites.
92  *
93  * @param texture The texture to draw
94  * @param position The position of the texture
95  * @param states The render states
96  */
97  void draw(const Texture& texture, Vector2f position, const RenderStates& states = RenderStates());
98 
99  /**
100  * @brief Add a portion of a raw texture to the batch
101  *
102  * You must call `begin()` before calling this function, and `end()` once
103  * you have finished to draw the sprites.
104  *
105  * @param texture The texture to draw
106  * @param textureRect The sub-rectangle of the texture to draw
107  * @param position The position of the texture
108  * @param states The render states
109  */
110  void draw(const Texture& texture, const RectF& textureRect, Vector2f position, const RenderStates& states = RenderStates());
111 
112  /**
113  * @brief End the batch
114  */
115  void end();
116 
117  private:
118  void renderBatch();
119 
120  private:
121  static constexpr std::size_t MaxSpriteCount = 1024;
122  static constexpr std::size_t VerticesPerSprite = 6;
123  static constexpr std::size_t MaxVertexCount = MaxSpriteCount * VerticesPerSprite;
124 
125  RenderTarget& m_target;
126  RenderStates m_currentRenderStates;
127  std::size_t m_count;
128  std::array<Vertex, MaxVertexCount> m_vertices;
129  };
130 
131 #ifndef DOXYGEN_SHOULD_SKIP_THIS
132 }
133 #endif
134 }
135 
136 #endif // GF_SPRITE_BATCH_H
A sprite batch.
Definition: SpriteBatch.h:62
SpriteBatch(RenderTarget &target)
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 end()
End the batch.
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
A drawable representation of a texture, with its own transformations, color, etc. ...
Definition: Sprite.h:87
void draw(const Texture &texture, Vector2f position, const RenderStates &states=RenderStates())
Add a raw texture to the batch.
void begin()
Begin the batch.
A texture for colored images.
Definition: Texture.h:339
void draw(const Texture &texture, const RectF &textureRect, Vector2f position, const RenderStates &states=RenderStates())
Add a portion of a raw texture to the batch.
The namespace for gf classes.
Definition: Action.h:34
void draw(Sprite &sprite, const RenderStates &states=RenderStates())
Add a sprite to the batch.
#define GF_API
Definition: Portability.h:35