SpriteBatch.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. {
  45. if (size <= 0)
  46. throw love::Exception("Invalid SpriteBatch size.");
  47. GLenum gl_usage = Mesh::getGLBufferUsage(usage);
  48. size_t vertex_size = sizeof(Vertex) * 4 * size;
  49. array_buf = new GLBuffer(vertex_size, nullptr, GL_ARRAY_BUFFER, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY);
  50. }
  51. SpriteBatch::~SpriteBatch()
  52. {
  53. delete color;
  54. delete array_buf;
  55. }
  56. int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
  57. {
  58. if (index < -1 || index >= size)
  59. throw love::Exception("Invalid sprite index: %d", index + 1);
  60. if (index == -1 && next >= size)
  61. setBufferSize(size * 2);
  62. Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
  63. addv(texture->getVertices(), t, (index == -1) ? next : index);
  64. // Increment counter.
  65. if (index == -1)
  66. return next++;
  67. return index;
  68. }
  69. int SpriteBatch::addq(Quad *quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
  70. {
  71. if (index < -1 || index >= size)
  72. throw love::Exception("Invalid sprite index: %d", index + 1);
  73. if (index == -1 && next >= size)
  74. setBufferSize(size * 2);
  75. Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
  76. addv(quad->getVertices(), t, (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. GLBuffer::Bind bind(*array_buf);
  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 = nullptr;
  128. {
  129. GLBuffer::Bind bind(*array_buf);
  130. old_data = array_buf->map();
  131. }
  132. size_t vertex_size = sizeof(Vertex) * 4 * newsize;
  133. GLBuffer *new_array_buf = nullptr;
  134. try
  135. {
  136. new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getTarget(), array_buf->getUsage(), array_buf->getMapFlags());
  137. // Copy as much of the old data into the new GLBuffer as can fit.
  138. GLBuffer::Bind bind(*new_array_buf);
  139. void *new_data = new_array_buf->map();
  140. memcpy(new_data, old_data, sizeof(Vertex) * 4 * std::min(newsize, size));
  141. quad_indices = QuadIndices(newsize);
  142. }
  143. catch (love::Exception &)
  144. {
  145. delete new_array_buf;
  146. throw;
  147. }
  148. // We don't need to unmap the old GLBuffer since we're deleting it.
  149. delete array_buf;
  150. array_buf = new_array_buf;
  151. size = newsize;
  152. next = std::min(next, newsize);
  153. }
  154. int SpriteBatch::getBufferSize() const
  155. {
  156. return size;
  157. }
  158. void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh)
  159. {
  160. AttachedAttribute oldattrib = {};
  161. AttachedAttribute newattrib = {};
  162. if (mesh->getVertexCount() < (size_t) next * 4)
  163. throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", next*4);
  164. auto it = attached_attributes.find(name);
  165. if (it != attached_attributes.end())
  166. oldattrib = it->second;
  167. newattrib.index = mesh->getAttributeIndex(name);
  168. if (newattrib.index < 0)
  169. throw love::Exception("The specified mesh does not have a vertex attribute named '%s'", name.c_str());
  170. newattrib.mesh = mesh;
  171. attached_attributes[name] = newattrib;
  172. }
  173. void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  174. {
  175. const size_t pos_offset = offsetof(Vertex, x);
  176. const size_t texel_offset = offsetof(Vertex, s);
  177. const size_t color_offset = offsetof(Vertex, r);
  178. if (next == 0)
  179. return;
  180. OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
  181. OpenGL::TempTransform transform(gl);
  182. transform.get() *= Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  183. gl.bindTexture(*(GLuint *) texture->getHandle());
  184. uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
  185. {
  186. // Scope this bind so it doesn't interfere with the
  187. // Mesh::bindAttributeToShaderInput calls below.
  188. GLBuffer::Bind array_bind(*array_buf);
  189. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  190. array_buf->unmap();
  191. // Apply per-sprite color, if a color is set.
  192. if (color)
  193. {
  194. enabledattribs |= ATTRIBFLAG_COLOR;
  195. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
  196. }
  197. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
  198. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
  199. }
  200. for (const auto &it : attached_attributes)
  201. {
  202. Mesh *mesh = it.second.mesh.get();
  203. // We have to do this check here as wll because setBufferSize can be
  204. // called after attachAttribute.
  205. if (mesh->getVertexCount() < (size_t) next * 4)
  206. throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
  207. int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
  208. if (location >= 0)
  209. enabledattribs |= 1u << (uint32) location;
  210. }
  211. gl.useVertexAttribArrays(enabledattribs);
  212. gl.prepareDraw();
  213. GLBuffer::Bind element_bind(*quad_indices.getBuffer());
  214. gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(next), quad_indices.getType(), quad_indices.getPointer(0));
  215. }
  216. void SpriteBatch::addv(const Vertex *v, const Matrix3 &m, int index)
  217. {
  218. // Needed for colors.
  219. Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
  220. const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
  221. m.transform(sprite, sprite, 4);
  222. if (color)
  223. setColorv(sprite, *color);
  224. GLBuffer::Bind bind(*array_buf);
  225. // Always keep the VBO mapped when adding data for now (it'll be unmapped
  226. // on draw.)
  227. array_buf->map();
  228. array_buf->fill(index * sprite_size, sprite_size, sprite);
  229. }
  230. void SpriteBatch::setColorv(Vertex *v, const Color &color)
  231. {
  232. for (size_t i = 0; i < 4; ++i)
  233. {
  234. v[i].r = color.r;
  235. v[i].g = color.g;
  236. v[i].b = color.b;
  237. v[i].a = color.a;
  238. }
  239. }
  240. } // opengl
  241. } // graphics
  242. } // love