From 12560ee4606ed2df729c398bc25603e937c17509 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 10 Dec 2022 19:25:06 +0100 Subject: [PATCH] Added create_texture helper method. --- gif_loader.cpp | 33 ++++++++++++++++++++++++++++++++- gif_loader.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/gif_loader.cpp b/gif_loader.cpp index 66ddbaa..7962ab3 100644 --- a/gif_loader.cpp +++ b/gif_loader.cpp @@ -7,11 +7,41 @@ #include "./thirdparty/gif_load/gif_load.h" #include "core/os/file_access.h" +#include "scene/resources/texture.h" Array GIFLoader::get_images() { return _images; } +Ref GIFLoader::create_texture(float fps) { + Ref tex; + tex.instance(); + tex->set_fps(0); + tex->set_flags(0); + + float per_frame_time = 1.0 / fps; + + for (int i = 0; i < _images.size(); ++i) { + Array arr = _images[i]; + + ERR_CONTINUE(arr.size() != 2); + + float frames = arr[0]; + Ref img = arr[1]; + + Ref imgt; + imgt.instance(); + imgt->create_from_image(img, 0); + + tex->set_frame_texture(i, imgt); + tex->set_frame_delay(i, per_frame_time * frames); + } + + tex->set_frames(_images.size()); + + return tex; +} + void GIFLoader::gif_frame(void *data, struct GIF_WHDR *whdr) { uint32_t *pict, *prev, x, y, yoff, iter, ifin, dsrc, ddst; //uint8_t head[18] = { 0 }; @@ -112,6 +142,7 @@ GIFLoader::~GIFLoader() { } void GIFLoader::_bind_methods() { - ClassDB::bind_method(D_METHOD("load_gif", "file"), &GIFLoader::load_gif); ClassDB::bind_method(D_METHOD("get_images"), &GIFLoader::get_images); + ClassDB::bind_method(D_METHOD("create_texture", "fps"), &GIFLoader::create_texture, 60); + ClassDB::bind_method(D_METHOD("load_gif", "file"), &GIFLoader::load_gif); } \ No newline at end of file diff --git a/gif_loader.h b/gif_loader.h index c60abab..61cd5d1 100644 --- a/gif_loader.h +++ b/gif_loader.h @@ -7,12 +7,14 @@ #include "core/io/image.h" struct GIF_WHDR; +class AnimatedTexture; class GIFLoader : public Reference { GDCLASS(GIFLoader, Reference); public: Array get_images(); + Ref create_texture(float fps = 60); void load_gif(const String &file);