summaryrefslogtreecommitdiffstats
path: root/gui/resources.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gui/resources.cpp')
-rw-r--r--gui/resources.cpp78
1 files changed, 51 insertions, 27 deletions
diff --git a/gui/resources.cpp b/gui/resources.cpp
index e0016fc7b..1e2e7f9c6 100644
--- a/gui/resources.cpp
+++ b/gui/resources.cpp
@@ -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 (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it)
+ if (name == (*it)->GetName())
+ return *it;
+ return NULL;
+}
- for (iter = mResources.begin(); iter != mResources.end(); iter++)
- {
- if (name == (*iter)->GetName())
- return (*iter);
- }
+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;
}
-ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip)
+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;
}