Add overloads for SkeletonRendererCocos2dX methods findBone, findSlot and getAttachment accepting const char * instead of std::string similar to setSkin overloads.

Make such overloads construct spine::String which owns and does not delete the cstring passed to its constructor so that we don't heap allocate/deallocate in such a temporary spine::String.
This commit is contained in:
Grigory Shabaganov 2026-03-08 20:25:15 +10:00
parent 4b72bb4a54
commit d5948a3f30
2 changed files with 22 additions and 4 deletions

View File

@ -685,23 +685,38 @@ namespace spine {
}
Bone *SkeletonRendererCocos2dX::findBone(const std::string &boneName) const {
return _skeleton->findBone(boneName.c_str());
return findBone(boneName.empty() ? nullptr : boneName.c_str());
}
Bone *SkeletonRendererCocos2dX::findBone(const char *boneName) const {
const String spineBoneName{ boneName, true, false };
return _skeleton->findBone(spineBoneName);
}
Slot *SkeletonRendererCocos2dX::findSlot(const std::string &slotName) const {
return _skeleton->findSlot(slotName.c_str());
return findSlot(slotName.empty() ? nullptr : slotName.c_str());
}
Slot *SkeletonRendererCocos2dX::findSlot(const char *slotName) const {
const String spineSlotName{ slotName, true, false };
return _skeleton->findSlot(spineSlotName);
}
void SkeletonRendererCocos2dX::setSkin(const std::string &skinName) {
_skeleton->setSkin(skinName.empty() ? 0 : skinName.c_str());
_skeleton->setSkin(skinName.empty() ? nullptr : skinName.c_str());
}
void SkeletonRendererCocos2dX::setSkin(const char *skinName) {
const String spineSkinName{ skinName, true, false };
_skeleton->setSkin(skinName);
}
Attachment *SkeletonRendererCocos2dX::getAttachment(const std::string &slotName, const std::string &attachmentName) const {
return _skeleton->getAttachment(slotName.c_str(), attachmentName.c_str());
return getAttachment(slotName.c_str(), attachmentName.c_str());
}
Attachment *SkeletonRendererCocos2dX::getAttachment(const char *slotName, const char *attachmentName) const {
const String spineSlotName{ slotName, true, false };
const String spineAttachmentName{ attachmentName, true, false };
return _skeleton->getAttachment(spineSlotName, spineAttachmentName);
}
bool SkeletonRendererCocos2dX::setAttachment(const std::string &slotName, const std::string &attachmentName) {
bool result = _skeleton->getAttachment(slotName.c_str(), attachmentName.empty() ? 0 : attachmentName.c_str()) ? true : false;
_skeleton->setAttachment(slotName.c_str(), attachmentName.empty() ? 0 : attachmentName.c_str());

View File

@ -79,8 +79,10 @@ namespace spine {
/* Returns 0 if the bone was not found. */
Bone *findBone(const std::string &boneName) const;
Bone *findBone(const char *boneName) const;
/* Returns 0 if the slot was not found. */
Slot *findSlot(const std::string &slotName) const;
Slot *findSlot(const char *slotName) const;
/* Sets the skin used to look up attachments not found in the SkeletonData defaultSkin. Attachments from the new skin are
* attached if the corresponding attachment from the old skin was attached.
@ -91,6 +93,7 @@ namespace spine {
/* Returns 0 if the slot or attachment was not found. */
Attachment *getAttachment(const std::string &slotName, const std::string &attachmentName) const;
Attachment *getAttachment(const char *slotName, const char *attachmentName) const;
/* Returns false if the slot or attachment was not found.
* @param attachmentName May be empty string ("") for no attachment. */
bool setAttachment(const std::string &slotName, const std::string &attachmentName);