• Editor
  • try to convert CCtexture2d to Attachment

Related Discussions
...

Hello,

First of all thank you Noch for sharing your code.
I am using Cocos2d-X 3.1 and I am interested in doing the same.
I have also ported this code but all I get is a black rectangle instead of the attachment.

Here is my code:

In SkeletonAnimation.h I added:

bool setTexture2D(cocos2d::Texture2D* texture, const char* slotName);
bool setSprite(cocos2d::Sprite* sprite, const char* slotName);
spRegionAttachment* regionWithTexture2d(cocos2d::Texture2D* tex2d, const char* name);

in SkeletonAnimation.cpp I added :

bool SkeletonAnimation::setTexture2D(cocos2d::Texture2D* texture,const char* slotName){
   spRegionAttachment *reg = this->regionWithTexture2d(texture,slotName);
   spSlot* slot = this->findSlot(slotName);
   if ((slot!=NULL)&&(reg!=NULL)) {
      spSlot_setAttachment(slot,(spAttachment*)reg);
      return true;
   }else{
      return false;
   }
   
return false; }
bool SkeletonAnimation::setSprite(cocos2d::Sprite* sprite,const char* slotName){ //render for get all CCSprite setting sprite->setPosition(Vec2(sprite->getContentSize().width,sprite->getContentSize().height)); sprite->setAnchorPoint(Vec2(0,0)); //set sprite to origin sprite->setRotation(180); //the rendered texture will flip sprite->setFlippedX(true); // flip back
RenderTexture* rt = RenderTexture::create(sprite->getContentSize().width, sprite->getContentSize().height);
rt->beginWithClear(0, 0, 0, 0);//start with clear sprite->visit();//visit for draw
rt->end();//end
return this->setTexture2D(rt->getSprite()->getTexture(),slotName); //return
}

spRegionAttachment* SkeletonAnimation::regionWithTexture2d(cocos2d::Texture2D* tex2d, const char* name){ TextureAtlas* tex = TextureAtlas::createWithTexture(tex2d, 1);
spAtlasPage *atPage = spAtlasPage_create(NEW(spAtlas), name);
atPage->rendererObject = tex;
atPage->width = tex2d->getContentSizeInPixels().width; atPage->height = tex2d->getContentSizeInPixels().height;
spAtlasRegion *reg = spAtlasRegion_create(); reg->page = atPage;
spRegionAttachment *region = spRegionAttachment_create(name); region->rendererObject = reg;
Size size = tex2d->getContentSizeInPixels(); float u = 0; float u2 = 1; float v = 0; float v2 = 1; spRegionAttachment_setUVs(region,u,v,u2,v2,0);

spSlot* slot = this->findSlot(name); spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
region->regionOffsetX = 0; region->regionOffsetY = 0; region->regionWidth = size.width; region->regionHeight = size.height; region->regionOriginalWidth = region->regionWidth; // same if not doing whitespace stripping region->regionOriginalHeight = region->regionHeight;

region->x = attachment->x; region->y = attachment->y; region->scaleX = 1.0f;//this->nodeScale;//TODO find replacement region->scaleY = 1.0f;//this->nodeScale;//TODO find replacement region->rotation = attachment->rotation; region->width = size.width; region->height = size.height; spRegionAttachment_updateOffset(region);
return region; }

I have tried using both setSprite and setTexture2D directly and all I get are black rectangles.

Does anyone see what I am doing wrong?
Shoog how does it compare to your code?
Noch how did you handle the change to AtlasPage_create?
I used "spAtlasPage_create(NEW(spAtlas), name)" could that be the problem?

Thank you for your help.


Nobody has any ideas ?

8 days later

Sorry for the delay. The spine-cocos2dx for 3.1 no longer uses TextureAtlas. spAtlasPage rendererObject is a Texture2D, see spine-cocos2dx.cpp.

a year later

Hi I created a new thread here with some additional questions based upon this solution: http://esotericsoftware.com/forum/cocos2d-iphone-dynamic-image-on-spine-animation-4686

I'm going to repost my questions here.

I've been following the answer in this thread, but I have been having trouble adapting it for the latest version of spine and cocos2d v3.4. I've been having two main problems.

  1. ) The implementation in the answer in the second link above shows creating a CCTextureAtlas from a CCTexture2D in the following method:
    -(RegionAttachment*) regionWithTexture2d:(CCTexture2D*)tex2d name:(NSString*)_name{
        CCTextureAtlas *tex = [[CCTextureAtlas alloc] initWithTexture:tex2d capacity:128];
        ...
    
    However in cocos2d v3.4, CCTextureAtlas has been retired. I've been trying to work around it by using a CCTexture instead and setting that directly as the renderObject, but I'm not sure if this is correct.

2.) The original implementation also created an AtlasPage with just a name before as follows:

AtlasPage *page = AtlasPage_create([_name UTF8String]);

However in the latest spine version, the AtlasPage_create() requires an Atlas and name as follows:

spAtlasPage* spAtlasPage_create (spAtlas* atlas, const char* name);

I'm not sure what to put as the atlas here. I've tried to put different things into spAtlas_create() or spAtlas_createFromFile(), but no matter what I do it just seems to crash.

Any help anyone can offer will be greatly appreciated! Thank you!