CmGUIMaterialManager.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "CmGUIMaterialManager.h"
  2. #include "CmMaterial.h"
  3. #include "CmDebug.h"
  4. #include "CmBuiltinMaterialManager.h"
  5. namespace CamelotEngine
  6. {
  7. const MaterialHandle& GUIMaterialManager::requestTextMaterial(const TextureHandle& texture) const
  8. {
  9. for(auto& matHandle : mTextMaterials)
  10. {
  11. const MaterialHandle& material = matHandle.handle;
  12. if(material->getTexture("mainTexture") == texture)
  13. {
  14. matHandle.refCount++;
  15. return material;
  16. }
  17. }
  18. mTextMaterials.push_back(GUIMaterial());
  19. GUIMaterial& material = mTextMaterials[mTextMaterials.size() - 1];
  20. material.handle = BuiltinMaterialManager::instance().createSpriteTextMaterial();
  21. material.handle->setTexture("mainTexture", texture);
  22. material.refCount = 1;
  23. return material.handle;
  24. }
  25. const MaterialHandle& GUIMaterialManager::requestImageMaterial(const TextureHandle& texture) const
  26. {
  27. for(auto& matHandle : mImageMaterials)
  28. {
  29. const MaterialHandle& material = matHandle.handle;
  30. if(material->getTexture("mainTexture") == texture)
  31. {
  32. matHandle.refCount++;
  33. return material;
  34. }
  35. }
  36. mImageMaterials.push_back(GUIMaterial());
  37. GUIMaterial& material = mImageMaterials[mImageMaterials.size() - 1];
  38. material.handle = BuiltinMaterialManager::instance().createSpriteImageMaterial();
  39. material.handle->setTexture("mainTexture", texture);
  40. material.refCount = 1;
  41. return material.handle;
  42. }
  43. void GUIMaterialManager::releaseMaterial(const MaterialHandle& material) const
  44. {
  45. bool released = false;
  46. UINT32 i = 0;
  47. for(auto& matHandle : mTextMaterials)
  48. {
  49. if(matHandle.handle == material)
  50. {
  51. if(--matHandle.refCount == 0)
  52. {
  53. mTextMaterials.erase(mTextMaterials.begin() + i);
  54. released = true;
  55. break;
  56. }
  57. }
  58. i++;
  59. }
  60. i = 0;
  61. for(auto& matHandle : mImageMaterials)
  62. {
  63. if(matHandle.handle == material)
  64. {
  65. if(--matHandle.refCount == 0)
  66. {
  67. mImageMaterials.erase(mImageMaterials.begin() + i);
  68. released = true;
  69. break;
  70. }
  71. }
  72. i++;
  73. }
  74. if(!released)
  75. {
  76. LOGWRN("Trying to release a material that doesn't exist in the GUI Manager. Material ID: " + toString(material->getInternalID()));
  77. }
  78. }
  79. }