TextureList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "TextureList.h"
  2. #include "Text.h"
  3. #include "Texture.h"
  4. using namespace Framework;
  5. int TextureList::id = 0;
  6. Critical TextureList::cs;
  7. // Contents of the TextureList class
  8. // Constructor
  9. TextureList::TextureList()
  10. : ReferenceCounter()
  11. {
  12. textures = new RCArray<Texture>();
  13. names = new RCArray<Text>();
  14. }
  15. // Destructor
  16. TextureList::~TextureList()
  17. {
  18. textures->release();
  19. names->release();
  20. }
  21. // Deletes all textures
  22. __declspec(dllexport) void TextureList::clear()
  23. {
  24. cs.lock();
  25. textures->clear();
  26. names->clear();
  27. cs.unlock();
  28. }
  29. // Adds a texture to the list
  30. // t: The texture
  31. // name: The name under which the texture is stored in the list
  32. bool TextureList::addTexture(Texture* t, const char* name)
  33. {
  34. cs.lock();
  35. for (auto i : *names)
  36. {
  37. if (i->isEqual(name))
  38. {
  39. t->release();
  40. cs.unlock();
  41. return 0;
  42. }
  43. }
  44. t->id = id++;
  45. textures->add(t);
  46. names->add(new Text(name));
  47. cs.unlock();
  48. return 1;
  49. }
  50. // Removes a texture from the list
  51. // name: The name of the texture
  52. void TextureList::removeTexture(const char* name)
  53. {
  54. cs.lock();
  55. int index = 0;
  56. for (auto i : *names)
  57. {
  58. if (i->isEqual(name))
  59. {
  60. names->remove(index);
  61. textures->remove(index);
  62. cs.unlock();
  63. return;
  64. }
  65. index++;
  66. }
  67. cs.unlock();
  68. }
  69. // Checks whether a texture has been stored under a specific name
  70. // name: The name
  71. // return: true if a texture with the name exists
  72. bool TextureList::hasTexture(const char* name) const
  73. {
  74. cs.lock();
  75. for (auto i : *names)
  76. {
  77. if (i->isEqual(name))
  78. {
  79. cs.unlock();
  80. return 1;
  81. }
  82. }
  83. cs.unlock();
  84. return 0;
  85. }
  86. // Returns a specific texture
  87. // name: The name of the texture
  88. Texture* TextureList::getTexture(const char* name) const
  89. {
  90. cs.lock();
  91. int index = 0;
  92. for (auto i : *names)
  93. {
  94. if (i->isEqual(name))
  95. {
  96. cs.unlock();
  97. return textures->get(index);
  98. }
  99. index++;
  100. }
  101. cs.unlock();
  102. return 0;
  103. }
  104. // Returns a specific texture
  105. // id: The id of the texture
  106. Texture* TextureList::getTexture(int id) const
  107. {
  108. cs.lock();
  109. for (auto i : *textures)
  110. {
  111. if (i->getId() == id)
  112. {
  113. cs.unlock();
  114. return dynamic_cast<Texture*>(i->getThis());
  115. }
  116. }
  117. cs.unlock();
  118. return 0;
  119. }
  120. // Returns a specific texture without increased reference counter
  121. // name: The name of the texture
  122. Texture* TextureList::zTexture(const char* name) const
  123. {
  124. cs.lock();
  125. int index = 0;
  126. for (auto i : *names)
  127. {
  128. if (i->isEqual(name))
  129. {
  130. cs.unlock();
  131. return textures->z(index);
  132. }
  133. index++;
  134. }
  135. cs.unlock();
  136. return 0;
  137. }
  138. // Returns a specific texture without increased reference counter
  139. // id: The id of the texture
  140. Texture* TextureList::zTexture(int id) const
  141. {
  142. cs.lock();
  143. for (auto i : *textures)
  144. {
  145. if (i->getId() == id)
  146. {
  147. cs.unlock();
  148. return i;
  149. }
  150. }
  151. cs.unlock();
  152. return 0;
  153. }
  154. // static functions
  155. // Initializes static private members. Called automatically by the framework.
  156. void TextureList::init()
  157. {
  158. id = 0;
  159. }
  160. // Deletes static private members. Called automatically by the framework.
  161. void TextureList::destroy() {}