00001 // Filename: textureCollection.cxx 00002 // Created by: drose (16Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "textureCollection.h" 00020 00021 #include "indent.h" 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: TextureCollection::Constructor 00025 // Access: Published 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 TextureCollection:: 00029 TextureCollection() { 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: TextureCollection::Copy Constructor 00034 // Access: Published 00035 // Description: 00036 //////////////////////////////////////////////////////////////////// 00037 TextureCollection:: 00038 TextureCollection(const TextureCollection ©) : 00039 _textures(copy._textures) 00040 { 00041 } 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: TextureCollection::Copy Assignment Operator 00045 // Access: Published 00046 // Description: 00047 //////////////////////////////////////////////////////////////////// 00048 void TextureCollection:: 00049 operator = (const TextureCollection ©) { 00050 _textures = copy._textures; 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: TextureCollection::add_texture 00055 // Access: Published 00056 // Description: Adds a new Texture to the collection. 00057 //////////////////////////////////////////////////////////////////// 00058 void TextureCollection:: 00059 add_texture(Texture *node_texture) { 00060 // If the pointer to our internal array is shared by any other 00061 // TextureCollections, we have to copy the array now so we won't 00062 // inadvertently modify any of our brethren TextureCollection 00063 // objects. 00064 00065 if (_textures.get_ref_count() > 1) { 00066 Textures old_textures = _textures; 00067 _textures = Textures::empty_array(0); 00068 _textures.v() = old_textures.v(); 00069 } 00070 00071 _textures.push_back(node_texture); 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: TextureCollection::remove_texture 00076 // Access: Published 00077 // Description: Removes the indicated Texture from the collection. 00078 // Returns true if the texture was removed, false if it was 00079 // not a member of the collection. 00080 //////////////////////////////////////////////////////////////////// 00081 bool TextureCollection:: 00082 remove_texture(Texture *node_texture) { 00083 int texture_index = -1; 00084 for (int i = 0; texture_index == -1 && i < (int)_textures.size(); i++) { 00085 if (_textures[i] == node_texture) { 00086 texture_index = i; 00087 } 00088 } 00089 00090 if (texture_index == -1) { 00091 // The indicated texture was not a member of the collection. 00092 return false; 00093 } 00094 00095 // If the pointer to our internal array is shared by any other 00096 // TextureCollections, we have to copy the array now so we won't 00097 // inadvertently modify any of our brethren TextureCollection 00098 // objects. 00099 00100 if (_textures.get_ref_count() > 1) { 00101 Textures old_textures = _textures; 00102 _textures = Textures::empty_array(0); 00103 _textures.v() = old_textures.v(); 00104 } 00105 00106 _textures.erase(_textures.begin() + texture_index); 00107 return true; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: TextureCollection::add_textures_from 00112 // Access: Published 00113 // Description: Adds all the Textures indicated in the other 00114 // collection to this texture. The other textures are simply 00115 // appended to the end of the textures in this list; 00116 // duplicates are not automatically removed. 00117 //////////////////////////////////////////////////////////////////// 00118 void TextureCollection:: 00119 add_textures_from(const TextureCollection &other) { 00120 int other_num_textures = other.get_num_textures(); 00121 for (int i = 0; i < other_num_textures; i++) { 00122 add_texture(other.get_texture(i)); 00123 } 00124 } 00125 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: TextureCollection::remove_textures_from 00129 // Access: Published 00130 // Description: Removes from this collection all of the Textures 00131 // listed in the other collection. 00132 //////////////////////////////////////////////////////////////////// 00133 void TextureCollection:: 00134 remove_textures_from(const TextureCollection &other) { 00135 Textures new_textures; 00136 int num_textures = get_num_textures(); 00137 for (int i = 0; i < num_textures; i++) { 00138 PT(Texture) texture = get_texture(i); 00139 if (!other.has_texture(texture)) { 00140 new_textures.push_back(texture); 00141 } 00142 } 00143 _textures = new_textures; 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function: TextureCollection::remove_duplicate_textures 00148 // Access: Published 00149 // Description: Removes any duplicate entries of the same Textures 00150 // on this collection. If a Texture appears multiple 00151 // times, the first appearance is retained; subsequent 00152 // appearances are removed. 00153 //////////////////////////////////////////////////////////////////// 00154 void TextureCollection:: 00155 remove_duplicate_textures() { 00156 Textures new_textures; 00157 00158 int num_textures = get_num_textures(); 00159 for (int i = 0; i < num_textures; i++) { 00160 PT(Texture) texture = get_texture(i); 00161 bool duplicated = false; 00162 00163 for (int j = 0; j < i && !duplicated; j++) { 00164 duplicated = (texture == get_texture(j)); 00165 } 00166 00167 if (!duplicated) { 00168 new_textures.push_back(texture); 00169 } 00170 } 00171 00172 _textures = new_textures; 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function: TextureCollection::has_texture 00177 // Access: Published 00178 // Description: Returns true if the indicated Texture appears in 00179 // this collection, false otherwise. 00180 //////////////////////////////////////////////////////////////////// 00181 bool TextureCollection:: 00182 has_texture(Texture *texture) const { 00183 for (int i = 0; i < get_num_textures(); i++) { 00184 if (texture == get_texture(i)) { 00185 return true; 00186 } 00187 } 00188 return false; 00189 } 00190 00191 //////////////////////////////////////////////////////////////////// 00192 // Function: TextureCollection::clear 00193 // Access: Published 00194 // Description: Removes all Textures from the collection. 00195 //////////////////////////////////////////////////////////////////// 00196 void TextureCollection:: 00197 clear() { 00198 _textures.clear(); 00199 } 00200 00201 //////////////////////////////////////////////////////////////////// 00202 // Function: TextureCollection::find_texture 00203 // Access: Published 00204 // Description: Returns the texture in the collection with the 00205 // indicated name, if any, or NULL if no texture has 00206 // that name. 00207 //////////////////////////////////////////////////////////////////// 00208 Texture *TextureCollection:: 00209 find_texture(const string &name) const { 00210 int num_textures = get_num_textures(); 00211 for (int i = 0; i < num_textures; i++) { 00212 Texture *texture = get_texture(i); 00213 if (texture->get_name() == name) { 00214 return texture; 00215 } 00216 } 00217 return NULL; 00218 } 00219 00220 //////////////////////////////////////////////////////////////////// 00221 // Function: TextureCollection::get_num_textures 00222 // Access: Published 00223 // Description: Returns the number of Textures in the collection. 00224 //////////////////////////////////////////////////////////////////// 00225 int TextureCollection:: 00226 get_num_textures() const { 00227 return _textures.size(); 00228 } 00229 00230 //////////////////////////////////////////////////////////////////// 00231 // Function: TextureCollection::get_texture 00232 // Access: Published 00233 // Description: Returns the nth Texture in the collection. 00234 //////////////////////////////////////////////////////////////////// 00235 Texture *TextureCollection:: 00236 get_texture(int index) const { 00237 nassertr(index >= 0 && index < (int)_textures.size(), NULL); 00238 00239 return _textures[index]; 00240 } 00241 00242 //////////////////////////////////////////////////////////////////// 00243 // Function: TextureCollection::operator [] 00244 // Access: Published 00245 // Description: Returns the nth Texture in the collection. This is 00246 // the same as get_texture(), but it may be a more 00247 // convenient way to access it. 00248 //////////////////////////////////////////////////////////////////// 00249 Texture *TextureCollection:: 00250 operator [] (int index) const { 00251 nassertr(index >= 0 && index < (int)_textures.size(), NULL); 00252 00253 return _textures[index]; 00254 } 00255 00256 //////////////////////////////////////////////////////////////////// 00257 // Function: TextureCollection::output 00258 // Access: Published 00259 // Description: Writes a brief one-line description of the 00260 // TextureCollection to the indicated output stream. 00261 //////////////////////////////////////////////////////////////////// 00262 void TextureCollection:: 00263 output(ostream &out) const { 00264 if (get_num_textures() == 1) { 00265 out << "1 Texture"; 00266 } else { 00267 out << get_num_textures() << " Textures"; 00268 } 00269 } 00270 00271 //////////////////////////////////////////////////////////////////// 00272 // Function: TextureCollection::write 00273 // Access: Published 00274 // Description: Writes a complete multi-line description of the 00275 // TextureCollection to the indicated output stream. 00276 //////////////////////////////////////////////////////////////////// 00277 void TextureCollection:: 00278 write(ostream &out, int indent_level) const { 00279 for (int i = 0; i < get_num_textures(); i++) { 00280 indent(out, indent_level) << *get_texture(i) << "\n"; 00281 } 00282 }