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