CmTestTextSprite.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "CmTestTextSprite.h"
  2. #include "CmGameObject.h"
  3. #include "CmRenderable.h"
  4. #include "CmMesh.h"
  5. #include "CmVector2.h"
  6. #include "CmTextSprite.h"
  7. #include "CmFont.h"
  8. #include "CmMaterial.h"
  9. namespace CamelotEngine
  10. {
  11. TestTextSprite::TestTextSprite(GameObjectPtr parent)
  12. :Component(parent), mTextSprite(nullptr)
  13. {
  14. mTextRenderable = gameObject()->addComponent<Renderable>();
  15. mTextMesh = Mesh::create();
  16. mTextSprite = new TextSprite();
  17. }
  18. TestTextSprite::~TestTextSprite()
  19. {
  20. if(mTextSprite != nullptr)
  21. delete mTextSprite;
  22. }
  23. void TestTextSprite::setText(const String& text, FontHandle font, UINT32 fontSize, MaterialHandle textMaterial)
  24. {
  25. mTextSprite->setText(text);
  26. mTextSprite->setFont(font.getInternalPtr(), fontSize);
  27. UINT32 numTextFaces = mTextSprite->getNumQuads(0);
  28. UINT32 numVertices = numTextFaces * 4;
  29. UINT32 numIndices = numTextFaces * 6;
  30. std::shared_ptr<MeshData> textData(new MeshData());
  31. auto indices = new UINT32[numIndices];
  32. auto vertices = new Vector3[numVertices];
  33. auto uvs = new Vector2[numVertices];
  34. auto vec2Buffer = new Vector2[numVertices];
  35. mTextSprite->fillBuffer(vec2Buffer, uvs, indices, 0, numTextFaces, 0);
  36. for(UINT32 i = 0; i < numVertices; i++)
  37. vertices[i] = Vector3(vec2Buffer[i].x, vec2Buffer[i].y, 0.0f);
  38. delete[] vec2Buffer;
  39. textData->setPositions(vertices, numVertices);
  40. textData->setUV0(uvs, numVertices);
  41. textData->setIndices(indices, numIndices);
  42. mTextMesh->setMeshData(textData);
  43. mTextRenderable->setMesh(mTextMesh);
  44. UINT32 nearestSize = font->getClosestAvailableSize(12);
  45. const FontData* fontData = font->getFontDataForSize(nearestSize);
  46. TextureHandle texturePage = fontData->texturePages[0]; // TODO - This won't work if font uses multiple pages
  47. textMaterial->setTexture("tex", texturePage);
  48. mTextRenderable->setMaterial(textMaterial);
  49. }
  50. void TestTextSprite::update()
  51. {
  52. }
  53. }