SpriteBatch.cpp 7.7 KB

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