00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "TextureProcessor.h"
00018 #include "../core/GameManager.h"
00019
00020 #include <algorithm>
00021
00022
00023
00024 TextureProcessor::TextureProcessor(AssetGroup *parent)
00025 : AssetProcessor(parent, "textures"), mIsReloading(false)
00026 {
00027 }
00028
00029
00030 TextureProcessor::~TextureProcessor()
00031 {
00032 removeAssets();
00033 }
00034
00035
00036 ITexture* TextureProcessor::getTexture(const std::string &fileName)
00037 {
00038 #ifdef WIN32
00039 std::string tmp = fileName;
00040 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00041 std::map<std::string, ITexture*>::iterator it = mTextures.find(tmp);
00042 #else
00043 std::map<std::string, ITexture*>::iterator it = mTextures.find(fileName);
00044 #endif
00045
00046 if(it != mTextures.end())
00047 return it->second;
00048
00049 return NULL;
00050 }
00051
00052
00053 bool TextureProcessor::loadAsset(const std::string &fileName)
00054 {
00055
00056 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00057 IVideoDriver *pDriver = GameManager::Instance()->getDriver();
00058
00059
00060 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00061
00062 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00063 return false;
00064
00065 if(!fileSystem->changeWorkingDirectoryTo("./textures/"))
00066 return false;
00067
00068
00069 ITexture *texture = pDriver->getTexture(fileName.c_str());
00070
00071 if(texture)
00072 {
00073
00074 mTextures[fileName] = texture;
00075 texture->grab();
00076
00077
00078 GameManager::Instance()->getDriver()->removeTexture(texture);
00079
00080
00081 if(mIsReloading)
00082 pParent->emitEvent(std::string("textures/") + fileName, texture);
00083
00084 else
00085 pParent->createEventSlot(std::string("textures/") + fileName);
00086 }
00087
00088
00089 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00090
00091 return (texture) ? true : false;
00092 }
00093
00094
00095 void TextureProcessor::loadAssets()
00096 {
00097
00098 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00099 IVideoDriver *pDriver = GameManager::Instance()->getDriver();
00100
00101
00102 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00103
00104 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00105 return;
00106
00107 if(!fileSystem->changeWorkingDirectoryTo("./textures/"))
00108 return;
00109
00110
00111 IFileList *fileList = fileSystem->createFileList();
00112
00113 for(u32 i = 0; i < fileList->getFileCount(); i++)
00114 {
00115
00116 std::string fileName = (fileList->getFileName(i)).c_str();
00117
00118 if(fileName == ".." || fileName == ".")
00119 continue;
00120
00121
00122 ITexture *texture = pDriver->getTexture(fileName.c_str());
00123
00124 if(texture)
00125 {
00126
00127 mTextures[fileName] = texture;
00128 texture->grab();
00129
00130
00131 GameManager::Instance()->getDriver()->removeTexture(texture);
00132
00133
00134 if(mIsReloading)
00135 pParent->emitEvent(std::string("textures/") + fileName, texture);
00136
00137 else
00138 pParent->createEventSlot(std::string("textures/") + fileName);
00139
00140 }
00141 }
00142
00143
00144 fileList->drop();
00145
00146 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00147 }
00148
00149
00150 bool TextureProcessor::reloadAsset(const std::string &fileName)
00151 {
00152 mIsReloading = true;
00153
00154 removeAsset(fileName);
00155 bool succeeded = loadAsset(fileName);
00156
00157 mIsReloading = false;
00158
00159 return succeeded;
00160 }
00161
00162
00163 void TextureProcessor::reloadAssets()
00164 {
00165 mIsReloading = true;
00166
00167 removeAssets();
00168 loadAssets();
00169
00170 mIsReloading = false;
00171 }
00172
00173
00174 bool TextureProcessor::removeAsset(const std::string &fileName)
00175 {
00176
00177 #ifdef WIN32
00178 std::string tmp = fileName;
00179 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00180 std::map<std::string, ITexture*>::iterator it = mTextures.find(tmp);
00181 #else
00182 std::map<std::string, ITexture*>::iterator it = mTextures.find(fileName);
00183 #endif
00184
00185
00186 if(it != mTextures.end())
00187 {
00188
00189 if(!mIsReloading)
00190 {
00191 std::string slotName = std::string("textures/") + it->first;
00192
00193 pParent->emitEvent(slotName, NULL);
00194 pParent->removeEventSlot(slotName);
00195 }
00196
00197
00198 ITexture *texture = it->second;
00199 texture->drop();
00200 mTextures.erase(it);
00201
00202 return true;
00203 }
00204
00205
00206 else return false;
00207 }
00208
00209
00210 void TextureProcessor::removeAssets()
00211 {
00212
00213 std::map<std::string, ITexture*>::iterator it;
00214
00215 for(it = mTextures.begin(); it != mTextures.end(); it++)
00216 {
00217
00218 if(!mIsReloading)
00219 {
00220 std::string slotName = std::string("textures/") + it->first;
00221
00222 pParent->emitEvent(slotName, NULL);
00223 pParent->removeEventSlot(slotName);
00224 }
00225
00226
00227 ITexture *texture = it->second;
00228 texture->drop();
00229 }
00230
00231
00232 mTextures.clear();
00233 }
00234
00235
00236 AssetProcessor* TextureProcessor::createProcessor(AssetGroup *parent)
00237 {
00238 return new TextureProcessor(parent);
00239 }
00240
00241