SpriteBatch.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * Copyright (c) 2006-2016 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. // OpenGL
  23. #include "OpenGL.h"
  24. // LOVE
  25. #include "GLBuffer.h"
  26. #include "graphics/Texture.h"
  27. // C++
  28. #include <algorithm>
  29. // C
  30. #include <stddef.h>
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. namespace opengl
  36. {
  37. love::Type SpriteBatch::type(&Drawable::type);
  38. SpriteBatch::SpriteBatch(Texture *texture, int size, Mesh::Usage usage)
  39. : texture(texture)
  40. , size(size)
  41. , next(0)
  42. , color(0)
  43. , array_buf(nullptr)
  44. , quad_indices(size)
  45. , range_start(-1)
  46. , range_count(-1)
  47. {
  48. if (size <= 0)
  49. throw love::Exception("Invalid SpriteBatch size.");
  50. GLenum gl_usage = Mesh::getGLBufferUsage(usage);
  51. size_t vertex_size = sizeof(Vertex) * 4 * size;
  52. array_buf = new GLBuffer(vertex_size, nullptr, BUFFER_VERTEX, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY);
  53. }
  54. SpriteBatch::~SpriteBatch()
  55. {
  56. delete color;
  57. delete array_buf;
  58. }
  59. int SpriteBatch::add(const Matrix4 &m, int index /*= -1*/)
  60. {
  61. if (index < -1 || index >= size)
  62. throw love::Exception("Invalid sprite index: %d", index + 1);
  63. if (index == -1 && next >= size)
  64. setBufferSize(size * 2);
  65. addv(texture->getVertices(), m, (index == -1) ? next : index);
  66. // Increment counter.
  67. if (index == -1)
  68. return next++;
  69. return index;
  70. }
  71. int SpriteBatch::addq(Quad *quad, const Matrix4 &m, int index /*= -1*/)
  72. {
  73. if (index < -1 || index >= size)
  74. throw love::Exception("Invalid sprite index: %d", index + 1);
  75. if (index == -1 && next >= size)
  76. setBufferSize(size * 2);
  77. addv(quad->getVertices(), m, (index == -1) ? next : index);
  78. // Increment counter.
  79. if (index == -1)
  80. return next++;
  81. return index;
  82. }
  83. void SpriteBatch::clear()
  84. {
  85. // Reset the position of the next index.
  86. next = 0;
  87. }
  88. void SpriteBatch::flush()
  89. {
  90. array_buf->unmap();
  91. }
  92. void SpriteBatch::setTexture(Texture *newtexture)
  93. {
  94. texture.set(newtexture);
  95. }
  96. Texture *SpriteBatch::getTexture() const
  97. {
  98. return texture.get();
  99. }
  100. void SpriteBatch::setColor(const Color &color)
  101. {
  102. if (!this->color)
  103. this->color = new Color(color);
  104. else
  105. *(this->color) = color;
  106. }
  107. void SpriteBatch::setColor()
  108. {
  109. delete color;
  110. color = nullptr;
  111. }
  112. const Color *SpriteBatch::getColor() const
  113. {
  114. return color;
  115. }
  116. int SpriteBatch::getCount() const
  117. {
  118. return next;
  119. }
  120. void SpriteBatch::setBufferSize(int newsize)
  121. {
  122. if (newsize <= 0)
  123. throw love::Exception("Invalid SpriteBatch size.");
  124. if (newsize == size)
  125. return;
  126. // Map the old GLBuffer to get a pointer to its data.
  127. void *old_data = array_buf->map();
  128. size_t vertex_size = sizeof(Vertex) * 4 * newsize;
  129. GLBuffer *new_array_buf = nullptr;
  130. int new_next = std::min(next, newsize);
  131. try
  132. {
  133. new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getType(), array_buf->getUsage(), array_buf->getMapFlags());
  134. // Copy as much of the old data into the new GLBuffer as can fit.
  135. size_t copy_size = sizeof(Vertex) * 4 * new_next;
  136. memcpy(new_array_buf->map(), old_data, copy_size);
  137. new_array_buf->setMappedRangeModified(0, copy_size);
  138. quad_indices = QuadIndices(newsize);
  139. }
  140. catch (love::Exception &)
  141. {
  142. delete new_array_buf;
  143. throw;
  144. }
  145. // We don't need to unmap the old GLBuffer since we're deleting it.
  146. delete array_buf;
  147. array_buf = new_array_buf;
  148. size = newsize;
  149. next = new_next;
  150. }
  151. int SpriteBatch::getBufferSize() const
  152. {
  153. return size;
  154. }
  155. void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh)
  156. {
  157. AttachedAttribute oldattrib = {};
  158. AttachedAttribute newattrib = {};
  159. if (mesh->getVertexCount() < (size_t) next * 4)
  160. throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", next*4);
  161. auto it = attached_attributes.find(name);
  162. if (it != attached_attributes.end())
  163. oldattrib = it->second;
  164. newattrib.index = mesh->getAttributeIndex(name);
  165. if (newattrib.index < 0)
  166. throw love::Exception("The specified mesh does not have a vertex attribute named '%s'", name.c_str());
  167. newattrib.mesh = mesh;
  168. attached_attributes[name] = newattrib;
  169. }
  170. void SpriteBatch::setDrawRange(int start, int count)
  171. {
  172. if (start < 0 || count <= 0)
  173. throw love::Exception("Invalid draw range.");
  174. range_start = start;
  175. range_count = count;
  176. }
  177. void SpriteBatch::setDrawRange()
  178. {
  179. range_start = range_count = -1;
  180. }
  181. bool SpriteBatch::getDrawRange(int &start, int &count) const
  182. {
  183. if (range_start < 0 || range_count <= 0)
  184. return false;
  185. start = range_start;
  186. count = range_count;
  187. return true;
  188. }
  189. void SpriteBatch::draw(const Matrix4 &m)
  190. {
  191. const size_t pos_offset = offsetof(Vertex, x);
  192. const size_t texel_offset = offsetof(Vertex, s);
  193. const size_t color_offset = offsetof(Vertex, r);
  194. if (next == 0)
  195. return;
  196. OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
  197. OpenGL::TempTransform transform(gl);
  198. transform.get() *= m;
  199. gl.bindTextureToUnit(*(GLuint *) texture->getHandle(), 0, false);
  200. uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
  201. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  202. array_buf->unmap();
  203. array_buf->bind();
  204. // Apply per-sprite color, if a color is set.
  205. if (color)
  206. {
  207. enabledattribs |= ATTRIBFLAG_COLOR;
  208. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
  209. }
  210. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
  211. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
  212. for (const auto &it : attached_attributes)
  213. {
  214. Mesh *mesh = it.second.mesh.get();
  215. // We have to do this check here as wll because setBufferSize can be
  216. // called after attachAttribute.
  217. if (mesh->getVertexCount() < (size_t) next * 4)
  218. throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
  219. int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
  220. if (location >= 0)
  221. enabledattribs |= 1u << (uint32) location;
  222. }
  223. gl.useVertexAttribArrays(enabledattribs);
  224. gl.prepareDraw();
  225. int start = std::min(std::max(0, range_start), next - 1);
  226. int count = next;
  227. if (range_count > 0)
  228. count = std::min(count, range_count);
  229. count = std::min(count, next - start);
  230. quad_indices.getBuffer()->bind();
  231. const void *indices = quad_indices.getPointer(start * quad_indices.getElementSize());
  232. if (count > 0)
  233. gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(count), quad_indices.getType(), indices);
  234. }
  235. void SpriteBatch::addv(const Vertex *v, const Matrix4 &m, int index)
  236. {
  237. // Needed for colors.
  238. Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
  239. const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
  240. m.transform(sprite, sprite, 4);
  241. if (color)
  242. setColorv(sprite, *color);
  243. // Always keep the VBO mapped when adding data for now (it'll be unmapped
  244. // on draw.)
  245. array_buf->map();
  246. array_buf->fill(index * sprite_size, sprite_size, sprite);
  247. }
  248. void SpriteBatch::setColorv(Vertex *v, const Color &color)
  249. {
  250. for (size_t i = 0; i < 4; ++i)
  251. {
  252. v[i].r = color.r;
  253. v[i].g = color.g;
  254. v[i].b = color.b;
  255. v[i].a = color.a;
  256. }
  257. }
  258. } // opengl
  259. } // graphics
  260. } // love