From a3f23dd4bf82bfcd468576ff79fb7c73474aef98 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 31 Mar 2013 20:09:20 +0200 Subject: [PATCH] Refactoring for cleaner OOP. --- spine-c/example/main.c | 69 +++++----- spine-c/include/spine/Animation.h | 19 ++- spine-c/include/spine/Atlas.h | 12 +- spine-c/include/spine/AtlasAttachmentLoader.h | 2 +- spine-c/include/spine/Attachment.h | 5 +- spine-c/include/spine/AttachmentLoader.h | 5 +- spine-c/include/spine/Bone.h | 4 +- spine-c/include/spine/BoneData.h | 4 +- spine-c/include/spine/RegionAttachment.h | 2 +- spine-c/include/spine/Skeleton.h | 6 +- spine-c/include/spine/SkeletonData.h | 4 +- spine-c/include/spine/SkeletonJson.h | 6 +- spine-c/include/spine/Skin.h | 4 +- spine-c/include/spine/Slot.h | 4 +- spine-c/include/spine/SlotData.h | 4 +- spine-c/src/spine/Animation.c | 120 +++++++++--------- spine-c/src/spine/Atlas.c | 40 +++--- spine-c/src/spine/AtlasAttachmentLoader.c | 18 +-- spine-c/src/spine/Attachment.c | 16 ++- spine-c/src/spine/AttachmentLoader.c | 26 ++-- spine-c/src/spine/Bone.c | 52 ++++---- spine-c/src/spine/BoneData.c | 14 +- spine-c/src/spine/Json.c | 29 ++--- spine-c/src/spine/Json.h | 10 +- spine-c/src/spine/RegionAttachment.c | 5 +- spine-c/src/spine/Skeleton.c | 34 ++--- spine-c/src/spine/SkeletonData.c | 9 +- spine-c/src/spine/SkeletonJson.c | 102 +++++++-------- spine-c/src/spine/Skin.c | 36 +++--- spine-c/src/spine/Slot.c | 25 ++-- spine-c/src/spine/SlotData.c | 14 +- spine-c/src/spine/extension.h | 46 ++++++- spine-c/src/spine/util.c | 2 +- spine-c/src/spine/util.h | 29 ++++- spine-sfml/src/main.cpp | 12 +- spine-sfml/src/spine/spine-sfml.cpp | 78 ++++++------ 36 files changed, 459 insertions(+), 408 deletions(-) diff --git a/spine-c/example/main.c b/spine-c/example/main.c index 8fea240b7..928836e30 100644 --- a/spine-c/example/main.c +++ b/spine-c/example/main.c @@ -5,7 +5,6 @@ #include #include #include -#include /**/ @@ -14,23 +13,23 @@ typedef struct { int extraData; } ExampleAtlasPage; -void _ExampleAtlasPage_dispose (AtlasPage* page) { - ExampleAtlasPage* self = (ExampleAtlasPage*)page; - _AtlasPage_deinit(&self->super); +void _ExampleAtlasPage_free (AtlasPage* page) { + ExampleAtlasPage* self = SUB_CAST(ExampleAtlasPage, page); + _AtlasPage_deinit(SUPER(self)); self->extraData = 0; - FREE(self) + FREE(self); } -AtlasPage* AtlasPage_create (const char* name) { - ExampleAtlasPage* self = CALLOC(ExampleAtlasPage, 1) - _AtlasPage_init(&self->super, name); - self->super._dispose = _ExampleAtlasPage_dispose; +AtlasPage* AtlasPage_new (const char* name) { + ExampleAtlasPage* self = NEW(ExampleAtlasPage); + _AtlasPage_init(SUPER(self), name); + VTABLE(AtlasPage, self) ->free = _ExampleAtlasPage_free; self->extraData = 123; - return &self->super; + return SUPER(self); } /**/ @@ -40,23 +39,23 @@ typedef struct { int extraData; } ExampleSkeleton; -void _ExampleSkeleton_dispose (Skeleton* skeleton) { - ExampleSkeleton* self = (ExampleSkeleton*)skeleton; - _Skeleton_deinit(&self->super); +void _ExampleSkeleton_free (Skeleton* skeleton) { + ExampleSkeleton* self = SUB_CAST(ExampleSkeleton, skeleton); + _Skeleton_deinit(SUPER(self)); self->extraData = 0; - FREE(self) + FREE(self); } -Skeleton* Skeleton_create (SkeletonData* data) { - ExampleSkeleton* self = CALLOC(ExampleSkeleton, 1) - _Skeleton_init(&self->super, data); - self->super._dispose = _ExampleSkeleton_dispose; +Skeleton* Skeleton_new (SkeletonData* data) { + ExampleSkeleton* self = NEW(ExampleSkeleton); + _Skeleton_init(SUPER(self), data); + VTABLE(Skeleton, self) ->free = _ExampleSkeleton_free; self->extraData = 789; - return &self->super; + return SUPER(self); } /**/ @@ -66,13 +65,13 @@ typedef struct { int extraData; } ExampleRegionAttachment; -void _ExampleRegionAttachment_dispose (Attachment* attachment) { - ExampleRegionAttachment* self = (ExampleRegionAttachment*)attachment; - _RegionAttachment_deinit(&self->super); +void _ExampleRegionAttachment_free (Attachment* attachment) { + ExampleRegionAttachment* self = SUB_CAST(ExampleRegionAttachment, attachment); + _RegionAttachment_deinit(SUPER(self)); self->extraData = 0; - FREE(self) + FREE(self); } void _ExampleRegionAttachment_draw (Attachment* attachment, Slot* slot) { @@ -80,15 +79,15 @@ void _ExampleRegionAttachment_draw (Attachment* attachment, Slot* slot) { // Draw or queue region for drawing. } -RegionAttachment* RegionAttachment_create (const char* name, AtlasRegion* region) { - ExampleRegionAttachment* self = CALLOC(ExampleRegionAttachment, 1) - _RegionAttachment_init(&self->super, name); - self->super.super._dispose = _ExampleRegionAttachment_dispose; - self->super.super._draw = _ExampleRegionAttachment_draw; +RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) { + ExampleRegionAttachment* self = NEW(ExampleRegionAttachment); + _RegionAttachment_init(SUPER(self), name); + VTABLE(Attachment, self) ->free = _ExampleRegionAttachment_free; + VTABLE(Attachment, self) ->draw = _ExampleRegionAttachment_draw; self->extraData = 456; - return &self->super; + return SUPER(self); } /**/ @@ -98,22 +97,22 @@ int main (void) { printf("First region name: %s, x: %d, y: %d\n", atlas->regions->name, atlas->regions->x, atlas->regions->y); printf("First page name: %s, extraData: %d\n", atlas->pages->name, ((ExampleAtlasPage*)atlas->pages)->extraData); - SkeletonJson* json = SkeletonJson_create(atlas); + SkeletonJson* json = SkeletonJson_new(atlas); SkeletonData *skeletonData = SkeletonJson_readSkeletonDataFile(json, "data/spineboy-skeleton.json"); if (!skeletonData) printf("Error: %s\n", json->error); printf("Attachment extraData: %d\n", ((ExampleRegionAttachment*)skeletonData->defaultSkin->entries->attachment)->extraData); - Skeleton* skeleton = Skeleton_create(skeletonData); + Skeleton* skeleton = Skeleton_new(skeletonData); printf("Skeleton extraData: %d\n", ((ExampleSkeleton*)skeleton)->extraData); Animation* animation = SkeletonJson_readAnimationFile(json, "data/spineboy-walk.json", skeletonData); if (!animation) printf("Error: %s\n", json->error); printf("Animation timelineCount: %d\n", animation->timelineCount); - Skeleton_dispose(skeleton); - SkeletonData_dispose(skeletonData); - SkeletonJson_dispose(json); - Atlas_dispose(atlas); + Skeleton_free(skeleton); + SkeletonData_free(skeletonData); + SkeletonJson_free(json); + Atlas_free(atlas); return 0; } diff --git a/spine-c/include/spine/Animation.h b/spine-c/include/spine/Animation.h index fc4474886..e2a6e56c8 100644 --- a/spine-c/include/spine/Animation.h +++ b/spine-c/include/spine/Animation.h @@ -42,8 +42,8 @@ typedef struct { float duration; } Animation; -Animation* Animation_create (int timelineCount); -void Animation_dispose (Animation* animation); +Animation* Animation_new (int timelineCount); +void Animation_free (Animation* animation); void Animation_apply (const Animation* animation, Skeleton* skeleton, float time, int/*bool*/loop); void Animation_mix (const Animation* animation, Skeleton* skeleton, float time, int/*bool*/loop, float alpha); @@ -51,11 +51,10 @@ void Animation_mix (const Animation* animation, Skeleton* skeleton, float time, /**/ struct Timeline { - void (*_apply) (const Timeline* timeline, Skeleton* skeleton, float time, float alpha); - void (*_dispose) (Timeline* timeline); + const void* const vtable; }; -void Timeline_dispose (Timeline* timeline); +void Timeline_free (Timeline* timeline); void Timeline_apply (const Timeline* timeline, Skeleton* skeleton, float time, float alpha); /**/ @@ -83,7 +82,7 @@ typedef struct BaseTimeline { int boneIndex; } RotateTimeline; -RotateTimeline* RotateTimeline_create (int frameCount); +RotateTimeline* RotateTimeline_new (int frameCount); void RotateTimeline_setFrame (RotateTimeline* timeline, int frameIndex, float time, float angle); @@ -91,7 +90,7 @@ void RotateTimeline_setFrame (RotateTimeline* timeline, int frameIndex, float ti typedef struct BaseTimeline TranslateTimeline; -TranslateTimeline* TranslateTimeline_create (int frameCount); +TranslateTimeline* TranslateTimeline_new (int frameCount); void TranslateTimeline_setFrame (TranslateTimeline* timeline, int frameIndex, float time, float x, float y); @@ -99,7 +98,7 @@ void TranslateTimeline_setFrame (TranslateTimeline* timeline, int frameIndex, fl typedef struct BaseTimeline ScaleTimeline; -ScaleTimeline* ScaleTimeline_create (int frameCount); +ScaleTimeline* ScaleTimeline_new (int frameCount); void ScaleTimeline_setFrame (ScaleTimeline* timeline, int frameIndex, float time, float x, float y); @@ -112,7 +111,7 @@ typedef struct { int slotIndex; } ColorTimeline; -ColorTimeline* ColorTimeline_create (int frameCount); +ColorTimeline* ColorTimeline_new (int frameCount); void ColorTimeline_setFrame (ColorTimeline* timeline, int frameIndex, float time, float r, float g, float b, float a); @@ -126,7 +125,7 @@ typedef struct { const char** const attachmentNames; } AttachmentTimeline; -AttachmentTimeline* AttachmentTimeline_create (int frameCount); +AttachmentTimeline* AttachmentTimeline_new (int frameCount); /* @param attachmentName May be 0. */ void AttachmentTimeline_setFrame (AttachmentTimeline* timeline, int frameIndex, float time, const char* attachmentName); diff --git a/spine-c/include/spine/Atlas.h b/spine-c/include/spine/Atlas.h index 5aa466c4c..14e8175fd 100644 --- a/spine-c/include/spine/Atlas.h +++ b/spine-c/include/spine/Atlas.h @@ -57,11 +57,11 @@ struct AtlasPage { AtlasWrap uWrap, vWrap; AtlasPage* next; - void (*_dispose) (AtlasPage* page); + const void* const vtable; }; -AtlasPage* AtlasPage_create (const char* name); -void AtlasPage_dispose (AtlasPage* page); +AtlasPage* AtlasPage_new (const char* name); +void AtlasPage_free (AtlasPage* page); /**/ @@ -80,8 +80,8 @@ struct AtlasRegion { AtlasRegion* next; }; -AtlasRegion* AtlasRegion_create (); -void AtlasRegion_dispose (AtlasRegion* region); +AtlasRegion* AtlasRegion_new (); +void AtlasRegion_free (AtlasRegion* region); /**/ @@ -92,7 +92,7 @@ typedef struct { Atlas* Atlas_readAtlas (const char* data); Atlas* Atlas_readAtlasFile (const char* path); -void Atlas_dispose (Atlas* atlas); +void Atlas_free (Atlas* atlas); AtlasRegion* Atlas_findRegion (const Atlas* atlas, const char* name); diff --git a/spine-c/include/spine/AtlasAttachmentLoader.h b/spine-c/include/spine/AtlasAttachmentLoader.h index 8f5956f05..e87cf07ef 100644 --- a/spine-c/include/spine/AtlasAttachmentLoader.h +++ b/spine-c/include/spine/AtlasAttachmentLoader.h @@ -39,7 +39,7 @@ typedef struct { Atlas* atlas; } AtlasAttachmentLoader; -AtlasAttachmentLoader* AtlasAttachmentLoader_create (Atlas* atlas); +AtlasAttachmentLoader* AtlasAttachmentLoader_new (Atlas* atlas); #ifdef __cplusplus } diff --git a/spine-c/include/spine/Attachment.h b/spine-c/include/spine/Attachment.h index 9d399939b..041280d16 100644 --- a/spine-c/include/spine/Attachment.h +++ b/spine-c/include/spine/Attachment.h @@ -42,11 +42,10 @@ struct Attachment { const char* const name; int type; - void (*_draw) (Attachment* attachment, struct Slot* slot); - void (*_dispose) (Attachment* attachment); + const void* const vtable; }; -void Attachment_dispose (Attachment* attachment); +void Attachment_free (Attachment* attachment); void Attachment_draw (Attachment* attachment, struct Slot* slot); diff --git a/spine-c/include/spine/AttachmentLoader.h b/spine-c/include/spine/AttachmentLoader.h index d0c9a86e5..ab49b44c8 100644 --- a/spine-c/include/spine/AttachmentLoader.h +++ b/spine-c/include/spine/AttachmentLoader.h @@ -38,11 +38,10 @@ struct AttachmentLoader { const char* error1; const char* error2; - Attachment* (*_newAttachment) (AttachmentLoader* loader, AttachmentType type, const char* name); - void (*_dispose) (AttachmentLoader* loader); + const void* const vtable; }; -void AttachmentLoader_dispose (AttachmentLoader* loader); +void AttachmentLoader_free (AttachmentLoader* loader); Attachment* AttachmentLoader_newAttachment (AttachmentLoader* loader, AttachmentType type, const char* name); diff --git a/spine-c/include/spine/Bone.h b/spine-c/include/spine/Bone.h index f6febad3f..fff25013e 100644 --- a/spine-c/include/spine/Bone.h +++ b/spine-c/include/spine/Bone.h @@ -50,8 +50,8 @@ struct Bone { void Bone_setYDown (int/*bool*/yDown); /* @param parent May be zero. */ -Bone* Bone_create (BoneData* data, Bone* parent); -void Bone_dispose (Bone* bone); +Bone* Bone_new (BoneData* data, Bone* parent); +void Bone_free (Bone* bone); void Bone_setToBindPose (Bone* bone); diff --git a/spine-c/include/spine/BoneData.h b/spine-c/include/spine/BoneData.h index 039fd7cfd..8c65623a6 100644 --- a/spine-c/include/spine/BoneData.h +++ b/spine-c/include/spine/BoneData.h @@ -41,8 +41,8 @@ struct BoneData { float scaleX, scaleY; }; -BoneData* BoneData_create (const char* name, BoneData* parent); -void BoneData_dispose (BoneData* boneData); +BoneData* BoneData_new (const char* name, BoneData* parent); +void BoneData_free (BoneData* boneData); #ifdef __cplusplus } diff --git a/spine-c/include/spine/RegionAttachment.h b/spine-c/include/spine/RegionAttachment.h index db4d3d942..438ecda1e 100644 --- a/spine-c/include/spine/RegionAttachment.h +++ b/spine-c/include/spine/RegionAttachment.h @@ -43,7 +43,7 @@ struct RegionAttachment { void RegionAttachment_updateOffset (RegionAttachment* attachment); -RegionAttachment* RegionAttachment_create (const char* name, AtlasRegion* region); +RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region); #ifdef __cplusplus } diff --git a/spine-c/include/spine/Skeleton.h b/spine-c/include/spine/Skeleton.h index 1dbc61743..4206a224b 100644 --- a/spine-c/include/spine/Skeleton.h +++ b/spine-c/include/spine/Skeleton.h @@ -51,11 +51,11 @@ struct Skeleton { float time; int/*bool*/flipX, flipY; - void (*_dispose) (Skeleton* skeleton); + const void* const vtable; }; -Skeleton* Skeleton_create (SkeletonData* data); -void Skeleton_dispose (Skeleton* skeleton); +Skeleton* Skeleton_new (SkeletonData* data); +void Skeleton_free (Skeleton* skeleton); void Skeleton_updateWorldTransform (const Skeleton* skeleton); diff --git a/spine-c/include/spine/SkeletonData.h b/spine-c/include/spine/SkeletonData.h index 45661bacd..3969ad730 100644 --- a/spine-c/include/spine/SkeletonData.h +++ b/spine-c/include/spine/SkeletonData.h @@ -47,8 +47,8 @@ typedef struct { Skin* defaultSkin; } SkeletonData; -SkeletonData* SkeletonData_create (); -void SkeletonData_dispose (SkeletonData* skeletonData); +SkeletonData* SkeletonData_new (); +void SkeletonData_free (SkeletonData* skeletonData); BoneData* SkeletonData_findBone (const SkeletonData* skeletonData, const char* boneName); int SkeletonData_findBoneIndex (const SkeletonData* skeletonData, const char* boneName); diff --git a/spine-c/include/spine/SkeletonJson.h b/spine-c/include/spine/SkeletonJson.h index dc7ac78d9..6ebb965f3 100644 --- a/spine-c/include/spine/SkeletonJson.h +++ b/spine-c/include/spine/SkeletonJson.h @@ -43,9 +43,9 @@ typedef struct { const char* const error; } SkeletonJson; -SkeletonJson* SkeletonJson_createWithLoader (AttachmentLoader* attachmentLoader); -SkeletonJson* SkeletonJson_create (Atlas* atlas); -void SkeletonJson_dispose (SkeletonJson* skeletonJson); +SkeletonJson* SkeletonJson_newWithLoader (AttachmentLoader* attachmentLoader); +SkeletonJson* SkeletonJson_new (Atlas* atlas); +void SkeletonJson_free (SkeletonJson* skeletonJson); SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* skeletonJson, const char* json); SkeletonData* SkeletonJson_readSkeletonDataFile (SkeletonJson* skeletonJson, const char* path); diff --git a/spine-c/include/spine/Skin.h b/spine-c/include/spine/Skin.h index 8ea987e12..5327a8a83 100644 --- a/spine-c/include/spine/Skin.h +++ b/spine-c/include/spine/Skin.h @@ -46,8 +46,8 @@ typedef struct { const SkinEntry* const entries; } Skin; -Skin* Skin_create (const char* name); -void Skin_dispose (Skin* skin); +Skin* Skin_new (const char* name); +void Skin_free (Skin* skin); /* The Skin owns the attachment. */ void Skin_addAttachment (Skin* skin, int slotIndex, const char* name, Attachment* attachment); diff --git a/spine-c/include/spine/Slot.h b/spine-c/include/spine/Slot.h index a949f1548..1c28151ed 100644 --- a/spine-c/include/spine/Slot.h +++ b/spine-c/include/spine/Slot.h @@ -45,8 +45,8 @@ typedef struct Slot { Attachment* const attachment; } Slot; -Slot* Slot_create (SlotData* data, struct Skeleton* skeleton, Bone* bone); -void Slot_dispose (Slot* slot); +Slot* Slot_new (SlotData* data, struct Skeleton* skeleton, Bone* bone); +void Slot_free (Slot* slot); /* @param attachment May be null. */ void Slot_setAttachment (Slot* slot, Attachment* attachment); diff --git a/spine-c/include/spine/SlotData.h b/spine-c/include/spine/SlotData.h index 6dfca2761..7b4a3d142 100644 --- a/spine-c/include/spine/SlotData.h +++ b/spine-c/include/spine/SlotData.h @@ -40,8 +40,8 @@ typedef struct { float r, g, b, a; } SlotData; -SlotData* SlotData_create (const char* name, BoneData* boneData); -void SlotData_dispose (SlotData* slotData); +SlotData* SlotData_new (const char* name, BoneData* boneData); +void SlotData_free (SlotData* slotData); /* @param attachmentName May be zero. */ void SlotData_setAttachmentName (SlotData* slotData, const char* attachmentName); diff --git a/spine-c/src/spine/Animation.c b/spine-c/src/spine/Animation.c index 4f5c8c4d9..a50e48462 100644 --- a/spine-c/src/spine/Animation.c +++ b/spine-c/src/spine/Animation.c @@ -25,21 +25,21 @@ #include #include -#include +#include -Animation* Animation_create (int timelineCount) { - Animation* self = CALLOC(Animation, 1); +Animation* Animation_new (int timelineCount) { + Animation* self = NEW(Animation); self->timelineCount = timelineCount; - self->timelines = MALLOC(Timeline*, timelineCount) + self->timelines = MALLOC(Timeline*, timelineCount); return self; } -void Animation_dispose (Animation* self) { +void Animation_free (Animation* self) { int i; for (i = 0; i < self->timelineCount; ++i) - Timeline_dispose(self->timelines[i]); - FREE(self->timelines) - FREE(self) + Timeline_free(self->timelines[i]); + FREE(self->timelines); + FREE(self); } void Animation_apply (const Animation* self, Skeleton* skeleton, float time, int/*bool*/loop) { @@ -60,18 +60,20 @@ void Animation_mix (const Animation* self, Skeleton* skeleton, float time, int/* /**/ -void _Timeline_init (Timeline* timeline) { +void _Timeline_init (Timeline* self) { + CONST_CAST(_TimelineVtable*, self->vtable) = NEW(_TimelineVtable); } -void _Timeline_deinit (Timeline* timeline) { +void _Timeline_deinit (Timeline* self) { + FREE(self->vtable); } -void Timeline_dispose (Timeline* self) { - self->_dispose(self); +void Timeline_free (Timeline* self) { + VTABLE(Timeline, self) ->dispose(self); } void Timeline_apply (const Timeline* self, Skeleton* skeleton, float time, float alpha) { - self->_apply(self, skeleton, time, alpha); + VTABLE(Timeline, self) ->apply(self, skeleton, time, alpha); } /**/ @@ -81,13 +83,13 @@ static const float CURVE_STEPPED = -1; static const int CURVE_SEGMENTS = 10; void _CurveTimeline_init (CurveTimeline* self, int frameCount) { - _Timeline_init(&self->super); - self->curves = CALLOC(float, (frameCount - 1) * 6) + _Timeline_init(SUPER(self)); + self->curves = CALLOC(float, (frameCount - 1) * 6); } void _CurveTimeline_deinit (CurveTimeline* self) { - _Timeline_deinit(&self->super); - FREE(self->curves) + _Timeline_deinit(SUPER(self)); + FREE(self->curves); } void CurveTimeline_setLinear (CurveTimeline* self, int frameIndex) { @@ -177,21 +179,21 @@ static int binarySearch (float *values, int valuesLength, float target, int step /**/ -void _BaseTimeline_dispose (Timeline* timeline) { - struct BaseTimeline* self = (struct BaseTimeline*)timeline; - _CurveTimeline_deinit(&self->super); +void _BaseTimeline_free (Timeline* timeline) { + struct BaseTimeline* self = SUB_CAST(struct BaseTimeline, timeline); + _CurveTimeline_deinit(SUPER(self)); FREE(self->frames); FREE(self); } /* Many timelines have structure identical to struct BaseTimeline and extend CurveTimeline. **/ -struct BaseTimeline* _BaseTimeline_create (int frameCount, int frameSize) { - struct BaseTimeline* self = CALLOC(struct BaseTimeline, 1) - _CurveTimeline_init(&self->super, frameCount); - ((Timeline*)self)->_dispose = _BaseTimeline_dispose; +struct BaseTimeline* _BaseTimeline_new (int frameCount, int frameSize) { + struct BaseTimeline* self = NEW(struct BaseTimeline); + _CurveTimeline_init(SUPER(self), frameCount); + VTABLE(Timeline, self) ->dispose = _BaseTimeline_free; - CAST(int, self->framesLength) = frameCount * frameSize; - CAST(float*, self->frames) = CALLOC(float, self->framesLength) + CONST_CAST(int, self->framesLength) = frameCount * frameSize; + CONST_CAST(float*, self->frames) = CALLOC(float, self->framesLength); return self; } @@ -202,7 +204,7 @@ static const int ROTATE_LAST_FRAME_TIME = -2; static const int ROTATE_FRAME_VALUE = 1; void _RotateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float time, float alpha) { - RotateTimeline* self = (RotateTimeline*)timeline; + RotateTimeline* self = SUB_CAST(RotateTimeline, timeline); if (time < self->frames[0]) return; /* Time is before first frame. */ @@ -223,7 +225,7 @@ void _RotateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float float lastFrameValue = self->frames[frameIndex - 1]; float frameTime = self->frames[frameIndex]; float percent = 1 - (time - frameTime) / (self->frames[frameIndex + ROTATE_LAST_FRAME_TIME] - frameTime); - percent = CurveTimeline_getCurvePercent(&self->super, frameIndex / 2 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); + percent = CurveTimeline_getCurvePercent(SUPER(self), frameIndex / 2 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); float amount = self->frames[frameIndex + ROTATE_FRAME_VALUE] - lastFrameValue; while (amount > 180) @@ -238,9 +240,9 @@ void _RotateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float bone->rotation += amount * alpha; } -RotateTimeline* RotateTimeline_create (int frameCount) { - RotateTimeline* self = _BaseTimeline_create(frameCount, 2); - ((Timeline*)self)->_apply = _RotateTimeline_apply; +RotateTimeline* RotateTimeline_new (int frameCount) { + RotateTimeline* self = _BaseTimeline_new(frameCount, 2); + VTABLE(Timeline, self) ->apply = _RotateTimeline_apply; return self; } @@ -257,7 +259,7 @@ static const int TRANSLATE_FRAME_X = 1; static const int TRANSLATE_FRAME_Y = 2; void _TranslateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float time, float alpha) { - TranslateTimeline* self = (TranslateTimeline*)timeline; + TranslateTimeline* self = SUB_CAST(TranslateTimeline, timeline); if (time < self->frames[0]) return; /* Time is before first frame. */ @@ -275,7 +277,7 @@ void _TranslateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, flo float lastFrameY = self->frames[frameIndex - 1]; float frameTime = self->frames[frameIndex]; float percent = 1 - (time - frameTime) / (self->frames[frameIndex + TRANSLATE_LAST_FRAME_TIME] - frameTime); - percent = CurveTimeline_getCurvePercent(&self->super, frameIndex / 3 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); + percent = CurveTimeline_getCurvePercent(SUPER(self), frameIndex / 3 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); bone->x += (bone->data->x + lastFrameX + (self->frames[frameIndex + TRANSLATE_FRAME_X] - lastFrameX) * percent - bone->x) * alpha; @@ -283,9 +285,9 @@ void _TranslateTimeline_apply (const Timeline* timeline, Skeleton* skeleton, flo * alpha; } -TranslateTimeline* TranslateTimeline_create (int frameCount) { - TranslateTimeline* self = _BaseTimeline_create(frameCount, 3); - ((Timeline*)self)->_apply = _TranslateTimeline_apply; +TranslateTimeline* TranslateTimeline_new (int frameCount) { + TranslateTimeline* self = _BaseTimeline_new(frameCount, 3); + VTABLE(Timeline, self) ->apply = _TranslateTimeline_apply; return self; } @@ -299,7 +301,7 @@ void TranslateTimeline_setFrame (TranslateTimeline* self, int frameIndex, float /**/ void _ScaleTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float time, float alpha) { - ScaleTimeline* self = (ScaleTimeline*)timeline; + ScaleTimeline* self = SUB_CAST(ScaleTimeline, timeline); if (time < self->frames[0]) return; /* Time is before first frame. */ @@ -316,7 +318,7 @@ void _ScaleTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float t float lastFrameY = self->frames[frameIndex - 1]; float frameTime = self->frames[frameIndex]; float percent = 1 - (time - frameTime) / (self->frames[frameIndex + TRANSLATE_LAST_FRAME_TIME] - frameTime); - percent = CurveTimeline_getCurvePercent(&self->super, frameIndex / 3 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); + percent = CurveTimeline_getCurvePercent(SUPER(self), frameIndex / 3 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); bone->scaleX += (bone->data->scaleX - 1 + lastFrameX + (self->frames[frameIndex + TRANSLATE_FRAME_X] - lastFrameX) * percent - bone->scaleX) * alpha; @@ -324,9 +326,9 @@ void _ScaleTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float t - bone->scaleY) * alpha; } -ScaleTimeline* ScaleTimeline_create (int frameCount) { - ScaleTimeline* self = _BaseTimeline_create(frameCount, 3); - ((Timeline*)self)->_apply = _ScaleTimeline_apply; +ScaleTimeline* ScaleTimeline_new (int frameCount) { + ScaleTimeline* self = _BaseTimeline_new(frameCount, 3); + VTABLE(Timeline, self) ->apply = _ScaleTimeline_apply; return self; } @@ -366,7 +368,7 @@ void _ColorTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float t float lastFrameA = self->frames[frameIndex - 1]; float frameTime = self->frames[frameIndex]; float percent = 1 - (time - frameTime) / (self->frames[frameIndex + COLOR_LAST_FRAME_TIME] - frameTime); - percent = CurveTimeline_getCurvePercent(&self->super, frameIndex / 5 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); + percent = CurveTimeline_getCurvePercent(SUPER(self), frameIndex / 5 - 1, percent < 0 ? 0 : (percent > 1 ? 1 : percent)); float r = lastFrameR + (self->frames[frameIndex + COLOR_FRAME_R] - lastFrameR) * percent; float g = lastFrameG + (self->frames[frameIndex + COLOR_FRAME_G] - lastFrameG) * percent; @@ -385,9 +387,9 @@ void _ColorTimeline_apply (const Timeline* timeline, Skeleton* skeleton, float t } } -ColorTimeline* ColorTimeline_create (int frameCount) { - ColorTimeline* self = (ColorTimeline*)_BaseTimeline_create(frameCount, 5); - ((Timeline*)self)->_apply = _ColorTimeline_apply; +ColorTimeline* ColorTimeline_new (int frameCount) { + ColorTimeline* self = (ColorTimeline*)_BaseTimeline_new(frameCount, 5); + VTABLE(Timeline, self) ->apply = _ColorTimeline_apply; return self; } @@ -418,36 +420,36 @@ void _AttachmentTimeline_apply (const Timeline* timeline, Skeleton* skeleton, fl attachmentName ? Skeleton_getAttachmentForSlotIndex(skeleton, self->slotIndex, attachmentName) : 0); } -void _AttachmentTimeline_dispose (Timeline* timeline) { +void _AttachmentTimeline_free (Timeline* timeline) { _Timeline_deinit(timeline); AttachmentTimeline* self = (AttachmentTimeline*)timeline; int i; for (i = 0; i < self->framesLength; ++i) - FREE(self->attachmentNames[i]) - FREE(self->attachmentNames) + FREE(self->attachmentNames[i]); + FREE(self->attachmentNames); - FREE(self) + FREE(self); } -AttachmentTimeline* AttachmentTimeline_create (int frameCount) { - AttachmentTimeline* self = CALLOC(AttachmentTimeline, 1) - _Timeline_init(&self->super); - ((Timeline*)self)->_dispose = _AttachmentTimeline_dispose; - ((Timeline*)self)->_apply = _AttachmentTimeline_apply; - CAST(char**, self->attachmentNames) = CALLOC(char*, frameCount) +AttachmentTimeline* AttachmentTimeline_new (int frameCount) { + AttachmentTimeline* self = NEW(AttachmentTimeline); + _Timeline_init(SUPER(self)); + VTABLE(Timeline, self) ->dispose = _AttachmentTimeline_free; + VTABLE(Timeline, self) ->apply = _AttachmentTimeline_apply; + CONST_CAST(char**, self->attachmentNames) = CALLOC(char*, frameCount); - CAST(int, self->framesLength) = frameCount; - CAST(float*, self->frames) = CALLOC(float, frameCount) + CONST_CAST(int, self->framesLength) = frameCount; + CONST_CAST(float*, self->frames) = CALLOC(float, frameCount); return self; } void AttachmentTimeline_setFrame (AttachmentTimeline* self, int frameIndex, float time, const char* attachmentName) { self->frames[frameIndex] = time; - FREE(self->attachmentNames[frameIndex]) + FREE(self->attachmentNames[frameIndex]); if (attachmentName) - MALLOC_STR(self->attachmentNames[frameIndex], attachmentName) + MALLOC_STR(self->attachmentNames[frameIndex], attachmentName); else self->attachmentNames[frameIndex] = 0; } diff --git a/spine-c/src/spine/Atlas.c b/spine-c/src/spine/Atlas.c index 8703c495c..e71c4b1df 100644 --- a/spine-c/src/spine/Atlas.c +++ b/spine-c/src/spine/Atlas.c @@ -25,31 +25,31 @@ #include #include -#include #include void _AtlasPage_init (AtlasPage* self, const char* name) { + CONST_CAST(_AtlasPageVtable*, self->vtable) = NEW(_AtlasPageVtable); self->name = name; /* name is guaranteed to be memory we allocated. */ } void _AtlasPage_deinit (AtlasPage* self) { + FREE(self->vtable); FREE(self->name); } -void AtlasPage_dispose (AtlasPage* self) { - if (self->next) AtlasPage_dispose(self->next); /* BOZO - Don't dispose all in the list. */ - self->_dispose(self); +void AtlasPage_free (AtlasPage* self) { + if (self->next) AtlasPage_free(self->next); /* BOZO - Don't dispose all in the list. */ + VTABLE(AtlasPage, self) ->free(self); } /**/ -AtlasRegion* AtlasRegion_create () { - AtlasRegion* self = CALLOC(AtlasRegion, 1) - return self; +AtlasRegion* AtlasRegion_new () { + return NEW(AtlasRegion) ; } -void AtlasRegion_dispose (AtlasRegion* self) { - if (self->next) AtlasRegion_dispose(self->next); +void AtlasRegion_free (AtlasRegion* self) { + if (self->next) AtlasRegion_free(self->next); FREE(self->name); FREE(self->splits); FREE(self->pads); @@ -139,7 +139,7 @@ static int readTuple (Str tuple[]) { static char* mallocString (Str* str) { int length = str->end - str->begin; - char* string = MALLOC(char, length + 1) + char* string = MALLOC(char, length + 1); memcpy(string, str->begin, length); string[length] = '\0'; return string; @@ -166,7 +166,7 @@ static const char* textureFilterNames[] = {"Nearest", "Linear", "MipMap", "MipMa "MipMapNearestLinear", "MipMapLinearLinear"}; Atlas* Atlas_readAtlas (const char* data) { - Atlas* self = CALLOC(Atlas, 1) + Atlas* self = NEW(Atlas); AtlasPage *page = 0; AtlasPage *lastPage = 0; @@ -178,7 +178,7 @@ Atlas* Atlas_readAtlas (const char* data) { if (str.end - str.begin == 0) { page = 0; } else if (!page) { - page = AtlasPage_create(mallocString(&str)); + page = AtlasPage_new(mallocString(&str)); if (lastPage) lastPage->next = page; else @@ -198,7 +198,7 @@ Atlas* Atlas_readAtlas (const char* data) { page->vWrap = *str.begin == 'x' ? ATLAS_CLAMPTOEDGE : (*str.begin == 'y' ? ATLAS_REPEAT : ATLAS_REPEAT); } } else { - AtlasRegion *region = AtlasRegion_create(); + AtlasRegion *region = AtlasRegion_new(); if (lastRegion) lastRegion->next = region; else @@ -222,7 +222,7 @@ Atlas* Atlas_readAtlas (const char* data) { int count; if (!(count = readTuple(tuple))) return 0; if (count == 4) { /* split is optional */ - region->splits = MALLOC(int, 4) + region->splits = MALLOC(int, 4); region->splits[0] = toInt(tuple); region->splits[1] = toInt(tuple + 1); region->splits[2] = toInt(tuple + 2); @@ -230,7 +230,7 @@ Atlas* Atlas_readAtlas (const char* data) { if (!(count = readTuple(tuple))) return 0; if (count == 4) { /* pad is optional, but only present with splits */ - region->pads = MALLOC(int, 4) + region->pads = MALLOC(int, 4); region->pads[0] = toInt(tuple); region->pads[1] = toInt(tuple + 1); region->pads[2] = toInt(tuple + 2); @@ -259,14 +259,14 @@ Atlas* Atlas_readAtlasFile (const char* path) { const char* data = readFile(path); if (!data) return 0; Atlas* atlas = Atlas_readAtlas(data); - FREE(data) + FREE(data); return atlas; } -void Atlas_dispose (Atlas* self) { - if (self->pages) AtlasPage_dispose(self->pages); - if (self->regions) AtlasRegion_dispose(self->regions); - FREE(self) +void Atlas_free (Atlas* self) { + if (self->pages) AtlasPage_free(self->pages); + if (self->regions) AtlasRegion_free(self->regions); + FREE(self); } AtlasRegion* Atlas_findRegion (const Atlas* self, const char* name) { diff --git a/spine-c/src/spine/AtlasAttachmentLoader.c b/spine-c/src/spine/AtlasAttachmentLoader.c index 29edbc5e8..c7b4915da 100644 --- a/spine-c/src/spine/AtlasAttachmentLoader.c +++ b/spine-c/src/spine/AtlasAttachmentLoader.c @@ -24,16 +24,15 @@ ******************************************************************************/ #include -#include #include #include -void _AtlasAttachmentLoader_dispose (AttachmentLoader* self) { +void _AtlasAttachmentLoader_free (AttachmentLoader* self) { _AttachmentLoader_deinit(self); } Attachment* _AtlasAttachmentLoader_newAttachment (AttachmentLoader* loader, AttachmentType type, const char* name) { - AtlasAttachmentLoader* self = (AtlasAttachmentLoader*)loader; + AtlasAttachmentLoader* self = SUB_CAST(AtlasAttachmentLoader, loader); switch (type) { case ATTACHMENT_REGION: { AtlasRegion* region = Atlas_findRegion(self->atlas, name); @@ -41,21 +40,22 @@ Attachment* _AtlasAttachmentLoader_newAttachment (AttachmentLoader* loader, Atta _AttachmentLoader_setError(loader, "Region not found: ", name); return 0; } - return (Attachment*)RegionAttachment_create(name, region); + return SUPER_CAST(Attachment, RegionAttachment_new(name, region)) ; } default: { char buffer[16]; - sprintf((char*)loader->error2, "%d", type); + sprintf(buffer, "%d", type); _AttachmentLoader_setError(loader, "Unknown attachment type: ", buffer); return 0; } } } -AtlasAttachmentLoader* AtlasAttachmentLoader_create (Atlas* atlas) { - AtlasAttachmentLoader* self = CALLOC(AtlasAttachmentLoader, 1) +AtlasAttachmentLoader* AtlasAttachmentLoader_new (Atlas* atlas) { + AtlasAttachmentLoader* self = NEW(AtlasAttachmentLoader); + _AttachmentLoader_init(SUPER(self)); self->atlas = atlas; - self->super._newAttachment = _AtlasAttachmentLoader_newAttachment; - self->super._dispose = _AtlasAttachmentLoader_dispose; + VTABLE(AttachmentLoader, self) ->newAttachment = _AtlasAttachmentLoader_newAttachment; + VTABLE(AttachmentLoader, self) ->free = _AtlasAttachmentLoader_free; return self; } diff --git a/spine-c/src/spine/Attachment.c b/spine-c/src/spine/Attachment.c index 9fca1aa2e..d17e7badb 100644 --- a/spine-c/src/spine/Attachment.c +++ b/spine-c/src/spine/Attachment.c @@ -24,23 +24,25 @@ ******************************************************************************/ #include -#include +#include #include -void _Attachment_init (Attachment* self, const char* name, int type) { +void _Attachment_init (Attachment* self, const char* name, AttachmentType type) { + CONST_CAST(_AttachmentVtable*, self->vtable) = NEW(_AttachmentVtable); MALLOC_STR(self->name, name); self->type = type; } void _Attachment_deinit (Attachment* self) { - FREE(self->name) - FREE(self) + FREE(self->vtable); + FREE(self->name); + FREE(self); } -void Attachment_dispose (Attachment* self) { - self->_dispose(self); +void Attachment_free (Attachment* self) { + VTABLE(Attachment, self)->free(self); } void Attachment_draw (Attachment* self, Slot* slot) { - self->_draw(self, slot); + VTABLE(Attachment, self)->draw(self, slot); } diff --git a/spine-c/src/spine/AttachmentLoader.c b/spine-c/src/spine/AttachmentLoader.c index c9b4a1423..3d9759333 100644 --- a/spine-c/src/spine/AttachmentLoader.c +++ b/spine-c/src/spine/AttachmentLoader.c @@ -24,31 +24,33 @@ ******************************************************************************/ #include -#include +#include void _AttachmentLoader_init (AttachmentLoader* self) { + CONST_CAST(_AttachmentLoaderVtable*, self->vtable) = NEW(_AttachmentLoaderVtable); } void _AttachmentLoader_deinit (AttachmentLoader* self) { - FREE(self->error1) - FREE(self->error2) + FREE(self->vtable); + FREE(self->error1); + FREE(self->error2); } -void AttachmentLoader_dispose (AttachmentLoader* self) { - self->_dispose(self); +void AttachmentLoader_free (AttachmentLoader* self) { + VTABLE(AttachmentLoader, self) ->free(self); } Attachment* AttachmentLoader_newAttachment (AttachmentLoader* self, AttachmentType type, const char* name) { - FREE(self->error1) - FREE(self->error2) + FREE(self->error1); + FREE(self->error2); self->error1 = 0; self->error2 = 0; - return self->_newAttachment(self, type, name); + return VTABLE(AttachmentLoader, self) ->newAttachment(self, type, name); } void _AttachmentLoader_setError (AttachmentLoader* self, const char* error1, const char* error2) { - FREE(self->error1) - FREE(self->error2) - MALLOC_STR(self->error1, error1) - MALLOC_STR(self->error2, error2) + FREE(self->error1); + FREE(self->error2); + MALLOC_STR(self->error1, error1); + MALLOC_STR(self->error2, error2); } diff --git a/spine-c/src/spine/Bone.c b/spine-c/src/spine/Bone.c index d2ebe890f..5c4452bad 100644 --- a/spine-c/src/spine/Bone.c +++ b/spine-c/src/spine/Bone.c @@ -33,17 +33,17 @@ void Bone_setYDown (int value) { yDown = value; } -Bone* Bone_create (BoneData* data, Bone* parent) { - Bone* self = CALLOC(Bone, 1) - CAST(BoneData*, self->data) = data; - CAST(Bone*, self->parent) = parent; +Bone* Bone_new (BoneData* data, Bone* parent) { + Bone* self = NEW(Bone); + CONST_CAST(BoneData*, self->data) = data; + CONST_CAST(Bone*, self->parent) = parent; self->scaleX = 1; self->scaleY = 1; return self; } -void Bone_dispose (Bone* self) { - FREE(self) +void Bone_free (Bone* self) { + FREE(self); } void Bone_setToBindPose (Bone* self) { @@ -56,35 +56,35 @@ void Bone_setToBindPose (Bone* self) { void Bone_updateWorldTransform (Bone* self, int flipX, int flipY) { if (self->parent) { - CAST(float, self->worldX) = self->x * self->parent->m00 + self->y * self->parent->m01 + self->parent->worldX; - CAST(float, self->worldY) = self->x * self->parent->m10 + self->y * self->parent->m11 + self->parent->worldY; - CAST(float, self->worldScaleX) = self->parent->worldScaleX * self->scaleX; - CAST(float, self->worldScaleY) = self->parent->worldScaleY * self->scaleY; - CAST(float, self->worldRotation) = self->parent->worldRotation + self->rotation; + CONST_CAST(float, self->worldX) = self->x * self->parent->m00 + self->y * self->parent->m01 + self->parent->worldX; + CONST_CAST(float, self->worldY) = self->x * self->parent->m10 + self->y * self->parent->m11 + self->parent->worldY; + CONST_CAST(float, self->worldScaleX) = self->parent->worldScaleX * self->scaleX; + CONST_CAST(float, self->worldScaleY) = self->parent->worldScaleY * self->scaleY; + CONST_CAST(float, self->worldRotation) = self->parent->worldRotation + self->rotation; } else { - CAST(float, self->worldX) = self->x; - CAST(float, self->worldY) = self->y; - CAST(float, self->worldScaleX) = self->scaleX; - CAST(float, self->worldScaleY) = self->scaleY; - CAST(float, self->worldRotation) = self->rotation; + CONST_CAST(float, self->worldX) = self->x; + CONST_CAST(float, self->worldY) = self->y; + CONST_CAST(float, self->worldScaleX) = self->scaleX; + CONST_CAST(float, self->worldScaleY) = self->scaleY; + CONST_CAST(float, self->worldRotation) = self->rotation; } float radians = (float)(self->worldRotation * 3.1415926535897932385 / 180); float cosine = cosf(radians); float sine = sinf(radians); - CAST(float, self->m00) = cosine * self->worldScaleX; - CAST(float, self->m10) = sine * self->worldScaleX; - CAST(float, self->m01) = -sine * self->worldScaleY; - CAST(float, self->m11) = cosine * self->worldScaleY; + CONST_CAST(float, self->m00) = cosine * self->worldScaleX; + CONST_CAST(float, self->m10) = sine * self->worldScaleX; + CONST_CAST(float, self->m01) = -sine * self->worldScaleY; + CONST_CAST(float, self->m11) = cosine * self->worldScaleY; if (flipX) { - CAST(float, self->m00) = -self->m00; - CAST(float, self->m01) = -self->m01; + CONST_CAST(float, self->m00) = -self->m00; + CONST_CAST(float, self->m01) = -self->m01; } if (flipY) { - CAST(float, self->m10) = -self->m10; - CAST(float, self->m11) = -self->m11; + CONST_CAST(float, self->m10) = -self->m10; + CONST_CAST(float, self->m11) = -self->m11; } if (yDown) { - CAST(float, self->m10) = -self->m10; - CAST(float, self->m11) = -self->m11; + CONST_CAST(float, self->m10) = -self->m10; + CONST_CAST(float, self->m11) = -self->m11; } } diff --git a/spine-c/src/spine/BoneData.c b/spine-c/src/spine/BoneData.c index 67cdfedca..4a7f4d45d 100644 --- a/spine-c/src/spine/BoneData.c +++ b/spine-c/src/spine/BoneData.c @@ -26,16 +26,16 @@ #include #include -BoneData* BoneData_create (const char* name, BoneData* parent) { - BoneData* self = CALLOC(BoneData, 1) - MALLOC_STR(self->name, name) - CAST(BoneData*, self->parent) = parent; +BoneData* BoneData_new (const char* name, BoneData* parent) { + BoneData* self = NEW(BoneData); + MALLOC_STR(self->name, name); + CONST_CAST(BoneData*, self->parent) = parent; self->scaleX = 1; self->scaleY = 1; return self; } -void BoneData_dispose (BoneData* self) { - FREE(self->name) - FREE(self) +void BoneData_free (BoneData* self) { + FREE(self->name); + FREE(self); } diff --git a/spine-c/src/spine/Json.c b/spine-c/src/spine/Json.c index 4457be299..cf74674ba 100644 --- a/spine-c/src/spine/Json.c +++ b/spine-c/src/spine/Json.c @@ -24,13 +24,10 @@ /* JSON parser in C. */ #include -#include -#include #include -#include -#include -#include +#include #include +#include static const char* ep; @@ -47,16 +44,16 @@ static int Json_strcasecmp (const char* s1, const char* s2) { } /* Internal constructor. */ -static Json *Json_create_Item (void) { - return (Json*)calloc(1, sizeof(Json)); +static Json *Json_new_Item (void) { + return (Json*)CALLOC(Json, 1); } /* Delete a Json structure. */ -void Json_dispose (Json *c) { +void Json_free (Json *c) { Json *next; while (c) { next = c->next; - if (c->child) Json_dispose(c->child); + if (c->child) Json_free(c->child); if (c->valuestring) free((char*)c->valuestring); if (c->name) free((char*)c->name); free(c); @@ -206,15 +203,15 @@ static const char* skip (const char* in) { } /* Parse an object - create a new root, and populate. */ -Json *Json_create (const char* value) { +Json *Json_new (const char* value) { const char* end = 0; - Json *c = Json_create_Item(); + Json *c = Json_new_Item(); ep = 0; if (!c) return 0; /* memory fail */ end = parse_value(c, skip(value)); if (!end) { - Json_dispose(c); + Json_free(c); return 0; } /* parse failure. ep is set. */ @@ -266,14 +263,14 @@ static const char* parse_array (Json *item, const char* value) { value = skip(value + 1); if (*value == ']') return value + 1; /* empty array. */ - item->child = child = Json_create_Item(); + item->child = child = Json_new_Item(); if (!item->child) return 0; /* memory fail */ value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */ if (!value) return 0; while (*value == ',') { Json *new_item; - if (!(new_item = Json_create_Item())) return 0; /* memory fail */ + if (!(new_item = Json_new_Item())) return 0; /* memory fail */ child->next = new_item; new_item->prev = child; child = new_item; @@ -298,7 +295,7 @@ static const char* parse_object (Json *item, const char* value) { value = skip(value + 1); if (*value == '}') return value + 1; /* empty array. */ - item->child = child = Json_create_Item(); + item->child = child = Json_new_Item(); if (!item->child) return 0; value = skip(parse_string(child, skip(value))); if (!value) return 0; @@ -313,7 +310,7 @@ static const char* parse_object (Json *item, const char* value) { while (*value == ',') { Json *new_item; - if (!(new_item = Json_create_Item())) return 0; /* memory fail */ + if (!(new_item = Json_new_Item())) return 0; /* memory fail */ child->next = new_item; new_item->prev = child; child = new_item; diff --git a/spine-c/src/spine/Json.h b/spine-c/src/spine/Json.h index 4b10dbba2..9907efd83 100644 --- a/spine-c/src/spine/Json.h +++ b/spine-c/src/spine/Json.h @@ -25,8 +25,6 @@ #ifndef SPINE_JSON_H_ #define SPINE_JSON_H_ -#include - #ifdef __cplusplus namespace spine { extern "C" { @@ -56,11 +54,11 @@ typedef struct Json { const char* name; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ } Json; -/* Supply a block of JSON, and this returns a Json object you can interrogate. Call Json_dispose when finished. */ -Json* Json_create (const char* value); +/* Supply a block of JSON, and this returns a Json object you can interrogate. Call Json_free when finished. */ +Json* Json_new (const char* value); /* Delete a Json entity and all subentities. */ -void Json_dispose (Json* json); +void Json_free (Json* json); /* Returns the number of items in an array (or object). */ int Json_getSize (Json* json); @@ -74,7 +72,7 @@ const char* Json_getString (Json* json, const char* name, const char* defaultVal float Json_getFloat (Json* json, const char* name, float defaultValue); int Json_getInt (Json* json, const char* name, int defaultValue); -/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when Json_create() returns 0. 0 when Json_create() succeeds. */ +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when Json_new() returns 0. 0 when Json_new() succeeds. */ const char* Json_getError (void); #ifdef __cplusplus diff --git a/spine-c/src/spine/RegionAttachment.c b/spine-c/src/spine/RegionAttachment.c index 34e52783d..95f4a6da6 100644 --- a/spine-c/src/spine/RegionAttachment.c +++ b/spine-c/src/spine/RegionAttachment.c @@ -25,17 +25,16 @@ #include #include -#include #include void _RegionAttachment_init (RegionAttachment* self, const char* name) { self->scaleX = 1; self->scaleY = 1; - _Attachment_init(&self->super, name, ATTACHMENT_REGION); + _Attachment_init(SUPER(self), name, ATTACHMENT_REGION); } void _RegionAttachment_deinit (RegionAttachment* self) { - _Attachment_deinit(&self->super); + _Attachment_deinit(SUPER(self)); } void RegionAttachment_updateOffset (RegionAttachment* self) { diff --git a/spine-c/src/spine/Skeleton.c b/spine-c/src/spine/Skeleton.c index e0fad492e..a1e39cfd9 100644 --- a/spine-c/src/spine/Skeleton.c +++ b/spine-c/src/spine/Skeleton.c @@ -24,13 +24,15 @@ ******************************************************************************/ #include -#include +#include void _Skeleton_init (Skeleton* self, SkeletonData* data) { - CAST(SkeletonData*, self->data) = data; + CONST_CAST(SkeletonData*, self->data) = data; + + CONST_CAST(_SkeletonVtable*, self->vtable) = NEW(_SkeletonVtable); self->boneCount = self->data->boneCount; - self->bones = MALLOC(Bone*, self->boneCount) + self->bones = MALLOC(Bone*, self->boneCount); int i, ii; for (i = 0; i < self->boneCount; ++i) { BoneData* boneData = self->data->bones[i]; @@ -44,11 +46,11 @@ void _Skeleton_init (Skeleton* self, SkeletonData* data) { } } } - self->bones[i] = Bone_create(boneData, parent); + self->bones[i] = Bone_new(boneData, parent); } self->slotCount = data->slotCount; - self->slots = MALLOC(Slot*, self->slotCount) + self->slots = MALLOC(Slot*, self->slotCount); for (i = 0; i < self->slotCount; ++i) { SlotData *slotData = data->slots[i]; @@ -61,10 +63,10 @@ void _Skeleton_init (Skeleton* self, SkeletonData* data) { } } - self->slots[i] = Slot_create(slotData, self, bone); + self->slots[i] = Slot_new(slotData, self, bone); } - self->drawOrder = MALLOC(Slot*, self->slotCount) + self->drawOrder = MALLOC(Slot*, self->slotCount); memcpy(self->drawOrder, self->slots, sizeof(Slot*) * self->slotCount); self->r = 1; @@ -74,20 +76,22 @@ void _Skeleton_init (Skeleton* self, SkeletonData* data) { } void _Skeleton_deinit (Skeleton* self) { + FREE(self->vtable); + int i; for (i = 0; i < self->boneCount; ++i) - Bone_dispose(self->bones[i]); - FREE(self->bones) + Bone_free(self->bones[i]); + FREE(self->bones); for (i = 0; i < self->slotCount; ++i) - Slot_dispose(self->slots[i]); - FREE(self->slots) + Slot_free(self->slots[i]); + FREE(self->slots); - FREE(self->drawOrder) + FREE(self->drawOrder); } -void Skeleton_dispose (Skeleton* self) { - self->_dispose(self); +void Skeleton_free (Skeleton* self) { + VTABLE(Skeleton, self) ->free(self); } void Skeleton_updateWorldTransform (const Skeleton* self) { @@ -166,7 +170,7 @@ void Skeleton_setSkin (Skeleton* self, Skin* newSkin) { entry = entry->next; } } - CAST(Skin*, self->skin) = newSkin; + CONST_CAST(Skin*, self->skin) = newSkin; } Attachment* Skeleton_getAttachmentForSlotName (const Skeleton* self, const char* slotName, const char* attachmentName) { diff --git a/spine-c/src/spine/SkeletonData.c b/spine-c/src/spine/SkeletonData.c index 71854afe3..cb3d85e8d 100644 --- a/spine-c/src/spine/SkeletonData.c +++ b/spine-c/src/spine/SkeletonData.c @@ -26,13 +26,12 @@ #include #include -SkeletonData* SkeletonData_create () { - SkeletonData* self = CALLOC(SkeletonData, 1) - return self; +SkeletonData* SkeletonData_new () { + return NEW(SkeletonData); } -void SkeletonData_dispose (SkeletonData* self) { - FREE(self) +void SkeletonData_free (SkeletonData* self) { + FREE(self); } BoneData* SkeletonData_findBone (const SkeletonData* self, const char* boneName) { diff --git a/spine-c/src/spine/SkeletonJson.c b/spine-c/src/spine/SkeletonJson.c index 094d04380..86078ee29 100644 --- a/spine-c/src/spine/SkeletonJson.c +++ b/spine-c/src/spine/SkeletonJson.c @@ -33,38 +33,38 @@ #include typedef struct { - SkeletonJson json; + SkeletonJson super; int ownsLoader; -} Internal; +} _Internal; -SkeletonJson* SkeletonJson_createWithLoader (AttachmentLoader* attachmentLoader) { - SkeletonJson* self = (SkeletonJson*)CALLOC(Internal, 1) +SkeletonJson* SkeletonJson_newWithLoader (AttachmentLoader* attachmentLoader) { + SkeletonJson* self = SUPER(NEW(_Internal)); self->scale = 1; self->attachmentLoader = attachmentLoader; return self; } -SkeletonJson* SkeletonJson_create (Atlas* atlas) { - AtlasAttachmentLoader* attachmentLoader = AtlasAttachmentLoader_create(atlas); - Internal* self = (Internal*)SkeletonJson_createWithLoader(&attachmentLoader->super); - self->ownsLoader = 1; - return &self->json; +SkeletonJson* SkeletonJson_new (Atlas* atlas) { + AtlasAttachmentLoader* attachmentLoader = AtlasAttachmentLoader_new(atlas); + SkeletonJson* self = SkeletonJson_newWithLoader(SUPER(attachmentLoader)); + SUB_CAST(_Internal, self) ->ownsLoader = 1; + return self; } -void SkeletonJson_dispose (SkeletonJson* self) { - if (((Internal*)self)->ownsLoader) AttachmentLoader_dispose(self->attachmentLoader); - FREE(self->error) - FREE(self) +void SkeletonJson_free (SkeletonJson* self) { + if (SUB_CAST(_Internal, self) ->ownsLoader) AttachmentLoader_free(self->attachmentLoader); + FREE(self->error); + FREE(self); } void _SkeletonJson_setError (SkeletonJson* self, Json* root, const char* value1, const char* value2) { - FREE(self->error) + FREE(self->error); char message[256]; strcpy(message, value1); int length = strlen(value1); if (value2) strncat(message + length, value2, 256 - length); - MALLOC_STR(self->error, message) - if (root) Json_dispose(root); + MALLOC_STR(self->error, message); + if (root) Json_free(root); } static float toColor (const char* value, int index) { @@ -81,32 +81,32 @@ static float toColor (const char* value, int index) { } SkeletonData* SkeletonJson_readSkeletonDataFile (SkeletonJson* self, const char* path) { - const char* data = readFile(path); - if (!data) { + const char* json = readFile(path); + if (!json) { _SkeletonJson_setError(self, 0, "Unable to read skeleton file: ", path); return 0; } - SkeletonData* skeletonData = SkeletonJson_readSkeletonData(self, data); - FREE(data) + SkeletonData* skeletonData = SkeletonJson_readSkeletonData(self, json); + FREE(json); return skeletonData; } SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* json) { - FREE(self->error) - CAST(char*, self->error) = 0; + FREE(self->error); + CONST_CAST(char*, self->error) = 0; - Json* root = Json_create(json); + Json* root = Json_new(json); if (!root) { _SkeletonJson_setError(self, 0, "Invalid skeleton JSON: ", Json_getError()); return 0; } - SkeletonData* skeletonData = SkeletonData_create(); + SkeletonData* skeletonData = SkeletonData_new(); int i, ii, iii; Json* bones = Json_getItem(root, "bones"); int boneCount = Json_getSize(bones); - skeletonData->bones = MALLOC(BoneData*, boneCount) + skeletonData->bones = MALLOC(BoneData*, boneCount); for (i = 0; i < boneCount; ++i) { Json* boneMap = Json_getItemAt(bones, i); @@ -117,13 +117,13 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso if (parentName) { parent = SkeletonData_findBone(skeletonData, parentName); if (!parent) { - SkeletonData_dispose(skeletonData); + SkeletonData_free(skeletonData); _SkeletonJson_setError(self, root, "Parent bone not found: ", parentName); return 0; } } - BoneData* boneData = BoneData_create(boneName, parent); + BoneData* boneData = BoneData_new(boneName, parent); boneData->length = Json_getFloat(boneMap, "parent", 0) * self->scale; boneData->x = Json_getFloat(boneMap, "x", 0) * self->scale; boneData->y = Json_getFloat(boneMap, "y", 0) * self->scale; @@ -138,7 +138,7 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso Json* slots = Json_getItem(root, "slots"); if (slots) { int slotCount = Json_getSize(slots); - skeletonData->slots = MALLOC(SlotData*, slotCount) + skeletonData->slots = MALLOC(SlotData*, slotCount); for (i = 0; i < slotCount; ++i) { Json* slotMap = Json_getItemAt(slots, i); @@ -147,12 +147,12 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso const char* boneName = Json_getString(slotMap, "bone", 0); BoneData* boneData = SkeletonData_findBone(skeletonData, boneName); if (!boneData) { - SkeletonData_dispose(skeletonData); + SkeletonData_free(skeletonData); _SkeletonJson_setError(self, root, "Slot bone not found: ", boneName); return 0; } - SlotData* slotData = SlotData_create(slotName, boneData); + SlotData* slotData = SlotData_new(slotName, boneData); const char* color = Json_getString(slotMap, "color", 0); if (color) { @@ -173,11 +173,11 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso Json* skinsMap = Json_getItem(root, "skins"); if (skinsMap) { int skinCount = Json_getSize(skinsMap); - skeletonData->skins = MALLOC(Skin*, skinCount) + skeletonData->skins = MALLOC(Skin*, skinCount); for (i = 0; i < skinCount; ++i) { Json* slotMap = Json_getItemAt(skinsMap, i); const char* skinName = slotMap->name; - Skin *skin = Skin_create(skinName); + Skin *skin = Skin_new(skinName); skeletonData->skins[i] = skin; skeletonData->skinCount++; if (strcmp(skinName, "default") == 0) skeletonData->defaultSkin = skin; @@ -201,14 +201,14 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso else if (strcmp(typeString, "regionSequence") == 0) type = ATTACHMENT_REGION_SEQUENCE; else { - SkeletonData_dispose(skeletonData); + SkeletonData_free(skeletonData); _SkeletonJson_setError(self, root, "Unknown attachment type: ", typeString); return 0; } Attachment* attachment = AttachmentLoader_newAttachment(self->attachmentLoader, type, attachmentName); if (!attachment && self->attachmentLoader->error1) { - SkeletonData_dispose(skeletonData); + SkeletonData_free(skeletonData); _SkeletonJson_setError(self, root, self->attachmentLoader->error1, self->attachmentLoader->error2); return 0; } @@ -231,7 +231,7 @@ SkeletonData* SkeletonJson_readSkeletonData (SkeletonJson* self, const char* jso } } - Json_dispose(root); + Json_free(root); return skeletonData; } @@ -242,7 +242,7 @@ Animation* SkeletonJson_readAnimationFile (SkeletonJson* self, const char* path, return 0; } Animation* animation = SkeletonJson_readAnimation(self, data, skeletonData); - FREE(data) + FREE(data); return animation; } @@ -258,10 +258,10 @@ static void readCurve (CurveTimeline* timeline, int frameIndex, Json* frame) { } Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, const SkeletonData *skeletonData) { - FREE(self->error) - CAST(char*, self->error) = 0; + FREE(self->error); + CONST_CAST(char*, self->error) = 0; - Json* root = Json_create(json); + Json* root = Json_new(json); if (!root) { _SkeletonJson_setError(self, 0, "Invalid animation JSON: ", Json_getError()); return 0; @@ -279,7 +279,7 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con timelineCount += Json_getSize(Json_getItemAt(bones, i)); for (i = 0; i < slotCount; ++i) timelineCount += Json_getSize(Json_getItemAt(slots, i)); - Animation* animation = Animation_create(timelineCount); + Animation* animation = Animation_new(timelineCount); animation->timelineCount = 0; for (i = 0; i < boneCount; ++i) { @@ -289,7 +289,7 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con int boneIndex = SkeletonData_findBoneIndex(skeletonData, boneName); if (boneIndex == -1) { - Animation_dispose(animation); + Animation_free(animation); _SkeletonJson_setError(self, root, "Bone not found: ", boneName); return 0; } @@ -301,12 +301,12 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con const char* timelineType = timelineArray->name; if (strcmp(timelineType, "rotate") == 0) { - RotateTimeline *timeline = RotateTimeline_create(frameCount); + RotateTimeline *timeline = RotateTimeline_new(frameCount); timeline->boneIndex = boneIndex; for (iii = 0; iii < frameCount; ++iii) { Json* frame = Json_getItemAt(timelineArray, iii); RotateTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), Json_getFloat(frame, "angle", 0)); - readCurve(&timeline->super, iii, frame); + readCurve(SUPER(timeline), iii, frame); } animation->timelines[animation->timelineCount++] = (Timeline*)timeline; animation->duration = fmaxf(animation->duration, timeline->frames[frameCount * 2 - 2]); @@ -314,19 +314,19 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con } else { int isScale = strcmp(timelineType, "scale") == 0; if (isScale || strcmp(timelineType, "translate") == 0) { - TranslateTimeline *timeline = isScale ? ScaleTimeline_create(frameCount) : TranslateTimeline_create(frameCount); + TranslateTimeline *timeline = isScale ? ScaleTimeline_new(frameCount) : TranslateTimeline_new(frameCount); float scale = isScale ? 1 : self->scale; timeline->boneIndex = boneIndex; for (iii = 0; iii < frameCount; ++iii) { Json* frame = Json_getItemAt(timelineArray, iii); TranslateTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), Json_getFloat(frame, "x", 0) * scale, Json_getFloat(frame, "y", 0) * scale); - readCurve(&timeline->super, iii, frame); + readCurve(SUPER(timeline), iii, frame); } animation->timelines[animation->timelineCount++] = (Timeline*)timeline; animation->duration = fmaxf(animation->duration, timeline->frames[frameCount * 3 - 3]); } else { - Animation_dispose(animation); + Animation_free(animation); _SkeletonJson_setError(self, 0, "Invalid timeline type for a bone: ", timelineType); return 0; } @@ -341,7 +341,7 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con int slotIndex = SkeletonData_findSlotIndex(skeletonData, slotName); if (slotIndex == -1) { - Animation_dispose(animation); + Animation_free(animation); _SkeletonJson_setError(self, root, "Slot not found: ", slotName); return 0; } @@ -353,20 +353,20 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con const char* timelineType = timelineArray->name; if (strcmp(timelineType, "color") == 0) { - ColorTimeline *timeline = ColorTimeline_create(frameCount); + ColorTimeline *timeline = ColorTimeline_new(frameCount); timeline->slotIndex = slotIndex; for (iii = 0; iii < frameCount; ++iii) { Json* frame = Json_getItemAt(timelineArray, iii); const char* s = Json_getString(frame, "color", 0); ColorTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), toColor(s, 0), toColor(s, 1), toColor(s, 2), toColor(s, 3)); - readCurve(&timeline->super, iii, frame); + readCurve(SUPER(timeline), iii, frame); } animation->timelines[animation->timelineCount++] = (Timeline*)timeline; animation->duration = fmaxf(animation->duration, timeline->frames[frameCount * 5 - 5]); } else if (strcmp(timelineType, "attachment") == 0) { - AttachmentTimeline *timeline = AttachmentTimeline_create(frameCount); + AttachmentTimeline *timeline = AttachmentTimeline_new(frameCount); timeline->slotIndex = slotIndex; for (iii = 0; iii < frameCount; ++iii) { Json* frame = Json_getItemAt(timelineArray, iii); @@ -378,7 +378,7 @@ Animation* SkeletonJson_readAnimation (SkeletonJson* self, const char* json, con animation->duration = fmaxf(animation->duration, timeline->frames[frameCount - 1]); } else { - Animation_dispose(animation); + Animation_free(animation); _SkeletonJson_setError(self, 0, "Invalid timeline type for a slot: ", timelineType); return 0; } diff --git a/spine-c/src/spine/Skin.c b/spine-c/src/spine/Skin.c index 2433152e1..1bc59c5fa 100644 --- a/spine-c/src/spine/Skin.c +++ b/spine-c/src/spine/Skin.c @@ -26,40 +26,40 @@ #include #include -SkinEntry* _SkinEntry_create (int slotIndex, const char* name, Attachment* attachment) { - SkinEntry* self = CALLOC(SkinEntry, 1) +SkinEntry* _SkinEntry_new (int slotIndex, const char* name, Attachment* attachment) { + SkinEntry* self = NEW(SkinEntry); self->slotIndex = slotIndex; - MALLOC_STR(self->name, name) + MALLOC_STR(self->name, name); self->attachment = attachment; return self; } -void _SkinEntry_dispose (SkinEntry* self) { - if (self->next) _SkinEntry_dispose((SkinEntry*)self->next); - Attachment_dispose(self->attachment); - FREE(self->name) - FREE(self) +void _SkinEntry_free (SkinEntry* self) { + if (self->next) _SkinEntry_free(CONST_CAST(SkinEntry*, self->next) ); + Attachment_free(self->attachment); + FREE(self->name); + FREE(self); } /**/ -Skin* Skin_create (const char* name) { - Skin* self = CALLOC(Skin, 1) - MALLOC_STR(self->name, name) +Skin* Skin_new (const char* name) { + Skin* self = NEW(Skin); + MALLOC_STR(self->name, name); return self; } -void Skin_dispose (Skin* self) { - _SkinEntry_dispose((SkinEntry*)self->entries); - FREE(self->name) - FREE(self) +void Skin_free (Skin* self) { + _SkinEntry_free(CONST_CAST(SkinEntry*, self->entries) ); + FREE(self->name); + FREE(self); } void Skin_addAttachment (Skin* self, int slotIndex, const char* name, Attachment* attachment) { - SkinEntry* newEntry = _SkinEntry_create(slotIndex, name, attachment); - SkinEntry* entry = (SkinEntry*)self->entries; + SkinEntry* newEntry = _SkinEntry_new(slotIndex, name, attachment); + SkinEntry* entry = CONST_CAST(SkinEntry*, self->entries); if (!entry) - CAST(SkinEntry*, self->entries) = newEntry; + CONST_CAST(SkinEntry*, self->entries) = newEntry; else { while (entry->next) entry = (SkinEntry*)entry->next; diff --git a/spine-c/src/spine/Slot.c b/spine-c/src/spine/Slot.c index ee1fd99ae..a41da8bb7 100644 --- a/spine-c/src/spine/Slot.c +++ b/spine-c/src/spine/Slot.c @@ -28,16 +28,15 @@ #include typedef struct { - Slot slot; + Slot super; float attachmentTime; -} SlotInternal; +} _Internal; -Slot* Slot_create (SlotData* data, Skeleton* skeleton, Bone* bone) { - SlotInternal* internal = CALLOC(SlotInternal, 1) - Slot* self = &internal->slot; - CAST(SlotData*, self->data) = data; - CAST(Skeleton*, self->skeleton) = skeleton; - CAST(Bone*, self->bone) = bone; +Slot* Slot_new (SlotData* data, Skeleton* skeleton, Bone* bone) { + Slot* self = SUPER(NEW(_Internal)); + CONST_CAST(SlotData*, self->data) = data; + CONST_CAST(Skeleton*, self->skeleton) = skeleton; + CONST_CAST(Bone*, self->bone) = bone; self->r = 1; self->g = 1; self->b = 1; @@ -45,22 +44,22 @@ Slot* Slot_create (SlotData* data, Skeleton* skeleton, Bone* bone) { return self; } -void Slot_dispose (Slot* self) { +void Slot_free (Slot* self) { FREE(self); } /* @param attachment May be null. */ void Slot_setAttachment (Slot* self, Attachment* attachment) { - CAST(Attachment*, self->attachment) = attachment; - ((SlotInternal*)self)->attachmentTime = self->skeleton->time; + CONST_CAST(Attachment*, self->attachment) = attachment; + SUB_CAST(_Internal, self) ->attachmentTime = self->skeleton->time; } void Slot_setAttachmentTime (Slot* self, float time) { - ((SlotInternal*)self)->attachmentTime = self->skeleton->time - time; + SUB_CAST(_Internal, self) ->attachmentTime = self->skeleton->time - time; } float Slot_getAttachmentTime (const Slot* self) { - return self->skeleton->time - ((SlotInternal*)self)->attachmentTime; + return self->skeleton->time - SUB_CAST(_Internal, self) ->attachmentTime; } void Slot_setToBindPose (Slot* self) { diff --git a/spine-c/src/spine/SlotData.c b/spine-c/src/spine/SlotData.c index 26f142213..1981c25d1 100644 --- a/spine-c/src/spine/SlotData.c +++ b/spine-c/src/spine/SlotData.c @@ -26,10 +26,10 @@ #include #include -SlotData* SlotData_create (const char* name, BoneData* boneData) { - SlotData* self = CALLOC(SlotData, 1) - MALLOC_STR(self->name, name) - CAST(BoneData*, self->boneData) = boneData; +SlotData* SlotData_new (const char* name, BoneData* boneData) { + SlotData* self = NEW(SlotData); + MALLOC_STR(self->name, name); + CONST_CAST(BoneData*, self->boneData) = boneData; self->r = 1; self->g = 1; self->b = 1; @@ -37,7 +37,7 @@ SlotData* SlotData_create (const char* name, BoneData* boneData) { return self; } -void SlotData_dispose (SlotData* self) { +void SlotData_free (SlotData* self) { FREE(self->name); FREE(self->attachmentName); FREE(self); @@ -46,7 +46,7 @@ void SlotData_dispose (SlotData* self) { void SlotData_setAttachmentName (SlotData* self, const char* attachmentName) { FREE(self->attachmentName); if (attachmentName) - MALLOC_STR(self->attachmentName, attachmentName) + MALLOC_STR(self->attachmentName, attachmentName); else - CAST(char*, self->attachmentName) = 0; + CONST_CAST(char*, self->attachmentName) = 0; } diff --git a/spine-c/src/spine/extension.h b/spine-c/src/spine/extension.h index 9440fc191..4c84f15fc 100644 --- a/spine-c/src/spine/extension.h +++ b/spine-c/src/spine/extension.h @@ -31,40 +31,76 @@ #include #include #include +#include #ifdef __cplusplus namespace spine { extern "C" { #endif -/* Methods that must be implemented: **/ +/* Public API that must be implemented: **/ -Skeleton* Skeleton_create (SkeletonData* data); +Skeleton* Skeleton_new (SkeletonData* data); -RegionAttachment* RegionAttachment_create (const char* name, AtlasRegion* region); +RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region); -AtlasPage* AtlasPage_create (const char* name); +AtlasPage* AtlasPage_new (const char* name); -/* Internal methods needed for extension: **/ +/* Internal API available for extension: **/ + +typedef struct _SkeletonVtable { + void (*free) (Skeleton* skeleton); +} _SkeletonVtable; void _Skeleton_init (Skeleton* skeleton, SkeletonData* data); void _Skeleton_deinit (Skeleton* skeleton); +/**/ + +typedef struct _AttachmentVtable { + void (*draw) (Attachment* attachment, struct Slot* slot); + void (*free) (Attachment* attachment); +} _AttachmentVtable; + void _Attachment_init (Attachment* attachment, const char* name, AttachmentType type); void _Attachment_deinit (Attachment* attachment); +/**/ + void _RegionAttachment_init (RegionAttachment* attachment, const char* name); void _RegionAttachment_deinit (RegionAttachment* attachment); +/**/ + +typedef struct _TimelineVtable { + void (*apply) (const Timeline* timeline, Skeleton* skeleton, float time, float alpha); + void (*dispose) (Timeline* timeline); +} _TimelineVtable; + void _Timeline_init (Timeline* timeline); void _Timeline_deinit (Timeline* timeline); +/**/ + void _CurveTimeline_init (CurveTimeline* timeline, int frameCount); void _CurveTimeline_deinit (CurveTimeline* timeline); +/**/ + +typedef struct _AtlasPageVtable { + void (*free) (AtlasPage* page); +} _AtlasPageVtable; + void _AtlasPage_init (AtlasPage* page, const char* name); void _AtlasPage_deinit (AtlasPage* page); +/**/ + +typedef struct _AttachmentLoaderVtable { + Attachment* (*newAttachment) (AttachmentLoader* loader, AttachmentType type, const char* name); + void (*free) (AttachmentLoader* loader); +} _AttachmentLoaderVtable; + void _AttachmentLoader_init (AttachmentLoader* loader); void _AttachmentLoader_deinit (AttachmentLoader* loader); void _AttachmentLoader_setError (AttachmentLoader* loader, const char* error1, const char* error2); diff --git a/spine-c/src/spine/util.c b/spine-c/src/spine/util.c index cb271fef4..d24890a8b 100644 --- a/spine-c/src/spine/util.c +++ b/spine-c/src/spine/util.c @@ -34,7 +34,7 @@ const char* readFile (const char* path) { long length = ftell(file); fseek(file, 0, SEEK_SET); - char* data = (char*)malloc(length + 1); + char* data = MALLOC(char, length + 1); fread(data, 1, length, file); fclose(file); data[length] = '\0'; diff --git a/spine-c/src/spine/util.h b/spine-c/src/spine/util.h index 3c2d17f79..c458ae84e 100644 --- a/spine-c/src/spine/util.h +++ b/spine-c/src/spine/util.h @@ -34,16 +34,33 @@ namespace spine { extern "C" { #endif -/* Used to cast away const on an lvalue. */ -#define CAST(TYPE,VALUE) *(TYPE*)&VALUE +/* All allocation uses these. */ +#define MALLOC(TYPE,COUNT) ((TYPE*)malloc(sizeof(TYPE) * COUNT)) +#define CALLOC(TYPE,COUNT) ((TYPE*)calloc(1, sizeof(TYPE) * COUNT)) +#define NEW(TYPE) CALLOC(TYPE,1) -#define CALLOC(TYPE,COUNT) (TYPE*)calloc(1, sizeof(TYPE) * COUNT); -#define MALLOC(TYPE,COUNT) (TYPE*)malloc(sizeof(TYPE) * COUNT); +/* Casts away const. Can be used as an lvalue. Not type safe, use with care. */ +#define CONST_CAST(TYPE,VALUE) (*(TYPE*)&VALUE) -#define MALLOC_STR(TO,FROM) strcpy(CAST(char*, TO) = (char*)malloc(strlen(FROM)), FROM); +/* Gets the direct super class. Type safe. */ +#define SUPER(VALUE) (&VALUE->super) -#define FREE(E) free((void*)E); +/* Cast to a super class. Not type safe, use with care. */ +#define SUPER_CAST(TYPE,VALUE) ((TYPE*)VALUE) +/* Cast to a sub class. Not type safe, use with care. */ +#define SUB_CAST(TYPE,VALUE) ((TYPE*)VALUE) + +/* Gets the vtable for the specified type. Can be used as an lvalue. */ +#define VTABLE(TYPE,VALUE) ((_##TYPE##Vtable*)((TYPE*)VALUE)->vtable) + +/* Allocates a new char[], assigns it to TO, and copies FROM to it. Can be used on const. */ +#define MALLOC_STR(TO,FROM) strcpy(CONST_CAST(char*, TO) = (char*)malloc(strlen(FROM)), FROM) + +/* Frees memory. Can be used on const. */ +#define FREE(VALUE) free((void*)VALUE) + +/* Read file at specific path to a new char[]. Return value must be freed. */ const char* readFile (const char* path); #ifdef __cplusplus diff --git a/spine-sfml/src/main.cpp b/spine-sfml/src/main.cpp index 69c78813b..338367503 100644 --- a/spine-sfml/src/main.cpp +++ b/spine-sfml/src/main.cpp @@ -32,12 +32,12 @@ using namespace spine; int main () { Atlas* atlas = Atlas_readAtlasFile("../data/spineboy.atlas"); - SkeletonJson* json = SkeletonJson_create(atlas); + SkeletonJson* json = SkeletonJson_new(atlas); SkeletonData *skeletonData = SkeletonJson_readSkeletonDataFile(json, "../data/spineboy-skeleton.json"); Animation* animation = SkeletonJson_readAnimationFile(json, "../data/spineboy-walk.json", skeletonData); - SkeletonJson_dispose(json); + SkeletonJson_free(json); - Skeleton* skeleton = Skeleton_create(skeletonData); + Skeleton* skeleton = Skeleton_new(skeletonData); skeleton->flipX = false; skeleton->flipY = false; Skeleton_setToBindPose(skeleton); @@ -65,7 +65,7 @@ int main () { Skeleton_updateWorldTransform(skeleton); } - Skeleton_dispose(skeleton); - SkeletonData_dispose(skeletonData); - Atlas_dispose(atlas); + Skeleton_free(skeleton); + SkeletonData_free(skeletonData); + Atlas_free(atlas); } diff --git a/spine-sfml/src/spine/spine-sfml.cpp b/spine-sfml/src/spine/spine-sfml.cpp index bcc487291..7fcc62b8e 100644 --- a/spine-sfml/src/spine/spine-sfml.cpp +++ b/spine-sfml/src/spine/spine-sfml.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -43,81 +42,82 @@ using sf::VertexArray; namespace spine { -void _SfmlAtlasPage_dispose (AtlasPage* page) { - SfmlAtlasPage* self = (SfmlAtlasPage*)page; - _AtlasPage_deinit(&self->super); +void _SfmlAtlasPage_free (AtlasPage* page) { + SfmlAtlasPage* self = SUB_CAST(SfmlAtlasPage, page); + _AtlasPage_deinit(SUPER(self)); delete self->texture; - FREE(page) + FREE(page); } -AtlasPage* AtlasPage_create (const char* name) { - SfmlAtlasPage* self = CALLOC(SfmlAtlasPage, 1) - _AtlasPage_init(&self->super, name); - self->super._dispose = _SfmlAtlasPage_dispose; +AtlasPage* AtlasPage_new (const char* name) { + SfmlAtlasPage* self = NEW(SfmlAtlasPage); + _AtlasPage_init(SUPER(self), name); + VTABLE(AtlasPage, self) ->free = _SfmlAtlasPage_free; self->texture = new Texture(); self->texture->loadFromFile(name); - return &self->super; + return SUPER(self); } /**/ -void _SfmlSkeleton_dispose (Skeleton* skeleton) { - SfmlSkeleton* self = (SfmlSkeleton*)skeleton; - _Skeleton_deinit(&self->super); +void _SfmlSkeleton_free (Skeleton* skeleton) { + SfmlSkeleton* self = SUB_CAST(SfmlSkeleton, skeleton); + _Skeleton_deinit(SUPER(self)); delete self->vertexArray; delete self->drawable; - FREE(self) + FREE(self); } -Skeleton* Skeleton_create (SkeletonData* data) { +Skeleton* Skeleton_new (SkeletonData* data) { Bone_setYDown(1); - SfmlSkeleton* self = CALLOC(SfmlSkeleton, 1) - _Skeleton_init(&self->super, data); - self->super._dispose = _SfmlSkeleton_dispose; + SfmlSkeleton* self = NEW(SfmlSkeleton); + _Skeleton_init(SUPER(self), data); + VTABLE(Skeleton, self) ->free = _SfmlSkeleton_free; - self->drawable = new SkeletonDrawable(&self->super); + self->drawable = new SkeletonDrawable(SUPER(self)); self->vertexArray = new VertexArray(Quads, data->boneCount * 4); - return &self->super; + return SUPER(self); } SkeletonDrawable& Skeleton_getDrawable (const Skeleton* self) { - return *((SfmlSkeleton*)self)->drawable; + return *SUB_CAST(SfmlSkeleton, self) ->drawable; } SkeletonDrawable::SkeletonDrawable (Skeleton* self) : - skeleton((SfmlSkeleton*)self) { + skeleton(SUB_CAST(SfmlSkeleton, self) ) { } void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const { skeleton->vertexArray->clear(); - for (int i = 0; i < skeleton->super.slotCount; ++i) - if (skeleton->super.slots[i]->attachment) Attachment_draw(skeleton->super.slots[i]->attachment, skeleton->super.slots[i]); + for (int i = 0; i < SUPER(skeleton)->slotCount; ++i) + if (SUPER(skeleton)->slots[i]->attachment) + Attachment_draw(SUPER(skeleton)->slots[i]->attachment, SUPER(skeleton)->slots[i]); states.texture = skeleton->texture; target.draw(*skeleton->vertexArray, states); } /**/ -void _SfmlRegionAttachment_dispose (Attachment* self) { - _RegionAttachment_deinit((RegionAttachment*)self); - FREE(self) +void _SfmlRegionAttachment_free (Attachment* self) { + _RegionAttachment_deinit(SUB_CAST(RegionAttachment, self) ); + FREE(self); } void _SfmlRegionAttachment_draw (Attachment* attachment, Slot* slot) { - SfmlRegionAttachment* self = (SfmlRegionAttachment*)attachment; + SfmlRegionAttachment* self = SUB_CAST(SfmlRegionAttachment, attachment); SfmlSkeleton* skeleton = (SfmlSkeleton*)slot->skeleton; - Uint8 r = skeleton->super.r * slot->r * 255; - Uint8 g = skeleton->super.g * slot->g * 255; - Uint8 b = skeleton->super.b * slot->b * 255; - Uint8 a = skeleton->super.a * slot->a * 255; + Uint8 r = SUPER(skeleton)->r * slot->r * 255; + Uint8 g = SUPER(skeleton)->g * slot->g * 255; + Uint8 b = SUPER(skeleton)->b * slot->b * 255; + Uint8 a = SUPER(skeleton)->a * slot->a * 255; sf::Vertex* vertices = self->vertices; vertices[0].color.r = r; vertices[0].color.g = g; @@ -136,7 +136,7 @@ void _SfmlRegionAttachment_draw (Attachment* attachment, Slot* slot) { vertices[3].color.b = b; vertices[3].color.a = a; - float* offset = self->super.offset; + float* offset = SUPER(self)->offset; Bone* bone = slot->bone; vertices[0].position.x = offset[0] * bone->m00 + offset[1] * bone->m01 + bone->worldX; vertices[0].position.y = offset[0] * bone->m10 + offset[1] * bone->m11 + bone->worldY; @@ -155,11 +155,11 @@ void _SfmlRegionAttachment_draw (Attachment* attachment, Slot* slot) { skeleton->vertexArray->append(vertices[3]); } -RegionAttachment* RegionAttachment_create (const char* name, AtlasRegion* region) { - SfmlRegionAttachment* self = CALLOC(SfmlRegionAttachment, 1) - _RegionAttachment_init(&self->super, name); - self->super.super._dispose = _SfmlRegionAttachment_dispose; - self->super.super._draw = _SfmlRegionAttachment_draw; +RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) { + SfmlRegionAttachment* self = NEW(SfmlRegionAttachment); + _RegionAttachment_init(SUPER(self), name); + VTABLE(Attachment, self) ->free = _SfmlRegionAttachment_free; + VTABLE(Attachment, self) ->draw = _SfmlRegionAttachment_draw; self->texture = ((SfmlAtlasPage*)region->page)->texture; int u = region->x; @@ -186,7 +186,7 @@ RegionAttachment* RegionAttachment_create (const char* name, AtlasRegion* region self->vertices[3].texCoords.y = v2; } - return &self->super; + return SUPER(self); } }