CmSprite.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmPoint.h"
  4. #include "CmRect.h"
  5. namespace CamelotEngine
  6. {
  7. enum SpriteAnchor
  8. {
  9. SA_TopLeft,
  10. SA_TopCenter,
  11. SA_TopRight,
  12. SA_MiddleLeft,
  13. SA_MiddleCenter,
  14. SA_MiddleRight,
  15. SA_BottomLeft,
  16. SA_BottomCenter,
  17. SA_BottomRight
  18. };
  19. struct SpriteRenderElement
  20. {
  21. SpriteRenderElement()
  22. :vertices(nullptr), uvs(nullptr), indexes(nullptr), numQuads(0)
  23. { }
  24. Vector2* vertices;
  25. Vector2* uvs;
  26. UINT32* indexes;
  27. UINT32 numQuads;
  28. HMaterial material;
  29. };
  30. class CM_EXPORT Sprite
  31. {
  32. public:
  33. Sprite();
  34. virtual ~Sprite();
  35. void setOffset(const Point& offset) { mOffset = offset; setDirty(); }
  36. void setSize(UINT32 width, UINT32 height) { mWidth = width; mHeight = height; setDirty(); }
  37. void setClipRect(const Rect& clipRect) { mClipRect = clipRect; setDirty(); }
  38. void setAnchor(SpriteAnchor anchor) { mAnchor = anchor; setDirty(); }
  39. Point getOffset() const { return mOffset; }
  40. UINT32 getWidth() const { return mWidth; }
  41. UINT32 getHeight() const { return mHeight; }
  42. Rect getClipRect() const { return mClipRect; }
  43. SpriteAnchor getAnchor() const { return mAnchor; }
  44. const Rect& getBounds() const;
  45. /**
  46. * @brief Returns the number of separate render elements in the sprite. Normally this is one, but some sprites
  47. * may consist of multiple materials, in which case each will require it's own mesh (render element)
  48. *
  49. * @return The number render elements.
  50. */
  51. UINT32 getNumRenderElements() const;
  52. /**
  53. * @brief Gets a material for the specified render element index.
  54. *
  55. * @see getNumRenderElements()
  56. *
  57. * @return Handle to the material.
  58. */
  59. const HMaterial& getMaterial(UINT32 renderElementIdx) const;
  60. /**
  61. * @brief Returns the number of quads that the specified render element will use. You will need this
  62. * value when creating the buffers before calling "fillBuffer".
  63. *
  64. * @see getNumRenderElements()
  65. * @see fillBuffer()
  66. *
  67. * @note Number of vertices = Number of quads * 4
  68. * Number of indices = Number of quads * 6
  69. *
  70. * @return Number of quads for the specified render element.
  71. */
  72. UINT32 getNumQuads(UINT32 renderElementIdx) const;
  73. /**
  74. * @brief Fill the pre-allocated vertex, uv and index buffers with the mesh data for the specified render element.
  75. *
  76. * @see getNumRenderElements()
  77. * @see getNumQuads()
  78. *
  79. * @param vertices Previously allocated buffer where to store the vertices.
  80. * @param uv Previously allocated buffer where to store the uv coordinates.
  81. * @param indices Previously allocated buffer where to store the indices.
  82. * @param startingQuad At which quad should the method start filling the buffer.
  83. * @param maxNumQuads Total number of quads the buffers were allocated for. Used only for memory safety.
  84. * @param renderElementIdx Zero-based index of the render element.
  85. */
  86. UINT32 fillBuffer(Vector2* vertices, Vector2* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, UINT32 renderElementIdx) const;
  87. protected:
  88. Point mOffset;
  89. UINT32 mWidth, mHeight;
  90. Rect mClipRect;
  91. SpriteAnchor mAnchor;
  92. mutable Rect mBounds;
  93. mutable bool mIsDirty;
  94. mutable vector<SpriteRenderElement>::type mCachedRenderElements;
  95. void setDirty() { mIsDirty = true; }
  96. Point getAnchorOffset() const;
  97. bool isClipRectangleValid() const;
  98. virtual void updateMesh() const = 0;
  99. void updateBounds() const;
  100. void clearMesh() const;
  101. };
  102. }