From d4a3444328a72ce9b7a4622dbaef83bd9d682253 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Mon, 7 Oct 2024 08:20:38 +0200 Subject: [PATCH] [godot] Add support for load from disk for Godot 3.x and example --- .../13-load-from-disk/load_from_disk.gd | 24 ++++++++++++++++ .../13-load-from-disk/load_from_disk.tscn | 6 ++++ .../spine_godot/SpineAtlasResource.cpp | 28 +++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 spine-godot/example/examples/13-load-from-disk/load_from_disk.gd create mode 100644 spine-godot/example/examples/13-load-from-disk/load_from_disk.tscn diff --git a/spine-godot/example/examples/13-load-from-disk/load_from_disk.gd b/spine-godot/example/examples/13-load-from-disk/load_from_disk.gd new file mode 100644 index 000000000..f56a0319f --- /dev/null +++ b/spine-godot/example/examples/13-load-from-disk/load_from_disk.gd @@ -0,0 +1,24 @@ +extends Node2D + +func _ready(): + # Load the skeleton file + var skeleton_file_res = SpineSkeletonFileResource.new(); + skeleton_file_res.load_from_file("/Users/badlogic/workspaces/spine-runtimes/examples/coin/export/coin-pro.skel"); + + # Load the atlas file + var atlas_res = SpineAtlasResource.new(); + atlas_res.load_from_atlas_file("/Users/badlogic/workspaces/spine-runtimes/examples/coin/export/coin.atlas"); + + # Create a skeleton data resource, you can share this across multiple sprites + var skeleton_data_res = SpineSkeletonDataResource.new(); + skeleton_data_res.skeleton_file_res = skeleton_file_res; + skeleton_data_res.atlas_res = atlas_res + + # Create a sprite from the skeleton data and add it as a child + var sprite = SpineSprite.new(); + sprite.skeleton_data_res = skeleton_data_res; + sprite.position.x = 200; + sprite.position.y = 200; + sprite.get_animation_state().set_animation("animation", true, 0); + self.add_child(sprite) + pass diff --git a/spine-godot/example/examples/13-load-from-disk/load_from_disk.tscn b/spine-godot/example/examples/13-load-from-disk/load_from_disk.tscn new file mode 100644 index 000000000..e1435abff --- /dev/null +++ b/spine-godot/example/examples/13-load-from-disk/load_from_disk.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://examples/13-load-from-disk/load_from_disk.gd" type="Script" id=1] + +[node name="load_from_disk" type="Node2D"] +script = ExtResource( 1 ) diff --git a/spine-godot/spine_godot/SpineAtlasResource.cpp b/spine-godot/spine_godot/SpineAtlasResource.cpp index a2ede8879..2175a46ce 100644 --- a/spine-godot/spine_godot/SpineAtlasResource.cpp +++ b/spine-godot/spine_godot/SpineAtlasResource.cpp @@ -30,11 +30,16 @@ #include "SpineAtlasResource.h" #include "SpineRendererObject.h" #include "core/io/json.h" -#include "core/io/image.h" -#include "scene/resources/image_texture.h" #include "scene/resources/texture.h" #include +#if VERSION_MAJOR > 3 +#include "core/io/image.h" +#include "scene/resources/image_texture.h" +#else +#include "core/image.h" +#endif + #ifdef TOOLS_ENABLED #include "editor/editor_file_system.h" #endif @@ -90,22 +95,21 @@ public: Vector buf = FileAccess::get_file_as_array(path, &error); if (error == OK) { Ref img; - img.instantiate(); - String filename = path.get_filename().to_lower(); - if (filename.ends_with(".png")) { - img->load_png_from_buffer(buf); - } else if (filename_lower.ends_with(".jpg")) { - img->load_jpg_from_buffer(buf); - } - return ImageTexture::create_from_image(img); + INSTANTIATE(img); + img->load(path); + + Ref texture; + INSTANTIATE(texture); + texture->create_from_image(img); + return texture; } + return Ref(); } - return Ref(); } #endif void import_image_resource(const String &path) { -#ifdef VERSION_MAJOR> 4 +#if VERSION_MAJOR > 4 #ifdef TOOLS_ENABLED // Required when importing into editor by e.g. drag & drop. The .png files // of the atlas might not have been imported yet.