SpriteBatch.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2006-2017 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 "graphics/Buffer.h"
  26. #include "graphics/Texture.h"
  27. #include "graphics/Graphics.h"
  28. // C++
  29. #include <algorithm>
  30. // C
  31. #include <stddef.h>
  32. namespace love
  33. {
  34. namespace graphics
  35. {
  36. namespace opengl
  37. {
  38. SpriteBatch::SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usage usage)
  39. : love::graphics::SpriteBatch(gfx, texture, size, usage)
  40. {
  41. }
  42. SpriteBatch::~SpriteBatch()
  43. {
  44. }
  45. void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m)
  46. {
  47. using namespace vertex;
  48. if (next == 0)
  49. return;
  50. gfx->flushStreamDraws();
  51. Shader *prevdefaultshader = nullptr;
  52. if (texture.get())
  53. {
  54. TextureType textype = texture->getTextureType();
  55. if (textype == TEXTURE_2D_ARRAY && Shader::isDefaultActive())
  56. {
  57. if (!Shader::standardShaders[Shader::STANDARD_ARRAY])
  58. throw love::Exception("Standard array texture shader has not been initialized!");
  59. prevdefaultshader = Shader::current;
  60. Shader::standardShaders[Shader::STANDARD_ARRAY]->attach();
  61. }
  62. if (Shader::current)
  63. Shader::current->checkMainTexture(texture);
  64. }
  65. OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
  66. Graphics::TempTransform transform(gfx, m);
  67. gl.bindTextureToUnit(texture, 0, false);
  68. // Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
  69. array_buf->unmap();
  70. CommonFormat format = vertex_format;
  71. if (!color_active)
  72. {
  73. if (format == CommonFormat::XYf_STPf_RGBAub)
  74. format = CommonFormat::XYf_STPf;
  75. else
  76. format = CommonFormat::XYf_STf;
  77. }
  78. uint32 enabledattribs = getFormatFlags(format);
  79. gl.setVertexPointers(format, array_buf, format_stride, 0);
  80. for (const auto &it : attached_attributes)
  81. {
  82. Mesh *mesh = it.second.mesh.get();
  83. // We have to do this check here as wll because setBufferSize can be
  84. // called after attachAttribute.
  85. if (mesh->getVertexCount() < (size_t) next * 4)
  86. throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
  87. int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
  88. if (location >= 0)
  89. enabledattribs |= 1u << (uint32) location;
  90. }
  91. gl.useVertexAttribArrays(enabledattribs);
  92. gl.prepareDraw();
  93. int start = std::min(std::max(0, range_start), next - 1);
  94. int count = next;
  95. if (range_count > 0)
  96. count = std::min(count, range_count);
  97. count = std::min(count, next - start);
  98. if (count > 0)
  99. {
  100. gl.bindBuffer(BUFFER_INDEX, (GLuint) quad_indices.getBuffer()->getHandle());
  101. const void *indices = BUFFER_OFFSET(start * quad_indices.getElementSize());
  102. GLenum gltype = OpenGL::getGLIndexDataType(quad_indices.getType());
  103. gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(count), gltype, indices);
  104. }
  105. if (prevdefaultshader != nullptr)
  106. prevdefaultshader->attach();
  107. }
  108. } // opengl
  109. } // graphics
  110. } // love