1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #pragma once |
22 | |
23 | // C |
24 | #include <cstring> |
25 | |
26 | // C++ |
27 | #include <unordered_map> |
28 | |
29 | // LOVE |
30 | #include "common/math.h" |
31 | #include "common/Matrix.h" |
32 | #include "common/Color.h" |
33 | #include "Drawable.h" |
34 | #include "Mesh.h" |
35 | #include "vertex.h" |
36 | |
37 | namespace love |
38 | { |
39 | namespace graphics |
40 | { |
41 | |
42 | // Forward declarations. |
43 | class Graphics; |
44 | class Texture; |
45 | class Quad; |
46 | class Buffer; |
47 | |
48 | class SpriteBatch : public Drawable |
49 | { |
50 | public: |
51 | |
52 | static love::Type type; |
53 | |
54 | SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usage usage); |
55 | virtual ~SpriteBatch(); |
56 | |
57 | int add(const Matrix4 &m, int index = -1); |
58 | int add(Quad *quad, const Matrix4 &m, int index = -1); |
59 | int addLayer(int layer, const Matrix4 &m, int index = -1); |
60 | int addLayer(int layer, Quad *quad, const Matrix4 &m, int index = -1); |
61 | |
62 | void clear(); |
63 | |
64 | void flush(); |
65 | |
66 | void setTexture(Texture *newtexture); |
67 | Texture *getTexture() const; |
68 | |
69 | /** |
70 | * Set the current color for this SpriteBatch. The sprites added |
71 | * after this call will use this color. Note that global color |
72 | * will not longer apply to the SpriteBatch if this is used. |
73 | * |
74 | * @param color The color to use for the following sprites. |
75 | */ |
76 | void setColor(const Colorf &color); |
77 | |
78 | /** |
79 | * Disable per-sprite colors for this SpriteBatch. The next call to |
80 | * draw will use the global color for all sprites. |
81 | */ |
82 | void setColor(); |
83 | |
84 | /** |
85 | * Get the current color for this SpriteBatch. |
86 | **/ |
87 | Colorf getColor(bool &active) const; |
88 | |
89 | /** |
90 | * Get the number of sprites currently in this SpriteBatch. |
91 | **/ |
92 | int getCount() const; |
93 | |
94 | /** |
95 | * Get the total number of sprites this SpriteBatch can currently hold. |
96 | **/ |
97 | int getBufferSize() const; |
98 | |
99 | /** |
100 | * Attaches a specific vertex attribute from a Mesh to this SpriteBatch. |
101 | * The vertex attribute will be used when drawing the SpriteBatch. |
102 | **/ |
103 | void attachAttribute(const std::string &name, Mesh *mesh); |
104 | |
105 | void setDrawRange(int start, int count); |
106 | void setDrawRange(); |
107 | bool getDrawRange(int &start, int &count) const; |
108 | |
109 | // Implements Drawable. |
110 | void draw(Graphics *gfx, const Matrix4 &m) override; |
111 | |
112 | private: |
113 | |
114 | struct AttachedAttribute |
115 | { |
116 | StrongRef<Mesh> mesh; |
117 | int index; |
118 | }; |
119 | |
120 | /** |
121 | * Sets the total number of sprites this SpriteBatch can hold. |
122 | * Leaves existing sprite data intact when possible. |
123 | **/ |
124 | void setBufferSize(int newsize); |
125 | |
126 | StrongRef<Texture> texture; |
127 | |
128 | // Max number of sprites in the batch. |
129 | int size; |
130 | |
131 | // The next free element. |
132 | int next; |
133 | |
134 | // Current color. This color, if present, will be applied to the next |
135 | // added sprite. |
136 | Color32 color; |
137 | bool color_active; |
138 | |
139 | vertex::CommonFormat vertex_format; |
140 | size_t vertex_stride; |
141 | |
142 | love::graphics::Buffer *array_buf; |
143 | |
144 | std::unordered_map<std::string, AttachedAttribute> attached_attributes; |
145 | |
146 | int range_start; |
147 | int range_count; |
148 | |
149 | }; // SpriteBatch |
150 | |
151 | } // graphics |
152 | } // love |
153 | |