|
#include "PreCompile.h"
#include "AtlasMaster.h"
#include "ImageGL.h"
#include "XParser.h"
#include "AtlasImage.h"
#include "Util.h"
#include "ResManager.h"
#include "ImagePVR.h"
#include "ImageFactory.h"
AtlasMaster::AtlasMaster()
{
}
AtlasMaster::~AtlasMaster()
{
//map<string, AtlasImage*>::const_iterator iterMap;
//for (iterMap = m_mapImage.begin(); iterMap != m_mapImage.end(); iterMap++)
// delete iterMap->second;
m_allocator.Release();
int n;
for (n = 0; n < (int)m_listImageGL.size(); n++)
{
m_listImageGL[n]->Release();
delete m_listImageGL[n];
}
}
void AtlasMaster::Load(const string& fileName)
{
AtlasImage* pAtlasImage = NULL;
ImageGL* pImageGL = NULL;
XParser* pXParser = g_resManager.GetXParser();
pXParser->Open(fileName);
pXParser->ReadNextElement();
if (!pXParser->IsElementOpen("atlas"))
throw RacException("missing section, atlas");
int count = pXParser->GetValueInt("numImages");
m_allocator.Init(count);
string fileNameImage;
bool transparent;
string value;
do
{
pXParser->ReadNextElement();
if (pXParser->IsElement("texture"))
{
fileNameImage = g_resManager.GetDirResources() + pXParser->GetValue("file");
transparent = true;
if (pXParser->GetOptionalValue("trans", value))
{
if (value == "false")
transparent = false;
}
pImageGL = ImageFactory::GetImageGL(fileNameImage);
int timeNow = GetMilliSeconds();
pImageGL->Load(fileNameImage, transparent);
int timeLast = GetMilliSeconds();
int timeElapsed = abs(timeLast-timeNow);
cout << fileNameImage << " load:" << timeElapsed << "ms" << endl;
m_listImageGL.push_back(pImageGL);
}
else if (pXParser->IsElement("image"))
{
pAtlasImage = m_allocator.New();
pAtlasImage->SetImageGL(pImageGL);
string name = pXParser->GetValue("name");
pair <fmap<string, AtlasImage*>::iterator,bool> ret;
ret = m_mapImage.insert( std::make_pair (name, pAtlasImage) );
if (ret.second == false)
throw RacException("duplicate image in texture atlas: ", name);
// first item is iterator to newly created map entry
pAtlasImage->SetName(ret.first->first);
pAtlasImage->Load(pXParser);
}
} while (pXParser->IsElementOpen("atlas"));
}
AtlasImage* AtlasMaster::GetImage(const string& nameImage)
{
fmap<string, AtlasImage*>::const_iterator iter;
iter = m_mapImage.find(nameImage);
if (iter == m_mapImage.end())
throw RacException("unknown image in texture atlas: ", nameImage);
return iter->second;
}
AtlasImage* AtlasMaster::GetImageIfExist(const string& nameImage)
{
fmap<string, AtlasImage*>::const_iterator iter;
iter = m_mapImage.find(nameImage);
if (iter == m_mapImage.end())
return NULL;
return iter->second;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
After working in the software industry for many years, I've started my own
games company that specialises in strategy games for mobile platforms.