TextureList.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. textures->add(t);
  37. names->add(new Text(name));
  38. lock.unlockWrite();
  39. return 1;
  40. }
  41. // Checks whether a texture has been stored under a specific name
  42. // name: The name
  43. // return: true if a texture with the name exists
  44. bool TextureList::hasTexture(const char* name) const
  45. {
  46. lock.lockRead();
  47. for (auto i : *names)
  48. {
  49. if (i->isEqual(name))
  50. {
  51. lock.unlockRead();
  52. return 1;
  53. }
  54. }
  55. lock.unlockRead();
  56. return 0;
  57. }
  58. // Returns a specific texture
  59. // name: The name of the texture
  60. Texture* TextureList::getTexture(const char* name) const
  61. {
  62. lock.lockRead();
  63. int index = 0;
  64. for (auto i : *names)
  65. {
  66. if (i->isEqual(name))
  67. {
  68. Texture* result = textures->get(index);
  69. lock.unlockRead();
  70. return result;
  71. }
  72. index++;
  73. }
  74. lock.unlockRead();
  75. return 0;
  76. }
  77. // Returns a specific texture
  78. // id: The id of the texture
  79. Texture* TextureList::getTexture(int id) const
  80. {
  81. lock.lockRead();
  82. for (auto i : *textures)
  83. {
  84. if (i->getId() == id)
  85. {
  86. Texture* result = dynamic_cast<Texture*>(i->getThis());
  87. lock.unlockRead();
  88. return result;
  89. }
  90. }
  91. lock.unlockRead();
  92. return 0;
  93. }
  94. // Returns a specific texture without increased reference counter
  95. // name: The name of the texture
  96. Texture* TextureList::zTexture(const char* name) const
  97. {
  98. lock.lockRead();
  99. int index = 0;
  100. for (auto i : *names)
  101. {
  102. if (i->isEqual(name))
  103. {
  104. Texture* result = textures->z(index);
  105. lock.unlockRead();
  106. return result;
  107. }
  108. index++;
  109. }
  110. lock.unlockRead();
  111. return 0;
  112. }
  113. // Returns a specific texture without increased reference counter
  114. // id: The id of the texture
  115. Texture* TextureList::zTexture(int id) const
  116. {
  117. lock.lockRead();
  118. for (auto i : *textures)
  119. {
  120. if (i->getId() == id)
  121. {
  122. lock.unlockRead();
  123. return i;
  124. }
  125. }
  126. lock.unlockRead();
  127. return 0;
  128. }
  129. const RCArray<Texture>* Framework::TextureList::zTextures() const
  130. {
  131. return textures;
  132. }