CmSprite.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. UINT32 startVert = startingQuad * 4;
  16. UINT32 startIndex = startingQuad * 4;
  17. UINT32 maxVertIdx = maxNumQuads * 4;
  18. UINT32 maxIndexIdx = maxNumQuads * 6;
  19. UINT32 mNumVertices = mNumMeshQuads * 4;
  20. UINT32 mNumIndices = mNumMeshQuads * 6;
  21. assert((startVert + mNumVertices) < maxVertIdx);
  22. assert((startIndex + mNumIndices) < maxIndexIdx);
  23. memcpy(&vertices[startVert], mVertices, mNumVertices * sizeof(Vector2));
  24. memcpy(&uv[startVert], mUVs, mNumVertices * sizeof(Vector2));
  25. memcpy(&indices[startIndex], mIndexes, mNumVertices * sizeof(UINT32));
  26. return mNumMeshQuads;
  27. }
  28. Point Sprite::getAnchorOffset() const
  29. {
  30. switch(mAnchor)
  31. {
  32. case SA_TopLeft:
  33. return -Point(0, 0);
  34. case SA_TopCenter:
  35. return -Point(mWidth / 2, 0);
  36. case SA_TopRight:
  37. return -Point(mWidth, 0);
  38. case SA_MiddleLeft:
  39. return -Point(0, mHeight / 2);
  40. case SA_MiddleCenter:
  41. return -Point(mWidth / 2, mHeight / 2);
  42. case SA_MiddleRight:
  43. return -Point(mWidth, mHeight / 2);
  44. case SA_BottomLeft:
  45. return -Point(0, mHeight);
  46. case SA_BottomCenter:
  47. return -Point(mWidth / 2, mHeight);
  48. case SA_BottomRight:
  49. return -Point(mWidth, mHeight);
  50. }
  51. return Point();
  52. }
  53. void Sprite::clearMesh()
  54. {
  55. if(mVertices != nullptr)
  56. delete[] mVertices;
  57. if(mUVs != nullptr)
  58. delete[] mUVs;
  59. if(mIndexes != nullptr)
  60. delete[] mIndexes;
  61. mNumMeshQuads = 0;
  62. }
  63. }