diff options
author | kobewi <kobewi4e@gmail.com> | 2022-05-04 01:49:20 +0200 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2022-07-08 13:40:47 +0200 |
commit | d2900429e81175a9f48240b180f1d8e3ab52865c (patch) | |
tree | 9a22ed1cd2ecea275dac8e9420d7a1d56c661382 /core | |
parent | ca18a02e00f7009d084c55b7e9de17df634f3d47 (diff) |
Add static methods for creating Image and ImageTexture
Diffstat (limited to 'core')
-rw-r--r-- | core/io/image.cpp | 16 | ||||
-rw-r--r-- | core/io/image.h | 1 |
2 files changed, 17 insertions, 0 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp index f065dac212..473d70bd7c 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -2284,6 +2284,21 @@ Error Image::load(const String &p_path) { return ImageLoader::load_image(p_path, this); } +Ref<Image> Image::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 file, this will not work on export: '" + p_path + "'. Instead, import the image file as an Image resource and load it normally as a resource."); + } +#endif + Ref<Image> image; + image.instantiate(); + Error err = ImageLoader::load_image(p_path, image); + if (err != OK) { + ERR_FAIL_V_MSG(Ref<Image>(), vformat("Failed to load image. Error %d", err)); + } + return image; +} + Error Image::save_png(const String &p_path) const { if (save_png_func == nullptr) { return ERR_UNAVAILABLE; @@ -3183,6 +3198,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); ClassDB::bind_method(D_METHOD("load", "path"), &Image::load); + ClassDB::bind_static_method("Image", D_METHOD("load_from_file", "path"), &Image::load_from_file); ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png); ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer); ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75)); diff --git a/core/io/image.h b/core/io/image.h index 2cad26f3e9..6b323e5eb3 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -290,6 +290,7 @@ public: Vector<uint8_t> get_data() const; Error load(const String &p_path); + static Ref<Image> load_from_file(const String &p_path); Error save_png(const String &p_path) const; Error save_jpg(const String &p_path, float p_quality = 0.75) const; Vector<uint8_t> save_png_to_buffer() const; |