TextureList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Deletes all textures
  21. __declspec(dllexport) void TextureList::clear()
  22. {
  23. lock.lockWrite();
  24. textures->clear();
  25. names->clear();
  26. lock.unlockWrite();
  27. }
  28. // Adds a texture to the list
  29. // t: The texture
  30. // name: The name under which the texture is stored in the list
  31. bool TextureList::addTexture(Texture* t, const char* name)
  32. {
  33. lock.lockWrite();
  34. for (auto i : *names)
  35. {
  36. if (i->isEqual(name))
  37. {
  38. t->release();
  39. lock.unlockWrite();
  40. return 0;
  41. }
  42. }
  43. t->id = nextId++;
  44. textures->add(t);
  45. names->add(new Text(name));
  46. lock.unlockWrite();
  47. return 1;
  48. }
  49. // Removes a texture from the list
  50. // name: The name of the texture
  51. void TextureList::removeTexture(const char* name)
  52. {
  53. lock.lockWrite();
  54. int index = 0;
  55. for (auto i : *names)
  56. {
  57. if (i->isEqual(name))
  58. {
  59. names->remove(index);
  60. textures->remove(index);
  61. lock.unlockWrite();
  62. return;
  63. }
  64. index++;
  65. }
  66. lock.unlockWrite();
  67. }
  68. // Checks whether a texture has been stored under a specific name
  69. // name: The name
  70. // return: true if a texture with the name exists
  71. bool TextureList::hasTexture(const char* name) const
  72. {
  73. lock.lockRead();
  74. for (auto i : *names)
  75. {
  76. if (i->isEqual(name))
  77. {
  78. lock.unlockRead();
  79. return 1;
  80. }
  81. }
  82. lock.unlockRead();
  83. return 0;
  84. }
  85. // Returns a specific texture
  86. // name: The name of the texture
  87. Texture* TextureList::getTexture(const char* name) const
  88. {
  89. lock.lockRead();
  90. int index = 0;
  91. for (auto i : *names)
  92. {
  93. if (i->isEqual(name))
  94. {
  95. Texture* result = textures->get(index);
  96. lock.unlockRead();
  97. return result;
  98. }
  99. index++;
  100. }
  101. lock.unlockRead();
  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. lock.lockRead();
  109. for (auto i : *textures)
  110. {
  111. if (i->getId() == id)
  112. {
  113. Texture* result = dynamic_cast<Texture*>(i->getThis());
  114. lock.unlockRead();
  115. return result;
  116. }
  117. }
  118. lock.unlockRead();
  119. return 0;
  120. }
  121. // Returns a specific texture without increased reference counter
  122. // name: The name of the texture
  123. Texture* TextureList::zTexture(const char* name) const
  124. {
  125. lock.lockRead();
  126. int index = 0;
  127. for (auto i : *names)
  128. {
  129. if (i->isEqual(name))
  130. {
  131. Texture* result = textures->z(index);
  132. lock.unlockRead();
  133. return result;
  134. }
  135. index++;
  136. }
  137. lock.unlockRead();
  138. return 0;
  139. }
  140. // Returns a specific texture without increased reference counter
  141. // id: The id of the texture
  142. Texture* TextureList::zTexture(int id) const
  143. {
  144. lock.lockRead();
  145. for (auto i : *textures)
  146. {
  147. if (i->getId() == id)
  148. {
  149. lock.unlockRead();
  150. return i;
  151. }
  152. }
  153. lock.unlockRead();
  154. return 0;
  155. }