Hey
I'm new to Spine, so excuse me if this question is a little dumb.
So I'm working on a game using Cinder. It's not really a game engine, it's a C++ creative coding library. Anyway, that's not the important bit. It means I'm coding in C++, and I'm using the generic C runtime for Spine.
I pretty much got the basics done, I implemented spAtlasPage_createTexture, spAtlasPage_disposeTexture and _spUtil_readFile, but I still can't get it to work properly. Sadly, the official documentation is not very extensive. I tried to follow the sfml example on GitHub as much as possible, but it still doesn't work for me. Here are the relevant bits of my code (this is not nearly everything I will need of course, but for now, I'm only trying to get the most basic stuff to work):
class spineCharacter {
public:
spineCharacter(const string& name);
~spineCharacter();
spSkeleton* getSkeleton();
void render();
private:
string name;
spSkeleton* skeleton;
};
spineCharacter::spineCharacter(const string& name) : name{name} {
string dir = "characters/" + name + "/";
string jsonPath = dir + name + ".json";
string atlasPath = dir + name + ".atlas";
string atlasData, jsonData;
// readFile is my own function
readFile(atlasPath, atlasData);
readFile(jsonPath, jsonData);
auto atlas = spAtlas_create(atlasData.c_str(), atlasData.length(), dir.c_str(), 0);
auto skeletonJson = spSkeletonJson_create(atlas);
skeletonJson->scale = 1.0;
auto skeletonData = spSkeletonJson_readSkeletonData(skeletonJson, jsonData.c_str());
if (!skeletonData) {
cout << skeletonJson->error << endl;
exit(0);
}
skeleton = spSkeleton_create(skeletonData);
// temporary position, so I can see if the character renders properly
skeleton->x = 200.0f;
skeleton->y = 200.0f;
spSkeletonJson_dispose(skeletonJson);
spSkeletonData_dispose(skeletonData);
}
spineCharacter::~spineCharacter() {
cout << endl << "Bye, " + this->name + "!" << endl;
free(skeleton);
}
spSkeleton* spineCharacter::getSkeleton() {
return skeleton;
}
void spineCharacter::render() {
// I'm not sure how to use these yet... but these are not relevant until I get my textures back, so ignore these for now
//spSkeleton_update(skeleton, 0.0);
//spSkeleton_updateWorldTransform(skeleton);
for (int i=0; i<skeleton->slotsCount; i++) {
auto slot = skeleton->drawOrder[i];
auto attachment = slot->attachment;
if (!attachment) continue;
if (attachment->type == SP_ATTACHMENT_REGION) {
spRegionAttachment* regionAttachment = (spRegionAttachment*)attachment;
//spBone* bone = slot->bone;
// When posting the code, the forum somehow screws it up. int[i] is an int pointer, and spAtlasRegion[/i] is an spAtlasRegion pointer
//This whole casting thing is the same as in the sfml example, except the result is int pointer and not Texture pointer
int *i = (int[i])((spAtlasRegion[/i])regionAttachment->rendererObject)->page->rendererObject;
cout << *i << endl;
}
}
}
// Extension implementations (in another .cpp file):
static int spineNumber = 10;
char* _spUtil_readFile (const char* path, int* length) {
return _readFile(path, length);
}
void _spAtlasPage_createTexture(spAtlasPage* self, const char* path) {
int* tmp = new int;
*tmp = spineNumber;
cout << "Assign: " << spineNumber << endl;
self->rendererObject = tmp;
self->width = spineNumber;
self->height = spineNumber;
spineNumber++;
}
void _spAtlasPage_disposeTexture(spAtlasPage* self) {
delete (int*)self->rendererObject;
}
So as you can see, I'm simply assigning int pointers to the rendererObject instead of textures for simplicity (I have another implementation where I actually make the appropriate textures, but this is easier to debug!).
My problem is that I don't get my textures (the integers in this case) back in the render function. The console output is the following:
Assign: 10
10
1838608
-559038803
So when the "texture" gets created, 10 is passed to the rendererObject. Good. When the render function makes the first loop, I get 10 back from the rendererObject. Good. But then, I get all kinds of junk data!!! That's no good. If I tried to handle that junk data as a texture, and render it out, my game would simply crash.
So how can I properly get back the data I assigned to rendererObject, without getting all sorts of junk data? Is there something I'm missing out? ๐
Btw, I'm instantiating the class with "raptor", one of the default example characters in the trial version of Spine (I copied the contents of the "export" folder into my game assets). It does succeed reading in the atlas, json, and all that, I can output the number of bones and stuff like that. So I assume that what's inside the constructor is correct.