Skip to content

Commit

Permalink
[godot] Fix statics deinitialization
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Oct 9, 2024
1 parent 62aad81 commit 5600d0d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
25 changes: 21 additions & 4 deletions spine-godot/spine_godot/SpineSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
// Needed due to shared lib initializers in GDExtension.
// See: https://x.com/badlogicgames/status/1843661872404591068
struct SpineSpriteStatics {
private:
static SpineSpriteStatics *_instance;

public:
Ref<CanvasItemMaterial> default_materials[4] = {};
int sprite_count;
Expand Down Expand Up @@ -126,11 +129,21 @@ struct SpineSpriteStatics {
}

static SpineSpriteStatics &instance() {
static SpineSpriteStatics inst;
return inst;
if (!_instance) {
_instance = new SpineSpriteStatics();
}
return *_instance;
}

static void clear() {
if (_instance) {
delete _instance;
}
_instance = nullptr;
}
};

SpineSpriteStatics *SpineSpriteStatics::_instance = nullptr;

static void
clear_triangles(SpineMesh2D *mesh_instance) {
Expand Down Expand Up @@ -230,10 +243,10 @@ void SpineMesh2D::update_mesh(const PackedVector2Array &vertices,
surface_offsets[RS::ARRAY_TEX_UV] = RS::get_singleton()->mesh_surface_get_format_offset(surface_format, vertices.size(), RS::ARRAY_TEX_UV);
vertex_stride = RS::get_singleton()->mesh_surface_get_format_vertex_stride(surface_format, vertices.size());
attribute_stride = RS::get_singleton()->mesh_surface_get_format_attribute_stride(surface_format, vertices.size());
num_vertices = vertices.size();
num_indices = indices.size();
vertex_buffer = surface["vertex_data"];
attribute_buffer = surface["attribute_data"];
num_vertices = vertices.size();
num_indices = indices.size();
indices_changed = false;
} else {
AABB aabb_new;
Expand Down Expand Up @@ -394,6 +407,10 @@ void SpineMesh2D::update_mesh(const Vector<Point2> &vertices,
}
#endif

void SpineSprite::clear_statics() {
SpineSpriteStatics::clear();
}

void SpineSprite::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_skeleton_data_res", "skeleton_data_res"), &SpineSprite::set_skeleton_data_res);
ClassDB::bind_method(D_METHOD("get_skeleton_data_res"), &SpineSprite::get_skeleton_data_res);
Expand Down
2 changes: 2 additions & 0 deletions spine-godot/spine_godot/SpineSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,6 @@ class SpineSprite : public Node2D,
virtual bool _edit_use_rect() const;
#endif
#endif

static void clear_statics();
};
5 changes: 4 additions & 1 deletion spine-godot/spine_godot/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ void register_spine_godot_types() {

#if VERSION_MAJOR > 3
void uninitialize_spine_godot_module(ModuleInitializationLevel level) {
return;
if (level == MODULE_INITIALIZATION_LEVEL_SCENE) {
SpineSprite::clear_statics();
return;
}
if (level != MODULE_INITIALIZATION_LEVEL_CORE) return;
#else
void unregister_spine_godot_types() {
Expand Down

0 comments on commit 5600d0d

Please sign in to comment.