Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/gobj/texturePool.cxx

Go to the documentation of this file.
00001 // Filename: texturePool.cxx
00002 // Created by:  drose (26Apr00)
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 "texturePool.h"
00020 #include "config_gobj.h"
00021 #include "config_util.h"
00022 #include "config_express.h"
00023 #include "virtualFileSystem.h"
00024 
00025 
00026 TexturePool *TexturePool::_global_ptr = (TexturePool *)NULL;
00027 
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: TexturePool::ns_has_texture
00031 //       Access: Private
00032 //  Description: The nonstatic implementation of has_texture().
00033 ////////////////////////////////////////////////////////////////////
00034 bool TexturePool::
00035 ns_has_texture(const Filename &orig_filename) {
00036   Filename filename(orig_filename);
00037 
00038   if (!fake_texture_image.empty()) {
00039     filename = fake_texture_image;
00040   }
00041 
00042   if (use_vfs) {
00043     VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00044     vfs->resolve_filename(filename, get_texture_path());
00045     vfs->resolve_filename(filename, get_model_path());
00046 
00047   } else {
00048     filename.resolve_filename(get_texture_path());
00049     filename.resolve_filename(get_model_path());
00050   }
00051 
00052   Textures::const_iterator ti;
00053   ti = _textures.find(filename);
00054   if (ti != _textures.end()) {
00055     // This texture was previously loaded.
00056     return true;
00057   }
00058 
00059   return false;
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: TexturePool::ns_load_texture
00064 //       Access: Private
00065 //  Description: The nonstatic implementation of load_texture().
00066 ////////////////////////////////////////////////////////////////////
00067 Texture *TexturePool::
00068 ns_load_texture(const Filename &orig_filename, int primary_file_num_channels) {
00069   Filename filename(orig_filename);
00070 
00071   if (!fake_texture_image.empty()) {
00072     filename = fake_texture_image;
00073   }
00074 
00075   if (use_vfs) {
00076     VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00077     vfs->resolve_filename(filename, get_texture_path()) ||
00078       vfs->resolve_filename(filename, get_model_path());
00079 
00080   } else {
00081     filename.resolve_filename(get_texture_path()) ||
00082       filename.resolve_filename(get_model_path());
00083   }
00084 
00085   Textures::const_iterator ti;
00086   ti = _textures.find(filename);
00087   if (ti != _textures.end()) {
00088     // This texture was previously loaded.
00089     return (*ti).second;
00090   }
00091 
00092   gobj_cat.info()
00093     << "Loading texture " << filename << "\n";
00094   PT(Texture) tex = new Texture;
00095   if (!tex->read(filename, primary_file_num_channels)) {
00096     // This texture was not found.
00097     gobj_cat.error()
00098       << "Unable to read texture " << filename << "\n";
00099     return NULL;
00100   }
00101 
00102   // Set the original filename, before we searched along the path.
00103   tex->set_filename(orig_filename);
00104 
00105   _textures[filename] = tex;
00106   return tex;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: TexturePool::ns_load_texture
00111 //       Access: Private
00112 //  Description: The nonstatic implementation of load_texture().
00113 ////////////////////////////////////////////////////////////////////
00114 Texture *TexturePool::
00115 ns_load_texture(const Filename &orig_filename, 
00116                 const Filename &orig_alpha_filename,
00117                 int primary_file_num_channels,
00118                 int alpha_file_channel) {
00119   Filename filename(orig_filename);
00120   Filename alpha_filename(orig_alpha_filename);
00121 
00122   if (!fake_texture_image.empty()) {
00123     return ns_load_texture(fake_texture_image, primary_file_num_channels);
00124   }
00125 
00126   if (use_vfs) {
00127     VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00128     vfs->resolve_filename(filename, get_texture_path()) ||
00129       vfs->resolve_filename(filename, get_model_path());
00130 
00131     vfs->resolve_filename(alpha_filename, get_texture_path()) ||
00132       vfs->resolve_filename(alpha_filename, get_model_path());
00133 
00134   } else {
00135     filename.resolve_filename(get_texture_path()) ||
00136       filename.resolve_filename(get_model_path());
00137 
00138     alpha_filename.resolve_filename(get_texture_path()) ||
00139       alpha_filename.resolve_filename(get_model_path());
00140   }
00141 
00142   Textures::const_iterator ti;
00143   ti = _textures.find(filename);
00144   if (ti != _textures.end()) {
00145     // This texture was previously loaded.
00146     return (*ti).second;
00147   }
00148 
00149   gobj_cat.info()
00150     << "Loading texture " << filename << " and alpha component "
00151     << alpha_filename << endl;
00152   PT(Texture) tex = new Texture;
00153   if (!tex->read(filename, alpha_filename, primary_file_num_channels,
00154                  alpha_file_channel)) {
00155     // This texture was not found.
00156     gobj_cat.error() << "Unable to read texture " << filename << "\n";
00157     return NULL;
00158   }
00159 
00160   // Set the original filenames, before we searched along the path.
00161   tex->set_filename(orig_filename);
00162   tex->set_alpha_filename(orig_alpha_filename);
00163 
00164   _textures[filename] = tex;
00165   return tex;
00166 }
00167 
00168 ////////////////////////////////////////////////////////////////////
00169 //     Function: TexturePool::ns_add_texture
00170 //       Access: Private
00171 //  Description: The nonstatic implementation of add_texture().
00172 ////////////////////////////////////////////////////////////////////
00173 void TexturePool::
00174 ns_add_texture(Texture *tex) {
00175   string filename = tex->get_filename();
00176   if (filename.empty()) {
00177     gobj_cat.error() << "Attempt to call add_texture() on an unnamed texture.\n";
00178   }
00179 
00180   // We blow away whatever texture was there previously, if any.
00181   _textures[filename] = tex;
00182 }
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: TexturePool::ns_release_texture
00186 //       Access: Private
00187 //  Description: The nonstatic implementation of release_texture().
00188 ////////////////////////////////////////////////////////////////////
00189 void TexturePool::
00190 ns_release_texture(Texture *tex) {
00191   string filename = tex->get_filename();
00192   Textures::iterator ti;
00193   ti = _textures.find(filename);
00194   if (ti != _textures.end() && (*ti).second == tex) {
00195     _textures.erase(ti);
00196   }
00197 }
00198 
00199 ////////////////////////////////////////////////////////////////////
00200 //     Function: TexturePool::ns_release_all_textures
00201 //       Access: Private
00202 //  Description: The nonstatic implementation of release_all_textures().
00203 ////////////////////////////////////////////////////////////////////
00204 void TexturePool::
00205 ns_release_all_textures() {
00206   _textures.clear();
00207 }
00208 
00209 ////////////////////////////////////////////////////////////////////
00210 //     Function: TexturePool::ns_garbage_collect
00211 //       Access: Private
00212 //  Description: The nonstatic implementation of garbage_collect().
00213 ////////////////////////////////////////////////////////////////////
00214 int TexturePool::
00215 ns_garbage_collect() {
00216   int num_released = 0;
00217   Textures new_set;
00218 
00219   Textures::iterator ti;
00220   for (ti = _textures.begin(); ti != _textures.end(); ++ti) {
00221     Texture *tex = (*ti).second;
00222     if (tex->get_ref_count() == 1) {
00223       if (gobj_cat.is_debug()) {
00224         gobj_cat.debug()
00225           << "Releasing " << (*ti).first << "\n";
00226       }
00227       num_released++;
00228     } else {
00229       new_set.insert(new_set.end(), *ti);
00230     }
00231   }
00232 
00233   _textures.swap(new_set);
00234   return num_released;
00235 }
00236 
00237 ////////////////////////////////////////////////////////////////////
00238 //     Function: TexturePool::ns_list_contents
00239 //       Access: Private
00240 //  Description: The nonstatic implementation of list_contents().
00241 ////////////////////////////////////////////////////////////////////
00242 void TexturePool::
00243 ns_list_contents(ostream &out) {
00244   out << _textures.size() << " textures:\n";
00245   Textures::iterator ti;
00246   for (ti = _textures.begin(); ti != _textures.end(); ++ti) {
00247     Texture *texture = (*ti).second;
00248     out << "  " << (*ti).first
00249         << " (count = " << texture->get_ref_count() << ", ram = "
00250         << texture->_pbuffer->_image.size() / 1024 << " Kb)\n";
00251   }
00252 }
00253 
00254 ////////////////////////////////////////////////////////////////////
00255 //     Function: TexturePool::get_ptr
00256 //       Access: Private, Static
00257 //  Description: Initializes and/or returns the global pointer to the
00258 //               one TexturePool object in the system.
00259 ////////////////////////////////////////////////////////////////////
00260 TexturePool *TexturePool::
00261 get_ptr() {
00262   if (_global_ptr == (TexturePool *)NULL) {
00263     _global_ptr = new TexturePool;
00264   }
00265   return _global_ptr;
00266 }

Generated on Fri May 2 00:39:45 2003 for Panda by doxygen1.3