00001 // Filename: materialPool.cxx 00002 // Created by: drose (30Apr01) 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 "materialPool.h" 00020 #include "config_gobj.h" 00021 00022 00023 MaterialPool *MaterialPool::_global_ptr = (MaterialPool *)NULL; 00024 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: MaterialPool::ns_get_material 00028 // Access: Public 00029 // Description: The nonstatic implementation of get_material(). 00030 //////////////////////////////////////////////////////////////////// 00031 const Material *MaterialPool:: 00032 ns_get_material(const CPT(Material) &temp) { 00033 Materials::iterator mi = _materials.insert(temp).first; 00034 return (*mi); 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: MaterialPool::ns_garbage_collect 00039 // Access: Private 00040 // Description: The nonstatic implementation of garbage_collect(). 00041 //////////////////////////////////////////////////////////////////// 00042 int MaterialPool:: 00043 ns_garbage_collect() { 00044 int num_released = 0; 00045 Materials new_set; 00046 00047 Materials::iterator mi; 00048 for (mi = _materials.begin(); mi != _materials.end(); ++mi) { 00049 const Material *mat = (*mi); 00050 if (mat->get_ref_count() == 1) { 00051 if (gobj_cat.is_debug()) { 00052 gobj_cat.debug() 00053 << "Releasing " << *mat << "\n"; 00054 } 00055 num_released++; 00056 } else { 00057 new_set.insert(new_set.end(), *mi); 00058 } 00059 } 00060 00061 _materials.swap(new_set); 00062 return num_released; 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: MaterialPool::ns_list_contents 00067 // Access: Private 00068 // Description: The nonstatic implementation of list_contents(). 00069 //////////////////////////////////////////////////////////////////// 00070 void MaterialPool:: 00071 ns_list_contents(ostream &out) { 00072 out << _materials.size() << " materials:\n"; 00073 Materials::iterator mi; 00074 for (mi = _materials.begin(); mi != _materials.end(); ++mi) { 00075 const Material *mat = (*mi); 00076 out << " " << *mat 00077 << " (count = " << mat->get_ref_count() << ")\n"; 00078 } 00079 } 00080 00081 //////////////////////////////////////////////////////////////////// 00082 // Function: MaterialPool::get_ptr 00083 // Access: Private, Static 00084 // Description: Initializes and/or returns the global pointer to the 00085 // one MaterialPool object in the system. 00086 //////////////////////////////////////////////////////////////////// 00087 MaterialPool *MaterialPool:: 00088 get_ptr() { 00089 if (_global_ptr == (MaterialPool *)NULL) { 00090 _global_ptr = new MaterialPool; 00091 } 00092 return _global_ptr; 00093 }