SpriteBatch.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef LOVE_GRAPHICS_OPENGL_SPRITE_BATCH_H
  21. #define LOVE_GRAPHICS_OPENGL_SPRITE_BATCH_H
  22. // C
  23. #include <cstring>
  24. // LOVE
  25. #include "common/math.h"
  26. #include "common/Matrix.h"
  27. #include "common/StringMap.h"
  28. #include "graphics/Drawable.h"
  29. #include "graphics/Volatile.h"
  30. #include "graphics/Color.h"
  31. #include "graphics/Quad.h"
  32. #include "GLBuffer.h"
  33. namespace love
  34. {
  35. namespace graphics
  36. {
  37. // Forward declarations.
  38. class Texture;
  39. namespace opengl
  40. {
  41. class SpriteBatch : public Drawable
  42. {
  43. public:
  44. enum UsageHint
  45. {
  46. USAGE_DYNAMIC,
  47. USAGE_STATIC,
  48. USAGE_STREAM,
  49. USAGE_MAX_ENUM
  50. };
  51. SpriteBatch(Texture *texture, int size, int usage);
  52. virtual ~SpriteBatch();
  53. int add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index = -1);
  54. int addq(Quad *quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index = -1);
  55. void clear();
  56. void flush();
  57. void setTexture(Texture *newtexture);
  58. Texture *getTexture() const;
  59. /**
  60. * Set the current color for this SpriteBatch. The sprites added
  61. * after this call will use this color. Note that global color
  62. * will not longer apply to the SpriteBatch if this is used.
  63. *
  64. * @param color The color to use for the following sprites.
  65. */
  66. void setColor(const Color &color);
  67. /**
  68. * Disable per-sprite colors for this SpriteBatch. The next call to
  69. * draw will use the global color for all sprites.
  70. */
  71. void setColor();
  72. /**
  73. * Get the current color for this SpriteBatch. Returns NULL if no color is
  74. * set.
  75. **/
  76. const Color *getColor() const;
  77. /**
  78. * Get the number of sprites currently in this SpriteBatch.
  79. **/
  80. int getCount() const;
  81. /**
  82. * Sets the total number of sprites this SpriteBatch can hold.
  83. * Leaves existing sprite data intact when possible.
  84. **/
  85. void setBufferSize(int newsize);
  86. /**
  87. * Get the total number of sprites this SpriteBatch can hold.
  88. **/
  89. int getBufferSize() const;
  90. // Implements Drawable.
  91. void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  92. static bool getConstant(const char *in, UsageHint &out);
  93. static bool getConstant(UsageHint in, const char *&out);
  94. private:
  95. void addv(const Vertex *v, const Matrix &m, int index);
  96. /**
  97. * Set the color for vertices.
  98. *
  99. * @param v The vertices to set the color for. Must be an array of
  100. * of size 4.
  101. * @param color The color to assign to each vertex.
  102. */
  103. void setColorv(Vertex *v, const Color &color);
  104. StrongRef<Texture> texture;
  105. // Max number of sprites in the batch.
  106. int size;
  107. // The next free element.
  108. int next;
  109. // Current color. This color, if present, will be applied to the next
  110. // added sprite.
  111. Color *color;
  112. GLBuffer *array_buf;
  113. VertexIndex element_buf;
  114. // The portion of the vertex buffer that's been modified while mapped.
  115. size_t buffer_used_offset;
  116. size_t buffer_used_size;
  117. static StringMap<UsageHint, USAGE_MAX_ENUM>::Entry usageHintEntries[];
  118. static StringMap<UsageHint, USAGE_MAX_ENUM> usageHints;
  119. }; // SpriteBatch
  120. } // opengl
  121. } // graphics
  122. } // love
  123. #endif // LOVE_GRAPHICS_OPENGL_SPRITE_BATCH_H