Improved support for custom image loaders

This commit is contained in:
Fabian 2018-09-03 14:06:35 +09:30
parent 9efc6d85d8
commit a01608c389
3 changed files with 35 additions and 17 deletions

View File

@ -60,7 +60,7 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_c
String extension = p_file.get_extension(); String extension = p_file.get_extension();
for (int i = 0; i < loader_count; i++) { for (int i = 0; i < loader.size(); i++) {
if (!loader[i]->recognize(extension)) if (!loader[i]->recognize(extension))
continue; continue;
@ -83,30 +83,45 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_c
void ImageLoader::get_recognized_extensions(List<String> *p_extensions) { void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
for (int i = 0; i < loader_count; i++) { for (int i = 0; i < loader.size(); i++) {
loader[i]->get_recognized_extensions(p_extensions); loader[i]->get_recognized_extensions(p_extensions);
} }
} }
bool ImageLoader::recognize(const String &p_extension) { ImageFormatLoader *ImageLoader::recognize(const String &p_extension) {
for (int i = 0; i < loader_count; i++) { for (int i = 0; i < loader.size(); i++) {
if (loader[i]->recognize(p_extension)) if (loader[i]->recognize(p_extension))
return true; return loader[i];
} }
return false; return NULL;
} }
ImageFormatLoader *ImageLoader::loader[MAX_LOADERS]; Vector<ImageFormatLoader *> ImageLoader::loader;
int ImageLoader::loader_count = 0;
void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) { void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
ERR_FAIL_COND(loader_count >= MAX_LOADERS); loader.push_back(p_loader);
loader[loader_count++] = p_loader; }
void ImageLoader::remove_image_format_loader(ImageFormatLoader *p_loader) {
loader.erase(p_loader);
}
const Vector<ImageFormatLoader *> &ImageLoader::get_image_format_loaders() {
return loader;
}
void ImageLoader::cleanup() {
while (loader.size()) {
remove_image_format_loader(loader[0]);
}
} }
///////////////// /////////////////
@ -137,7 +152,7 @@ RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_origin
int idx = -1; int idx = -1;
for (int i = 0; i < ImageLoader::loader_count; i++) { for (int i = 0; i < ImageLoader::loader.size(); i++) {
if (ImageLoader::loader[i]->recognize(extension)) { if (ImageLoader::loader[i]->recognize(extension)) {
idx = i; idx = i;
break; break;

View File

@ -70,20 +70,21 @@ public:
class ImageLoader { class ImageLoader {
enum { static Vector<ImageFormatLoader *> loader;
MAX_LOADERS = 32
};
friend class ResourceFormatLoaderImage; friend class ResourceFormatLoaderImage;
static ImageFormatLoader *loader[MAX_LOADERS];
static int loader_count;
protected: protected:
public: public:
static Error load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom = NULL, bool p_force_linear = false, float p_scale = 1.0); static Error load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom = NULL, bool p_force_linear = false, float p_scale = 1.0);
static void get_recognized_extensions(List<String> *p_extensions); static void get_recognized_extensions(List<String> *p_extensions);
static bool recognize(const String &p_extension); static ImageFormatLoader *recognize(const String &p_extension);
static void add_image_format_loader(ImageFormatLoader *p_loader); static void add_image_format_loader(ImageFormatLoader *p_loader);
static void remove_image_format_loader(ImageFormatLoader *p_loader);
static const Vector<ImageFormatLoader *> &get_image_format_loaders();
static void cleanup();
}; };
class ResourceFormatLoaderImage : public ResourceFormatLoader { class ResourceFormatLoaderImage : public ResourceFormatLoader {

View File

@ -1992,6 +1992,8 @@ void Main::cleanup() {
memdelete(arvr_server); memdelete(arvr_server);
} }
ImageLoader::cleanup();
unregister_driver_types(); unregister_driver_types();
unregister_module_types(); unregister_module_types();
unregister_platform_apis(); unregister_platform_apis();