SpriteBatch.cpp 7.8 KB

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