SpriteBatch.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. , buffer_used_offset(0)
  45. , buffer_used_size(0)
  46. {
  47. if (size <= 0)
  48. throw love::Exception("Invalid SpriteBatch size.");
  49. GLenum gl_usage = Mesh::getGLBufferUsage(usage);
  50. const size_t vertex_size = sizeof(Vertex) * 4 * size;
  51. try
  52. {
  53. array_buf = new GLBuffer(vertex_size, nullptr, GL_ARRAY_BUFFER, gl_usage);
  54. }
  55. catch (love::Exception &)
  56. {
  57. delete array_buf;
  58. throw;
  59. }
  60. catch (std::bad_alloc &)
  61. {
  62. delete array_buf;
  63. throw love::Exception("Out of memory.");
  64. }
  65. }
  66. SpriteBatch::~SpriteBatch()
  67. {
  68. delete color;
  69. delete array_buf;
  70. }
  71. int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
  72. {
  73. // Only do this if there's a free slot.
  74. if ((index == -1 && next >= size) || index < -1 || index >= size)
  75. return -1;
  76. Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
  77. addv(texture->getVertices(), t, (index == -1) ? next : index);
  78. // Increment counter.
  79. if (index == -1)
  80. return next++;
  81. return index;
  82. }
  83. 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*/)
  84. {
  85. // Only do this if there's a free slot.
  86. if ((index == -1 && next >= size) || index < -1 || index >= next)
  87. return -1;
  88. Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
  89. addv(quad->getVertices(), t, (index == -1) ? next : index);
  90. // Increment counter.
  91. if (index == -1)
  92. return next++;
  93. return index;
  94. }
  95. void SpriteBatch::clear()
  96. {
  97. // Reset the position of the next index.
  98. next = 0;
  99. }
  100. void SpriteBatch::flush()
  101. {
  102. GLBuffer::Bind bind(*array_buf);
  103. array_buf->unmap(buffer_used_offset, buffer_used_size);
  104. buffer_used_offset = buffer_used_size = 0;
  105. }
  106. void SpriteBatch::setTexture(Texture *newtexture)
  107. {
  108. texture.set(newtexture);
  109. }
  110. Texture *SpriteBatch::getTexture() const
  111. {
  112. return texture.get();
  113. }
  114. void SpriteBatch::setColor(const Color &color)
  115. {
  116. if (!this->color)
  117. this->color = new Color(color);
  118. else
  119. *(this->color) = color;
  120. }
  121. void SpriteBatch::setColor()
  122. {
  123. delete color;
  124. color = nullptr;
  125. }
  126. const Color *SpriteBatch::getColor() const
  127. {
  128. return color;
  129. }
  130. int SpriteBatch::getCount() const
  131. {
  132. return next;
  133. }
  134. void SpriteBatch::setBufferSize(int newsize)
  135. {
  136. if (newsize <= 0)
  137. throw love::Exception("Invalid SpriteBatch size.");
  138. if (newsize == size)
  139. return;
  140. // Map the old GLBuffer to get a pointer to its data.
  141. void *old_data = nullptr;
  142. {
  143. GLBuffer::Bind bind(*array_buf);
  144. old_data = array_buf->map();
  145. }
  146. size_t vertex_size = sizeof(Vertex) * 4 * newsize;
  147. GLBuffer *new_array_buf = nullptr;
  148. try
  149. {
  150. new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getTarget(), array_buf->getUsage());
  151. // Copy as much of the old data into the new GLBuffer as can fit.
  152. GLBuffer::Bind bind(*new_array_buf);
  153. void *new_data = new_array_buf->map();
  154. memcpy(new_data, old_data, sizeof(Vertex) * 4 * std::min(newsize, size));
  155. quad_indices = VertexIndex(newsize);
  156. }
  157. catch (love::Exception &)
  158. {
  159. delete new_array_buf;
  160. throw;
  161. }
  162. // We don't need to unmap the old GLBuffer since we're deleting it.
  163. delete array_buf;
  164. array_buf = new_array_buf;
  165. size = newsize;
  166. next = std::min(next, newsize);
  167. }
  168. int SpriteBatch::getBufferSize() const
  169. {
  170. return size;
  171. }
  172. void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  173. {
  174. const size_t pos_offset = offsetof(Vertex, x);
  175. const size_t texel_offset = offsetof(Vertex, s);
  176. const size_t color_offset = offsetof(Vertex, r);
  177. if (next == 0)
  178. return;
  179. OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
  180. Matrix t(x, y, angle, sx, sy, ox, oy, kx, ky);
  181. OpenGL::TempTransform transform(gl);
  182. transform.get() *= t;
  183. gl.bindTexture(*(GLuint *) texture->getHandle());
  184. GLBuffer::Bind array_bind(*array_buf);
  185. GLBuffer::Bind element_bind(*quad_indices.getBuffer());
  186. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  187. array_buf->unmap(buffer_used_offset, buffer_used_size);
  188. buffer_used_offset = buffer_used_size = 0;
  189. Color curcolor = gl.getColor();
  190. // Apply per-sprite color, if a color is set.
  191. if (color)
  192. {
  193. glEnableVertexAttribArray(ATTRIB_COLOR);
  194. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
  195. }
  196. glEnableVertexAttribArray(ATTRIB_POS);
  197. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
  198. glEnableVertexAttribArray(ATTRIB_TEXCOORD);
  199. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
  200. gl.prepareDraw();
  201. gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(next), quad_indices.getType(), quad_indices.getPointer(0));
  202. glDisableVertexAttribArray(ATTRIB_TEXCOORD);
  203. glDisableVertexAttribArray(ATTRIB_POS);
  204. if (color)
  205. {
  206. glDisableVertexAttribArray(ATTRIB_COLOR);
  207. gl.setColor(curcolor);
  208. }
  209. }
  210. void SpriteBatch::addv(const Vertex *v, const Matrix &m, int index)
  211. {
  212. // Needed for colors.
  213. Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
  214. const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
  215. m.transform(sprite, sprite, 4);
  216. if (color)
  217. setColorv(sprite, *color);
  218. GLBuffer::Bind bind(*array_buf);
  219. // Always keep the VBO mapped when adding data for now (it'll be unmapped
  220. // on draw.)
  221. array_buf->map();
  222. array_buf->fill(index * sprite_size, sprite_size, sprite);
  223. buffer_used_offset = std::min(buffer_used_offset, index * sprite_size);
  224. buffer_used_size = std::max(buffer_used_size, (index + 1) * sprite_size - buffer_used_offset);
  225. }
  226. void SpriteBatch::setColorv(Vertex *v, const Color &color)
  227. {
  228. for (size_t i = 0; i < 4; ++i)
  229. {
  230. v[i].r = color.r;
  231. v[i].g = color.g;
  232. v[i].b = color.b;
  233. v[i].a = color.a;
  234. }
  235. }
  236. } // opengl
  237. } // graphics
  238. } // love