From 607668908231b72969e63ea0db8948516588433b Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 1 Apr 2013 19:24:02 +0200 Subject: [PATCH] Moved drawing stuff into SkeletonDrawable. --- spine-sfml/.cproject | 1 + spine-sfml/{src => example}/main.cpp | 5 +-- spine-sfml/src/spine/spine-sfml.cpp | 50 ++++++++++++++-------------- spine-sfml/src/spine/spine-sfml.h | 14 ++++---- 4 files changed, 35 insertions(+), 35 deletions(-) rename spine-sfml/{src => example}/main.cpp (95%) diff --git a/spine-sfml/.cproject b/spine-sfml/.cproject index d9ada5c62..609073823 100644 --- a/spine-sfml/.cproject +++ b/spine-sfml/.cproject @@ -66,6 +66,7 @@ + diff --git a/spine-sfml/src/main.cpp b/spine-sfml/example/main.cpp similarity index 95% rename from spine-sfml/src/main.cpp rename to spine-sfml/example/main.cpp index 338367503..dff1ce36e 100644 --- a/spine-sfml/src/main.cpp +++ b/spine-sfml/example/main.cpp @@ -37,7 +37,8 @@ int main () { Animation* animation = SkeletonJson_readAnimationFile(json, "../data/spineboy-walk.json", skeletonData); SkeletonJson_free(json); - Skeleton* skeleton = Skeleton_new(skeletonData); + SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData); + Skeleton* skeleton = drawable->skeleton; skeleton->flipX = false; skeleton->flipY = false; Skeleton_setToBindPose(skeleton); @@ -54,7 +55,7 @@ int main () { while (window.pollEvent(event)) if (event.type == sf::Event::Closed) window.close(); window.clear(); - window.draw(Skeleton_getDrawable(skeleton)); + window.draw(*drawable); window.display(); float delta = deltaClock.getElapsedTime().asSeconds(); diff --git a/spine-sfml/src/spine/spine-sfml.cpp b/spine-sfml/src/spine/spine-sfml.cpp index 7fcc62b8e..61e429406 100644 --- a/spine-sfml/src/spine/spine-sfml.cpp +++ b/spine-sfml/src/spine/spine-sfml.cpp @@ -24,7 +24,6 @@ ******************************************************************************/ #include -#include #include #include #include @@ -67,41 +66,36 @@ AtlasPage* AtlasPage_new (const char* name) { void _SfmlSkeleton_free (Skeleton* skeleton) { SfmlSkeleton* self = SUB_CAST(SfmlSkeleton, skeleton); _Skeleton_deinit(SUPER(self)); - - delete self->vertexArray; - delete self->drawable; - FREE(self); } -Skeleton* Skeleton_new (SkeletonData* data) { - Bone_setYDown(1); - +Skeleton* Skeleton_new (SkeletonData* data, SkeletonDrawable* drawable) { SfmlSkeleton* self = NEW(SfmlSkeleton); _Skeleton_init(SUPER(self), data); VTABLE(Skeleton, self) ->free = _SfmlSkeleton_free; - self->drawable = new SkeletonDrawable(SUPER(self)); - self->vertexArray = new VertexArray(Quads, data->boneCount * 4); + CONST_CAST(SkeletonDrawable*, self->drawable) = drawable; return SUPER(self); } -SkeletonDrawable& Skeleton_getDrawable (const Skeleton* self) { - return *SUB_CAST(SfmlSkeleton, self) ->drawable; +SkeletonDrawable::SkeletonDrawable (SkeletonData* skeletonData) : + vertexArray(new VertexArray(Quads, skeletonData->boneCount * 4)), + texture(0) { + Bone_setYDown(1); + skeleton = Skeleton_new(skeletonData, this); } -SkeletonDrawable::SkeletonDrawable (Skeleton* self) : - skeleton(SUB_CAST(SfmlSkeleton, self) ) { +SkeletonDrawable::~SkeletonDrawable () { + delete vertexArray; } void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const { - skeleton->vertexArray->clear(); - 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); + vertexArray->clear(); + for (int i = 0; i < skeleton->slotCount; ++i) + if (skeleton->slots[i]->attachment) Attachment_draw(skeleton->slots[i]->attachment, skeleton->slots[i]); + states.texture = texture; + target.draw(*vertexArray, states); } /**/ @@ -148,11 +142,11 @@ void _SfmlRegionAttachment_draw (Attachment* attachment, Slot* slot) { vertices[3].position.y = offset[6] * bone->m10 + offset[7] * bone->m11 + bone->worldY; // SMFL doesn't handle batching for us, so we'll just force a single texture per skeleton. - skeleton->texture = self->texture; - skeleton->vertexArray->append(vertices[0]); - skeleton->vertexArray->append(vertices[1]); - skeleton->vertexArray->append(vertices[2]); - skeleton->vertexArray->append(vertices[3]); + skeleton->drawable->texture = self->texture; + skeleton->drawable->vertexArray->append(vertices[0]); + skeleton->drawable->vertexArray->append(vertices[1]); + skeleton->drawable->vertexArray->append(vertices[2]); + skeleton->drawable->vertexArray->append(vertices[3]); } RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) { @@ -189,4 +183,10 @@ RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) { return SUPER(self); } +/**/ + +char* _Util_readFile (const char* path, int* length) { + return _readFile(path, length); +} + } diff --git a/spine-sfml/src/spine/spine-sfml.h b/spine-sfml/src/spine/spine-sfml.h index ade93ec9b..25571f68e 100644 --- a/spine-sfml/src/spine/spine-sfml.h +++ b/spine-sfml/src/spine/spine-sfml.h @@ -43,22 +43,21 @@ class SkeletonDrawable; typedef struct { Skeleton super; - sf::VertexArray* vertexArray; - sf::Texture* texture; // All region attachments must use the same texture. - SkeletonDrawable* drawable; + SkeletonDrawable* const drawable; } SfmlSkeleton; class SkeletonDrawable: public sf::Drawable { public: - SfmlSkeleton* skeleton; + Skeleton* skeleton; + sf::VertexArray* vertexArray; + sf::Texture* texture; // All region attachments must use the same texture. - SkeletonDrawable (Skeleton* skeleton); + SkeletonDrawable (SkeletonData* skeleton); + ~SkeletonDrawable (); virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const; }; -SkeletonDrawable& Skeleton_getDrawable (const Skeleton* skeleton); - /**/ typedef struct { @@ -67,6 +66,5 @@ typedef struct { sf::Texture* texture; } SfmlRegionAttachment; - } /* namespace spine */ #endif /* SPINE_SFML_H_ */