Panda3D
Loading...
Searching...
No Matches
textureCollection.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file textureCollection.cxx
10 * @author drose
11 * @date 2002-03-16
12 */
13
14#include "textureCollection.h"
15#include "indent.h"
16
17/**
18 *
19 */
20TextureCollection::
21TextureCollection() {
22}
23
24/**
25 *
26 */
27TextureCollection::
28TextureCollection(const TextureCollection &copy) :
29 _textures(copy._textures)
30{
31}
32
33/**
34 *
35 */
36void TextureCollection::
37operator = (const TextureCollection &copy) {
38 _textures = copy._textures;
39}
40
41/**
42 * Adds a new Texture to the collection.
43 */
45add_texture(Texture *texture) {
46 // If the pointer to our internal array is shared by any other
47 // TextureCollections, we have to copy the array now so we won't
48 // inadvertently modify any of our brethren TextureCollection objects.
49
50 if (_textures.get_ref_count() > 1) {
51 Textures old_textures = _textures;
52 _textures = Textures::empty_array(0);
53 _textures.v() = old_textures.v();
54 }
55
56 _textures.push_back(texture);
57}
58
59/**
60 * Removes the indicated Texture from the collection. Returns true if the
61 * texture was removed, false if it was not a member of the collection.
62 */
64remove_texture(Texture *texture) {
65 int texture_index = -1;
66 for (int i = 0; texture_index == -1 && i < (int)_textures.size(); i++) {
67 if (_textures[i] == texture) {
68 texture_index = i;
69 }
70 }
71
72 if (texture_index == -1) {
73 // The indicated texture was not a member of the collection.
74 return false;
75 }
76
77 // If the pointer to our internal array is shared by any other
78 // TextureCollections, we have to copy the array now so we won't
79 // inadvertently modify any of our brethren TextureCollection objects.
80
81 if (_textures.get_ref_count() > 1) {
82 Textures old_textures = _textures;
83 _textures = Textures::empty_array(0);
84 _textures.v() = old_textures.v();
85 }
86
87 _textures.erase(_textures.begin() + texture_index);
88 return true;
89}
90
91/**
92 * Adds all the Textures indicated in the other collection to this texture.
93 * The other textures are simply appended to the end of the textures in this
94 * list; duplicates are not automatically removed.
95 */
98 int other_num_textures = other.get_num_textures();
99 for (int i = 0; i < other_num_textures; i++) {
100 add_texture(other.get_texture(i));
101 }
102}
103
104
105/**
106 * Removes from this collection all of the Textures listed in the other
107 * collection.
108 */
111 Textures new_textures;
112 int num_textures = get_num_textures();
113 for (int i = 0; i < num_textures; i++) {
114 PT(Texture) texture = get_texture(i);
115 if (!other.has_texture(texture)) {
116 new_textures.push_back(texture);
117 }
118 }
119 _textures = new_textures;
120}
121
122/**
123 * Removes any duplicate entries of the same Textures on this collection. If
124 * a Texture appears multiple times, the first appearance is retained;
125 * subsequent appearances are removed.
126 */
129 Textures new_textures;
130
131 int num_textures = get_num_textures();
132 for (int i = 0; i < num_textures; i++) {
133 PT(Texture) texture = get_texture(i);
134 bool duplicated = false;
135
136 for (int j = 0; j < i && !duplicated; j++) {
137 duplicated = (texture == get_texture(j));
138 }
139
140 if (!duplicated) {
141 new_textures.push_back(texture);
142 }
143 }
144
145 _textures = new_textures;
146}
147
148/**
149 * Returns true if the indicated Texture appears in this collection, false
150 * otherwise.
151 */
153has_texture(Texture *texture) const {
154 for (int i = 0; i < get_num_textures(); i++) {
155 if (texture == get_texture(i)) {
156 return true;
157 }
158 }
159 return false;
160}
161
162/**
163 * Removes all Textures from the collection.
164 */
166clear() {
167 _textures.clear();
168}
169
170/**
171 * This is a hint to Panda to allocate enough memory to hold the given number
172 * of NodePaths, if you know ahead of time how many you will be adding.
173 */
175reserve(size_t num) {
176 _textures.reserve(num);
177}
178
179/**
180 * Returns the texture in the collection with the indicated name, if any, or
181 * NULL if no texture has that name.
182 */
184find_texture(const std::string &name) const {
185 int num_textures = get_num_textures();
186 for (int i = 0; i < num_textures; i++) {
187 Texture *texture = get_texture(i);
188 if (texture->get_name() == name) {
189 return texture;
190 }
191 }
192 return nullptr;
193}
194
195/**
196 * Returns the number of Textures in the collection.
197 */
199get_num_textures() const {
200 return _textures.size();
201}
202
203/**
204 * Returns the nth Texture in the collection.
205 */
207get_texture(int index) const {
208 nassertr(index >= 0 && index < (int)_textures.size(), nullptr);
209
210 return _textures[index];
211}
212
213/**
214 * Returns the nth Texture in the collection. This is the same as
215 * get_texture(), but it may be a more convenient way to access it.
216 */
218operator [] (int index) const {
219 nassertr(index >= 0 && index < (int)_textures.size(), nullptr);
220
221 return _textures[index];
222}
223
224/**
225 * Returns the number of textures in the collection. This is the same thing
226 * as get_num_textures().
227 */
229size() const {
230 return _textures.size();
231}
232
233/**
234 * Writes a brief one-line description of the TextureCollection to the
235 * indicated output stream.
236 */
238output(std::ostream &out) const {
239 if (get_num_textures() == 1) {
240 out << "1 Texture";
241 } else {
242 out << get_num_textures() << " Textures";
243 }
244}
245
246/**
247 * Writes a complete multi-line description of the TextureCollection to the
248 * indicated output stream.
249 */
251write(std::ostream &out, int indent_level) const {
252 for (int i = 0; i < get_num_textures(); i++) {
253 indent(out, indent_level) << *get_texture(i) << "\n";
254 }
255}
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
get_num_textures
Returns the number of Textures in the collection.
void add_texture(Texture *texture)
Adds a new Texture to the collection.
void remove_duplicate_textures()
Removes any duplicate entries of the same Textures on this collection.
int size() const
Returns the number of textures in the collection.
bool has_texture(Texture *texture) const
Returns true if the indicated Texture appears in this collection, false otherwise.
bool remove_texture(Texture *texture)
Removes the indicated Texture 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.
void reserve(size_t num)
This is a hint to Panda to allocate enough memory to hold the given number of NodePaths,...
void write(std::ostream &out, int indent_level=0) const
Writes a complete multi-line description of the TextureCollection to the indicated output stream.
void clear()
Removes all Textures from the collection.
Texture * find_texture(const std::string &name) const
Returns the texture in the collection with the indicated name, if any, or NULL if no texture has that...
Texture * operator[](int index) const
Returns the nth Texture in the collection.
void output(std::ostream &out) const
Writes a brief one-line description of the TextureCollection to the indicated output stream.
get_texture
Returns the nth Texture in the collection.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition texture.h:72
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.