Panda3D
 All Classes Functions Variables Enumerations
textureCollection.cxx
1 // Filename: textureCollection.cxx
2 // Created by: drose (16Mar02)
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 
15 #include "textureCollection.h"
16 #include "indent.h"
17 
18 #ifdef HAVE_PYTHON
19 #include "py_panda.h"
20 #endif
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: TextureCollection::Constructor
24 // Access: Published
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 TextureCollection::
28 TextureCollection() {
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: TextureCollection::Copy Constructor
33 // Access: Published
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 TextureCollection::
37 TextureCollection(const TextureCollection &copy) :
38  _textures(copy._textures)
39 {
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: TextureCollection::Copy Assignment Operator
44 // Access: Published
45 // Description:
46 ////////////////////////////////////////////////////////////////////
47 void TextureCollection::
48 operator = (const TextureCollection &copy) {
49  _textures = copy._textures;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: TextureCollection::add_texture
54 // Access: Published
55 // Description: Adds a new Texture to the collection.
56 ////////////////////////////////////////////////////////////////////
58 add_texture(Texture *texture) {
59  // If the pointer to our internal array is shared by any other
60  // TextureCollections, we have to copy the array now so we won't
61  // inadvertently modify any of our brethren TextureCollection
62  // objects.
63 
64  if (_textures.get_ref_count() > 1) {
65  Textures old_textures = _textures;
66  _textures = Textures::empty_array(0);
67  _textures.v() = old_textures.v();
68  }
69 
70  _textures.push_back(texture);
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: TextureCollection::remove_texture
75 // Access: Published
76 // Description: Removes the indicated Texture from the collection.
77 // Returns true if the texture was removed, false if it was
78 // not a member of the collection.
79 ////////////////////////////////////////////////////////////////////
82  int texture_index = -1;
83  for (int i = 0; texture_index == -1 && i < (int)_textures.size(); i++) {
84  if (_textures[i] == texture) {
85  texture_index = i;
86  }
87  }
88 
89  if (texture_index == -1) {
90  // The indicated texture was not a member of the collection.
91  return false;
92  }
93 
94  // If the pointer to our internal array is shared by any other
95  // TextureCollections, we have to copy the array now so we won't
96  // inadvertently modify any of our brethren TextureCollection
97  // objects.
98 
99  if (_textures.get_ref_count() > 1) {
100  Textures old_textures = _textures;
101  _textures = Textures::empty_array(0);
102  _textures.v() = old_textures.v();
103  }
104 
105  _textures.erase(_textures.begin() + texture_index);
106  return true;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: TextureCollection::add_textures_from
111 // Access: Published
112 // Description: Adds all the Textures indicated in the other
113 // collection to this texture. The other textures are simply
114 // appended to the end of the textures in this list;
115 // duplicates are not automatically removed.
116 ////////////////////////////////////////////////////////////////////
119  int other_num_textures = other.get_num_textures();
120  for (int i = 0; i < other_num_textures; i++) {
121  add_texture(other.get_texture(i));
122  }
123 }
124 
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: TextureCollection::remove_textures_from
128 // Access: Published
129 // Description: Removes from this collection all of the Textures
130 // listed in the other collection.
131 ////////////////////////////////////////////////////////////////////
134  Textures new_textures;
135  int num_textures = get_num_textures();
136  for (int i = 0; i < num_textures; i++) {
137  PT(Texture) texture = get_texture(i);
138  if (!other.has_texture(texture)) {
139  new_textures.push_back(texture);
140  }
141  }
142  _textures = new_textures;
143 }
144 
145 ////////////////////////////////////////////////////////////////////
146 // Function: TextureCollection::remove_duplicate_textures
147 // Access: Published
148 // Description: Removes any duplicate entries of the same Textures
149 // on this collection. If a Texture appears multiple
150 // times, the first appearance is retained; subsequent
151 // appearances are removed.
152 ////////////////////////////////////////////////////////////////////
155  Textures new_textures;
156 
157  int num_textures = get_num_textures();
158  for (int i = 0; i < num_textures; i++) {
159  PT(Texture) texture = get_texture(i);
160  bool duplicated = false;
161 
162  for (int j = 0; j < i && !duplicated; j++) {
163  duplicated = (texture == get_texture(j));
164  }
165 
166  if (!duplicated) {
167  new_textures.push_back(texture);
168  }
169  }
170 
171  _textures = new_textures;
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: TextureCollection::has_texture
176 // Access: Published
177 // Description: Returns true if the indicated Texture appears in
178 // this collection, false otherwise.
179 ////////////////////////////////////////////////////////////////////
181 has_texture(Texture *texture) const {
182  for (int i = 0; i < get_num_textures(); i++) {
183  if (texture == get_texture(i)) {
184  return true;
185  }
186  }
187  return false;
188 }
189 
190 ////////////////////////////////////////////////////////////////////
191 // Function: TextureCollection::clear
192 // Access: Published
193 // Description: Removes all Textures from the collection.
194 ////////////////////////////////////////////////////////////////////
196 clear() {
197  _textures.clear();
198 }
199 
200 ////////////////////////////////////////////////////////////////////
201 // Function: TextureCollection::reserve
202 // Access: Published
203 // Description: This is a hint to Panda to allocate enough memory
204 // to hold the given number of NodePaths, if you know
205 // ahead of time how many you will be adding.
206 ////////////////////////////////////////////////////////////////////
208 reserve(size_t num) {
209  _textures.reserve(num);
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: TextureCollection::find_texture
214 // Access: Published
215 // Description: Returns the texture in the collection with the
216 // indicated name, if any, or NULL if no texture has
217 // that name.
218 ////////////////////////////////////////////////////////////////////
220 find_texture(const string &name) const {
221  int num_textures = get_num_textures();
222  for (int i = 0; i < num_textures; i++) {
223  Texture *texture = get_texture(i);
224  if (texture->get_name() == name) {
225  return texture;
226  }
227  }
228  return NULL;
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: TextureCollection::get_num_textures
233 // Access: Published
234 // Description: Returns the number of Textures in the collection.
235 ////////////////////////////////////////////////////////////////////
238  return _textures.size();
239 }
240 
241 ////////////////////////////////////////////////////////////////////
242 // Function: TextureCollection::get_texture
243 // Access: Published
244 // Description: Returns the nth Texture in the collection.
245 ////////////////////////////////////////////////////////////////////
247 get_texture(int index) const {
248  nassertr(index >= 0 && index < (int)_textures.size(), NULL);
249 
250  return _textures[index];
251 }
252 
253 ////////////////////////////////////////////////////////////////////
254 // Function: TextureCollection::operator []
255 // Access: Published
256 // Description: Returns the nth Texture in the collection. This is
257 // the same as get_texture(), but it may be a more
258 // convenient way to access it.
259 ////////////////////////////////////////////////////////////////////
261 operator [] (int index) const {
262  nassertr(index >= 0 && index < (int)_textures.size(), NULL);
263 
264  return _textures[index];
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: TextureCollection::size
269 // Access: Published
270 // Description: Returns the number of textures in the collection. This
271 // is the same thing as get_num_textures().
272 ////////////////////////////////////////////////////////////////////
274 size() const {
275  return _textures.size();
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: TextureCollection::output
280 // Access: Published
281 // Description: Writes a brief one-line description of the
282 // TextureCollection to the indicated output stream.
283 ////////////////////////////////////////////////////////////////////
285 output(ostream &out) const {
286  if (get_num_textures() == 1) {
287  out << "1 Texture";
288  } else {
289  out << get_num_textures() << " Textures";
290  }
291 }
292 
293 ////////////////////////////////////////////////////////////////////
294 // Function: TextureCollection::write
295 // Access: Published
296 // Description: Writes a complete multi-line description of the
297 // TextureCollection to the indicated output stream.
298 ////////////////////////////////////////////////////////////////////
300 write(ostream &out, int indent_level) const {
301  for (int i = 0; i < get_num_textures(); i++) {
302  indent(out, indent_level) << *get_texture(i) << "\n";
303  }
304 }
Texture * find_texture(const string &name) const
Returns the texture in the collection with the indicated name, if any, or NULL if no texture has that...
void reserve(size_t num)
This is a hint to Panda to allocate enough memory to hold the given number of NodePaths, if you know ahead of time how many you will be adding.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
void output(ostream &out) const
Writes a brief one-line description of the TextureCollection to the indicated output stream...
void add_texture(Texture *texture)
Adds a new Texture to the collection.
bool has_texture(Texture *texture) const
Returns true if the indicated Texture appears in this collection, false otherwise.
void write(ostream &out, int indent_level=0) const
Writes a complete multi-line description of the TextureCollection to the indicated output stream...
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
void clear()
Removes all Textures from the collection.
void add_textures_from(const TextureCollection &other)
Adds all the Textures indicated in the other collection to this texture.
void remove_textures_from(const TextureCollection &other)
Removes from this collection all of the Textures listed in the other collection.
bool remove_texture(Texture *texture)
Removes the indicated Texture from the collection.
void remove_duplicate_textures()
Removes any duplicate entries of the same Textures on this collection.
Texture * get_texture(int index) const
Returns the nth Texture in the collection.
int get_num_textures() const
Returns the number of Textures in the collection.
int size() const
Returns the number of textures in the collection.
Texture * operator[](int index) const
Returns the nth Texture in the collection.