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>
</folderInfo>
<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="src"/>
</sourceEntries>

View File

@ -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();

View File

@ -24,7 +24,6 @@
******************************************************************************/
#include <spine/spine-sfml.h>
#include <spine/spine.h>
#include <spine/extension.h>
#include <SFML/Graphics/Vertex.hpp>
#include <SFML/Graphics/VertexArray.hpp>
@ -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);
}
}

View File

@ -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_ */