/* Copyright (c) 2018 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/Matrix4x4f.h" #include "gamemath/Armature.h" #include "utilities/IOUtilities.h" #include #include #include #include #define stemobject_implementation Armature stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); Armature * Armature_create(unsigned int boneCount, const ArmatureBone * bones) { stemobject_create_implementation(init, boneCount, bones) } bool Armature_init(Armature * self, unsigned int boneCount, const ArmatureBone * bones) { call_super(init, self); self->boneCount = boneCount; self->bones = memdup(bones, boneCount * sizeof(*bones)); return true; } void Armature_dispose(Armature * self) { free(self->bones); call_super(dispose, self); } Armature * Armature_copy(Armature * self) { return Armature_create(self->boneCount, self->bones); } void Armature_resolveParentBoneIndexes(Armature * self) { for (unsigned int boneIndex = 0; boneIndex < self->boneCount; boneIndex++) { self->bones[boneIndex].parentIndex = Armature_getBoneIndex(self, self->bones[boneIndex].parentID); } } void Armature_addBone(Armature * self, ArmatureBone bone) { Armature_insertBone(self, self->boneCount, bone); } void Armature_insertBone(Armature * self, unsigned int boneIndex, ArmatureBone bone) { assert(boneIndex <= self->boneCount); self->bones = realloc(self->bones, (self->boneCount + 1) * sizeof(*self->bones)); for (unsigned int boneIndex2 = self->boneCount; boneIndex2 > boneIndex; boneIndex2--) { self->bones[boneIndex2] = self->bones[boneIndex2 - 1]; } self->bones[boneIndex] = bone; self->boneCount++; } ArmatureBone * Armature_getBone(Armature * self, ArmatureBoneID boneID) { unsigned int boneIndex = Armature_getBoneIndex(self, boneID); if (boneIndex != BONE_INDEX_NOT_FOUND) { return &self->bones[boneIndex]; } return NULL; } unsigned int Armature_getBoneIndex(Armature * self, ArmatureBoneID boneID) { if (boneID != BONE_ID_NONE) { for (unsigned int boneIndex = 0; boneIndex < self->boneCount; boneIndex++) { if (self->bones[boneIndex].boneID == boneID) { return boneIndex; } } } return BONE_INDEX_NOT_FOUND; } void Armature_removeBoneAtIndex(Armature * self, unsigned int boneIndex) { if (boneIndex < self->boneCount) { self->boneCount--; for (; boneIndex < self->boneCount; boneIndex++) { self->bones[boneIndex] = self->bones[boneIndex + 1]; } } }