summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-09-28 02:09:04 +0300
committerAndrei Karas <akaras@inbox.ru>2012-09-30 04:19:00 +0300
commit48a21f6542e799e41735febbda76815e058954ac (patch)
treed42804ec7de20a3d0c1f417164c547328279ce10
parent2364404717e7e7b93daf00f22ee17f72db6e5b27 (diff)
downloadmv-48a21f6542e799e41735febbda76815e058954ac.tar.gz
mv-48a21f6542e799e41735febbda76815e058954ac.tar.bz2
mv-48a21f6542e799e41735febbda76815e058954ac.tar.xz
mv-48a21f6542e799e41735febbda76815e058954ac.zip
Override inc/dec ref to add/remove atlas images to resource cache.
-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.