Moved drawing stuff into SkeletonDrawable.

This commit is contained in:
NathanSweet 2013-04-01 19:24:02 +02:00
parent 6af401e888
commit 6076689082
4 changed files with 35 additions and 35 deletions

View File

@ -66,6 +66,7 @@
</toolChain> </toolChain>
</folderInfo> </folderInfo>
<sourceEntries> <sourceEntries>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="example"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="spine-c src"/> <entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="spine-c src"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src"/> <entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src"/>
</sourceEntries> </sourceEntries>

View File

@ -37,7 +37,8 @@ int main () {
Animation* animation = SkeletonJson_readAnimationFile(json, "../data/spineboy-walk.json", skeletonData); Animation* animation = SkeletonJson_readAnimationFile(json, "../data/spineboy-walk.json", skeletonData);
SkeletonJson_free(json); SkeletonJson_free(json);
Skeleton* skeleton = Skeleton_new(skeletonData); SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData);
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false; skeleton->flipX = false;
skeleton->flipY = false; skeleton->flipY = false;
Skeleton_setToBindPose(skeleton); Skeleton_setToBindPose(skeleton);
@ -54,7 +55,7 @@ int main () {
while (window.pollEvent(event)) while (window.pollEvent(event))
if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::Closed) window.close();
window.clear(); window.clear();
window.draw(Skeleton_getDrawable(skeleton)); window.draw(*drawable);
window.display(); window.display();
float delta = deltaClock.getElapsedTime().asSeconds(); float delta = deltaClock.getElapsedTime().asSeconds();

View File

@ -24,7 +24,6 @@
******************************************************************************/ ******************************************************************************/
#include <spine/spine-sfml.h> #include <spine/spine-sfml.h>
#include <spine/spine.h>
#include <spine/extension.h> #include <spine/extension.h>
#include <SFML/Graphics/Vertex.hpp> #include <SFML/Graphics/Vertex.hpp>
#include <SFML/Graphics/VertexArray.hpp> #include <SFML/Graphics/VertexArray.hpp>
@ -67,41 +66,36 @@ AtlasPage* AtlasPage_new (const char* name) {
void _SfmlSkeleton_free (Skeleton* skeleton) { void _SfmlSkeleton_free (Skeleton* skeleton) {
SfmlSkeleton* self = SUB_CAST(SfmlSkeleton, skeleton); SfmlSkeleton* self = SUB_CAST(SfmlSkeleton, skeleton);
_Skeleton_deinit(SUPER(self)); _Skeleton_deinit(SUPER(self));
delete self->vertexArray;
delete self->drawable;
FREE(self); FREE(self);
} }
Skeleton* Skeleton_new (SkeletonData* data) { Skeleton* Skeleton_new (SkeletonData* data, SkeletonDrawable* drawable) {
Bone_setYDown(1);
SfmlSkeleton* self = NEW(SfmlSkeleton); SfmlSkeleton* self = NEW(SfmlSkeleton);
_Skeleton_init(SUPER(self), data); _Skeleton_init(SUPER(self), data);
VTABLE(Skeleton, self) ->free = _SfmlSkeleton_free; VTABLE(Skeleton, self) ->free = _SfmlSkeleton_free;
self->drawable = new SkeletonDrawable(SUPER(self)); CONST_CAST(SkeletonDrawable*, self->drawable) = drawable;
self->vertexArray = new VertexArray(Quads, data->boneCount * 4);
return SUPER(self); return SUPER(self);
} }
SkeletonDrawable& Skeleton_getDrawable (const Skeleton* self) { SkeletonDrawable::SkeletonDrawable (SkeletonData* skeletonData) :
return *SUB_CAST(SfmlSkeleton, self) ->drawable; vertexArray(new VertexArray(Quads, skeletonData->boneCount * 4)),
texture(0) {
Bone_setYDown(1);
skeleton = Skeleton_new(skeletonData, this);
} }
SkeletonDrawable::SkeletonDrawable (Skeleton* self) : SkeletonDrawable::~SkeletonDrawable () {
skeleton(SUB_CAST(SfmlSkeleton, self) ) { delete vertexArray;
} }
void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const { void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
skeleton->vertexArray->clear(); vertexArray->clear();
for (int i = 0; i < SUPER(skeleton)->slotCount; ++i) for (int i = 0; i < skeleton->slotCount; ++i)
if (SUPER(skeleton)->slots[i]->attachment) if (skeleton->slots[i]->attachment) Attachment_draw(skeleton->slots[i]->attachment, skeleton->slots[i]);
Attachment_draw(SUPER(skeleton)->slots[i]->attachment, SUPER(skeleton)->slots[i]); states.texture = texture;
states.texture = skeleton->texture; target.draw(*vertexArray, states);
target.draw(*skeleton->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; 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. // SMFL doesn't handle batching for us, so we'll just force a single texture per skeleton.
skeleton->texture = self->texture; skeleton->drawable->texture = self->texture;
skeleton->vertexArray->append(vertices[0]); skeleton->drawable->vertexArray->append(vertices[0]);
skeleton->vertexArray->append(vertices[1]); skeleton->drawable->vertexArray->append(vertices[1]);
skeleton->vertexArray->append(vertices[2]); skeleton->drawable->vertexArray->append(vertices[2]);
skeleton->vertexArray->append(vertices[3]); skeleton->drawable->vertexArray->append(vertices[3]);
} }
RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) { RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) {
@ -189,4 +183,10 @@ RegionAttachment* RegionAttachment_new (const char* name, AtlasRegion* region) {
return SUPER(self); return SUPER(self);
} }
/**/
char* _Util_readFile (const char* path, int* length) {
return _readFile(path, length);
}
} }

View File

@ -43,22 +43,21 @@ class SkeletonDrawable;
typedef struct { typedef struct {
Skeleton super; Skeleton super;
sf::VertexArray* vertexArray; SkeletonDrawable* const drawable;
sf::Texture* texture; // All region attachments must use the same texture.
SkeletonDrawable* drawable;
} SfmlSkeleton; } SfmlSkeleton;
class SkeletonDrawable: public sf::Drawable { class SkeletonDrawable: public sf::Drawable {
public: 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; virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const;
}; };
SkeletonDrawable& Skeleton_getDrawable (const Skeleton* skeleton);
/**/ /**/
typedef struct { typedef struct {
@ -67,6 +66,5 @@ typedef struct {
sf::Texture* texture; sf::Texture* texture;
} SfmlRegionAttachment; } SfmlRegionAttachment;
} /* namespace spine */ } /* namespace spine */
#endif /* SPINE_SFML_H_ */ #endif /* SPINE_SFML_H_ */