diff --git a/spine-cocos2dx/example/Classes/BatchingExample.cpp b/spine-cocos2dx/example/Classes/BatchingExample.cpp index ce5d0be7b..e14073d9a 100644 --- a/spine-cocos2dx/example/Classes/BatchingExample.cpp +++ b/spine-cocos2dx/example/Classes/BatchingExample.cpp @@ -34,7 +34,9 @@ USING_NS_CC; using namespace spine; -#define NUM_SKELETONS 50 +#define NUM_SKELETONS 50 + +Cocos2dTextureLoader textureLoader; Scene* BatchingExample::scene () { Scene *scene = Scene::create(); @@ -45,8 +47,8 @@ Scene* BatchingExample::scene () { bool BatchingExample::init () { if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false; - // Load the texture atlas. - Cocos2dTextureLoader textureLoader; + // Load the texture atlas. Note that the texture loader has to live + // as long as the Atlas, as the Atlas destructor will call TextureLoader::unload. _atlas = new (__FILE__, __LINE__) Atlas("spineboy.atlas", &textureLoader); CCASSERT(_atlas, "Error reading atlas file."); diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp index 8a2c78bcb..0223480c7 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp @@ -106,10 +106,6 @@ GLuint filter (TextureFilter filter) { Cocos2dTextureLoader::Cocos2dTextureLoader() : TextureLoader() { } Cocos2dTextureLoader::~Cocos2dTextureLoader() { } -static void unloadTexture (void* texture) { - ((Texture2D*)texture)->release(); -} - void Cocos2dTextureLoader::load(AtlasPage& page, const spine::String& path) { Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(path.buffer()); CCASSERT(texture != nullptr, "Invalid image"); @@ -118,13 +114,13 @@ void Cocos2dTextureLoader::load(AtlasPage& page, const spine::String& path) { Texture2D::TexParams textureParams = {filter(page.minFilter), filter(page.magFilter), wrap(page.uWrap), wrap(page.vWrap)}; texture->setTexParameters(textureParams); - page.setRendererObject(texture, unloadTexture); + page.setRendererObject(texture); page.width = texture->getPixelsWide(); page.height = texture->getPixelsHigh(); } void Cocos2dTextureLoader::unload(void* texture) { - unloadTexture(texture); + ((Texture2D*)texture)->release(); } diff --git a/spine-cpp/spine-cpp/include/spine/HasRendererObject.h b/spine-cpp/spine-cpp/include/spine/HasRendererObject.h index 4096313ec..3799ab4c6 100644 --- a/spine-cpp/spine-cpp/include/spine/HasRendererObject.h +++ b/spine-cpp/spine-cpp/include/spine/HasRendererObject.h @@ -37,7 +37,7 @@ typedef void (*DisposeRendererObject) (void* rendererObject); class HasRendererObject { public: - explicit HasRendererObject() : _rendererObject(NULL) {}; + explicit HasRendererObject() : _rendererObject(NULL), _dispose(NULL) {}; virtual ~HasRendererObject() { if (_dispose && _rendererObject) diff --git a/spine-cpp/spine-cpp/src/spine/Atlas.cpp b/spine-cpp/spine-cpp/src/spine/Atlas.cpp index 15484e0dd..6b6595493 100644 --- a/spine-cpp/spine-cpp/src/spine/Atlas.cpp +++ b/spine-cpp/spine-cpp/src/spine/Atlas.cpp @@ -71,6 +71,11 @@ Atlas::Atlas(const char *data, int length, const char *dir, TextureLoader *textu } Atlas::~Atlas() { + if (_textureLoader) { + for (size_t i = 0, n = _pages.size(); i < n; ++i) { + _textureLoader->unload(_pages[i]->getRendererObject()); + } + } ContainerUtil::cleanUpVectorOfPointers(_pages); ContainerUtil::cleanUpVectorOfPointers(_regions); } diff --git a/spine-sfml/cpp/src/spine/spine-sfml.cpp b/spine-sfml/cpp/src/spine/spine-sfml.cpp index e3ee1ffed..32ad83b33 100644 --- a/spine-sfml/cpp/src/spine/spine-sfml.cpp +++ b/spine-sfml/cpp/src/spine/spine-sfml.cpp @@ -281,10 +281,6 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const { if (vertexEffect != 0) vertexEffect->end(); } -void deleteTexture(void* texture) { - delete (Texture *) texture; -} - void SFMLTextureLoader::load(AtlasPage &page, const String &path) { Texture *texture = new Texture(); if (!texture->loadFromFile(path.buffer())) return; @@ -292,14 +288,14 @@ void SFMLTextureLoader::load(AtlasPage &page, const String &path) { if (page.magFilter == TextureFilter_Linear) texture->setSmooth(true); if (page.uWrap == TextureWrap_Repeat && page.vWrap == TextureWrap_Repeat) texture->setRepeated(true); - page.setRendererObject(texture, deleteTexture); + page.setRendererObject(texture); Vector2u size = texture->getSize(); page.width = size.x; page.height = size.y; } void SFMLTextureLoader::unload(void *texture) { - deleteTexture(texture); + delete (Texture *) texture; } SpineExtension *getDefaultExtension() {