void OgreXmlSerializer::ReadBoneHierarchy(Skeleton *skeleton) { if (skeleton->bones.empty()) { throw DeadlyImportError("Cannot read <bonehierarchy> for a Skeleton without bones"); } while(NextNode() == nnBoneParent) { const std::string name = ReadAttribute<std::string>("bone"); const std::string parentName = ReadAttribute<std::string>("parent"); Bone *bone = skeleton->BoneByName(name); Bone *parent = skeleton->BoneByName(parentName); if (bone && parent) parent->AddChild(bone); else throw DeadlyImportError("Failed to find bones for parenting: Child " + name + " for parent " + parentName); } // Calculate bone matrices for root bones. Recursively calculates their children. for (size_t i=0, len=skeleton->bones.size(); i<len; ++i) { Bone *bone = skeleton->bones[i]; if (!bone->IsParented()) bone->CalculateWorldMatrixAndDefaultPose(skeleton); } }
SkeletonSprite::SkeletonSprite(const String& filename) : Sprite(NULL) { // Cargamos los datos del XML SkeletonData* data = new SkeletonData(filename); root = new Bone("world", NULL, 0, 0, 0, 0); // Generamos huesos for ( uint32 i = 0; i < data->GetBoneDatas().Size(); i++ ) { // Obtenemos hueso const BoneData& boneData = data->GetBoneDatas()[i]; // Obtenemos padre del hueso Bone* parent = root; if ( boneData.GetParentName() != "world" ) parent = root->FindChild(boneData.GetParentName()); // Obtenemos imagen Image* image = ResourceManager::Instance().LoadImage(filename.ExtractDir() + "/" + boneData.GetImageFilename()); // Creamos hueso Bone bone(boneData.GetId(), image, boneData.GetPivotX(), boneData.GetPivotY(), boneData.GetHandleX(), boneData.GetHandleY()); // Aniadimos frames for ( uint32 i = 0; i < boneData.GetFrames().Size(); i++ ) bone.AddFrame(boneData.GetFrames()[i]); // Aniadimos hueso parent->AddChild(bone); } // Establecemos el rango de la animacion const Bone* bone = root->GetChild(0); int32 lastframe = 0; for ( uint32 index = 0; index < bone->CountFrames(); index++ ) { lastframe = max(lastframe, bone->GetFrame(index)->GetId()); } SetFrameRange(0, lastframe); // Eliminamos los datos cargados del XML delete data; }