anitty 0.1.0

Anime character animation rendering for terminals
Documentation
/* Internal bridge to the pinned Spine 3.8 runtime. Not a public C ABI. */
#include <spine/spine.h>
#include <spine/extension.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    spAtlas *atlas;
    spSkeletonData *data;
    spSkeleton *skeleton;
    spSkeletonClipping *clipper;
    spFloatArray *vertices;
    int next_slot;
} AnittySpine;

typedef struct {
    const float *vertices, *uvs;
    const unsigned short *indices;
    int vertices_len, indices_len, page, blend;
    float light[4], dark[3];
} AnittyMesh;

/* Rust owns decoded textures. Keep atlas dimensions intact: UVs also work
   when the page image has been uniformly resized after atlas export. */
void _spAtlasPage_createTexture(spAtlasPage *page, const char *path) {
    (void)page; (void)path;
}
void _spAtlasPage_disposeTexture(spAtlasPage *page) { (void)page; }
char *_spUtil_readFile(const char *path, int *length) {
    return _spReadFile(path, length);
}

void anitty_spine38_dispose(AnittySpine *self) {
    if (!self) return;
    if (self->vertices) spFloatArray_dispose(self->vertices);
    if (self->clipper) spSkeletonClipping_dispose(self->clipper);
    if (self->skeleton) spSkeleton_dispose(self->skeleton);
    if (self->data) spSkeletonData_dispose(self->data);
    if (self->atlas) spAtlas_dispose(self->atlas);
    free(self);
}

AnittySpine *anitty_spine38_create(const char *atlas, int atlas_len,
                               const unsigned char *bytes, int len,
                               char *error, int error_len) {
    AnittySpine *self = calloc(1, sizeof(AnittySpine));
    self->atlas = spAtlas_create(atlas, atlas_len, "", NULL);
    if (!self->atlas || !self->atlas->pages) {
        snprintf(error, error_len, "invalid or empty Spine atlas");
        goto fail;
    }
    int index = 0;
    for (spAtlasPage *page = self->atlas->pages; page; page = page->next) {
        if (page->width <= 0 || page->height <= 0 ||
            page->uWrap != SP_ATLAS_CLAMPTOEDGE || page->vWrap != SP_ATLAS_CLAMPTOEDGE) {
            snprintf(error, error_len, "atlas pages need a positive size and repeat: none");
            goto fail;
        }
        page->rendererObject = (void *)(intptr_t)index++;
    }
    spSkeletonBinary *reader = spSkeletonBinary_create(self->atlas);
    self->data = spSkeletonBinary_readSkeletonData(reader, bytes, len);
    if (!self->data) {
        snprintf(error, error_len, "%s", reader->error ? reader->error : "invalid Spine skeleton");
        spSkeletonBinary_dispose(reader);
        goto fail;
    }
    spSkeletonBinary_dispose(reader);
    if (!self->data->bonesCount) {
        snprintf(error, error_len, "skeleton has no bones");
        goto fail;
    }
    self->skeleton = spSkeleton_create(self->data);
    self->clipper = spSkeletonClipping_create();
    self->vertices = spFloatArray_create(8);
    return self;
fail:
    anitty_spine38_dispose(self);
    return NULL;
}

const char *anitty_spine38_page(AnittySpine *self, int index) {
    spAtlasPage *page = self->atlas->pages;
    while (page && index--) page = page->next;
    return page ? page->name : NULL;
}

int anitty_spine38_animation_count(AnittySpine *self) { return self->data->animationsCount; }
const char *anitty_spine38_animation(AnittySpine *self, int index, float *duration) {
    spAnimation *animation = self->data->animations[index];
    *duration = animation->duration;
    return animation->name;
}

void anitty_spine38_pose(AnittySpine *self, int animation, float time) {
    spSkeleton_setToSetupPose(self->skeleton);
    self->skeleton->time = time;
    spAnimation_apply(self->data->animations[animation], self->skeleton,
                      -1, time, 0, NULL, NULL, 1, SP_MIX_BLEND_SETUP, SP_MIX_DIRECTION_IN);
    spSkeleton_updateWorldTransform(self->skeleton);
    spSkeletonClipping_clipEnd2(self->clipper);
    self->next_slot = 0;
}

/* Mesh pointers remain valid until the next call that mutates this instance. */
int anitty_spine38_next_mesh(AnittySpine *self, AnittyMesh *out) {
    static unsigned short quad[6] = {0, 1, 2, 2, 3, 0};
    spSkeleton *skeleton = self->skeleton;
    while (self->next_slot < skeleton->slotsCount) {
        spSlot *slot = skeleton->drawOrder[self->next_slot++];
        spAttachment *attachment = slot->attachment;
        spColor *color;
        spAtlasRegion *region;
        if (!attachment || !slot->bone->active || slot->color.a == 0) {
            spSkeletonClipping_clipEnd(self->clipper, slot);
            continue;
        }
        if (attachment->type == SP_ATTACHMENT_CLIPPING) {
            spSkeletonClipping_clipStart(self->clipper, slot, (spClippingAttachment *)attachment);
            continue;
        }
        if (attachment->type == SP_ATTACHMENT_REGION) {
            spRegionAttachment *a = (spRegionAttachment *)attachment;
            spFloatArray_setSize(self->vertices, 8);
            spRegionAttachment_computeWorldVertices(a, slot->bone, self->vertices->items, 0, 2);
            out->vertices_len = 8;
            out->uvs = a->uvs;
            out->indices = quad;
            out->indices_len = 6;
            color = &a->color;
            region = (spAtlasRegion *)a->rendererObject;
        } else if (attachment->type == SP_ATTACHMENT_MESH) {
            spMeshAttachment *a = (spMeshAttachment *)attachment;
            out->vertices_len = a->super.worldVerticesLength;
            spFloatArray_setSize(self->vertices, out->vertices_len);
            spVertexAttachment_computeWorldVertices(&a->super, slot, 0, out->vertices_len,
                                                    self->vertices->items, 0, 2);
            out->uvs = a->uvs;
            out->indices = a->triangles;
            out->indices_len = a->trianglesCount;
            color = &a->color;
            region = (spAtlasRegion *)a->rendererObject;
        } else {
            spSkeletonClipping_clipEnd(self->clipper, slot);
            continue;
        }
        out->vertices = self->vertices->items;
        out->page = (int)(intptr_t)region->page->rendererObject;
        out->blend = slot->data->blendMode;
        out->light[0] = skeleton->color.r * slot->color.r * color->r;
        out->light[1] = skeleton->color.g * slot->color.g * color->g;
        out->light[2] = skeleton->color.b * slot->color.b * color->b;
        out->light[3] = skeleton->color.a * slot->color.a * color->a;
        out->dark[0] = slot->darkColor ? slot->darkColor->r : 0;
        out->dark[1] = slot->darkColor ? slot->darkColor->g : 0;
        out->dark[2] = slot->darkColor ? slot->darkColor->b : 0;
        if (spSkeletonClipping_isClipping(self->clipper)) {
            spSkeletonClipping_clipTriangles(self->clipper, (float *)out->vertices, out->vertices_len,
                    (unsigned short *)out->indices, out->indices_len, (float *)out->uvs, 2);
            out->vertices = self->clipper->clippedVertices->items;
            out->vertices_len = self->clipper->clippedVertices->size;
            out->uvs = self->clipper->clippedUVs->items;
            out->indices = self->clipper->clippedTriangles->items;
            out->indices_len = self->clipper->clippedTriangles->size;
        }
        spSkeletonClipping_clipEnd(self->clipper, slot);
        if (out->indices_len && out->light[3] > 0) return 1;
    }
    spSkeletonClipping_clipEnd2(self->clipper);
    return 0;
}