[c][cpp] SpineExtension::strdup convenience method, better spine_skin_entries api in spine-c extensions

This commit is contained in:
Mario Zechner 2025-07-29 22:18:42 +02:00
parent 48081c7f20
commit fd939f4401
3 changed files with 53 additions and 53 deletions

View File

@ -134,38 +134,6 @@ void spine_report_leaks() {
fflush(stdout);
}
// Bounds functions
float spine_bounds_get_x(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->x;
}
float spine_bounds_get_y(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->y;
}
float spine_bounds_get_width(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->width;
}
float spine_bounds_get_height(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->height;
}
// Vector functions
float spine_vector_get_x(spine_vector vector) {
if (!vector) return 0;
return ((_spine_vector *) vector)->x;
}
float spine_vector_get_y(spine_vector vector) {
if (!vector) return 0;
return ((_spine_vector *) vector)->y;
}
// Atlas functions
class SpineCTextureLoader : public TextureLoader {
void load(AtlasPage &page, const String &path) {
@ -256,7 +224,7 @@ spine_skeleton_data_result spine_skeleton_data_load_json(spine_atlas atlas, cons
SkeletonData *data = json.readSkeletonData(skeletonData);
if (!data) {
result->error = (const char *) strdup("Failed to load skeleton data");
result->error = SpineExtension::strdup("Failed to load skeleton data", __FILE__, __LINE__);
return (spine_skeleton_data_result) result;
}
@ -292,7 +260,7 @@ spine_skeleton_data_result spine_skeleton_data_load_binary(spine_atlas atlas, co
SkeletonData *data = binary.readSkeletonData((const unsigned char *) skeletonData, length);
if (!data) {
result->error = (const char *) strdup("Failed to load skeleton data");
result->error = SpineExtension::strdup("Failed to load skeleton data", __FILE__, __LINE__);
return (spine_skeleton_data_result) result;
}
@ -412,11 +380,8 @@ spine_animation_state_events spine_skeleton_drawable_get_animation_state_events(
return ((_spine_skeleton_drawable *) drawable)->animationStateEvents;
}
// Skin entries
spine_skin_entries spine_skin_entries_create() {
_spine_skin_entries *entries = SpineExtension::calloc<_spine_skin_entries>(1, __FILE__, __LINE__);
return (spine_skin_entries) entries;
}
// Skin entries functions
void spine_skin_entries_dispose(spine_skin_entries entries) {
if (!entries) return;
@ -457,4 +422,40 @@ const char *spine_skin_entry_get_name(spine_skin_entry entry) {
spine_attachment spine_skin_entry_get_attachment(spine_skin_entry entry) {
if (!entry) return nullptr;
return ((_spine_skin_entry *) entry)->attachment;
}
// Skin functions
spine_skin_entries spine_skin_get_entries(spine_skin skin) {
if (!skin) return nullptr;
Skin *_skin = (Skin *) skin;
_spine_skin_entries *result = SpineExtension::calloc<_spine_skin_entries>(1, __FILE__, __LINE__);
// First pass: count the entries
{
Skin::AttachmentMap::Entries entries = _skin->getAttachments();
int count = 0;
while (entries.hasNext()) {
entries.next();
count++;
}
result->numEntries = count;
}
// Second pass: populate the entries
if (result->numEntries > 0) {
result->entries = SpineExtension::calloc<_spine_skin_entry>(result->numEntries, __FILE__, __LINE__);
Skin::AttachmentMap::Entries entries = _skin->getAttachments();
int index = 0;
while (entries.hasNext()) {
Skin::AttachmentMap::Entry &entry = entries.next();
result->entries[index].slotIndex = entry._slotIndex;
result->entries[index].name = SpineExtension::strdup(entry._name.buffer(), __FILE__, __LINE__);
result->entries[index].attachment = (spine_attachment) entry._attachment;
index++;
}
}
return (spine_skin_entries) result;
}

View File

@ -41,8 +41,6 @@ extern "C" {
// Custom types for spine-c-new (not generated)
SPINE_OPAQUE_TYPE(spine_atlas_result)
SPINE_OPAQUE_TYPE(spine_skeleton_data_result)
SPINE_OPAQUE_TYPE(spine_bounds)
SPINE_OPAQUE_TYPE(spine_vector)
SPINE_OPAQUE_TYPE(spine_skeleton_drawable)
SPINE_OPAQUE_TYPE(spine_animation_state_events)
SPINE_OPAQUE_TYPE(spine_skin_entry)
@ -63,16 +61,6 @@ SPINE_C_API int32_t spine_minor_version(void);
SPINE_C_API void spine_enable_debug_extension(bool enable);
SPINE_C_API void spine_report_leaks(void);
// Bounds functions
SPINE_C_API float spine_bounds_get_x(spine_bounds bounds);
SPINE_C_API float spine_bounds_get_y(spine_bounds bounds);
SPINE_C_API float spine_bounds_get_width(spine_bounds bounds);
SPINE_C_API float spine_bounds_get_height(spine_bounds bounds);
// Vector functions
SPINE_C_API float spine_vector_get_x(spine_vector vector);
SPINE_C_API float spine_vector_get_y(spine_vector vector);
// Atlas functions
SPINE_C_API spine_atlas_result spine_atlas_load(const char *atlasData);
SPINE_C_API spine_atlas_result spine_atlas_load_callback(const char *atlasData, const char *atlasDir, spine_texture_loader_load_func load,
@ -89,7 +77,7 @@ SPINE_C_API const char *spine_skeleton_data_result_get_error(spine_skeleton_data
SPINE_C_API spine_skeleton_data spine_skeleton_data_result_get_data(spine_skeleton_data_result result);
SPINE_C_API void spine_skeleton_data_result_dispose(spine_skeleton_data_result result);
// Skeleton drawable functions
// Skeleton drawable functionsp
SPINE_C_API spine_skeleton_drawable spine_skeleton_drawable_create(spine_skeleton_data skeletonData);
SPINE_C_API spine_render_command spine_skeleton_drawable_render(spine_skeleton_drawable drawable);
SPINE_C_API void spine_skeleton_drawable_dispose(spine_skeleton_drawable drawable);
@ -98,8 +86,10 @@ SPINE_C_API spine_animation_state spine_skeleton_drawable_get_animation_state(sp
SPINE_C_API spine_animation_state_data spine_skeleton_drawable_get_animation_state_data(spine_skeleton_drawable drawable);
SPINE_C_API spine_animation_state_events spine_skeleton_drawable_get_animation_state_events(spine_skeleton_drawable drawable);
// Skin functions
SPINE_C_API spine_skin_entries spine_skin_get_entries(spine_skin skin);
// Skin entries functions
SPINE_C_API spine_skin_entries spine_skin_entries_create(void);
SPINE_C_API void spine_skin_entries_dispose(spine_skin_entries entries);
SPINE_C_API int32_t spine_skin_entries_get_num_entries(spine_skin_entries entries);
SPINE_C_API spine_skin_entry spine_skin_entries_get_entry(spine_skin_entries entries, int32_t index);

View File

@ -32,6 +32,7 @@
#include <stdlib.h>
#include <string.h>
#include <spine/dll.h>
#define SP_UNUSED(x) (void) (x)
@ -70,6 +71,14 @@ namespace spine {
return getInstance()->_readFile(path, length);
}
static char *strdup(const char *str, const char *file, int line) {
if (!str) return nullptr;
size_t len = strlen(str) + 1;
char *copy = (char *) getInstance()->_alloc(len, file, line);
memcpy(copy, str, len);
return copy;
}
static void setInstance(SpineExtension *inSpineExtension);
static SpineExtension *getInstance();