// 加载纹理集数据
ITextureAtlas* DBCCFactoryEx::loadDotaTextureAtlas(
	const std::string &fcaFile,
	const std::string &name/* = ""*/)
{
	ITextureAtlas *existTextureAtlas = getTextureAtlas(name);

	if (existTextureAtlas)
	{
		refreshTextureAtlasTexture(name.empty() ? existTextureAtlas->textureAtlasData->name : name);
		return existTextureAtlas;
	}

	// textureAtlas scale
	float scale = cocos2d::Director::getInstance()->getContentScaleFactor();

	DotaAnimParser parser;
	DBCCTextureAtlasEx *textureAtlas = new DBCCTextureAtlasEx();
	textureAtlas->_fcaFile = fcaFile;
	textureAtlas->textureAtlasData = parser.parseTextureAtlasData(fcaFile, scale);
	textureAtlas->textureAtlasData->name = name;

	addTextureAtlas(textureAtlas, name);
	refreshTextureAtlasTexture(name.empty() ? textureAtlas->textureAtlasData->name : name);
	return textureAtlas;
}
Exemple #2
0
ITextureAtlas* DBCCFactory::loadTextureAtlas(const std::string &textureAtlasFile, const std::string &name)
{
    ITextureAtlas *existTextureAtlas = getTextureAtlas(name);

    if (existTextureAtlas)
    {
        refreshTextureAtlasTexture(name.empty() ? existTextureAtlas->textureAtlasData->name : name);
        return existTextureAtlas;
    }

    const auto &data = cocos2d::FileUtils::getInstance()->getDataFromFile(textureAtlasFile);
    if (data.getSize() == 0)
    {
        return nullptr;
    }

    // textureAtlas scale
    float scale = cocos2d::Director::getInstance()->getContentScaleFactor();

    dragonBones::XMLDocument doc;
    doc.Parse(reinterpret_cast<char*>(data.getBytes()), data.getSize());
    dragonBones::XMLDataParser parser;
    DBCCTextureAtlas *textureAtlas = new DBCCTextureAtlas();
    textureAtlas->textureAtlasData = parser.parseTextureAtlasData(doc.RootElement(), scale);

    int pos = textureAtlasFile.find_last_of("/");

    if (std::string::npos != pos)
    {
        std::string base_path = textureAtlasFile.substr(0, pos + 1);
        textureAtlas->textureAtlasData->imagePath = base_path + textureAtlas->textureAtlasData->imagePath;
    }

    //
    addTextureAtlas(textureAtlas, name);
    refreshTextureAtlasTexture(name.empty() ? textureAtlas->textureAtlasData->name : name);
    return textureAtlas;
}
void CCSkeleton::draw () {
	CC_NODE_DRAW_SETUP();

	ccGLBlendFunc(blendFunc.src, blendFunc.dst);
	ccColor3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;

	CCTextureAtlas* textureAtlas = 0;
	ccV3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		Slot* slot = skeleton->slots[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
		CCTextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);
		if (regionTextureAtlas != textureAtlas) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
		}
		textureAtlas = regionTextureAtlas;
		if (textureAtlas->getCapacity() == textureAtlas->getTotalQuads() &&
			!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		RegionAttachment_updateQuad(attachment, slot, &quad);
		textureAtlas->updateQuad(&quad, textureAtlas->getTotalQuads());
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

	if (debugSlots) {
		// Slots.
		ccDrawColor4B(0, 0, 255, 255);
		glLineWidth(1);
		CCPoint points[4];
		ccV3F_C4B_T2F_Quad quad;
		for (int i = 0, n = skeleton->slotCount; i < n; i++) {
			Slot* slot = skeleton->slots[i];
			if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
			RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
			RegionAttachment_updateQuad(attachment, slot, &quad);
			points[0] = ccp(quad.bl.vertices.x, quad.bl.vertices.y);
			points[1] = ccp(quad.br.vertices.x, quad.br.vertices.y);
			points[2] = ccp(quad.tr.vertices.x, quad.tr.vertices.y);
			points[3] = ccp(quad.tl.vertices.x, quad.tl.vertices.y);
			ccDrawPoly(points, 4, true);
		}
	}
	if (debugBones) {
		// Bone lengths.
		glLineWidth(2);
		ccDrawColor4B(255, 0, 0, 255);
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			float x = bone->data->length * bone->m00 + bone->worldX;
			float y = bone->data->length * bone->m10 + bone->worldY;
			ccDrawLine(ccp(bone->worldX, bone->worldY), ccp(x, y));
		}
		// Bone origins.
		ccPointSize(4);
		ccDrawColor4B(0, 0, 255, 255); // Root bone is blue.
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			ccDrawPoint(ccp(bone->worldX, bone->worldY));
			if (i == 0) ccDrawColor4B(0, 255, 0, 255);
		}
	}
}
    /**
    * Build and returns a new Armature instance.
    * @example 
    * <listing>
    * var armature:Armature = factory.buildArmature('dragon');
    * </listing>
    * @param    armatureName The name of this Armature instance.
    * @param    The name of this animation
    * @param    The name of this SkeletonData.
    * @param    The name of this textureAtlas.
    * @param    The name of this skin.
    * @return A Armature instance.
    */
    Armature* BaseFactory::buildArmature(const String &armatureName,
                                         const String &animationName,
                                         const String &skeletonName,
                                         const String &textureAtlasName,
                                         const String &skinName)
    {
        ArmatureData* armatureData = 0;
        SkeletonData *data = 0;
        if(!skeletonName.empty())
        {
            std::map<String , SkeletonData*>::iterator iter = _dataDic.find(skeletonName);
            if(iter != _dataDic.end())
            {
                data = iter->second;
                armatureData = data->getArmatureData(armatureName);
            }
        }
        //else
        //{
        //    for(skeletonName in _dataDic)
        //    {
        //        data = _dataDic[skeletonName];
        //        armatureData = data->getArmatureData(armatureName);
        //        if(armatureData)
        //        {
        //            break;
        //        }
        //    }
        //}

        if(!armatureData)
        {
            return nullptr;
        }

        _currentDataName = skeletonName;
        _currentTextureAtlasName = textureAtlasName.empty() ? skeletonName : textureAtlasName;

        Armature* armature = generateArmature();
        armature->name = armatureName;
        Bone* bone;
        for(size_t i = 0 ; i < armatureData->boneDataList.size() ; i ++)
        {
            BoneData* boneData = armatureData->boneDataList[i];
            bone = new Bone();
            bone->name = boneData->name;
            bone->fixedRotation = boneData->fixedRotation;
            bone->scaleMode = boneData->scaleMode;
            bone->origin = boneData->transform;
            if(armatureData->getBoneData(boneData->parent))
            {
                armature->addBone(bone, boneData->parent);
            }
            else
            {
                armature->addBone(bone);
            }
        }

        ArmatureData* animationArmatureData = 0;
        SkinData *skinDataCopy = 0;
        if(!animationName.empty() && animationName != armatureName)
        {
            //ArmatureData* animationArmatureData = data->getArmatureData(animationName);
            // Get the default animation
            //if(!animationArmatureData)
            //{
            //    for (skeletonName in _dataDic)
            //    {
            //        data = _dataDic[skeletonName];
            //        animationArmatureData = data->getArmatureData(animationName);
            //        if(animationArmatureData)
            //        {
            //            break;
            //        }
            //    }
            //}

            ArmatureData* armatureDataCopy = data->getArmatureData(animationName);
            if(armatureDataCopy)
            {
                skinDataCopy = armatureDataCopy->getSkinData("");
            }
        }

        if(animationArmatureData)
        {
            armature->getAnimation()->setAnimationDataList(animationArmatureData->animationDataList);
        }
        else
        {
            armature->getAnimation()->setAnimationDataList(armatureData->animationDataList);
        }

        SkinData* skinData = armatureData->getSkinData(skinName);
        if(!skinData)
        {
            return nullptr;
            //throw new ArgumentError();
        }

        Slot* slot;
        DisplayData* displayData;
        Armature* childArmature;
        size_t i;
        //var helpArray:Array = [];
        for(size_t j = 0 ; j < skinData->slotDataList.size() ; j ++)
        {
            SlotData* slotData = skinData->slotDataList[j];
            bone = armature->getBone(slotData->parent);
            if(!bone)
            {
                continue;
            }
            slot = generateSlot();
            slot->name = slotData->name;
            slot->setBlendMode(slotData->blendMode);
            slot->_originZOrder = slotData->zOrder;
            slot->_dislayDataList = slotData->displayDataList;

            std::vector<Object*> helpArray;

            i = slotData->displayDataList.size();
            helpArray.resize(i);
            while(i --)
            {
                displayData = slotData->displayDataList[i];

                if(displayData->type == DisplayData::ARMATURE)
                {
                    DisplayData* displayDataCopy = 0;
                    if(skinDataCopy)
                    {
                        SlotData* slotDataCopy = skinDataCopy->getSlotData(slotData->name);
                        if(slotDataCopy)
                        {
                            displayDataCopy = slotDataCopy->displayDataList[i];
                        }
                    }
                    else
                    {
                        displayDataCopy = 0;
                    }

                    childArmature = buildArmature(displayData->name, displayDataCopy?displayDataCopy->name:"", _currentDataName, _currentTextureAtlasName);
                    if(childArmature)
                    {
                        helpArray[i] = childArmature;
                    }
				   //fix by Wayne Dimart:
                   // break; we don't use break here, or will crach the program due to incomplete helpArray.
					continue;
                }
                else
                {
                    helpArray[i] = generateDisplay(getTextureAtlas(_currentTextureAtlasName), displayData->name, displayData->pivot.x, displayData->pivot.y);
                }
            }
            slot->setDisplayList(helpArray);
            slot->changeDisplay(0);
            bone->addChild(slot);
        }

        //
        i = armature->_boneList.size();
        while(i --)
        {
            armature->_boneList[i]->update();
        }

        i = armature->_slotList.size();
        while(i --)
        {
            slot = armature->_slotList[i];
            slot->update();
        }
        armature->updateSlotsZOrder();

        return armature;
    }
void Skeleton::onDraw(const Mat4 &transform, uint32_t flags)
{
    getGLProgram()->use();
    getGLProgram()->setUniformsForBuiltins(transform);

    GL::blendFunc(blendFunc.src, blendFunc.dst);
	Color3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;
	if (premultipliedAlpha) {
		skeleton->r *= skeleton->a;
		skeleton->g *= skeleton->a;
		skeleton->b *= skeleton->a;
	}

	int additive = 0;
	TextureAtlas* textureAtlas = 0;
	V3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
		TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);

		if (slot->data->additiveBlending != additive) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
			additive = !additive;
            GL::blendFunc(blendFunc.src, additive ? GL_ONE : blendFunc.dst);
		} else if (regionTextureAtlas != textureAtlas && textureAtlas) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
		}
		textureAtlas = regionTextureAtlas;
        setFittedBlendingFunc(textureAtlas);

		ssize_t quadCount = textureAtlas->getTotalQuads();
		if (textureAtlas->getCapacity() == quadCount) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
			if (!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		}

		spRegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
		textureAtlas->updateQuad(&quad, quadCount);
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

    if(debugBones || debugSlots) {
        Director* director = Director::getInstance();
        CCASSERT(nullptr != director, "Director is null when seting matrix stack");
        director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
        director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);

        if (debugSlots) {
            // Slots.
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
            glLineWidth(1);
            Vec2 points[4];
            V3F_C4B_T2F_Quad tmpQuad;
            for (int i = 0, n = skeleton->slotCount; i < n; i++) {
                spSlot* slot = skeleton->drawOrder[i];
                if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
                spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
                spRegionAttachment_updateQuad(attachment, slot, &tmpQuad);
                points[0] = Vec2(tmpQuad.bl.vertices.x, tmpQuad.bl.vertices.y);
                points[1] = Vec2(tmpQuad.br.vertices.x, tmpQuad.br.vertices.y);
                points[2] = Vec2(tmpQuad.tr.vertices.x, tmpQuad.tr.vertices.y);
                points[3] = Vec2(tmpQuad.tl.vertices.x, tmpQuad.tl.vertices.y);
                DrawPrimitives::drawPoly(points, 4, true);
            }
        }
        if (debugBones) {
            // Bone lengths.
            glLineWidth(2);
            DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                float x = bone->data->length * bone->m00 + bone->worldX;
                float y = bone->data->length * bone->m10 + bone->worldY;
                DrawPrimitives::drawLine(Vec2(bone->worldX, bone->worldY), Vec2(x, y));
            }
            // Bone origins.
            DrawPrimitives::setPointSize(4);
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                DrawPrimitives::drawPoint(Vec2(bone->worldX, bone->worldY));
                if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
            }
        }
        
        director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    }
}
void Skeleton::onDraw ()
{
	CC_NODE_DRAW_SETUP();

    GL::blendFunc(blendFunc.src, blendFunc.dst);
	Color3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;
	if (premultipliedAlpha) {
		skeleton->r *= skeleton->a;
		skeleton->g *= skeleton->a;
		skeleton->b *= skeleton->a;
	}

	int additive = 0;
	TextureAtlas* textureAtlas = 0;
	V3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
		TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);

		if (slot->data->additiveBlending != additive) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
			additive = !additive;
            GL::blendFunc(blendFunc.src, additive ? GL_ONE : blendFunc.dst);
		} else if (regionTextureAtlas != textureAtlas && textureAtlas) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
		}
		textureAtlas = regionTextureAtlas;
        setFittedBlendingFunc(textureAtlas);

		ssize_t quadCount = textureAtlas->getTotalQuads();
		if (textureAtlas->getCapacity() == quadCount) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
			if (!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		}

		spRegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
		textureAtlas->updateQuad(&quad, quadCount);
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

    if(debugBones || debugSlots) {
        kmGLPushMatrix();
        kmGLLoadMatrix(&_modelViewTransform);

        if (debugSlots) {
            // Slots.
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
            glLineWidth(1);
            Point points[4];
            V3F_C4B_T2F_Quad tmpQuad;
            for (int i = 0, n = skeleton->slotCount; i < n; i++) {
                spSlot* slot = skeleton->drawOrder[i];
                if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
                spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
                spRegionAttachment_updateQuad(attachment, slot, &tmpQuad);
                points[0] = Point(tmpQuad.bl.vertices.x, tmpQuad.bl.vertices.y);
                points[1] = Point(tmpQuad.br.vertices.x, tmpQuad.br.vertices.y);
                points[2] = Point(tmpQuad.tr.vertices.x, tmpQuad.tr.vertices.y);
                points[3] = Point(tmpQuad.tl.vertices.x, tmpQuad.tl.vertices.y);
                DrawPrimitives::drawPoly(points, 4, true);
            }
        }
        if (debugBones) {
            // Bone lengths.
            glLineWidth(2);
            DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                float x = bone->data->length * bone->m00 + bone->worldX;
                float y = bone->data->length * bone->m10 + bone->worldY;
                DrawPrimitives::drawLine(Point(bone->worldX, bone->worldY), Point(x, y));
            }
            // Bone origins.
            DrawPrimitives::setPointSize(4);
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                DrawPrimitives::drawPoint(Point(bone->worldX, bone->worldY));
                if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
            }
        }
        
        kmGLPopMatrix();
    }
}