00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00031
00032
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
00056 return true;
00057 }
00058
00059 return false;
00060 }
00061
00062
00063
00064
00065
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
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
00097 gobj_cat.error()
00098 << "Unable to read texture " << filename << "\n";
00099 return NULL;
00100 }
00101
00102
00103 tex->set_filename(orig_filename);
00104
00105 _textures[filename] = tex;
00106 return tex;
00107 }
00108
00109
00110
00111
00112
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
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
00156 gobj_cat.error() << "Unable to read texture " << filename << "\n";
00157 return NULL;
00158 }
00159
00160
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
00170
00171
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
00181 _textures[filename] = tex;
00182 }
00183
00184
00185
00186
00187
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
00201
00202
00203
00204 void TexturePool::
00205 ns_release_all_textures() {
00206 _textures.clear();
00207 }
00208
00209
00210
00211
00212
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
00239
00240
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
00256
00257
00258
00259
00260 TexturePool *TexturePool::
00261 get_ptr() {
00262 if (_global_ptr == (TexturePool *)NULL) {
00263 _global_ptr = new TexturePool;
00264 }
00265 return _global_ptr;
00266 }