Pārlūkot izejas kodu

[c] Removed dependency on kvec

badlogic 7 gadi atpakaļ
vecāks
revīzija
4731a584f3

+ 44 - 48
spine-c/spine-c/src/spine/SkeletonBinary.c

@@ -33,7 +33,7 @@
 #include <spine/extension.h>
 #include <spine/AtlasAttachmentLoader.h>
 #include <spine/Animation.h>
-#include "kvec.h"
+#include <spine/Array.h>
 
 typedef struct {
 	const unsigned char* cursor; 
@@ -244,17 +244,18 @@ static void _spSkeletonBinary_addLinkedMesh (spSkeletonBinary* self, spMeshAttac
 	linkedMesh->parent = parent;
 }
 
+_SP_ARRAY_DECLARE_TYPE(spTimelineArray, spTimeline*)
+_SP_ARRAY_IMPLEMENT_TYPE(spTimelineArray, spTimeline*)
+
 static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, const char* name,
 		_dataInput* input, spSkeletonData *skeletonData) {
-	kvec_t(spTimeline*) timelines;
+	spTimelineArray* timelines = spTimelineArray_create(18);
 	float duration = 0;
 	int i, n, ii, nn, iii, nnn;
 	int frameIndex;
 	int drawOrderCount, eventCount;
 	spAnimation* animation;
 
-	kv_init(timelines);
-
 	/* Slot timelines. */
 	for (i = 0, n = readVarint(input, 1); i < n; ++i) {
 		int slotIndex = readVarint(input, 1);
@@ -272,7 +273,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spAttachmentTimeline_setFrame(timeline, frameIndex, time, attachmentName);
 						FREE(attachmentName);
 					}
-					kv_push(spTimeline*, timelines, SUPER(timeline));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[frameCount - 1]);
 					break;
 				}
@@ -286,7 +287,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spColorTimeline_setFrame(timeline, frameIndex, time, r, g, b, a);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * COLOR_ENTRIES]);
 					break;
 				}
@@ -302,15 +303,15 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spTwoColorTimeline_setFrame(timeline, frameIndex, time, r, g, b, a, r2, g2, b2);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * TWOCOLOR_ENTRIES]);
 					break;
 				}
 				default: {
 					int i;
-					for (i = 0; i < kv_size(timelines); ++i)
-						spTimeline_dispose(kv_A(timelines, i));
-					kv_destroy(timelines);
+					for (i = 0; i < timelines->size; ++i)
+						spTimeline_dispose(timelines->items[i]);
+					spTimelineArray_dispose(timelines);
 					_spSkeletonBinary_setError(self, "Invalid timeline type for a slot: ", skeletonData->slots[slotIndex]->name);
 					return 0;
 				}
@@ -334,7 +335,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spRotateTimeline_setFrame(timeline, frameIndex, time, degrees);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * ROTATE_ENTRIES]);
 					break;
 				}
@@ -365,15 +366,15 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spTranslateTimeline_setFrame(timeline, frameIndex, time, x, y);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER_CAST(spTimeline, timeline));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * TRANSLATE_ENTRIES]);
 					break;
 				}
 				default: {
 					int i;
-					for (i = 0; i < kv_size(timelines); ++i)
-						spTimeline_dispose(kv_A(timelines, i));
-					kv_destroy(timelines);
+					for (i = 0; i < timelines->size; ++i)
+						spTimeline_dispose(timelines->items[i]);
+					spTimelineArray_dispose(timelines);
 					_spSkeletonBinary_setError(self, "Invalid timeline type for a bone: ", skeletonData->bones[boneIndex]->name);
 					return 0;
 				}
@@ -394,7 +395,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 			spIkConstraintTimeline_setFrame(timeline, frameIndex, time, mix, bendDirection);
 			if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 		}
-		kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+		spTimelineArray_add(timelines, (spTimeline*)timeline);
 		duration = MAX(duration, timeline->frames[(frameCount - 1) * IKCONSTRAINT_ENTRIES]);
 	}
 
@@ -414,7 +415,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 					scaleMix, shearMix);
 			if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 		}
-		kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+		spTimelineArray_add(timelines, (spTimeline*)timeline);
 		duration = MAX(duration, timeline->frames[(frameCount - 1) * TRANSFORMCONSTRAINT_ENTRIES]);
 	}
 
@@ -446,7 +447,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spPathConstraintPositionTimeline_setFrame(timeline, frameIndex, time, value);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * PATHCONSTRAINTPOSITION_ENTRIES]);
 					break;
 				}
@@ -460,7 +461,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 						spPathConstraintMixTimeline_setFrame(timeline, frameIndex, time, rotateMix, translateMix);
 						if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
 					}
-					kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+					spTimelineArray_add(timelines, (spTimeline*)timeline);
 					duration = MAX(duration, timeline->frames[(frameCount - 1) * PATHCONSTRAINTMIX_ENTRIES]);
 				}
 			}
@@ -482,10 +483,9 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 				spVertexAttachment* attachment = SUB_CAST(spVertexAttachment,
 						spSkin_getAttachment(skin, slotIndex, attachmentName));
 				if (!attachment) {
-					int i;
-					for (i = 0; i < kv_size(timelines); ++i)
-						spTimeline_dispose(kv_A(timelines, i));
-					kv_destroy(timelines);
+					for (i = 0; i < timelines->size; ++i)
+						spTimeline_dispose(timelines->items[i]);
+					spTimelineArray_dispose(timelines);
 					_spSkeletonBinary_setError(self, "Attachment not found: ", attachmentName);
 					FREE(attachmentName);
 					return 0;
@@ -535,7 +535,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 				}
 				FREE(tempDeform);
 
-				kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
+				spTimelineArray_add(timelines, (spTimeline*)timeline);
 				duration = MAX(duration, timeline->frames[frameCount - 1]);
 			}
 		}
@@ -572,7 +572,7 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 			spDrawOrderTimeline_setFrame(timeline, i, time, drawOrder);
 			FREE(drawOrder);
 		}
-		kv_push(spTimeline*, timelines, SUPER(timeline));
+		spTimelineArray_add(timelines, (spTimeline*)timeline);
 		duration = MAX(duration, timeline->frames[drawOrderCount - 1]);
 	}
 
@@ -592,17 +592,16 @@ static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, con
 				MALLOC_STR(event->stringValue, eventData->stringValue);
 			spEventTimeline_setFrame(timeline, i, event);
 		}
-		kv_push(spTimeline*, timelines, SUPER(timeline));
+		spTimelineArray_add(timelines, (spTimeline*)timeline);
 		duration = MAX(duration, timeline->frames[eventCount - 1]);
 	}
 
-	kv_trim(spTimeline*, timelines);
-
 	animation = spAnimation_create(name, 0);
 	FREE(animation->timelines);
 	animation->duration = duration;
-	animation->timelinesCount = kv_size(timelines);
-	animation->timelines = kv_array(timelines);
+	animation->timelinesCount = timelines->size;
+	animation->timelines = timelines->items;
+	FREE(timelines);
 	return animation;
 }
 
@@ -634,8 +633,8 @@ static void _readVertices(spSkeletonBinary* self, _dataInput* input, spVertexAtt
 		int vertexCount) {
 	int i, ii;
 	int verticesLength = vertexCount << 1;
-	kvec_t(float) weights;
-	kvec_t(int) bones;
+	spFloatArray* weights = spFloatArray_create(8);
+	spIntArray* bones = spIntArray_create(8);
 
 	attachment->worldVerticesLength = verticesLength;
 
@@ -647,30 +646,27 @@ static void _readVertices(spSkeletonBinary* self, _dataInput* input, spVertexAtt
 		return;
 	}
 
-	kv_init(weights);
-	kv_resize(float, weights, verticesLength * 3 * 3);
-
-	kv_init(bones);
-	kv_resize(int, bones, verticesLength * 3);
+	spFloatArray_ensureCapacity(weights, verticesLength * 3 * 3);
+	spIntArray_ensureCapacity(bones, verticesLength * 3);
 
 	for (i = 0; i < vertexCount; ++i) {
 		int boneCount = readVarint(input, 1);
-		kv_push(int, bones, boneCount);
+		spIntArray_add(bones, boneCount);
 		for (ii = 0; ii < boneCount; ++ii) {
-			kv_push(int, bones, readVarint(input, 1));
-			kv_push(float, weights, readFloat(input) * self->scale);
-			kv_push(float, weights, readFloat(input) * self->scale);
-			kv_push(float, weights, readFloat(input));
+			spIntArray_add(bones, readVarint(input, 1));
+			spFloatArray_add(weights, readFloat(input) * self->scale);
+			spFloatArray_add(weights, readFloat(input) * self->scale);
+			spFloatArray_add(weights, readFloat(input));
 		}
 	}
 
-	kv_trim(float, weights);
-	attachment->verticesCount = kv_size(weights);
-	attachment->vertices = kv_array(weights);
+	attachment->verticesCount = weights->size;
+	attachment->vertices = weights->items;
+	FREE(weights);
 
-	kv_trim(int, bones);
-	attachment->bonesCount = kv_size(bones);
-	attachment->bones = kv_array(bones);
+	attachment->bonesCount = bones->size;
+	attachment->bones = bones->items;
+	FREE(bones);
 }
 
 spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput* input,

+ 0 - 105
spine-c/spine-c/src/spine/kvec.h

@@ -1,105 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008, by Attractive Chaos <[email protected]>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/*
-  An example:
-
-#include "kvec.h"
-int main() {
-	kvec_t(int) array;
-	kv_init(array);
-	kv_push(int, array, 10); // append
-	kv_a(int, array, 20) = 5; // dynamic
-	kv_A(array, 20) = 4; // static
-	kv_destroy(array);
-	return 0;
-}
-*/
-
-/*
-  2008-09-22 (0.1.0):
-
-	* The initial version.
-
-  2017-19-18 (0.1.1):
-
-	Spine Special Edition
-	* Made helper macros for alloc, free and memcpy, which can be overridden.
-	* Made these helpers point to the Spine C Runtime alloc and free functions by default
-	* Reimplemented kv_resize to use alloc and free instead of realloc
-	* Changed kv_push to use kv_resize instead of realloc
-	* Removed kv_pushp and kv_a macros because the weren't used
-	* Removed stdlib include
-*/
-
-#ifndef AC_KVEC_H
-#define AC_KVEC_H
-
-#ifndef _kv_free
-#define _kv_free(type, p) (FREE(p))
-#endif
-
-#ifndef _kv_alloc
-#define _kv_alloc(type, s) ((type*)(MALLOC(type, (s))))
-#endif
-
-#ifndef _kv_copy
-#define _kv_copy(type, d, s, n) memcpy((d), (s), sizeof(type) * (n))
-#endif
-
-#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-
-#define kvec_t(type) struct { size_t n, m; type *a; }
-#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
-#define kv_destroy(v) _kv_free(type, (v).a)
-#define kv_A(v, i) ((v).a[(i)])
-#define kv_array(v) ((v).a)
-#define kv_pop(v) ((v).a[--(v).n])
-#define kv_size(v) ((v).n)
-#define kv_max(v) ((v).m)
-
-#define kv_resize(type, v, s) do {									\
-		type* b = _kv_alloc(type, (s));								\
-		if (((s) > 0) && ((v).m > 0))								\
-			_kv_copy(type, b, (v).a, ((s) < (v).m)? (s) : (v).m);	\
-		_kv_free(type, (v).a);										\
-		(v).a = b; (v).m = (s);										\
-	} while (0)
-	
-#define kv_trim(type, v) kv_resize(type, (v), kv_size(v))
-
-#define kv_copy(type, v1, v0) do {								\
-		if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n);		\
-		(v1).n = (v0).n;										\
-		_kv_copy(type, (v1).a, (v0).a, (v0).n);					\
-	} while (0)													\
-
-#define kv_push(type, v, x) do {								\
-		if ((v).n == (v).m) 									\
-			kv_resize(type, (v), ((v).m? (v).m<<1 : 2));		\
-		(v).a[(v).n++] = (x);									\
-	} while (0)
-
-#endif