SpriteBatch.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "VertexBuffer.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. , element_buf(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 = VertexBuffer::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. VertexBuffer::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 VertexBuffer to get a pointer to its data.
  154. void *old_data = nullptr;
  155. {
  156. VertexBuffer::Bind bind(*array_buf);
  157. old_data = array_buf->map();
  158. }
  159. size_t vertex_size = sizeof(Vertex) * 4 * newsize;
  160. VertexBuffer *new_array_buf = nullptr;
  161. try
  162. {
  163. new_array_buf = VertexBuffer::Create(vertex_size, array_buf->getTarget(), array_buf->getUsage());
  164. // Copy as much of the old data into the new VertexBuffer as can fit.
  165. VertexBuffer::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. element_buf = 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 VertexBuffer 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. Matrix t(x, y, angle, sx, sy, ox, oy, kx, ky);
  193. OpenGL::TempTransform transform(gl);
  194. transform.get() *= t;
  195. gl.bindTexture(*(GLuint *) texture->getHandle());
  196. VertexBuffer::Bind array_bind(*array_buf);
  197. VertexBuffer::Bind element_bind(*element_buf.getVertexBuffer());
  198. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  199. array_buf->unmap(buffer_used_offset, buffer_used_size);
  200. buffer_used_offset = buffer_used_size = 0;
  201. Color curcolor = gl.getColor();
  202. // Apply per-sprite color, if a color is set.
  203. if (color)
  204. {
  205. glEnableVertexAttribArray(ATTRIB_COLOR);
  206. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
  207. }
  208. glEnableVertexAttribArray(ATTRIB_POS);
  209. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
  210. glEnableVertexAttribArray(ATTRIB_TEXCOORD);
  211. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
  212. gl.prepareDraw();
  213. gl.drawElements(GL_TRIANGLES, (GLsizei) element_buf.getIndexCount(next), element_buf.getType(), element_buf.getPointer(0));
  214. glDisableVertexAttribArray(ATTRIB_TEXCOORD);
  215. glDisableVertexAttribArray(ATTRIB_POS);
  216. if (color)
  217. {
  218. glDisableVertexAttribArray(ATTRIB_COLOR);
  219. gl.setColor(curcolor);
  220. }
  221. }
  222. void SpriteBatch::addv(const Vertex *v, const Matrix &m, int index)
  223. {
  224. // Needed for colors.
  225. Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
  226. const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
  227. m.transform(sprite, sprite, 4);
  228. if (color)
  229. setColorv(sprite, *color);
  230. VertexBuffer::Bind bind(*array_buf);
  231. // Always keep the VBO mapped when adding data for now (it'll be unmapped
  232. // on draw.)
  233. array_buf->map();
  234. array_buf->fill(index * sprite_size, sprite_size, sprite);
  235. buffer_used_offset = std::min(buffer_used_offset, index * sprite_size);
  236. buffer_used_size = std::max(buffer_used_size, (index + 1) * sprite_size - buffer_used_offset);
  237. }
  238. void SpriteBatch::setColorv(Vertex *v, const Color &color)
  239. {
  240. for (size_t i = 0; i < 4; ++i)
  241. {
  242. v[i].r = color.r;
  243. v[i].g = color.g;
  244. v[i].b = color.b;
  245. v[i].a = color.a;
  246. }
  247. }
  248. bool SpriteBatch::getConstant(const char *in, UsageHint &out)
  249. {
  250. return usageHints.find(in, out);
  251. }
  252. bool SpriteBatch::getConstant(UsageHint in, const char *&out)
  253. {
  254. return usageHints.find(in, out);
  255. }
  256. StringMap<SpriteBatch::UsageHint, SpriteBatch::USAGE_MAX_ENUM>::Entry SpriteBatch::usageHintEntries[] =
  257. {
  258. {"dynamic", SpriteBatch::USAGE_DYNAMIC},
  259. {"static", SpriteBatch::USAGE_STATIC},
  260. {"stream", SpriteBatch::USAGE_STREAM},
  261. };
  262. StringMap<SpriteBatch::UsageHint, SpriteBatch::USAGE_MAX_ENUM> SpriteBatch::usageHints(usageHintEntries, sizeof(usageHintEntries));
  263. } // opengl
  264. } // graphics
  265. } // love