-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GIF file loading Add `ResourceFormatLoader` for `AnimatedTexture` Add `ImageFrames` resource for handling image sequence Add `ImageFramesLoader` for resource loading image sequences Add `ResourceFormatLoader` for `ImageFrames`
- Loading branch information
1 parent
ac1a497
commit 89ca9c5
Showing
48 changed files
with
5,697 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/**************************************************************************/ | ||
/* image_frames.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* REDOT ENGINE */ | ||
/* https://redotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2024-present Redot Engine contributors */ | ||
/* (see REDOT_AUTHORS.md) */ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "image_frames.h" | ||
#include "core/io/image_frames_loader.h" | ||
#include "core/io/resource_loader.h" | ||
|
||
ImageFramesMemLoadFunc ImageFrames::_gif_mem_loader_func = nullptr; | ||
|
||
void ImageFrames::set_frame_count(int p_frames) { | ||
ERR_FAIL_COND(p_frames < 1); | ||
|
||
frames.resize(p_frames); | ||
} | ||
|
||
int ImageFrames::get_frame_count() const { | ||
return frames.size(); | ||
} | ||
|
||
void ImageFrames::set_frame_image(int p_frame, Ref<Image> p_image) { | ||
ERR_FAIL_INDEX(p_frame, frames.size()); | ||
|
||
frames.write[p_frame].image = p_image; | ||
} | ||
|
||
Ref<Image> ImageFrames::get_frame_image(int p_frame) const { | ||
ERR_FAIL_INDEX_V(p_frame, frames.size(), Ref<Image>()); | ||
|
||
return frames[p_frame].image; | ||
} | ||
|
||
void ImageFrames::set_frame_delay(int p_frame, float p_delay) { | ||
ERR_FAIL_INDEX(p_frame, frames.size()); | ||
|
||
frames.write[p_frame].delay = p_delay; | ||
} | ||
|
||
float ImageFrames::get_frame_delay(int p_frame) const { | ||
ERR_FAIL_INDEX_V(p_frame, frames.size(), 0); | ||
|
||
return frames[p_frame].delay; | ||
} | ||
|
||
Error ImageFrames::_load_from_buffer(const Vector<uint8_t> &p_array, ImageFramesMemLoadFunc p_loader, int p_max_frames) { | ||
int buffer_size = p_array.size(); | ||
|
||
ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); | ||
ERR_FAIL_NULL_V(p_loader, ERR_INVALID_PARAMETER); | ||
|
||
const uint8_t *r = p_array.ptr(); | ||
|
||
Ref<ImageFrames> img_frames = p_loader(r, buffer_size, p_max_frames); | ||
ERR_FAIL_COND_V(!img_frames.is_valid(), ERR_PARSE_ERROR); | ||
|
||
copy_internals_from(img_frames); | ||
|
||
return OK; | ||
} | ||
|
||
Error ImageFrames::load(const String &p_path) { | ||
#ifdef DEBUG_ENABLED | ||
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) { | ||
WARN_PRINT("Loaded resource as image frames file, this will not work on export: '" + p_path + "'. Instead, import the image frames file as an ImageFrames resource and load it normally as a resource."); | ||
} | ||
#endif | ||
return ImageFramesLoader::load_image_frames(p_path, this); | ||
} | ||
|
||
Ref<ImageFrames> ImageFrames::load_from_file(const String &p_path) { | ||
#ifdef DEBUG_ENABLED | ||
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) { | ||
WARN_PRINT("Loaded resource as image frames file, this will not work on export: '" + p_path + "'. Instead, import the image frames file as an ImageFrames resource and load it normally as a resource."); | ||
} | ||
#endif | ||
Ref<ImageFrames> img_frames; | ||
img_frames.instantiate(); | ||
Error err = ImageFramesLoader::load_image_frames(p_path, img_frames); | ||
if (err != OK) { | ||
ERR_FAIL_V_MSG(Ref<ImageFrames>(), vformat("Failed to load image frames. Error %d", err)); | ||
} | ||
return img_frames; | ||
} | ||
|
||
Error ImageFrames::load_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames) { | ||
ERR_FAIL_NULL_V_MSG( | ||
_gif_mem_loader_func, | ||
ERR_UNAVAILABLE, | ||
"The GIF module isn't enabled. Recompile the Redot editor or export template binary with the `module_gif_enabled=yes` SCons option."); | ||
return _load_from_buffer(p_array, _gif_mem_loader_func, p_max_frames); | ||
} | ||
|
||
void ImageFrames::_bind_methods() { | ||
ClassDB::bind_method(D_METHOD("set_frame_count", "frames"), &ImageFrames::set_frame_count); | ||
ClassDB::bind_method(D_METHOD("get_frame_count"), &ImageFrames::get_frame_count); | ||
|
||
ClassDB::bind_method(D_METHOD("set_frame_image", "frame", "image"), &ImageFrames::set_frame_image); | ||
ClassDB::bind_method(D_METHOD("get_frame_image", "frame"), &ImageFrames::get_frame_image); | ||
|
||
ClassDB::bind_method(D_METHOD("set_frame_delay", "frame", "delay"), &ImageFrames::set_frame_delay); | ||
ClassDB::bind_method(D_METHOD("get_frame_delay", "frame"), &ImageFrames::get_frame_delay); | ||
|
||
ClassDB::bind_method(D_METHOD("load", "path"), &ImageFrames::load); | ||
ClassDB::bind_static_method("ImageFrames", D_METHOD("load_from_file", "path"), &ImageFrames::load_from_file); | ||
|
||
ClassDB::bind_method(D_METHOD("load_gif_from_buffer", "buffer", "max_frames"), &ImageFrames::load_gif_from_buffer); | ||
|
||
ADD_PROPERTY(PropertyInfo(Variant::INT, "frame_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frame_count", "get_frame_count"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/**************************************************************************/ | ||
/* image_frames.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* REDOT ENGINE */ | ||
/* https://redotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2024-present Redot Engine contributors */ | ||
/* (see REDOT_AUTHORS.md) */ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef IMAGE_FRAMES_H | ||
#define IMAGE_FRAMES_H | ||
|
||
#include "core/io/image.h" | ||
#include "core/io/resource.h" | ||
#include "core/variant/variant.h" | ||
|
||
class ImageFrames; | ||
typedef Ref<ImageFrames> (*ImageFramesMemLoadFunc)(const uint8_t *p_png, int p_size, int p_max_frames); | ||
|
||
class ImageFrames : public Resource { | ||
GDCLASS(ImageFrames, Resource); | ||
|
||
public: | ||
static ImageFramesMemLoadFunc _gif_mem_loader_func; | ||
|
||
private: | ||
struct Frame { | ||
Ref<Image> image; | ||
float delay = 1.0; | ||
}; | ||
|
||
Vector<Frame> frames; | ||
|
||
Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageFramesMemLoadFunc p_loader, int p_max_frames); | ||
|
||
void copy_internals_from(const Ref<ImageFrames> &p_frames) { | ||
ERR_FAIL_COND_MSG(p_frames.is_null(), "Cannot copy image internals: invalid ImageFrames object."); | ||
frames = p_frames->frames; | ||
} | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
void set_frame_count(int p_frames); | ||
int get_frame_count() const; | ||
|
||
void set_frame_image(int p_frame, Ref<Image> p_image); | ||
Ref<Image> get_frame_image(int p_frame) const; | ||
|
||
void set_frame_delay(int p_frame, float p_delay); | ||
float get_frame_delay(int p_frame) const; | ||
|
||
Error load(const String &p_path); | ||
static Ref<ImageFrames> load_from_file(const String &p_path); | ||
Error load_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames = 0); | ||
}; | ||
|
||
#endif // IMAGE_FRAMES_H |
Oops, something went wrong.