TextureList.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "TextureList.h"
  2. #include "Text.h"
  3. #include "Texture.h"
  4. using namespace Framework;
  5. // Contents of the TextureList class
  6. // Constructor
  7. TextureList::TextureList()
  8. : ReferenceCounter(),
  9. nextId(0)
  10. {
  11. textures = new RCArray<Texture>();
  12. names = new RCArray<Text>();
  13. }
  14. // Destructor
  15. TextureList::~TextureList()
  16. {
  17. textures->release();
  18. names->release();
  19. }
  20. // Adds a texture to the list
  21. // t: The texture
  22. // name: The name under which the texture is stored in the list
  23. bool TextureList::addTexture(Texture* t, const char* name)
  24. {
  25. lock.lockWrite();
  26. for (auto i : *names)
  27. {
  28. if (i->isEqual(name))
  29. {
  30. t->release();
  31. lock.unlockWrite();
  32. return 0;
  33. }
  34. }
  35. t->id = nextId++;
  36. t->setName(name);
  37. textures->add(t);
  38. names->add(new Text(name));
  39. lock.unlockWrite();
  40. return 1;
  41. }
  42. // Checks whether a texture has been stored under a specific name
  43. // name: The name
  44. // return: true if a texture with the name exists
  45. bool TextureList::hasTexture(const char* name) const
  46. {
  47. lock.lockRead();
  48. for (auto i : *names)
  49. {
  50. if (i->isEqual(name))
  51. {
  52. lock.unlockRead();
  53. return 1;
  54. }
  55. }
  56. lock.unlockRead();
  57. return 0;
  58. }
  59. // Returns a specific texture
  60. // name: The name of the texture
  61. Texture* TextureList::getTexture(const char* name) const
  62. {
  63. lock.lockRead();
  64. int index = 0;
  65. for (auto i : *names)
  66. {
  67. if (i->isEqual(name))
  68. {
  69. Texture* result = textures->get(index);
  70. lock.unlockRead();
  71. return result;
  72. }
  73. index++;
  74. }
  75. lock.unlockRead();
  76. return 0;
  77. }
  78. // Returns a specific texture
  79. // id: The id of the texture
  80. Texture* TextureList::getTexture(int id) const
  81. {
  82. lock.lockRead();
  83. for (auto i : *textures)
  84. {
  85. if (i->getId() == id)
  86. {
  87. Texture* result = dynamic_cast<Texture*>(i->getThis());
  88. lock.unlockRead();
  89. return result;
  90. }
  91. }
  92. lock.unlockRead();
  93. return 0;
  94. }
  95. // Returns a specific texture without increased reference counter
  96. // name: The name of the texture
  97. Texture* TextureList::zTexture(const char* name) const
  98. {
  99. lock.lockRead();
  100. int index = 0;
  101. for (auto i : *names)
  102. {
  103. if (i->isEqual(name))
  104. {
  105. Texture* result = textures->z(index);
  106. lock.unlockRead();
  107. return result;
  108. }
  109. index++;
  110. }
  111. lock.unlockRead();
  112. return 0;
  113. }
  114. // Returns a specific texture without increased reference counter
  115. // id: The id of the texture
  116. Texture* TextureList::zTexture(int id) const
  117. {
  118. lock.lockRead();
  119. for (auto i : *textures)
  120. {
  121. if (i->getId() == id)
  122. {
  123. lock.unlockRead();
  124. return i;
  125. }
  126. }
  127. lock.unlockRead();
  128. return 0;
  129. }
  130. const RCArray<Texture>* Framework::TextureList::zTextures() const
  131. {
  132. return textures;
  133. }