SpriteBatch.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Copyright (c) 2006-2012 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/Vector.h"
  28. #include "common/Matrix.h"
  29. #include "common/StringMap.h"
  30. #include "graphics/Drawable.h"
  31. #include "graphics/Volatile.h"
  32. #include "graphics/Color.h"
  33. // OpenGL
  34. #include "GLee.h"
  35. namespace love
  36. {
  37. namespace graphics
  38. {
  39. namespace opengl
  40. {
  41. // Forward declarations.
  42. class Image;
  43. class Quad;
  44. class VertexBuffer;
  45. class VertexIndex;
  46. class SpriteBatch : public Drawable
  47. {
  48. public:
  49. enum UsageHint
  50. {
  51. USAGE_DYNAMIC = 1,
  52. USAGE_STATIC,
  53. USAGE_STREAM,
  54. USAGE_MAX_ENUM
  55. };
  56. SpriteBatch(Image *image, int size, int usage);
  57. virtual ~SpriteBatch();
  58. int add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index = -1);
  59. 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);
  60. void clear();
  61. void *lock();
  62. void unlock();
  63. void setImage(Image *newimage);
  64. Image *getImage();
  65. /**
  66. * Set the current color for this SpriteBatch. The geometry added
  67. * after this call will use this color. Note that global color
  68. * will not longer apply to the SpriteBatch if this is used.
  69. *
  70. * @param color The color to use for the following geometry.
  71. */
  72. void setColor(const Color &color);
  73. /**
  74. * Disable per-quad colors for this SpriteBatch. The next call to
  75. * draw will use the global color for all sprites.
  76. */
  77. void setColor();
  78. // Implements Drawable.
  79. void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
  80. static bool getConstant(const char *in, UsageHint &out);
  81. static bool getConstant(UsageHint in, const char *&out);
  82. private:
  83. void addv(const vertex *v, int index);
  84. /**
  85. * Set the color for vertices.
  86. *
  87. * @param v The vertices to set the color for. Must be an array of
  88. * of size 4.
  89. * @param color The color to assign to each vertex.
  90. */
  91. void setColorv(vertex *v, const Color &color);
  92. static StringMap<UsageHint, USAGE_MAX_ENUM>::Entry usageHintEntries[];
  93. static StringMap<UsageHint, USAGE_MAX_ENUM> usageHints;
  94. Image *image;
  95. // Max number of sprites in the batch.
  96. int size;
  97. // The next free element.
  98. int next;
  99. vertex sprite[4];
  100. // Current color. This color, if present, will be applied to the next
  101. // added quad.
  102. Color *color;
  103. VertexBuffer *array_buf;
  104. VertexIndex *element_buf;
  105. }; // SpriteBatch
  106. } // opengl
  107. } // graphics
  108. } // love
  109. #endif // LOVE_GRAPHICS_OPENGL_SPRITE_BATCH_H