[cpp] Added preliminary SFML backend, fixed up a few APIs.

This commit is contained in:
badlogic 2018-02-21 17:52:56 +01:00
parent 6d243eca48
commit ed7dc4ea93
17 changed files with 276 additions and 315 deletions

View File

@ -45,7 +45,7 @@ namespace Spine {
explicit EventData(const String& name);
/// The name of the event, which is unique within the skeleton.
const String& getName();
const String& getName() const;
int getIntValue();
void setIntValue(int inValue);

View File

@ -62,8 +62,8 @@ namespace Spine {
Vector<float>& getUVs();
void setUVs(Vector<float>& inValue);
Vector<short>& getTriangles();
void setTriangles(Vector<short>& inValue);
Vector<unsigned short>& getTriangles();
void setTriangles(Vector<unsigned short>& inValue);
Color& getColor();
@ -115,8 +115,8 @@ namespace Spine {
void setParentMesh(MeshAttachment* inValue);
// Nonessential.
Vector<short>& getEdges();
void setEdges(Vector<short>& inValue);
Vector<unsigned short>& getEdges();
void setEdges(Vector<unsigned short>& inValue);
float getWidth();
void setWidth(float inValue);
float getHeight();
@ -127,8 +127,8 @@ namespace Spine {
MeshAttachment* _parentMesh;
Vector<float> _uvs;
Vector<float> _regionUVs;
Vector<short> _triangles;
Vector<short> _edges;
Vector<unsigned short> _triangles;
Vector<unsigned short> _edges;
void* _rendererObject;
String _path;
float _regionU;

View File

@ -158,6 +158,7 @@ namespace Spine {
Color& getColor();
float getTime();
void setTime(float inValue);
void setPosition(float x, float y);
float getX();
void setX(float inValue);
float getY();

View File

@ -78,6 +78,10 @@ namespace Spine {
SkeletonData* readSkeletonData(const unsigned char* binary, int length);
SkeletonData* readSkeletonDataFile(const String& path);
void setScale(float scale) { _scale = scale; }
String& getError() { return _error; }
private:
struct DataInput : public SpineObject {
@ -115,9 +119,9 @@ namespace Spine {
void readVertices(DataInput* input, VertexAttachment* attachment, int vertexCount);
Vector<float> readFloatArray(DataInput *input, int n, float scale);
void readFloatArray(DataInput *input, int n, float scale, Vector<float>& array);
Vector<short> readShortArray(DataInput *input);
void readShortArray(DataInput *input, Vector<unsigned short>& array);
Animation* readAnimation(const char* name, DataInput* input, SkeletonData *skeletonData);

View File

@ -48,12 +48,12 @@ namespace Spine {
void clipEnd();
void clipTriangles(Vector<float>& vertices, int verticesLength, Vector<int>& triangles, int trianglesLength, Vector<float>& uvs);
void clipTriangles(Vector<float>& vertices, int verticesLength, Vector<unsigned short>& triangles, int trianglesLength, Vector<float>& uvs);
bool isClipping();
Vector<float>& getClippedVertices();
Vector<int>& getClippedTriangles();
Vector<unsigned short>& getClippedTriangles();
Vector<float>& getClippedUVs();
private:
@ -61,7 +61,7 @@ namespace Spine {
Vector<float> _clippingPolygon;
Vector<float> _clipOutput;
Vector<float> _clippedVertices;
Vector<int> _clippedTriangles;
Vector<unsigned short> _clippedTriangles;
Vector<float> _clippedUVs;
Vector<float> _scratch;
ClippingAttachment* _clipAttachment;

View File

@ -57,6 +57,10 @@ namespace Spine {
SkeletonData* readSkeletonDataFile(const String& path);
SkeletonData* readSkeletonData(const char* json);
void setScale (float scale) { _scale = scale; }
String& getError() { return _error; }
private:
AttachmentLoader* _attachmentLoader;

View File

@ -175,6 +175,10 @@ namespace Spine {
return !(lhs == rhs);
}
T* buffer() {
return _buffer;
}
private:
size_t _size;
size_t _capacity;

View File

@ -88,6 +88,8 @@
#include <spine/SkeletonData.h>
#include <spine/SkeletonJson.h>
#include <spine/Skin.h>
#include <spine/Slot.h>
#include <spine/SlotData.h>
#include <spine/SpineObject.h>
#include <spine/String.h>
#include <spine/TextureLoader.h>

View File

@ -42,7 +42,7 @@ namespace Spine {
}
/// The name of the event, which is unique within the skeleton.
const String& EventData::getName() {
const String& EventData::getName() const {
return _name;
}

View File

@ -32,18 +32,6 @@
#include <math.h>
namespace Spine {
float MathUtil::SIN_TABLE[SIN_COUNT] = {0.0f};
MathUtil::MathUtil() {
for (int i = 0; i < SIN_COUNT; ++i) {
SIN_TABLE[i] = sin((i + 0.5f) / SIN_COUNT * RadFull);
}
for (int i = 0; i < 360; i += 90) {
SIN_TABLE[i * DegToIndex & SIN_MASK] = sin(i * DegRad);
}
}
int MathUtil::sign(float val) {
return (0 < val) - (val < 0);
}
@ -74,53 +62,28 @@ namespace Spine {
/// Returns the sine in radians from a lookup table.
float MathUtil::sin(float radians) {
return SIN_TABLE[(int)(radians * RadToIndex) & SIN_MASK];
return ::sin(radians);
}
/// Returns the cosine in radians from a lookup table.
float MathUtil::cos(float radians) {
return SIN_TABLE[(int)((radians + SPINE_PI / 2) * RadToIndex) & SIN_MASK];
return ::cos(radians);
}
/// Returns the sine in radians from a lookup table.
float MathUtil::sinDeg(float degrees) {
return SIN_TABLE[(int)(degrees * DegToIndex) & SIN_MASK];
return ::sin(degrees * DegRad);
}
/// Returns the cosine in radians from a lookup table.
float MathUtil::cosDeg(float degrees) {
return SIN_TABLE[(int)((degrees + 90) * DegToIndex) & SIN_MASK];
return ::cos(degrees * DegRad);
}
/// Returns atan2 in radians, faster but less accurate than Math.Atan2. Average error of 0.00231 radians (0.1323
/// degrees), largest error of 0.00488 radians (0.2796 degrees).
float MathUtil::atan2(float y, float x) {
if (areFloatsPracticallyEqual(x, 0.0f)) {
if (y > 0.0f) {
return SPINE_PI / 2;
}
if (areFloatsPracticallyEqual(y, 0.0f)) {
return 0.0f;
}
return -SPINE_PI / 2;
}
float atan, z = y / x;
if (fabs(z) < 1.0f) {
atan = z / (1.0f + 0.28f * z * z);
if (x < 0.0f) {
return atan + (y < 0.0f ? -SPINE_PI : SPINE_PI);
}
return atan;
}
atan = SPINE_PI / 2 - z / (z * z + 0.28f);
return y < 0.0f ? atan - SPINE_PI : atan;
return ::atan2(y, x);
}
float MathUtil::acos(float v) {

View File

@ -104,11 +104,11 @@
_uvs = inValue;
}
Vector<short>& MeshAttachment::getTriangles() {
Vector<unsigned short>& MeshAttachment::getTriangles() {
return _triangles;
}
void MeshAttachment::setTriangles(Vector<short>& inValue) {
void MeshAttachment::setTriangles(Vector<unsigned short>& inValue) {
_triangles = inValue;
}
@ -243,11 +243,11 @@
}
}
Vector<short>& MeshAttachment::getEdges() {
Vector<unsigned short>& MeshAttachment::getEdges() {
return _edges;
}
void MeshAttachment::setEdges(Vector<short>& inValue) {
void MeshAttachment::setEdges(Vector<unsigned short>& inValue) {
_edges = inValue;
}

View File

@ -119,7 +119,6 @@ namespace Spine {
}
updateCache();
updateWorldTransform();
}
Skeleton::~Skeleton() {
@ -496,6 +495,11 @@ namespace Spine {
void Skeleton::setTime(float inValue) {
_time = inValue;
}
void Skeleton::setPosition(float x, float y) {
_x = x;
_y = y;
}
float Skeleton::getX() {
return _x;

View File

@ -579,16 +579,13 @@ namespace Spine {
mesh->_path = String(path);
readColor(input, mesh->getColor());
vertexCount = readVarint(input, true);
Vector<float> float_array = readFloatArray(input, vertexCount << 1, 1);
mesh->setRegionUVs(float_array);
Vector<short> triangles = readShortArray(input);
mesh->setTriangles(triangles);
readFloatArray(input, vertexCount << 1, 1, mesh->getRegionUVs());
readShortArray(input, mesh->getTriangles());
readVertices(input, static_cast<VertexAttachment*>(mesh), vertexCount);
mesh->updateUVs();
mesh->_hullLength = readVarint(input, true) << 1;
if (nonessential) {
Vector<short> edges = readShortArray(input);
mesh->setEdges(edges);
readShortArray(input, mesh->getEdges());
mesh->_width = readFloat(input) * _scale;
mesh->_height = readFloat(input) * _scale;
}
@ -705,7 +702,7 @@ namespace Spine {
int verticesLength = vertexCount << 1;
if (!readBoolean(input)) {
attachment->setVertices(readFloatArray(input, verticesLength, scale));
readFloatArray(input, verticesLength, scale, attachment->getVertices());
return;
}
@ -728,8 +725,7 @@ namespace Spine {
attachment->setBones(vertices._bones);
}
Vector<float> SkeletonBinary::readFloatArray(DataInput *input, int n, float scale) {
Vector<float> array;
void SkeletonBinary::readFloatArray(DataInput *input, int n, float scale, Vector<float>& array) {
array.setSize(n);
int i;
@ -743,14 +739,10 @@ namespace Spine {
array[i] = readFloat(input) * scale;
}
}
return array;
}
Vector<short> SkeletonBinary::readShortArray(DataInput *input) {
void SkeletonBinary::readShortArray(DataInput *input, Vector<unsigned short>& array) {
int n = readVarint(input, true);
Vector<short> array;
array.setSize(n);
int i;
@ -758,8 +750,6 @@ namespace Spine {
array[i] = readByte(input) << 8;
array[i] |= readByte(input);
}
return array;
}
Animation* SkeletonBinary::readAnimation(const char* name, DataInput* input, SkeletonData *skeletonData) {

View File

@ -85,10 +85,10 @@ namespace Spine {
_clippingPolygon.clear();
}
void SkeletonClipping::clipTriangles(Vector<float>& vertices, int verticesLength, Vector<int>& triangles, int trianglesLength, Vector<float>& uvs) {
void SkeletonClipping::clipTriangles(Vector<float>& vertices, int verticesLength, Vector<unsigned short>& triangles, int trianglesLength, Vector<float>& uvs) {
Vector<float>& clipOutput = _clipOutput;
Vector<float>& clippedVertices = _clippedVertices;
Vector<int>& clippedTriangles = _clippedTriangles;
Vector<unsigned short>& clippedTriangles = _clippedTriangles;
Vector< Vector<float>* >& polygons = _clippingPolygons;
int polygonsCount = static_cast<int>(_clippingPolygons.size());
@ -184,7 +184,7 @@ namespace Spine {
return _clippedVertices;
}
Vector<int>& SkeletonClipping::getClippedTriangles() {
Vector<unsigned short>& SkeletonClipping::getClippedTriangles() {
return _clippedTriangles;
}

View File

@ -36,103 +36,102 @@
#include <SFML/Window/Mouse.hpp>
using namespace std;
using namespace spine;
using namespace Spine;
#include <stdio.h>
#include <stdlib.h>
void callback (AnimationState* state, EventType type, TrackEntry* entry, Event* event) {
const char* animationName = (entry && entry->animation) ? entry->animation->name : 0;
const String& animationName = (entry && entry->getAnimation()) ? entry->getAnimation()->getName() : String("");
switch (type) {
case ANIMATION_START:
printf("%d start: %s\n", entry->trackIndex, animationName);
case EventType_Start:
printf("%d start: %s\n", entry->getTrackIndex(), animationName.buffer());
break;
case ANIMATION_INTERRUPT:
printf("%d interrupt: %s\n", entry->trackIndex, animationName);
case EventType_Interrupt:
printf("%d interrupt: %s\n", entry->getTrackIndex(), animationName.buffer());
break;
case ANIMATION_END:
printf("%d end: %s\n", entry->trackIndex, animationName);
case EventType_End:
printf("%d end: %s\n", entry->getTrackIndex(), animationName.buffer());
break;
case ANIMATION_COMPLETE:
printf("%d complete: %s\n", entry->trackIndex, animationName);
case EventType_Complete:
printf("%d complete: %s\n", entry->getTrackIndex(), animationName.buffer());
break;
case ANIMATION_DISPOSE:
printf("%d dispose: %s\n", entry->trackIndex, animationName);
case EventType_Dispose:
printf("%d dispose: %s\n", entry->getTrackIndex(), animationName.buffer());
break;
case ANIMATION_EVENT:
printf("%d event: %s, %s: %d, %f, %s\n", entry->trackIndex, animationName, event->data->name, event->intValue, event->floatValue,
event->stringValue);
case EventType_Event:
printf("%d event: %s, %s: %d, %f, %s\n", entry->getTrackIndex(), animationName.buffer(), event->getData().getName().buffer(), event->getIntValue(), event->getFloatValue(),
event->getStringValue().buffer());
break;
}
fflush(stdout);
}
SkeletonData* readSkeletonJsonData (const char* filename, Atlas* atlas, float scale) {
SkeletonJson* json = SkeletonJson_create(atlas);
json->scale = scale;
SkeletonData* skeletonData = SkeletonJson_readSkeletonDataFile(json, filename);
SkeletonData* readSkeletonJsonData (const String& filename, Atlas* atlas, float scale) {
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(atlas);
json->setScale(scale);
SkeletonData* skeletonData = json->readSkeletonDataFile(filename);
if (!skeletonData) {
printf("%s\n", json->error);
printf("%s\n", json->getError().buffer());
exit(0);
}
SkeletonJson_dispose(json);
delete json;
return skeletonData;
}
SkeletonData* readSkeletonBinaryData (const char* filename, Atlas* atlas, float scale) {
SkeletonBinary* binary = SkeletonBinary_create(atlas);
binary->scale = scale;
SkeletonData *skeletonData = SkeletonBinary_readSkeletonDataFile(binary, filename);
SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(atlas);
binary->setScale(scale);
SkeletonData *skeletonData = binary->readSkeletonDataFile(filename);
if (!skeletonData) {
printf("%s\n", binary->error);
printf("%s\n", binary->getError().buffer());
exit(0);
}
SkeletonBinary_dispose(binary);
delete binary;
return skeletonData;
}
void testcase (void func(SkeletonData* skeletonData, Atlas* atlas),
const char* jsonName, const char* binaryName, const char* atlasName,
float scale) {
Atlas* atlas = Atlas_createFromFile(atlasName, 0);
Atlas* atlas = new (__FILE__, __LINE__) Atlas(atlasName, 0);
SkeletonData* skeletonData = readSkeletonJsonData(jsonName, atlas, scale);
func(skeletonData, atlas);
SkeletonData_dispose(skeletonData);
delete skeletonData;
skeletonData = readSkeletonBinaryData(binaryName, atlas, scale);
func(skeletonData, atlas);
SkeletonData_dispose(skeletonData);
delete skeletonData;
Atlas_dispose(atlas);
delete atlas;
}
void spineboy (SkeletonData* skeletonData, Atlas* atlas) {
SkeletonBounds* bounds = SkeletonBounds_create();
SkeletonBounds bounds;
// Configure mixing.
AnimationStateData* stateData = AnimationStateData_create(skeletonData);
AnimationStateData_setMixByName(stateData, "walk", "jump", 0.2f);
AnimationStateData_setMixByName(stateData, "jump", "run", 0.2f);
AnimationStateData stateData(skeletonData);
stateData.setMix("walk", "jump", 0.2f);
stateData.setMix("jump", "run", 0.2f);
SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData, stateData);
SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData, &stateData);
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
Skeleton_setToSetupPose(skeleton);
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->setToSetupPose();
skeleton->x = 320;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 590);
skeleton->updateWorldTransform();
Slot* headSlot = Skeleton_findSlot(skeleton, "head");
Slot* headSlot = skeleton->findSlot("head");
drawable->state->listener = callback;
AnimationState_addAnimationByName(drawable->state, 0, "walk", true, 0);
AnimationState_addAnimationByName(drawable->state, 0, "jump", false, 3);
AnimationState_addAnimationByName(drawable->state, 0, "run", true, 0);
drawable->state->setOnAnimationEventFunc(callback);
drawable->state->addAnimation(0, "walk", true, 0);
drawable->state->addAnimation(0, "jump", false, 3);
drawable->state->addAnimation(0, "run", true, 0);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - spineboy");
window.setFramerateLimit(60);
@ -145,14 +144,14 @@ void spineboy (SkeletonData* skeletonData, Atlas* atlas) {
float delta = deltaClock.getElapsedTime().asSeconds();
deltaClock.restart();
SkeletonBounds_update(bounds, skeleton, true);
bounds.update(*skeleton, true);
sf::Vector2i position = sf::Mouse::getPosition(window);
if (SkeletonBounds_containsPoint(bounds, position.x, position.y)) {
headSlot->color.g = 0;
headSlot->color.b = 0;
if (bounds.containsPoint(position.x, position.y)) {
headSlot->getColor()._g = 0;
headSlot->getColor()._b = 0;
} else {
headSlot->color.g = 1;
headSlot->color.b = 1;
headSlot->getColor()._g = 1;
headSlot->getColor()._b = 1;
}
drawable->update(delta);
@ -162,7 +161,7 @@ void spineboy (SkeletonData* skeletonData, Atlas* atlas) {
window.display();
}
SkeletonBounds_dispose(bounds);
delete drawable;
}
void goblins (SkeletonData* skeletonData, Atlas* atlas) {
@ -170,17 +169,15 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
Skeleton_setSkinByName(skeleton, "goblin");
Skeleton_setSlotsToSetupPose(skeleton);
//Skeleton_setAttachment(skeleton, "left hand item", "dagger");
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->setSkin("goblin");
skeleton->setSlotsToSetupPose();
skeleton->x = 320;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "walk", true);
drawable->state->setAnimation(0, "walk", true);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
window.setFramerateLimit(60);
@ -205,17 +202,16 @@ void raptor (SkeletonData* skeletonData, Atlas* atlas) {
SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData);
drawable->timeScale = 1;
spSwirlVertexEffect* effect = spSwirlVertexEffect_create(400);
effect->centerY = -200;
drawable->vertexEffect = &effect->super;
// BOZO spSwirlVertexEffect* effect = spSwirlVertexEffect_create(400);
// effect->centerY = -200;
// drawable->vertexEffect = &effect->super;
Skeleton* skeleton = drawable->skeleton;
skeleton->x = 320;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "walk", true);
AnimationState_addAnimationByName(drawable->state, 1, "gun-grab", false, 2);
drawable->state->setAnimation(0, "walk", true);
drawable->state->addAnimation(1, "gun-grab", false, 2);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - raptor");
window.setFramerateLimit(60);
@ -230,9 +226,9 @@ void raptor (SkeletonData* skeletonData, Atlas* atlas) {
deltaClock.restart();
swirlTime += delta;
float percent = fmod(swirlTime, 2);
float percent = MathUtil::fmod(swirlTime, 2);
if (percent > 1) percent = 1 - (percent - 1);
effect->angle = _spMath_interpolate(_spMath_pow2_apply, -60, 60, percent);
// BOZO effect->angle = _spMath_interpolate(_spMath_pow2_apply, -60, 60, percent);
drawable->update(delta);
@ -240,7 +236,7 @@ void raptor (SkeletonData* skeletonData, Atlas* atlas) {
window.draw(*drawable);
window.display();
}
spSwirlVertexEffect_dispose(effect);
// BOZO spSwirlVertexEffect_dispose(effect);
}
void tank (SkeletonData* skeletonData, Atlas* atlas) {
@ -248,11 +244,10 @@ void tank (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->x = 500;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(500, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "drive", true);
drawable->state->setAnimation(0, "drive", true);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - tank");
window.setFramerateLimit(60);
@ -277,11 +272,10 @@ void vine (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->x = 320;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "grow", true);
drawable->state->setAnimation(0, "grow", true);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - vine");
window.setFramerateLimit(60);
@ -307,14 +301,13 @@ void stretchyman (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->x = 100;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(100, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "sneak", true);
drawable->state->setAnimation(0, "sneak", true);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - Streatchyman");
window.setFramerateLimit(60);
@ -340,11 +333,10 @@ void coin (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->x = 320;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 590);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "rotate", true);
drawable->state->setAnimation(0, "rotate", true);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - vine");
window.setFramerateLimit(60);
@ -371,25 +363,24 @@ void owl (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->x = 320;
skeleton->y = 400;
Skeleton_updateWorldTransform(skeleton);
skeleton->setPosition(320, 400);
skeleton->updateWorldTransform();
AnimationState_setAnimationByName(drawable->state, 0, "idle", true);
AnimationState_setAnimationByName(drawable->state, 1, "blink", true);
spTrackEntry* left = AnimationState_setAnimationByName(drawable->state, 2, "left", true);
spTrackEntry* right = AnimationState_setAnimationByName(drawable->state, 3, "right", true);
spTrackEntry* up = AnimationState_setAnimationByName(drawable->state, 4, "up", true);
spTrackEntry* down = AnimationState_setAnimationByName(drawable->state, 5, "down", true);
drawable->state->setAnimation(0, "idle", true);
drawable->state->setAnimation(1, "blink", true);
TrackEntry* left = drawable->state->setAnimation(2, "left", true);
TrackEntry* right = drawable->state->setAnimation(3, "right", true);
TrackEntry* up = drawable->state->setAnimation(4, "up", true);
TrackEntry* down = drawable->state->setAnimation(5, "down", true);
left->alpha = 0;
left->mixBlend = SP_MIX_BLEND_ADD;
right->alpha = 0;
right->mixBlend = SP_MIX_BLEND_ADD;
up->alpha = 0;
up->mixBlend = SP_MIX_BLEND_ADD;
down->alpha = 0;
down->mixBlend = SP_MIX_BLEND_ADD;
left->setAlpha(0);
// BOZO left->setMixBlend(SP_MIX_BLEND_ADD);
right->setAlpha(0);
// BOZO right->mixBlend = SP_MIX_BLEND_ADD;
up->setAlpha(0);
// BOZO up->mixBlend = SP_MIX_BLEND_ADD;
down->setAlpha(0);
// BOZO down->mixBlend = SP_MIX_BLEND_ADD;
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - owl");
window.setFramerateLimit(60);
@ -400,12 +391,12 @@ void owl (SkeletonData* skeletonData, Atlas* atlas) {
if (event.type == sf::Event::Closed) window.close();
if (event.type == sf::Event::MouseMoved) {
float x = event.mouseMove.x / 640.0f;
left->alpha = (MAX(x, 0.5f) - 0.5f) * 2;
right->alpha = (0.5 - MIN(x, 0.5)) * 2;
left->setAlpha((MAX(x, 0.5f) - 0.5f) * 2);
right->setAlpha((0.5 - MIN(x, 0.5)) * 2);
float y = event.mouseMove.y / 640.0f;
down->alpha = (MAX(y, 0.5f) - 0.5f) * 2;
up->alpha = (0.5 - MIN(y, 0.5)) * 2;
down->setAlpha((MAX(y, 0.5f) - 0.5f) * 2);
up->setAlpha((0.5 - MIN(y, 0.5)) * 2);
}
}
@ -424,38 +415,40 @@ void owl (SkeletonData* skeletonData, Atlas* atlas) {
* Used for debugging purposes during runtime development
*/
void test (SkeletonData* skeletonData, Atlas* atlas) {
spSkeleton* skeleton = Skeleton_create(skeletonData);
spAnimationStateData* animData = spAnimationStateData_create(skeletonData);
spAnimationState* animState = spAnimationState_create(animData);
spAnimationState_setAnimationByName(animState, 0, "drive", true);
Skeleton* skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
AnimationStateData* animData = new (__FILE__, __LINE__) AnimationStateData(skeletonData);
AnimationState* animState = new (__FILE__, __LINE__) AnimationState(animData);
animState->setAnimation(0, "drive", true);
float d = 3;
for (int i = 0; i < 1; i++) {
spSkeleton_update(skeleton, d);
spAnimationState_update(animState, d);
spAnimationState_apply(animState, skeleton);
spSkeleton_updateWorldTransform(skeleton);
for (int ii = 0; ii < skeleton->bonesCount; ii++) {
spBone* bone = skeleton->bones[ii];
printf("%s %f %f %f %f %f %f\n", bone->data->name, bone->a, bone->b, bone->c, bone->d, bone->worldX, bone->worldY);
skeleton->update(d);
animState->update(d);
animState->apply(*skeleton);
skeleton->updateWorldTransform();
for (int ii = 0; ii < skeleton->getBones().size(); ii++) {
Bone* bone = skeleton->getBones()[ii];
printf("%s %f %f %f %f %f %f\n", bone->getData().getName().buffer(), bone->getA(), bone->getB(), bone->getC(), bone->getD(), bone->getWorldX(), bone->getWorldY());
}
printf("========================================\n");
d += 0.1f;
}
Skeleton_dispose(skeleton);
delete skeleton;
delete animData;
delete animState;
}
int main () {
testcase(test, "data/tank-pro.json", "data/tank-pro.skel", "data/tank.atlas", 1.0f);
testcase(owl, "data/owl-pro.json", "data/owl-pro.skel", "data/owl.atlas", 0.5f);
testcase(spineboy, "data/spineboy-ess.json", "data/spineboy-ess.skel", "data/spineboy.atlas", 0.6f);
/*testcase(owl, "data/owl-pro.json", "data/owl-pro.skel", "data/owl.atlas", 0.5f);
testcase(coin, "data/coin-pro.json", "data/coin-pro.skel", "data/coin.atlas", 0.5f);
testcase(vine, "data/vine-pro.json", "data/vine-pro.skel", "data/vine.atlas", 0.5f);
testcase(tank, "data/tank-pro.json", "data/tank-pro.skel", "data/tank.atlas", 0.2f);
testcase(raptor, "data/raptor-pro.json", "data/raptor-pro.skel", "data/raptor.atlas", 0.5f);
testcase(spineboy, "data/spineboy-ess.json", "data/spineboy-ess.skel", "data/spineboy.atlas", 0.6f);
testcase(goblins, "data/goblins-pro.json", "data/goblins-pro.skel", "data/goblins.atlas", 1.4f);
testcase(stretchyman, "data/stretchyman-pro.json", "data/stretchyman-pro.skel", "data/stretchyman.atlas", 0.6f);
testcase(stretchyman, "data/stretchyman-pro.json", "data/stretchyman-pro.skel", "data/stretchyman.atlas", 0.6f);*/
return 0;
}

View File

@ -47,14 +47,13 @@ sf::BlendMode additivePma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::One
sf::BlendMode multiplyPma = sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha);
sf::BlendMode screenPma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor);
_SP_ARRAY_IMPLEMENT_TYPE(spColorArray, spColor)
void _AtlasPage_createTexture (AtlasPage* self, const char* path){
void _AtlasPage_createTexture (Spine::AtlasPage* self, const char* path){
Texture* texture = new Texture();
if (!texture->loadFromFile(path)) return;
if (self->magFilter == SP_ATLAS_LINEAR) texture->setSmooth(true);
if (self->uWrap == SP_ATLAS_REPEAT && self->vWrap == SP_ATLAS_REPEAT) texture->setRepeated(true);
if (self->magFilter == Spine::TextureFilter_Nearest) texture->setSmooth(true);
if (self->uWrap == Spine::TextureWrap_Repeat && self->vWrap == Spine::TextureWrap_Repeat) texture->setRepeated(true);
self->rendererObject = texture;
Vector2u size = texture->getSize();
@ -62,147 +61,146 @@ void _AtlasPage_createTexture (AtlasPage* self, const char* path){
self->height = size.y;
}
void _AtlasPage_disposeTexture (AtlasPage* self){
void _AtlasPage_disposeTexture (Spine::AtlasPage* self){
delete (Texture*)self->rendererObject;
}
char* _Util_readFile (const char* path, int* length){
return _spReadFile(path, length);
return Spine::SpineExtension::readFile(Spine::String(path), length);
}
/**/
namespace spine {
namespace Spine {
SkeletonDrawable::SkeletonDrawable (SkeletonData* skeletonData, AnimationStateData* stateData) :
timeScale(1),
vertexArray(new VertexArray(Triangles, skeletonData->bonesCount * 4)),
vertexEffect(0),
worldVertices(0), clipper(0) {
Bone_setYDown(true);
worldVertices = MALLOC(float, SPINE_MESH_VERTEX_COUNT_MAX);
skeleton = Skeleton_create(skeletonData);
tempUvs = spFloatArray_create(16);
tempColors = spColorArray_create(16);
vertexArray(new VertexArray(Triangles, skeletonData->getBones().size() * 4)),
worldVertices(), clipper() {
Bone::setYDown(true);
worldVertices.setSize(SPINE_MESH_VERTEX_COUNT_MAX);
skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
tempUvs.ensureCapacity(16);
tempColors.ensureCapacity(16);
ownsAnimationStateData = stateData == 0;
if (ownsAnimationStateData) stateData = AnimationStateData_create(skeletonData);
if (ownsAnimationStateData) stateData = new (__FILE__, __LINE__) AnimationStateData(skeletonData);
state = AnimationState_create(stateData);
clipper = spSkeletonClipping_create();
state = new (__FILE__, __LINE__) AnimationState(stateData);
}
SkeletonDrawable::~SkeletonDrawable () {
delete vertexArray;
FREE(worldVertices);
if (ownsAnimationStateData) AnimationStateData_dispose(state->data);
AnimationState_dispose(state);
Skeleton_dispose(skeleton);
spSkeletonClipping_dispose(clipper);
spFloatArray_dispose(tempUvs);
spColorArray_dispose(tempColors);
if (ownsAnimationStateData) delete state->getData();
delete state;
delete skeleton;
}
void SkeletonDrawable::update (float deltaTime) {
Skeleton_update(skeleton, deltaTime);
AnimationState_update(state, deltaTime * timeScale);
AnimationState_apply(state, skeleton);
Skeleton_updateWorldTransform(skeleton);
skeleton->update(deltaTime);
state->update(deltaTime * timeScale);
state->apply(*skeleton);
skeleton->updateWorldTransform();
}
void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
vertexArray->clear();
states.texture = 0;
unsigned short quadIndices[6] = { 0, 1, 2, 2, 3, 0 };
states.texture = NULL;
Vector<unsigned short> quadIndices;
quadIndices.add(0);
quadIndices.add(1);
quadIndices.add(2);
quadIndices.add(2);
quadIndices.add(3);
quadIndices.add(0);
if (vertexEffect != 0) vertexEffect->begin(vertexEffect, skeleton);
// BOZO if (vertexEffect != 0) vertexEffect->begin(vertexEffect, skeleton);
sf::Vertex vertex;
Texture* texture = 0;
for (int i = 0; i < skeleton->slotsCount; ++i) {
Slot* slot = skeleton->drawOrder[i];
Attachment* attachment = slot->attachment;
Texture* texture = NULL;
for (int i = 0; i < skeleton->getSlots().size(); ++i) {
Slot& slot = *skeleton->getDrawOrder()[i];
Attachment* attachment = slot.getAttachment();
if (!attachment) continue;
float* vertices = worldVertices;
Vector<float>* vertices = &worldVertices;
int verticesCount = 0;
float* uvs = 0;
unsigned short* indices = 0;
Vector<float>* uvs = NULL;
Vector<unsigned short>* indices = NULL;
int indicesCount = 0;
spColor* attachmentColor;
Color* attachmentColor;
if (attachment->type == ATTACHMENT_REGION) {
if (attachment->rtti.derivesFrom(RegionAttachment::rtti)) {
RegionAttachment* regionAttachment = (RegionAttachment*)attachment;
spRegionAttachment_computeWorldVertices(regionAttachment, slot->bone, vertices, 0, 2);
regionAttachment->computeWorldVertices(slot.getBone(), worldVertices, 0, 2);
verticesCount = 4;
uvs = regionAttachment->uvs;
indices = quadIndices;
uvs = &regionAttachment->getUVs();
indices = &quadIndices;
indicesCount = 6;
texture = (Texture*)((AtlasRegion*)regionAttachment->rendererObject)->page->rendererObject;
attachmentColor = &regionAttachment->color;
texture = (Texture*)((AtlasRegion*)regionAttachment->getRendererObject())->page->rendererObject;
attachmentColor = &regionAttachment->getColor();
} else if (attachment->type == ATTACHMENT_MESH) {
} else if (attachment->rtti.derivesFrom(MeshAttachment::rtti)) {
MeshAttachment* mesh = (MeshAttachment*)attachment;
if (mesh->super.worldVerticesLength > SPINE_MESH_VERTEX_COUNT_MAX) continue;
texture = (Texture*)((AtlasRegion*)mesh->rendererObject)->page->rendererObject;
spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, worldVertices, 0, 2);
verticesCount = mesh->super.worldVerticesLength >> 1;
uvs = mesh->uvs;
indices = mesh->triangles;
indicesCount = mesh->trianglesCount;
attachmentColor = &mesh->color;
} else if (attachment->type == SP_ATTACHMENT_CLIPPING) {
spClippingAttachment* clip = (spClippingAttachment*)slot->attachment;
spSkeletonClipping_clipStart(clipper, slot, clip);
if (mesh->getWorldVerticesLength() > worldVertices.size()) worldVertices.setSize(mesh->getWorldVerticesLength());
texture = (Texture*)((AtlasRegion*)mesh->getRendererObject())->page->rendererObject;
mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices, 0, 2);
verticesCount = mesh->getWorldVerticesLength() >> 1;
uvs = &mesh->getUVs();
indices = &mesh->getTriangles();
indicesCount = mesh->getTriangles().size();
attachmentColor = &mesh->getColor();
} else if (attachment->rtti.derivesFrom(ClippingAttachment::rtti)) {
ClippingAttachment* clip = (ClippingAttachment*)slot.getAttachment();
clipper.clipStart(slot, clip);
continue;
} else continue;
Uint8 r = static_cast<Uint8>(skeleton->color.r * slot->color.r * attachmentColor->r * 255);
Uint8 g = static_cast<Uint8>(skeleton->color.g * slot->color.g * attachmentColor->g * 255);
Uint8 b = static_cast<Uint8>(skeleton->color.b * slot->color.b * attachmentColor->b * 255);
Uint8 a = static_cast<Uint8>(skeleton->color.a * slot->color.a * attachmentColor->a * 255);
Uint8 r = static_cast<Uint8>(skeleton->getColor()._r * slot.getColor()._r * attachmentColor->_r * 255);
Uint8 g = static_cast<Uint8>(skeleton->getColor()._g * slot.getColor()._g * attachmentColor->_g * 255);
Uint8 b = static_cast<Uint8>(skeleton->getColor()._b * slot.getColor()._b * attachmentColor->_b * 255);
Uint8 a = static_cast<Uint8>(skeleton->getColor()._a * slot.getColor()._a * attachmentColor->_a * 255);
vertex.color.r = r;
vertex.color.g = g;
vertex.color.b = b;
vertex.color.a = a;
spColor light;
light.r = r / 255.0f;
light.g = g / 255.0f;
light.b = b / 255.0f;
light.a = a / 255.0f;
Color light;
light._r = r / 255.0f;
light._g = g / 255.0f;
light._b = b / 255.0f;
light._a = a / 255.0f;
sf::BlendMode blend;
if (!usePremultipliedAlpha) {
switch (slot->data->blendMode) {
case BLEND_MODE_NORMAL:
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
blend = normal;
break;
case BLEND_MODE_ADDITIVE:
case BlendMode_Additive:
blend = additive;
break;
case BLEND_MODE_MULTIPLY:
case BlendMode_Multiply:
blend = multiply;
break;
case BLEND_MODE_SCREEN:
case BlendMode_Screen:
blend = screen;
break;
default:
blend = normal;
}
} else {
switch (slot->data->blendMode) {
case BLEND_MODE_NORMAL:
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
blend = normalPma;
break;
case BLEND_MODE_ADDITIVE:
case BlendMode_Additive:
blend = additivePma;
break;
case BLEND_MODE_MULTIPLY:
case BlendMode_Multiply:
blend = multiplyPma;
break;
case BLEND_MODE_SCREEN:
case BlendMode_Screen:
blend = screenPma;
break;
default:
@ -219,18 +217,18 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
states.texture = texture;
}
if (spSkeletonClipping_isClipping(clipper)) {
spSkeletonClipping_clipTriangles(clipper, vertices, verticesCount << 1, indices, indicesCount, uvs, 2);
vertices = clipper->clippedVertices->items;
verticesCount = clipper->clippedVertices->size >> 1;
uvs = clipper->clippedUVs->items;
indices = clipper->clippedTriangles->items;
indicesCount = clipper->clippedTriangles->size;
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, verticesCount << 1, *indices, indicesCount, *uvs);
vertices = &clipper.getClippedVertices();
verticesCount = clipper.getClippedVertices().size() >> 1;
uvs = &clipper.getClippedUVs();
indices = &clipper.getClippedTriangles();
indicesCount = clipper.getClippedTriangles().size();
}
Vector2u size = texture->getSize();
if (vertexEffect != 0) {
/* BOZO if (vertexEffect != 0) {
spFloatArray_clear(tempUvs);
spColorArray_clear(tempColors);
for (int i = 0; i < verticesCount; i++) {
@ -263,23 +261,23 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
vertex.color.a = static_cast<Uint8>(vertexColor.a * 255);
vertexArray->append(vertex);
}
} else {
} else {*/
for (int i = 0; i < indicesCount; ++i) {
int index = indices[i] << 1;
vertex.position.x = vertices[index];
vertex.position.y = vertices[index + 1];
vertex.texCoords.x = uvs[index] * size.x;
vertex.texCoords.y = uvs[index + 1] * size.y;
int index = (*indices)[i] << 1;
vertex.position.x = (*vertices)[index];
vertex.position.y = (*vertices)[index + 1];
vertex.texCoords.x = (*uvs)[index] * size.x;
vertex.texCoords.y = (*uvs)[index + 1] * size.y;
vertexArray->append(vertex);
}
}
// }
spSkeletonClipping_clipEnd(clipper, slot);
clipper.clipEnd(slot);
}
target.draw(*vertexArray, states);
spSkeletonClipping_clipEnd2(clipper);
clipper.clipEnd();
if (vertexEffect != 0) vertexEffect->end(vertexEffect);
// BOZO if (vertexEffect != 0) vertexEffect->end(vertexEffect);
}
} /* namespace spine */

View File

@ -38,34 +38,32 @@
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderStates.hpp>
_SP_ARRAY_DECLARE_TYPE(spColorArray, spColor)
namespace spine {
namespace Spine {
class SkeletonDrawable: public sf::Drawable {
public:
spSkeleton* skeleton;
spAnimationState* state;
Skeleton* skeleton;
AnimationState* state;
float timeScale;
sf::VertexArray* vertexArray;
spVertexEffect* vertexEffect;
SkeletonDrawable (spSkeletonData* skeleton, spAnimationStateData* stateData = 0);
SkeletonDrawable (SkeletonData* skeleton, AnimationStateData* stateData = 0);
~SkeletonDrawable ();
void update (float deltaTime);
virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const;
virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const ;
void setUsePremultipliedAlpha(bool usePMA) { usePremultipliedAlpha = usePMA; };
bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
private:
bool ownsAnimationStateData;
float* worldVertices;
spFloatArray* tempUvs;
spColorArray* tempColors;
spSkeletonClipping* clipper;
bool usePremultipliedAlpha;
mutable bool ownsAnimationStateData;
mutable Vector<float> worldVertices;
mutable Vector<float> tempUvs;
mutable Vector<Color> tempColors;
mutable SkeletonClipping clipper;
mutable bool usePremultipliedAlpha;
};
} /* namespace spine */