SpriteBatch.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Copyright (c) 2006-2015 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. // Only do this if there's a free slot.
  59. if ((index == -1 && next >= size) || index < -1 || index >= size)
  60. return -1;
  61. Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
  62. addv(texture->getVertices(), t, (index == -1) ? next : index);
  63. // Increment counter.
  64. if (index == -1)
  65. return next++;
  66. return index;
  67. }
  68. 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*/)
  69. {
  70. // Only do this if there's a free slot.
  71. if ((index == -1 && next >= size) || index < -1 || index >= next)
  72. return -1;
  73. Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
  74. addv(quad->getVertices(), t, (index == -1) ? next : index);
  75. // Increment counter.
  76. if (index == -1)
  77. return next++;
  78. return index;
  79. }
  80. void SpriteBatch::clear()
  81. {
  82. // Reset the position of the next index.
  83. next = 0;
  84. }
  85. void SpriteBatch::flush()
  86. {
  87. GLBuffer::Bind bind(*array_buf);
  88. array_buf->unmap();
  89. }
  90. void SpriteBatch::setTexture(Texture *newtexture)
  91. {
  92. texture.set(newtexture);
  93. }
  94. Texture *SpriteBatch::getTexture() const
  95. {
  96. return texture.get();
  97. }
  98. void SpriteBatch::setColor(const Color &color)
  99. {
  100. if (!this->color)
  101. this->color = new Color(color);
  102. else
  103. *(this->color) = color;
  104. }
  105. void SpriteBatch::setColor()
  106. {
  107. delete color;
  108. color = nullptr;
  109. }
  110. const Color *SpriteBatch::getColor() const
  111. {
  112. return color;
  113. }
  114. int SpriteBatch::getCount() const
  115. {
  116. return next;
  117. }
  118. void SpriteBatch::setBufferSize(int newsize)
  119. {
  120. if (newsize <= 0)
  121. throw love::Exception("Invalid SpriteBatch size.");
  122. if (newsize == size)
  123. return;
  124. // Map the old GLBuffer to get a pointer to its data.
  125. void *old_data = nullptr;
  126. {
  127. GLBuffer::Bind bind(*array_buf);
  128. old_data = array_buf->map();
  129. }
  130. size_t vertex_size = sizeof(Vertex) * 4 * newsize;
  131. GLBuffer *new_array_buf = nullptr;
  132. try
  133. {
  134. new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getTarget(), array_buf->getUsage(), array_buf->getMapFlags());
  135. // Copy as much of the old data into the new GLBuffer as can fit.
  136. GLBuffer::Bind bind(*new_array_buf);
  137. void *new_data = new_array_buf->map();
  138. memcpy(new_data, old_data, sizeof(Vertex) * 4 * std::min(newsize, size));
  139. quad_indices = QuadIndices(newsize);
  140. }
  141. catch (love::Exception &)
  142. {
  143. delete new_array_buf;
  144. throw;
  145. }
  146. // We don't need to unmap the old GLBuffer since we're deleting it.
  147. delete array_buf;
  148. array_buf = new_array_buf;
  149. size = newsize;
  150. next = std::min(next, newsize);
  151. }
  152. int SpriteBatch::getBufferSize() const
  153. {
  154. return size;
  155. }
  156. void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh)
  157. {
  158. AttachedAttribute oldattrib = {};
  159. AttachedAttribute newattrib = {};
  160. if (mesh->getVertexCount() < (size_t) getBufferSize() * 4)
  161. throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", getBufferSize()*4);
  162. auto it = attached_attributes.find(name);
  163. if (it != attached_attributes.end())
  164. oldattrib = it->second;
  165. newattrib.index = mesh->getAttributeIndex(name);
  166. if (newattrib.index < 0)
  167. throw love::Exception("The specified mesh does not have a vertex attribute named '%s'", name.c_str());
  168. newattrib.mesh = mesh;
  169. attached_attributes[name] = newattrib;
  170. }
  171. void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  172. {
  173. const size_t pos_offset = offsetof(Vertex, x);
  174. const size_t texel_offset = offsetof(Vertex, s);
  175. const size_t color_offset = offsetof(Vertex, r);
  176. if (next == 0)
  177. return;
  178. OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
  179. OpenGL::TempTransform transform(gl);
  180. transform.get() *= Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  181. gl.bindTexture(*(GLuint *) texture->getHandle());
  182. uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
  183. {
  184. // Scope this bind so it doesn't interfere with the
  185. // Mesh::bindAttributeToShaderInput calls below.
  186. GLBuffer::Bind array_bind(*array_buf);
  187. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  188. array_buf->unmap();
  189. // Apply per-sprite color, if a color is set.
  190. if (color)
  191. {
  192. enabledattribs |= ATTRIBFLAG_COLOR;
  193. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
  194. }
  195. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
  196. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
  197. }
  198. for (const auto &it : attached_attributes)
  199. {
  200. Mesh *mesh = it.second.mesh.get();
  201. // We have to do this check here as well because setBufferSize can be
  202. // called after attachAttribute.
  203. if (mesh->getVertexCount() < (size_t) getBufferSize() * 4)
  204. throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
  205. int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
  206. if (location >= 0)
  207. enabledattribs |= 1u << (uint32) location;
  208. }
  209. gl.useVertexAttribArrays(enabledattribs);
  210. gl.prepareDraw();
  211. GLBuffer::Bind element_bind(*quad_indices.getBuffer());
  212. gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(next), quad_indices.getType(), quad_indices.getPointer(0));
  213. }
  214. void SpriteBatch::addv(const Vertex *v, const Matrix3 &m, int index)
  215. {
  216. // Needed for colors.
  217. Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
  218. const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
  219. m.transform(sprite, sprite, 4);
  220. if (color)
  221. setColorv(sprite, *color);
  222. GLBuffer::Bind bind(*array_buf);
  223. // Always keep the VBO mapped when adding data for now (it'll be unmapped
  224. // on draw.)
  225. array_buf->map();
  226. array_buf->fill(index * sprite_size, sprite_size, sprite);
  227. }
  228. void SpriteBatch::setColorv(Vertex *v, const Color &color)
  229. {
  230. for (size_t i = 0; i < 4; ++i)
  231. {
  232. v[i].r = color.r;
  233. v[i].g = color.g;
  234. v[i].b = color.b;
  235. v[i].a = color.a;
  236. }
  237. }
  238. } // opengl
  239. } // graphics
  240. } // love