CmSprite.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  45. * @brief Returns the number of separate render elements in the sprite. Normally this is one, but some sprites
  46. * may consist of multiple materials, in which case each will require it's own mesh (render element)
  47. *
  48. * @return The number render elements.
  49. */
  50. UINT32 getNumRenderElements() const;
  51. /**
  52. * @brief Gets a material for the specified render element index.
  53. *
  54. * @see getNumRenderElements()
  55. *
  56. * @return Handle to the material.
  57. */
  58. const HMaterial& getMaterial(UINT32 renderElementIdx) const;
  59. /**
  60. * @brief Returns the number of quads that the specified render element will use. You will need this
  61. * value when creating the buffers before calling "fillBuffer".
  62. *
  63. * @see getNumRenderElements()
  64. * @see fillBuffer()
  65. *
  66. * @note Number of vertices = Number of quads * 4
  67. * Number of indices = Number of quads * 6
  68. *
  69. * @return Number of quads for the specified render element.
  70. */
  71. UINT32 getNumQuads(UINT32 renderElementIdx) const;
  72. /**
  73. * @brief Fill the pre-allocated vertex, uv and index buffers with the mesh data for the specified render element.
  74. *
  75. * @see getNumRenderElements()
  76. * @see getNumQuads()
  77. *
  78. * @param vertices Previously allocated buffer where to store the vertices.
  79. * @param uv Previously allocated buffer where to store the uv coordinates.
  80. * @param indices Previously allocated buffer where to store the indices.
  81. * @param startingQuad At which quad should the method start filling the buffer.
  82. * @param maxNumQuads Total number of quads the buffers were allocated for. Used only for memory safety.
  83. * @param renderElementIdx Zero-based index of the render element.
  84. */
  85. UINT32 fillBuffer(Vector2* vertices, Vector2* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, UINT32 renderElementIdx) const;
  86. protected:
  87. Point mOffset;
  88. UINT32 mWidth, mHeight;
  89. Rect mClipRect;
  90. SpriteAnchor mAnchor;
  91. mutable bool mIsDirty;
  92. mutable vector<SpriteRenderElement>::type mCachedRenderElements;
  93. void setDirty() { mIsDirty = true; }
  94. Point getAnchorOffset() const;
  95. bool isClipRectangleValid() const;
  96. virtual void updateMesh() const = 0;
  97. void clearMesh() const;
  98. };
  99. }