CmSprite.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "CmTextSprite.h"
  2. #include "CmVector2.h"
  3. namespace CamelotEngine
  4. {
  5. Sprite::Sprite()
  6. :mWidth(0), mHeight(0), mAnchor(SA_TopLeft), mIsDirty(true),
  7. mVertices(nullptr), mUVs(nullptr), mIndexes(nullptr), mNumMeshQuads(0)
  8. {
  9. }
  10. Sprite::~Sprite()
  11. {
  12. }
  13. UINT32 Sprite::fillBuffer(Vector2* vertices, Vector2* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads)
  14. {
  15. if(mIsDirty)
  16. {
  17. updateMesh();
  18. mIsDirty = false;
  19. }
  20. UINT32 startVert = startingQuad * 4;
  21. UINT32 startIndex = startingQuad * 4;
  22. UINT32 maxVertIdx = maxNumQuads * 4;
  23. UINT32 maxIndexIdx = maxNumQuads * 6;
  24. UINT32 mNumVertices = mNumMeshQuads * 4;
  25. UINT32 mNumIndices = mNumMeshQuads * 6;
  26. assert((startVert + mNumVertices) <= maxVertIdx);
  27. assert((startIndex + mNumIndices) <= maxIndexIdx);
  28. memcpy(&vertices[startVert], mVertices, mNumVertices * sizeof(Vector2));
  29. memcpy(&uv[startVert], mUVs, mNumVertices * sizeof(Vector2));
  30. memcpy(&indices[startIndex], mIndexes, mNumIndices * sizeof(UINT32));
  31. return mNumMeshQuads;
  32. }
  33. UINT32 Sprite::getNumFaces()
  34. {
  35. if(mIsDirty)
  36. {
  37. updateMesh();
  38. mIsDirty = false;
  39. }
  40. return mNumMeshQuads;
  41. }
  42. Point Sprite::getAnchorOffset() const
  43. {
  44. switch(mAnchor)
  45. {
  46. case SA_TopLeft:
  47. return -Point(0, 0);
  48. case SA_TopCenter:
  49. return -Point(mWidth / 2, 0);
  50. case SA_TopRight:
  51. return -Point(mWidth, 0);
  52. case SA_MiddleLeft:
  53. return -Point(0, mHeight / 2);
  54. case SA_MiddleCenter:
  55. return -Point(mWidth / 2, mHeight / 2);
  56. case SA_MiddleRight:
  57. return -Point(mWidth, mHeight / 2);
  58. case SA_BottomLeft:
  59. return -Point(0, mHeight);
  60. case SA_BottomCenter:
  61. return -Point(mWidth / 2, mHeight);
  62. case SA_BottomRight:
  63. return -Point(mWidth, mHeight);
  64. }
  65. return Point();
  66. }
  67. void Sprite::clearMesh()
  68. {
  69. if(mVertices != nullptr)
  70. delete[] mVertices;
  71. if(mUVs != nullptr)
  72. delete[] mUVs;
  73. if(mIndexes != nullptr)
  74. delete[] mIndexes;
  75. mNumMeshQuads = 0;
  76. }
  77. }