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