From 74ac6060ccb68e6ae80dbcb44d11f07a17e26034 Mon Sep 17 00:00:00 2001 From: that Date: Wed, 4 Mar 2015 22:39:34 +0100 Subject: 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 --- gui/resources.cpp | 78 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 27 deletions(-) (limited to 'gui/resources.cpp') 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::iterator iter; + for (std::vector::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::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::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::iterator iter; + for (std::vector::iterator it = mFonts.begin(); it != mFonts.end(); ++it) + delete *it; - for (iter = mResources.begin(); iter != mResources.end(); iter++) - delete *iter; + for (std::vector::iterator it = mImages.begin(); it != mImages.end(); ++it) + delete *it; - mResources.clear(); + for (std::vector::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it) + delete *it; } -- cgit v1.2.3