textureStageCollection.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Filename: textureStageCollection.cxx
  2. // Created by: drose (23Jul04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "textureStageCollection.h"
  15. #include "indent.h"
  16. #include "indirectLess.h"
  17. #include <algorithm>
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: TextureStageCollection::Constructor
  20. // Access: Published
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. TextureStageCollection::
  24. TextureStageCollection() {
  25. }
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: TextureStageCollection::Copy Constructor
  28. // Access: Published
  29. // Description:
  30. ////////////////////////////////////////////////////////////////////
  31. TextureStageCollection::
  32. TextureStageCollection(const TextureStageCollection &copy) :
  33. _texture_stages(copy._texture_stages)
  34. {
  35. }
  36. ////////////////////////////////////////////////////////////////////
  37. // Function: TextureStageCollection::Copy Assignment Operator
  38. // Access: Published
  39. // Description:
  40. ////////////////////////////////////////////////////////////////////
  41. void TextureStageCollection::
  42. operator = (const TextureStageCollection &copy) {
  43. _texture_stages = copy._texture_stages;
  44. }
  45. ////////////////////////////////////////////////////////////////////
  46. // Function: TextureStageCollection::add_texture_stage
  47. // Access: Published
  48. // Description: Adds a new TextureStage to the collection.
  49. ////////////////////////////////////////////////////////////////////
  50. void TextureStageCollection::
  51. add_texture_stage(TextureStage *node_texture_stage) {
  52. // If the pointer to our internal array is shared by any other
  53. // TextureStageCollections, we have to copy the array now so we won't
  54. // inadvertently modify any of our brethren TextureStageCollection
  55. // objects.
  56. if (_texture_stages.get_ref_count() > 1) {
  57. TextureStages old_texture_stages = _texture_stages;
  58. _texture_stages = TextureStages::empty_array(0);
  59. _texture_stages.v() = old_texture_stages.v();
  60. }
  61. _texture_stages.push_back(node_texture_stage);
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: TextureStageCollection::remove_texture_stage
  65. // Access: Published
  66. // Description: Removes the indicated TextureStage from the collection.
  67. // Returns true if the texture_stage was removed, false if it was
  68. // not a member of the collection.
  69. ////////////////////////////////////////////////////////////////////
  70. bool TextureStageCollection::
  71. remove_texture_stage(TextureStage *node_texture_stage) {
  72. int texture_stage_index = -1;
  73. for (int i = 0; texture_stage_index == -1 && i < (int)_texture_stages.size(); i++) {
  74. if (_texture_stages[i] == node_texture_stage) {
  75. texture_stage_index = i;
  76. }
  77. }
  78. if (texture_stage_index == -1) {
  79. // The indicated texture_stage was not a member of the collection.
  80. return false;
  81. }
  82. // If the pointer to our internal array is shared by any other
  83. // TextureStageCollections, we have to copy the array now so we won't
  84. // inadvertently modify any of our brethren TextureStageCollection
  85. // objects.
  86. if (_texture_stages.get_ref_count() > 1) {
  87. TextureStages old_texture_stages = _texture_stages;
  88. _texture_stages = TextureStages::empty_array(0);
  89. _texture_stages.v() = old_texture_stages.v();
  90. }
  91. _texture_stages.erase(_texture_stages.begin() + texture_stage_index);
  92. return true;
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function: TextureStageCollection::add_texture_stages_from
  96. // Access: Published
  97. // Description: Adds all the TextureStages indicated in the other
  98. // collection to this texture_stage. The other texture_stages are simply
  99. // appended to the end of the texture_stages in this list;
  100. // duplicates are not automatically removed.
  101. ////////////////////////////////////////////////////////////////////
  102. void TextureStageCollection::
  103. add_texture_stages_from(const TextureStageCollection &other) {
  104. int other_num_texture_stages = other.get_num_texture_stages();
  105. for (int i = 0; i < other_num_texture_stages; i++) {
  106. add_texture_stage(other.get_texture_stage(i));
  107. }
  108. }
  109. ////////////////////////////////////////////////////////////////////
  110. // Function: TextureStageCollection::remove_texture_stages_from
  111. // Access: Published
  112. // Description: Removes from this collection all of the TextureStages
  113. // listed in the other collection.
  114. ////////////////////////////////////////////////////////////////////
  115. void TextureStageCollection::
  116. remove_texture_stages_from(const TextureStageCollection &other) {
  117. TextureStages new_texture_stages;
  118. int num_texture_stages = get_num_texture_stages();
  119. for (int i = 0; i < num_texture_stages; i++) {
  120. PT(TextureStage) texture_stage = get_texture_stage(i);
  121. if (!other.has_texture_stage(texture_stage)) {
  122. new_texture_stages.push_back(texture_stage);
  123. }
  124. }
  125. _texture_stages = new_texture_stages;
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: TextureStageCollection::remove_duplicate_texture_stages
  129. // Access: Published
  130. // Description: Removes any duplicate entries of the same TextureStages
  131. // on this collection. If a TextureStage appears multiple
  132. // times, the first appearance is retained; subsequent
  133. // appearances are removed.
  134. ////////////////////////////////////////////////////////////////////
  135. void TextureStageCollection::
  136. remove_duplicate_texture_stages() {
  137. TextureStages new_texture_stages;
  138. int num_texture_stages = get_num_texture_stages();
  139. for (int i = 0; i < num_texture_stages; i++) {
  140. PT(TextureStage) texture_stage = get_texture_stage(i);
  141. bool duplicated = false;
  142. for (int j = 0; j < i && !duplicated; j++) {
  143. duplicated = (texture_stage == get_texture_stage(j));
  144. }
  145. if (!duplicated) {
  146. new_texture_stages.push_back(texture_stage);
  147. }
  148. }
  149. _texture_stages = new_texture_stages;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: TextureStageCollection::has_texture_stage
  153. // Access: Published
  154. // Description: Returns true if the indicated TextureStage appears in
  155. // this collection, false otherwise.
  156. ////////////////////////////////////////////////////////////////////
  157. bool TextureStageCollection::
  158. has_texture_stage(TextureStage *texture_stage) const {
  159. for (int i = 0; i < get_num_texture_stages(); i++) {
  160. if (texture_stage == get_texture_stage(i)) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. ////////////////////////////////////////////////////////////////////
  167. // Function: TextureStageCollection::clear
  168. // Access: Published
  169. // Description: Removes all TextureStages from the collection.
  170. ////////////////////////////////////////////////////////////////////
  171. void TextureStageCollection::
  172. clear() {
  173. _texture_stages.clear();
  174. }
  175. ////////////////////////////////////////////////////////////////////
  176. // Function: TextureStageCollection::find_texture_stage
  177. // Access: Published
  178. // Description: Returns the texture_stage in the collection with the
  179. // indicated name, if any, or NULL if no texture_stage has
  180. // that name.
  181. ////////////////////////////////////////////////////////////////////
  182. TextureStage *TextureStageCollection::
  183. find_texture_stage(const string &name) const {
  184. int num_texture_stages = get_num_texture_stages();
  185. for (int i = 0; i < num_texture_stages; i++) {
  186. TextureStage *texture_stage = get_texture_stage(i);
  187. if (texture_stage->get_name() == name) {
  188. return texture_stage;
  189. }
  190. }
  191. return NULL;
  192. }
  193. ////////////////////////////////////////////////////////////////////
  194. // Function: TextureStageCollection::get_num_texture_stages
  195. // Access: Published
  196. // Description: Returns the number of TextureStages in the collection.
  197. ////////////////////////////////////////////////////////////////////
  198. int TextureStageCollection::
  199. get_num_texture_stages() const {
  200. return _texture_stages.size();
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: TextureStageCollection::get_texture_stage
  204. // Access: Published
  205. // Description: Returns the nth TextureStage in the collection.
  206. ////////////////////////////////////////////////////////////////////
  207. TextureStage *TextureStageCollection::
  208. get_texture_stage(int index) const {
  209. nassertr(index >= 0 && index < (int)_texture_stages.size(), NULL);
  210. return _texture_stages[index];
  211. }
  212. ////////////////////////////////////////////////////////////////////
  213. // Function: TextureStageCollection::operator []
  214. // Access: Published
  215. // Description: Returns the nth TextureStage in the collection. This is
  216. // the same as get_texture_stage(), but it may be a more
  217. // convenient way to access it.
  218. ////////////////////////////////////////////////////////////////////
  219. TextureStage *TextureStageCollection::
  220. operator [] (int index) const {
  221. nassertr(index >= 0 && index < (int)_texture_stages.size(), NULL);
  222. return _texture_stages[index];
  223. }
  224. ////////////////////////////////////////////////////////////////////
  225. // Function: TextureStageCollection::size
  226. // Access: Published
  227. // Description: Returns the number of texture stages in the
  228. // collection. This is the same thing as
  229. // get_num_texture_stages().
  230. ////////////////////////////////////////////////////////////////////
  231. int TextureStageCollection::
  232. size() const {
  233. return _texture_stages.size();
  234. }
  235. ////////////////////////////////////////////////////////////////////
  236. // Function: TextureStageCollection::sort
  237. // Access: Published
  238. // Description: Sorts the TextureStages in this collection into order
  239. // by TextureStage::sort(), from lowest to highest.
  240. ////////////////////////////////////////////////////////////////////
  241. void TextureStageCollection::
  242. sort() {
  243. ::sort(_texture_stages.begin(), _texture_stages.end(),
  244. IndirectLess<TextureStage>());
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: TextureStageCollection::output
  248. // Access: Published
  249. // Description: Writes a brief one-line description of the
  250. // TextureStageCollection to the indicated output stream.
  251. ////////////////////////////////////////////////////////////////////
  252. void TextureStageCollection::
  253. output(ostream &out) const {
  254. if (get_num_texture_stages() == 1) {
  255. out << "1 TextureStage";
  256. } else {
  257. out << get_num_texture_stages() << " TextureStages";
  258. }
  259. }
  260. ////////////////////////////////////////////////////////////////////
  261. // Function: TextureStageCollection::write
  262. // Access: Published
  263. // Description: Writes a complete multi-line description of the
  264. // TextureStageCollection to the indicated output stream.
  265. ////////////////////////////////////////////////////////////////////
  266. void TextureStageCollection::
  267. write(ostream &out, int indent_level) const {
  268. for (int i = 0; i < get_num_texture_stages(); i++) {
  269. indent(out, indent_level) << *get_texture_stage(i) << "\n";
  270. }
  271. }