SpriteBatch.h 4.0 KB

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