SpriteBatch.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * Copyright (c) 2006-2017 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. #include "common/config.h"
  21. #include "SpriteBatch.h"
  22. // LOVE
  23. #include "Texture.h"
  24. #include "Quad.h"
  25. #include "Graphics.h"
  26. #include "Buffer.h"
  27. // C++
  28. #include <algorithm>
  29. // C
  30. #include <stddef.h>
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. love::Type SpriteBatch::type("SpriteBatch", &Drawable::type);
  36. SpriteBatch::SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usage usage)
  37. : texture(texture)
  38. , size(size)
  39. , next(0)
  40. , color(255, 255, 255, 255)
  41. , color_active(false)
  42. , array_buf(nullptr)
  43. , quad_indices(gfx, size)
  44. , range_start(-1)
  45. , range_count(-1)
  46. {
  47. if (size <= 0)
  48. throw love::Exception("Invalid SpriteBatch size.");
  49. if (texture == nullptr)
  50. throw love::Exception("A texture must be used when creating a SpriteBatch.");
  51. if (texture->getTextureType() == TEXTURE_2D_ARRAY)
  52. vertex_format = vertex::CommonFormat::XYf_STPf_RGBAub;
  53. else
  54. vertex_format = vertex::CommonFormat::XYf_STf_RGBAub;
  55. format_stride = vertex::getFormatStride(vertex_format);
  56. size_t vertex_size = format_stride * 4 * size;
  57. array_buf = gfx->newBuffer(vertex_size, nullptr, BUFFER_VERTEX, usage, Buffer::MAP_EXPLICIT_RANGE_MODIFY);
  58. }
  59. SpriteBatch::~SpriteBatch()
  60. {
  61. delete array_buf;
  62. }
  63. int SpriteBatch::add(const Matrix4 &m, int index /*= -1*/)
  64. {
  65. return add(texture->getQuad(), m, index);
  66. }
  67. int SpriteBatch::add(Quad *quad, const Matrix4 &m, int index /*= -1*/)
  68. {
  69. using namespace vertex;
  70. if (vertex_format == CommonFormat::XYf_STPf_RGBAub)
  71. return addLayer(quad->getLayer(), quad, m, index);
  72. if (index < -1 || index >= size)
  73. throw love::Exception("Invalid sprite index: %d", index + 1);
  74. if (index == -1 && next >= size)
  75. setBufferSize(size * 2);
  76. const XYf_STf *quadverts = quad->getVertices();
  77. // Always keep the VBO mapped when adding data (it'll be unmapped on draw.)
  78. size_t offset = (index == -1 ? next : index) * format_stride * 4;
  79. auto verts = (XYf_STf_RGBAub *) ((uint8 *) array_buf->map() + offset);
  80. m.transform(verts, quadverts, 4);
  81. for (int i = 0; i < 4; i++)
  82. {
  83. verts[i].s = quadverts[i].s;
  84. verts[i].t = quadverts[i].t;
  85. verts[i].color = color;
  86. }
  87. array_buf->setMappedRangeModified(offset, format_stride * 4);
  88. // Increment counter.
  89. if (index == -1)
  90. return next++;
  91. return index;
  92. }
  93. int SpriteBatch::addLayer(int layer, const Matrix4 &m, int index)
  94. {
  95. return addLayer(layer, texture->getQuad(), m, index);
  96. }
  97. int SpriteBatch::addLayer(int layer, Quad *quad, const Matrix4 &m, int index)
  98. {
  99. using namespace vertex;
  100. if (vertex_format != CommonFormat::XYf_STPf_RGBAub)
  101. throw love::Exception("addLayer can only be called on a SpriteBatch that uses an Array Texture!");
  102. if (index < -1 || index >= size)
  103. throw love::Exception("Invalid sprite index: %d", index + 1);
  104. if (layer < 0 || layer >= texture->getLayerCount())
  105. throw love::Exception("Invalid layer: %d (Texture has %d layers)", layer + 1, texture->getLayerCount());
  106. if (index == -1 && next >= size)
  107. setBufferSize(size * 2);
  108. const XYf_STf *quadverts = quad->getVertices();
  109. // Always keep the VBO mapped when adding data (it'll be unmapped on draw.)
  110. size_t offset = (index == -1 ? next : index) * format_stride * 4;
  111. auto verts = (XYf_STPf_RGBAub *) ((uint8 *) array_buf->map() + offset);
  112. m.transform(verts, quadverts, 4);
  113. for (int i = 0; i < 4; i++)
  114. {
  115. verts[i].s = quadverts[i].s;
  116. verts[i].t = quadverts[i].t;
  117. verts[i].p = (float) layer;
  118. verts[i].color = color;
  119. }
  120. array_buf->setMappedRangeModified(offset, format_stride * 4);
  121. // Increment counter.
  122. if (index == -1)
  123. return next++;
  124. return index;
  125. }
  126. void SpriteBatch::clear()
  127. {
  128. // Reset the position of the next index.
  129. next = 0;
  130. }
  131. void SpriteBatch::flush()
  132. {
  133. array_buf->unmap();
  134. }
  135. void SpriteBatch::setTexture(Texture *newtexture)
  136. {
  137. if (texture->getTextureType() != newtexture->getTextureType())
  138. throw love::Exception("Texture must have the same texture type as the SpriteBatch's previous texture.");
  139. texture.set(newtexture);
  140. }
  141. Texture *SpriteBatch::getTexture() const
  142. {
  143. return texture.get();
  144. }
  145. void SpriteBatch::setColor(const Colorf &c)
  146. {
  147. color_active = true;
  148. Colorf cclamped;
  149. cclamped.r = std::min(std::max(c.r, 0.0f), 1.0f);
  150. cclamped.g = std::min(std::max(c.g, 0.0f), 1.0f);
  151. cclamped.b = std::min(std::max(c.b, 0.0f), 1.0f);
  152. cclamped.a = std::min(std::max(c.a, 0.0f), 1.0f);
  153. this->color = toColor(cclamped);
  154. }
  155. void SpriteBatch::setColor()
  156. {
  157. color_active = false;
  158. color = Color(255, 255, 255, 255);
  159. }
  160. Colorf SpriteBatch::getColor(bool &active) const
  161. {
  162. active = color_active;
  163. return toColorf(color);
  164. }
  165. int SpriteBatch::getCount() const
  166. {
  167. return next;
  168. }
  169. void SpriteBatch::setBufferSize(int newsize)
  170. {
  171. if (newsize <= 0)
  172. throw love::Exception("Invalid SpriteBatch size.");
  173. if (newsize == size)
  174. return;
  175. size_t vertex_size = format_stride * 4 * newsize;
  176. love::graphics::Buffer *new_array_buf = nullptr;
  177. int new_next = std::min(next, newsize);
  178. try
  179. {
  180. auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
  181. new_array_buf = gfx->newBuffer(vertex_size, nullptr, array_buf->getType(), array_buf->getUsage(), array_buf->getMapFlags());
  182. // Copy as much of the old data into the new GLBuffer as can fit.
  183. size_t copy_size = format_stride * 4 * new_next;
  184. array_buf->copyTo(0, copy_size, new_array_buf, 0);
  185. quad_indices = QuadIndices(gfx, newsize);
  186. }
  187. catch (love::Exception &)
  188. {
  189. delete new_array_buf;
  190. throw;
  191. }
  192. // We don't need to unmap the old GLBuffer since we're deleting it.
  193. delete array_buf;
  194. array_buf = new_array_buf;
  195. size = newsize;
  196. next = new_next;
  197. }
  198. int SpriteBatch::getBufferSize() const
  199. {
  200. return size;
  201. }
  202. void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh)
  203. {
  204. AttachedAttribute oldattrib = {};
  205. AttachedAttribute newattrib = {};
  206. if (mesh->getVertexCount() < (size_t) next * 4)
  207. throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", next*4);
  208. auto it = attached_attributes.find(name);
  209. if (it != attached_attributes.end())
  210. oldattrib = it->second;
  211. newattrib.index = mesh->getAttributeIndex(name);
  212. if (newattrib.index < 0)
  213. throw love::Exception("The specified mesh does not have a vertex attribute named '%s'", name.c_str());
  214. newattrib.mesh = mesh;
  215. attached_attributes[name] = newattrib;
  216. }
  217. void SpriteBatch::setDrawRange(int start, int count)
  218. {
  219. if (start < 0 || count <= 0)
  220. throw love::Exception("Invalid draw range.");
  221. range_start = start;
  222. range_count = count;
  223. }
  224. void SpriteBatch::setDrawRange()
  225. {
  226. range_start = range_count = -1;
  227. }
  228. bool SpriteBatch::getDrawRange(int &start, int &count) const
  229. {
  230. if (range_start < 0 || range_count <= 0)
  231. return false;
  232. start = range_start;
  233. count = range_count;
  234. return true;
  235. }
  236. } // graphics
  237. } // love