Skip to content

Commit

Permalink
[godot] Add support for load from disk for Godot 3.x and example
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Oct 7, 2024
1 parent e499992 commit d4a3444
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
24 changes: 24 additions & 0 deletions spine-godot/example/examples/13-load-from-disk/load_from_disk.gd
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 )
28 changes: 16 additions & 12 deletions spine-godot/spine_godot/SpineAtlasResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <spine/TextureLoader.h>

#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
Expand Down Expand Up @@ -90,22 +95,21 @@ class GodotSpineTextureLoader : public spine::TextureLoader {
Vector<uint8_t> buf = FileAccess::get_file_as_array(path, &error);
if (error == OK) {
Ref<Image> 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<ImageTexture> texture;
INSTANTIATE(texture);
texture->create_from_image(img);
return texture;
}
return Ref<Texture>();
}
return Ref<Texture>();
}
#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.
Expand Down

0 comments on commit d4a3444

Please sign in to comment.