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 ?