/* 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 "3dmodelio/ArmatureAnimationIO.h" #include ArmatureAnimation * ArmatureAnimation_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { #define stemobject_implementation ArmatureAnimation stemobject_deserialize_implementation(loadSerializedDataMinimal, formatVersion) #undef stemobject_implementation } ArmatureAnimation * ArmatureAnimation_deserialize(compat_type(DeserializationContext *) deserializationContext) { #define stemobject_implementation ArmatureAnimation stemobject_deserialize_implementation(loadSerializedData) #undef stemobject_implementation } bool ArmatureAnimation_loadSerializedDataMinimal(ArmatureAnimation * animation, compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; if (context->status != SERIALIZATION_ERROR_OK || formatVersion > ARMATURE_ANIMATION_FORMAT_VERSION) { return false; } if (formatVersion == 0) { call_virtual(readStringNullable, context, "name"); } bool loop = call_virtual(readBoolean, context, "loop"); unsigned int keyframeCount = call_virtual(beginArray, context, "keyframes"); if (context->status != SERIALIZATION_ERROR_OK) { return false; } ArmatureAnimationKeyframe * keyframes = NULL; if (keyframeCount > 0) { keyframes = calloc(keyframeCount, sizeof(*keyframes)); for (unsigned int keyframeIndex = 0; keyframeIndex < keyframeCount; keyframeIndex++) { call_virtual(beginStructure, context, "keyframe"); keyframes[keyframeIndex].interval = call_virtual(readDouble, context, "interval"); if (formatVersion < 2) { keyframes[keyframeIndex].boneCount = call_virtual(beginDictionary, context, "bones"); } else { keyframes[keyframeIndex].boneCount = call_virtual(beginArray, context, "bones"); } keyframes[keyframeIndex].bones = malloc(sizeof(*keyframes[keyframeIndex].bones) * keyframes[keyframeIndex].boneCount); for (unsigned int boneIndex = 0; boneIndex < keyframes[keyframeIndex].boneCount; boneIndex++) { if (formatVersion < 2) { const char * boneIDString = call_virtual(readNextDictionaryKey, context); call_virtual(beginStructure, context, boneIDString); keyframes[keyframeIndex].bones[boneIndex].boneID = boneIndex; } else { call_virtual(beginStructure, context, NULL); keyframes[keyframeIndex].bones[boneIndex].boneID = call_virtual(readUInt32, context, "bone_id"); } call_virtual(beginStructure, context, "offset"); keyframes[keyframeIndex].bones[boneIndex].offset.x = call_virtual(readFloat, context, "x"); keyframes[keyframeIndex].bones[boneIndex].offset.y = call_virtual(readFloat, context, "y"); keyframes[keyframeIndex].bones[boneIndex].offset.z = call_virtual(readFloat, context, "z"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "offset_curve"); keyframes[keyframeIndex].bones[boneIndex].incomingOffsetBezierHandle.x = call_virtual(readFloat, context, "x_in"); keyframes[keyframeIndex].bones[boneIndex].incomingOffsetBezierHandle.y = call_virtual(readFloat, context, "y_in"); keyframes[keyframeIndex].bones[boneIndex].outgoingOffsetBezierHandle.x = call_virtual(readFloat, context, "x_out"); keyframes[keyframeIndex].bones[boneIndex].outgoingOffsetBezierHandle.y = call_virtual(readFloat, context, "y_out"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale"); keyframes[keyframeIndex].bones[boneIndex].scale.x = call_virtual(readFloat, context, "x"); keyframes[keyframeIndex].bones[boneIndex].scale.y = call_virtual(readFloat, context, "y"); keyframes[keyframeIndex].bones[boneIndex].scale.z = call_virtual(readFloat, context, "z"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale_curve"); keyframes[keyframeIndex].bones[boneIndex].incomingScaleBezierHandle.x = call_virtual(readFloat, context, "x_in"); keyframes[keyframeIndex].bones[boneIndex].incomingScaleBezierHandle.y = call_virtual(readFloat, context, "y_in"); keyframes[keyframeIndex].bones[boneIndex].outgoingScaleBezierHandle.x = call_virtual(readFloat, context, "x_out"); keyframes[keyframeIndex].bones[boneIndex].outgoingScaleBezierHandle.y = call_virtual(readFloat, context, "y_out"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation"); keyframes[keyframeIndex].bones[boneIndex].rotation.x = call_virtual(readFloat, context, "x"); keyframes[keyframeIndex].bones[boneIndex].rotation.y = call_virtual(readFloat, context, "y"); keyframes[keyframeIndex].bones[boneIndex].rotation.z = call_virtual(readFloat, context, "z"); keyframes[keyframeIndex].bones[boneIndex].rotation.w = call_virtual(readFloat, context, "w"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation_curve"); keyframes[keyframeIndex].bones[boneIndex].incomingRotationBezierHandle.x = call_virtual(readFloat, context, "x_in"); keyframes[keyframeIndex].bones[boneIndex].incomingRotationBezierHandle.y = call_virtual(readFloat, context, "y_in"); keyframes[keyframeIndex].bones[boneIndex].outgoingRotationBezierHandle.x = call_virtual(readFloat, context, "x_out"); keyframes[keyframeIndex].bones[boneIndex].outgoingRotationBezierHandle.y = call_virtual(readFloat, context, "y_out"); call_virtual(endStructure, context); call_virtual(endStructure, context); } call_virtual(endDictionary, context); call_virtual(endStructure, context); if (context->status != SERIALIZATION_ERROR_OK) { break; } } } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { for (unsigned int keyframeIndex2 = 0; keyframeIndex2 < keyframeCount; keyframeIndex2++) { free(keyframes[keyframeIndex2].bones); } free(keyframes); return false; } unsigned int markerCount; if (formatVersion < 2) { markerCount = call_virtual(beginDictionary, context, "markers"); } else { markerCount = call_virtual(beginArray, context, "markers"); } if (context->status != SERIALIZATION_ERROR_OK) { return false; } ArmatureAnimationMarker * markers = NULL; if (markerCount > 0) { markers = malloc(sizeof(*markers) * markerCount); for (unsigned int markerIndex = 0; markerIndex < markerCount; markerIndex++) { if (formatVersion < 2) { markers[markerIndex].identifier = markerIndex; markers[markerIndex].time = call_virtual(readDouble, context, call_virtual2(readNextDictionaryKey, context)); } else { call_virtual(beginStructure, context, NULL); markers[markerIndex].identifier = call_virtual(readUInt32, context, "id"); markers[markerIndex].time = call_virtual(readDouble, context, "time"); call_virtual(endStructure, context); } } } if (formatVersion < 2) { call_virtual(endDictionary, context); } else { call_virtual(endArray, context); } if (context->status != SERIALIZATION_ERROR_OK) { for (unsigned int keyframeIndex = 0; keyframeIndex < keyframeCount; keyframeIndex++) { free(keyframes[keyframeIndex].bones); } free(keyframes); free(markers); return false; } bool success = ArmatureAnimation_init(animation, loop, keyframeCount, keyframes, markerCount, markers); for (unsigned int keyframeIndex = 0; keyframeIndex < keyframeCount; keyframeIndex++) { free(keyframes[keyframeIndex].bones); } free(keyframes); free(markers); return success; } bool ArmatureAnimation_loadSerializedData(ArmatureAnimation * animation, compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, ARMATURE_ANIMATION_FORMAT_TYPE); const char * formatType = call_virtual(readString, context, "format_type"); if (context->status != SERIALIZATION_ERROR_OK || strcmp(formatType, ARMATURE_ANIMATION_FORMAT_TYPE)) { return NULL; } uint16_t formatVersion = call_virtual(readUInt16, context, "format_version"); bool success = ArmatureAnimation_loadSerializedDataMinimal(animation, context, formatVersion); call_virtual(endStructure, context); return success; } void ArmatureAnimation_serializeMinimal(ArmatureAnimation * animation, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(writeBoolean, context, "loop", animation->loop); call_virtual(beginArray, context, "keyframes"); for (unsigned int keyframeIndex = 0; keyframeIndex < animation->keyframeCount; keyframeIndex++) { call_virtual(beginStructure, context, "keyframe"); call_virtual(writeDouble, context, "interval", animation->keyframes[keyframeIndex].interval); call_virtual(beginArray, context, "bones"); for (unsigned int boneIndex = 0; boneIndex < animation->keyframes[keyframeIndex].boneCount; boneIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt32, context, "bone_id", animation->keyframes[keyframeIndex].bones[boneIndex].boneID); call_virtual(beginStructure, context, "offset"); call_virtual(writeFloat, context, "x", animation->keyframes[keyframeIndex].bones[boneIndex].offset.x); call_virtual(writeFloat, context, "y", animation->keyframes[keyframeIndex].bones[boneIndex].offset.y); call_virtual(writeFloat, context, "z", animation->keyframes[keyframeIndex].bones[boneIndex].offset.z); call_virtual(endStructure, context); call_virtual(beginStructure, context, "offset_curve"); call_virtual(writeFloat, context, "x_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingOffsetBezierHandle.x); call_virtual(writeFloat, context, "y_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingOffsetBezierHandle.y); call_virtual(writeFloat, context, "x_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingOffsetBezierHandle.x); call_virtual(writeFloat, context, "y_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingOffsetBezierHandle.y); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale"); call_virtual(writeFloat, context, "x", animation->keyframes[keyframeIndex].bones[boneIndex].scale.x); call_virtual(writeFloat, context, "y", animation->keyframes[keyframeIndex].bones[boneIndex].scale.y); call_virtual(writeFloat, context, "z", animation->keyframes[keyframeIndex].bones[boneIndex].scale.z); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale_curve"); call_virtual(writeFloat, context, "x_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingScaleBezierHandle.x); call_virtual(writeFloat, context, "y_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingScaleBezierHandle.y); call_virtual(writeFloat, context, "x_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingScaleBezierHandle.x); call_virtual(writeFloat, context, "y_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingScaleBezierHandle.y); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation"); call_virtual(writeFloat, context, "x", animation->keyframes[keyframeIndex].bones[boneIndex].rotation.x); call_virtual(writeFloat, context, "y", animation->keyframes[keyframeIndex].bones[boneIndex].rotation.y); call_virtual(writeFloat, context, "z", animation->keyframes[keyframeIndex].bones[boneIndex].rotation.z); call_virtual(writeFloat, context, "w", animation->keyframes[keyframeIndex].bones[boneIndex].rotation.w); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation_curve"); call_virtual(writeFloat, context, "x_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingRotationBezierHandle.x); call_virtual(writeFloat, context, "y_in", animation->keyframes[keyframeIndex].bones[boneIndex].incomingRotationBezierHandle.y); call_virtual(writeFloat, context, "x_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingRotationBezierHandle.x); call_virtual(writeFloat, context, "y_out", animation->keyframes[keyframeIndex].bones[boneIndex].outgoingRotationBezierHandle.y); call_virtual(endStructure, context); call_virtual(endStructure, context); } call_virtual(endDictionary, context); call_virtual(endStructure, context); } call_virtual(endArray, context); call_virtual(beginArray, context, "markers"); for (unsigned int markerIndex = 0; markerIndex < animation->markerCount; markerIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt32, context, "id", animation->markers[markerIndex].identifier); call_virtual(writeDouble, context, "time", animation->markers[markerIndex].time); call_virtual(endStructure, context); } call_virtual(endArray, context); } void ArmatureAnimation_serialize(ArmatureAnimation * animation, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginStructure, context, ARMATURE_ANIMATION_FORMAT_TYPE); call_virtual(writeString, context, "format_type", ARMATURE_ANIMATION_FORMAT_TYPE); call_virtual(writeUInt16, context, "format_version", ARMATURE_ANIMATION_FORMAT_VERSION); ArmatureAnimation_serializeMinimal(animation, context); call_virtual(endStructure, context); }