Browse Source

BMFont loading direct support from .fnt files.

Juan Linietsky 7 years ago
parent
commit
4de84f4c0a
3 changed files with 55 additions and 0 deletions
  1. 8 0
      scene/register_scene_types.cpp
  2. 39 0
      scene/resources/font.cpp
  3. 8 0
      scene/resources/font.h

+ 8 - 0
scene/register_scene_types.cpp

@@ -201,6 +201,8 @@ static ResourceFormatLoaderDynamicFont *resource_loader_dynamic_font = NULL;
 
 static ResourceFormatLoaderStreamTexture *resource_loader_stream_texture = NULL;
 
+static ResourceFormatLoaderBMFont *resource_loader_bmfont = NULL;
+
 static ResourceFormatSaverShader *resource_saver_shader = NULL;
 static ResourceFormatLoaderShader *resource_loader_shader = NULL;
 
@@ -233,6 +235,9 @@ void register_scene_types() {
 	resource_loader_shader = memnew(ResourceFormatLoaderShader);
 	ResourceLoader::add_resource_format_loader(resource_loader_shader, true);
 
+	resource_loader_bmfont = memnew(ResourceFormatLoaderBMFont);
+	ResourceLoader::add_resource_format_loader(resource_loader_bmfont, true);
+
 	OS::get_singleton()->yield(); //may take time to init
 
 	ClassDB::register_class<Object>();
@@ -652,6 +657,9 @@ void unregister_scene_types() {
 	if (resource_loader_shader) {
 		memdelete(resource_loader_shader);
 	}
+	if (resource_loader_bmfont) {
+		memdelete(resource_loader_bmfont);
+	}
 
 	SpatialMaterial::finish_shaders();
 	ParticlesMaterial::finish_shaders();

+ 39 - 0
scene/resources/font.cpp

@@ -596,3 +596,42 @@ BitmapFont::~BitmapFont() {
 
 	clear();
 }
+
+////////////
+
+RES ResourceFormatLoaderBMFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
+
+	if (r_error)
+		*r_error = ERR_FILE_CANT_OPEN;
+
+	Ref<BitmapFont> font;
+	font.instance();
+
+	Error err = font->create_from_fnt(p_path);
+
+	if (err) {
+		if (r_error)
+			*r_error = err;
+		return RES();
+	}
+
+	return font;
+}
+
+void ResourceFormatLoaderBMFont::get_recognized_extensions(List<String> *p_extensions) const {
+
+	p_extensions->push_back("fnt");
+}
+
+bool ResourceFormatLoaderBMFont::handles_type(const String &p_type) const {
+
+	return (p_type == "BitmapFont");
+}
+
+String ResourceFormatLoaderBMFont::get_resource_type(const String &p_path) const {
+
+	String el = p_path.get_extension().to_lower();
+	if (el == "fnt")
+		return "BitmapFont";
+	return "";
+}

+ 8 - 0
scene/resources/font.h

@@ -159,4 +159,12 @@ public:
 	~BitmapFont();
 };
 
+class ResourceFormatLoaderBMFont : public ResourceFormatLoader {
+public:
+	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
+	virtual void get_recognized_extensions(List<String> *p_extensions) const;
+	virtual bool handles_type(const String &p_type) const;
+	virtual String get_resource_type(const String &p_path) const;
+};
+
 #endif