00001 // Filename: modelPool.cxx 00002 // Created by: drose (12Mar02) 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 "modelPool.h" 00020 #include "loader.h" 00021 #include "config_pgraph.h" 00022 00023 00024 ModelPool *ModelPool::_global_ptr = (ModelPool *)NULL; 00025 00026 static Loader model_loader; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: ModelPool::ns_has_model 00030 // Access: Private 00031 // Description: The nonstatic implementation of has_model(). 00032 //////////////////////////////////////////////////////////////////// 00033 bool ModelPool:: 00034 ns_has_model(const string &filename) { 00035 Models::const_iterator ti; 00036 ti = _models.find(filename); 00037 if (ti != _models.end()) { 00038 // This model was previously loaded. 00039 return true; 00040 } 00041 00042 return false; 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: ModelPool::ns_load_model 00047 // Access: Private 00048 // Description: The nonstatic implementation of load_model(). 00049 //////////////////////////////////////////////////////////////////// 00050 PandaNode *ModelPool:: 00051 ns_load_model(const string &filename) { 00052 Models::const_iterator ti; 00053 ti = _models.find(filename); 00054 if (ti != _models.end()) { 00055 // This model was previously loaded. 00056 return (*ti).second; 00057 } 00058 00059 loader_cat.info() 00060 << "Loading model " << filename << "\n"; 00061 PT(PandaNode) node = model_loader.load_sync(filename); 00062 if (node.is_null()) { 00063 // This model was not found. 00064 return (PandaNode *)NULL; 00065 } 00066 00067 _models[filename] = node; 00068 return node; 00069 } 00070 00071 //////////////////////////////////////////////////////////////////// 00072 // Function: ModelPool::ns_add_model 00073 // Access: Private 00074 // Description: The nonstatic implementation of add_model(). 00075 //////////////////////////////////////////////////////////////////// 00076 void ModelPool:: 00077 ns_add_model(const string &filename, PandaNode *model) { 00078 // We blow away whatever model was there previously, if any. 00079 _models[filename] = model; 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: ModelPool::ns_release_model 00084 // Access: Private 00085 // Description: The nonstatic implementation of release_model(). 00086 //////////////////////////////////////////////////////////////////// 00087 void ModelPool:: 00088 ns_release_model(const string &filename) { 00089 Models::iterator ti; 00090 ti = _models.find(filename); 00091 if (ti != _models.end()) { 00092 _models.erase(ti); 00093 } 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: ModelPool::ns_release_all_models 00098 // Access: Private 00099 // Description: The nonstatic implementation of release_all_models(). 00100 //////////////////////////////////////////////////////////////////// 00101 void ModelPool:: 00102 ns_release_all_models() { 00103 _models.clear(); 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: ModelPool::ns_garbage_collect 00108 // Access: Private 00109 // Description: The nonstatic implementation of garbage_collect(). 00110 //////////////////////////////////////////////////////////////////// 00111 int ModelPool:: 00112 ns_garbage_collect() { 00113 int num_released = 0; 00114 Models new_set; 00115 00116 Models::iterator ti; 00117 for (ti = _models.begin(); ti != _models.end(); ++ti) { 00118 PandaNode *node = (*ti).second; 00119 if (node->get_ref_count() == 1) { 00120 if (loader_cat.is_debug()) { 00121 loader_cat.debug() 00122 << "Releasing " << (*ti).first << "\n"; 00123 } 00124 num_released++; 00125 } else { 00126 new_set.insert(new_set.end(), *ti); 00127 } 00128 } 00129 00130 _models.swap(new_set); 00131 return num_released; 00132 } 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function: ModelPool::ns_list_contents 00136 // Access: Private 00137 // Description: The nonstatic implementation of list_contents(). 00138 //////////////////////////////////////////////////////////////////// 00139 void ModelPool:: 00140 ns_list_contents(ostream &out) { 00141 out << _models.size() << " models:\n"; 00142 Models::iterator ti; 00143 for (ti = _models.begin(); ti != _models.end(); ++ti) { 00144 out << " " << (*ti).first 00145 << " (count = " << (*ti).second->get_ref_count() << ")\n"; 00146 } 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: ModelPool::get_ptr 00151 // Access: Private, Static 00152 // Description: Initializes and/or returns the global pointer to the 00153 // one ModelPool object in the system. 00154 //////////////////////////////////////////////////////////////////// 00155 ModelPool *ModelPool:: 00156 get_ptr() { 00157 if (_global_ptr == (ModelPool *)NULL) { 00158 _global_ptr = new ModelPool; 00159 } 00160 return _global_ptr; 00161 }