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