Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
SpriteBatch.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_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 
39  /**
40  * @ingroup graphics
41  * @brief A sprite batch
42  *
43  * A sprite batch is responsible for minimizing the number of draw calls by
44  * concatenating the different calls for sprites.
45  *
46  * Before using a sprite batch, you have to call `begin()`, then call
47  * `draw()` for each sprite you want to draw, and finally call `end()`.
48  *
49  * ~~~{.cc}
50  * gf::SpriteBatch batch(renderer);
51  *
52  * batch.begin();
53  * batch.draw(sprite1);
54  * batch.draw(sprite2);
55  * batch.draw(sprite3);
56  * batch.end();
57  * ~~~
58  *
59  * @sa gf::Sprite
60  */
62  public:
63  /**
64  * @brief Constructor
65  *
66  * @param target A render target where the sprites will be drawn
67  */
68  SpriteBatch(RenderTarget& target);
69 
70  /**
71  * @brief Begin the batch
72  */
73  void begin();
74 
75  /**
76  * @brief Add a sprite to the batch
77  *
78  * You must call `begin()` before calling this function, and `end()` once
79  * you have finished to draw the sprites.
80  *
81  * @param sprite The sprite to draw
82  * @param states The render states
83  */
84  void draw(Sprite& sprite, const RenderStates& states = RenderStates());
85 
86  /**
87  * @brief End the batch
88  */
89  void end();
90 
91  private:
92  void renderBatch();
93 
94  private:
95  static constexpr std::size_t MaxSpriteCount = 1024;
96  static constexpr std::size_t VerticesPerSprite = 6;
97  static constexpr std::size_t MaxVertexCount = MaxSpriteCount * VerticesPerSprite;
98 
99  RenderTarget& m_target;
100  RenderStates m_currentRenderStates;
101  std::size_t m_count;
102  std::array<Vertex, MaxVertexCount> m_vertices;
103  };
104 
105 #ifndef DOXYGEN_SHOULD_SKIP_THIS
106 }
107 #endif
108 }
109 
110 #endif // GF_SPRITE_BATCH_H
A sprite batch.
Definition: SpriteBatch.h:61
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:81
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 begin()
Begin the batch.
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