gui: type safe resources part 2

- separate collections for fonts, images, animations
- no more ugly casts
- fix crash if main ui.xml did not define any resources but include did
- don't stop loading resources if one "type" attribute is missing

Change-Id: I70c1c9ca66ca65d9fba1ba3eded34f3d8a07488e
This commit is contained in:
that
2015-03-04 22:39:34 +01:00
committed by Ethan Yonker
parent 18e13a9063
commit 74ac6060cc
4 changed files with 70 additions and 60 deletions
+52 -28
View File
@@ -258,21 +258,32 @@ AnimationResource::~AnimationResource()
mSurfaces.clear();
}
Resource* ResourceManager::FindResource(std::string name)
FontResource* ResourceManager::FindFont(const std::string& name) const
{
std::vector<Resource*>::iterator iter;
for (iter = mResources.begin(); iter != mResources.end(); iter++)
{
if (name == (*iter)->GetName())
return (*iter);
}
for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it)
if (name == (*it)->GetName())
return *it;
return NULL;
}
ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip)
ImageResource* ResourceManager::FindImage(const std::string& name) const
{
for (std::vector<ImageResource*>::const_iterator it = mImages.begin(); it != mImages.end(); ++it)
if (name == (*it)->GetName())
return *it;
return NULL;
}
AnimationResource* ResourceManager::FindAnimation(const std::string& name) const
{
for (std::vector<AnimationResource*>::const_iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
if (name == (*it)->GetName())
return *it;
return NULL;
}
ResourceManager::ResourceManager()
{
LoadResources(resList, pZip);
}
void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
@@ -285,14 +296,18 @@ void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
while (child != NULL)
{
xml_attribute<>* attr = child->first_attribute("type");
if (!attr)
break;
Resource* res = NULL;
std::string type = attr->value();
bool error = false;
std::string type = attr ? attr->value() : "*unspecified*";
if (type == "font")
{
res = new FontResource(child, pZip);
FontResource* res = new FontResource(child, pZip);
if (res->GetResource())
mFonts.push_back(res);
else {
error = true;
delete res;
}
}
else if (type == "image")
{
@@ -300,7 +315,13 @@ void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
xml_attribute<>* retain_aspect_ratio = child->first_attribute("retainaspect");
if (retain_aspect_ratio)
retain = 1; // the value does not matter, if retainaspect is present, we assume that we want to retain it
res = new ImageResource(child, pZip, retain);
ImageResource* res = new ImageResource(child, pZip, retain);
if (res->GetResource())
mImages.push_back(res);
else {
error = true;
delete res;
}
}
else if (type == "animation")
{
@@ -308,14 +329,21 @@ void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
xml_attribute<>* retain_aspect_ratio = child->first_attribute("retainaspect");
if (retain_aspect_ratio)
retain = 1; // the value does not matter, if retainaspect is present, we assume that we want to retain it
res = new AnimationResource(child, pZip, retain);
AnimationResource* res = new AnimationResource(child, pZip, retain);
if (res->GetResourceCount())
mAnimations.push_back(res);
else {
error = true;
delete res;
}
}
else
{
LOGERR("Resource type (%s) not supported.\n", type.c_str());
error = true;
}
if (res == NULL || !res->loadedOK())
if (error)
{
std::string res_name;
if (child->first_attribute("name"))
@@ -327,12 +355,6 @@ void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
} else
LOGERR("Resource type (%s) failed to load\n", type.c_str());
delete res;
}
else
{
mResources.push_back(res);
}
child = child->next_sibling("resource");
@@ -341,10 +363,12 @@ void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
ResourceManager::~ResourceManager()
{
std::vector<Resource*>::iterator iter;
for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it)
delete *it;
for (iter = mResources.begin(); iter != mResources.end(); iter++)
delete *iter;
for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it)
delete *it;
mResources.clear();
for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
delete *it;
}