summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/resources/atlasmanager.cpp69
-rw-r--r--src/resources/atlasmanager.h6
-rw-r--r--src/resources/resource.h4
3 files changed, 68 insertions, 11 deletions
diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp
index 308c8d9ba..a9745bd08 100644
--- a/src/resources/atlasmanager.cpp
+++ b/src/resources/atlasmanager.cpp
@@ -257,20 +257,21 @@ void AtlasManager::convertAtlas(TextureAtlas *atlas)
void AtlasManager::injectToResources(AtlasResource *resource)
{
ResourceManager *const resman = ResourceManager::getInstance();
- int k = 0;
for (std::vector<TextureAtlas*>::iterator it = resource->atlases.begin(),
- it_end = resource->atlases.end(); it != it_end; ++ it, k ++)
+ it_end = resource->atlases.end(); it != it_end; ++ it)
{
// add each atlas image to resources
TextureAtlas *const atlas = *it;
- resman->addResource(atlas->name, atlas->atlasImage);
if (atlas)
{
+ resman->addResource(atlas->name, atlas->atlasImage);
for (std::vector<AtlasItem*>::iterator it2 = atlas->items.begin(),
it2_end = atlas->items.end();
it2 != it2_end; ++ it2)
{
AtlasItem *const item = *it2;
+ if (!item)
+ continue;
// add each atlas sub image to resources
resman->addResource(item->name, item->image);
}
@@ -278,6 +279,42 @@ void AtlasManager::injectToResources(AtlasResource *resource)
}
}
+void AtlasManager::moveToDeleted(AtlasResource *resource)
+{
+ ResourceManager *const resman = ResourceManager::getInstance();
+ for (std::vector<TextureAtlas*>::iterator it = resource->atlases.begin(),
+ it_end = resource->atlases.end(); it != it_end; ++ it)
+ {
+ // move each atlas image to deleted
+ TextureAtlas *const atlas = *it;
+ if (atlas)
+ {
+ Image *const image = atlas->atlasImage;
+ if (image)
+ {
+ image->decRef();
+ resman->moveToDeleted(image);
+ }
+ for (std::vector<AtlasItem*>::iterator it2 = atlas->items.begin(),
+ it2_end = atlas->items.end();
+ it2 != it2_end; ++ it2)
+ {
+ AtlasItem *const item = *it2;
+ if (item)
+ {
+ Image *const image2 = item->image;
+ if (image2)
+ {
+ image2->decRef();
+ // move each atlas sub image to deleted
+ resman->moveToDeleted(image2);
+ }
+ }
+ }
+ }
+ }
+}
+
AtlasResource::~AtlasResource()
{
for (std::vector<TextureAtlas*>::iterator it = atlases.begin(),
@@ -293,18 +330,32 @@ AtlasResource::~AtlasResource()
AtlasItem *const item = *it2;
if (item)
{
- Image *const image = item->image;
- if (image)
- image->decRef();
+// Image *const image = item->image;
+// if (image)
+// image->decRef();
delete item;
}
}
- Image *const image = atlas->atlasImage;
- if (image)
- image->decRef();
+// Image *const image = atlas->atlasImage;
+// if (image)
+// image->decRef();
delete atlas;
}
}
}
+void AtlasResource::incRef()
+{
+ if (!getRefCount())
+ AtlasManager::injectToResources(this);
+ Resource::incRef();
+}
+
+void AtlasResource::decRef()
+{
+ Resource::decRef();
+ if (!getRefCount())
+ AtlasManager::moveToDeleted(this);
+}
+
#endif
diff --git a/src/resources/atlasmanager.h b/src/resources/atlasmanager.h
index 41aae86e5..1606fc4c9 100644
--- a/src/resources/atlasmanager.h
+++ b/src/resources/atlasmanager.h
@@ -73,6 +73,10 @@ class AtlasResource : public Resource
public:
~AtlasResource();
+ void incRef();
+
+ void decRef();
+
std::vector<TextureAtlas*> atlases;
};
@@ -86,6 +90,8 @@ class AtlasManager
static void injectToResources(AtlasResource *resource);
+ static void moveToDeleted(AtlasResource *resource);
+
private:
static void loadImages(const StringVect &files,
std::vector<Image*> &images);
diff --git a/src/resources/resource.h b/src/resources/resource.h
index b73597d14..cf3555cf5 100644
--- a/src/resources/resource.h
+++ b/src/resources/resource.h
@@ -52,7 +52,7 @@ class Resource
/**
* Increments the internal reference count.
*/
- void incRef();
+ virtual void incRef();
/**
* Decrements the reference count and deletes the object
@@ -61,7 +61,7 @@ class Resource
* @return <code>true</code> if the object was deleted
* <code>false</code> otherwise.
*/
- void decRef();
+ virtual void decRef();
/**
* Return the path identifying this resource.