mikymod 12 лет назад
Родитель
Сommit
ad041f090f

+ 2 - 0
engine/CMakeLists.txt

@@ -307,6 +307,7 @@ set (RESOURCE_SRC
 	resource/MeshResource.cpp
 	resource/LuaResource.cpp
 	resource/GuiResource.cpp
+	resource/FontResource.cpp
 )
 
 set (RESOURCE_HEADERS
@@ -327,6 +328,7 @@ set (RESOURCE_HEADERS
 	resource/SpriteResource.h
 	resource/PhysicsResource.h
 	resource/GuiResource.h
+	resource/FontResource.h
 )
 
 if (CROWN_DEBUG OR CROWN_DEVELOPMENT)

+ 5 - 0
engine/compilers/BundleCompiler.cpp

@@ -50,6 +50,7 @@ namespace sound_resource { extern void compile(Filesystem&, const char*, File*);
 namespace sprite_resource { extern void compile(Filesystem&, const char*, File*); }
 namespace material_resource { extern void compile(Filesystem&, const char*, File*); }
 namespace gui_resource { extern void compile(Filesystem&, const char*, File*); }
+namespace font_resource { extern void compile(Filesystem&, const char*, File*); }
 
 //-----------------------------------------------------------------------------
 BundleCompiler::BundleCompiler()
@@ -171,6 +172,10 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, con
 			{
 				physics_config_resource::compile(root_fs, filename, out_file);
 			}
+			else if (resource_type_hash == FONT_TYPE)
+			{
+				font_resource::compile(root_fs, filename, out_file);
+			}
 			else
 			{
 				Log::e("Oops, unknown resource type!");

+ 2 - 0
engine/renderers/RenderWorld.cpp

@@ -105,6 +105,8 @@ RenderWorld::RenderWorld()
 
 	default_program = r->create_gpu_program(default_vs, default_fs);
 	texture_program = r->create_gpu_program(default_vs, texture_fs);
+
+	Log::i("hash: %.8X", hash::murmur2_32("font", 4, 0));
 }
 
 //-----------------------------------------------------------------------------

+ 107 - 0
engine/resource/FontResource.cpp

@@ -0,0 +1,107 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+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 "FontResource.h"
+#include "JSONParser.h"
+#include "Allocator.h"
+#include "Filesystem.h"
+#include "Hash.h"
+#include "Bundle.h"
+#include "Types.h"
+
+
+namespace crown
+{
+namespace font_resource
+{
+
+//-----------------------------------------------------------------------------
+void parse_glyph(JSONElement e, FontGlyphData& glyph)
+{
+	JSONElement id = e.key("id");
+	JSONElement x = e.key("x");
+	JSONElement y = e.key("y");
+	JSONElement width = e.key("width");
+	JSONElement height = e.key("height");
+	JSONElement x_offset = e.key("x_offset");
+	JSONElement y_offset = e.key("y_offset");
+	JSONElement advance = e.key("advance");
+
+	glyph.id = id.int_value();
+	glyph.x = x.int_value();
+	glyph.y = y.int_value();
+	glyph.width = width.int_value();
+	glyph.height = height.int_value();
+	glyph.x_offset = x_offset.float_value();
+	glyph.y_offset = y_offset.float_value();
+	glyph.x_advance = advance.float_value();
+}
+
+//-----------------------------------------------------------------------------
+void compile(Filesystem& fs, const char* resource_path, File* out_file)
+{
+	File* file = fs.open(resource_path, FOM_READ);
+	char* buf = (char*)default_allocator().allocate(file->size());
+	file->read(buf, file->size());
+
+	// Out buffer
+	FontHeader h;
+	List<FontGlyphData> m_glyphs(default_allocator());
+
+	JSONParser json(buf);
+	JSONElement root = json.root();
+
+	JSONElement mat = root.key("material");
+	JSONElement count = root.key("count");
+	JSONElement glyphs = root.key("glyphs");
+
+	DynamicString material_name;
+	mat.string_value(material_name);
+	material_name += ".material";
+	
+	for (uint32_t i = 0; i < count.int_value(); i++)
+	{
+		FontGlyphData data;
+		parse_glyph(glyphs[i], data);
+		m_glyphs.push_back(data);
+	}
+
+	fs.close(file);
+	default_allocator().deallocate(buf);
+
+	h.material.id = hash::murmur2_64(material_name.c_str(), string::strlen(material_name.c_str()), 0);
+	h.num_glyphs = m_glyphs.size();
+
+	out_file->write((char*) &h, sizeof(FontHeader));
+
+	if (m_glyphs.size() > 0)
+	{
+		out_file->write((char*) m_glyphs.begin(), sizeof(FontGlyphData) * h.num_glyphs);
+	}
+}
+
+} // namespace font_resource
+} // namespace crown

+ 51 - 5
engine/resource/FontResource.h

@@ -30,19 +30,49 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Resource.h"
 #include "Bundle.h"
 #include "Allocator.h"
+#include "File.h"
+#include "Assert.h"
 
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
+struct FontHeader
+{
+	ResourceId material;
+	uint32_t num_glyphs;
+};
+
+//-----------------------------------------------------------------------------
+struct FontGlyphData
+{
+	uint32_t id;
+	uint32_t x;
+	uint32_t y;
+	uint32_t width;
+	uint32_t height;
+	float x_offset;
+	float y_offset;
+	float x_advance;
+};
+
+//-----------------------------------------------------------------------------
 class FontResource
 {
 public:
 
 	//-----------------------------------------------------------------------------
-	static void* load(Allocator& /*allocator*/, Bundle& /*bundle*/, ResourceId /*id*/)
+	static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
 	{
-		return NULL;
-		// TODO
+		File* file = bundle.open(id);
+		const size_t file_size = file->size();
+
+		void* res = allocator.allocate(file_size);
+		file->read(res, file_size);
+
+		bundle.close(file);
+
+		return res;
 	}
 
 	//-----------------------------------------------------------------------------
@@ -53,9 +83,10 @@ public:
 	}
 
 	//-----------------------------------------------------------------------------
-	static void unload(Allocator& /*allocator*/, void* /*resource*/)
+	static void unload(Allocator& allocator, void* resource)
 	{
-		// TODO
+		CE_ASSERT_NOT_NULL(resource);
+		allocator.deallocate(resource);
 	}
 
 	//-----------------------------------------------------------------------------
@@ -64,6 +95,21 @@ public:
 		// TODO
 	}
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_glyphs() const
+	{
+		return ((FontHeader*)this)->num_glyphs;
+	}
+
+	//-----------------------------------------------------------------------------
+	FontGlyphData get_glyph(uint32_t index) const
+	{
+		CE_ASSERT(index < num_glyphs(), "Index out of bounds");
+
+		FontGlyphData* begin = (FontGlyphData*) (((char*) this) + sizeof(FontHeader));
+		return begin[index];
+	}
+
 private:
 
 	// Disable construction

+ 31 - 1
engine/resource/PackageResource.cpp

@@ -59,6 +59,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	List<ResourceId> m_physics(default_allocator());
 	List<ResourceId> m_materials(default_allocator());
 	List<ResourceId> m_guis(default_allocator());
+	List<ResourceId> m_fonts(default_allocator());
 
 	// Check for resource types
 	if (root.has_key("texture"))
@@ -265,7 +266,30 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 			id.id = hash::murmur2_64(guis_name.c_str(), guis_name.length(), 0);
 			m_guis.push_back(id);
 		}
-	}	
+	}
+
+	// Check for fonts
+	if (root.has_key("font"))
+	{
+		JSONElement fonts_array = root.key("font");
+		uint32_t fonts_array_size = fonts_array.size();
+
+		for (uint32_t i = 0; i < fonts_array_size; i++)
+		{
+			DynamicString font_name;
+			fonts_array[i].string_value(font_name); font_name += ".font";
+
+			if (!fs.is_file(font_name.c_str()))
+			{
+				Log::e("font '%s' does not exist.", font_name.c_str());
+				return;				
+			}
+
+			ResourceId id;
+			id.id = hash::murmur2_64(font_name.c_str(), font_name.length(), 0);
+			m_fonts.push_back(id);
+		}
+	}
 
 	PackageHeader header;
 	header.num_textures = m_texture.size();
@@ -277,6 +301,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	header.num_physics = m_physics.size();
 	header.num_materials = m_materials.size();
 	header.num_guis = m_guis.size();
+	header.num_fonts = m_fonts.size();
 
 	header.textures_offset = sizeof(PackageHeader);
 	header.scripts_offset  = header.textures_offset + sizeof(ResourceId) * header.num_textures;
@@ -287,6 +312,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	header.physics_offset = header.sprites_offset + sizeof(ResourceId) * header.num_sprites;
 	header.materials_offset = header.physics_offset + sizeof(ResourceId) * header.num_physics;
 	header.guis_offset = header.materials_offset + sizeof(ResourceId) * header.num_materials;
+	header.fonts_offset = header.guis_offset + sizeof(ResourceId) * header.num_guis;
 
 	out_file->write((char*) &header, sizeof(PackageHeader));
 
@@ -326,6 +352,10 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	{
 		out_file->write((char*) m_guis.begin(), sizeof(ResourceId) * header.num_guis);
 	}
+	if (m_fonts.size() > 0)
+	{
+		out_file->write((char*) m_fonts.begin(), sizeof(ResourceId) * header.num_fonts);
+	}
 }
 
 } // namespace package_resource

+ 16 - 0
engine/resource/PackageResource.h

@@ -56,6 +56,8 @@ struct PackageHeader
 	uint32_t materials_offset;
 	uint32_t num_guis;
 	uint32_t guis_offset;
+	uint32_t num_fonts;
+	uint32_t fonts_offset;
 };
 
 struct PackageResource
@@ -145,6 +147,12 @@ struct PackageResource
 		return ((PackageHeader*) this)->num_guis;
 	}
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_fonts() const
+	{
+		return ((PackageHeader*) this)->num_fonts;
+	}
+
 	//-----------------------------------------------------------------------------
 	ResourceId get_texture_id(uint32_t i) const
 	{
@@ -226,6 +234,14 @@ struct PackageResource
 		return begin[i];
 	}
 
+	//-----------------------------------------------------------------------------
+	ResourceId get_font_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_fonts(), "Index out of bounds");
+
+		ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->fonts_offset);
+		return begin[i];
+	}
 
 private:
 

+ 2 - 0
engine/resource/Resource.h

@@ -47,6 +47,7 @@ namespace crown
 #define PHYSICS_EXTENSION			"physics"
 #define GUI_EXTENSION				"gui"
 #define PHYSICS_CONFIG_EXTENSION	"physics_config"
+#define FONT_EXTENSION				"font"
 
 #define TEXTURE_TYPE				0x0DEED4F7
 #define MESH_TYPE					0x742FBC9A
@@ -61,6 +62,7 @@ namespace crown
 #define PHYSICS_TYPE				0xFA32C012
 #define GUI_TYPE					0x2C56149A
 #define PHYSICS_CONFIG_TYPE			0x514F14A1
+#define FONT_TYPE					0x536DC7D4
 
 /// ResourceId uniquely identifies a resource by its name and type.
 /// In order to speed up the lookup by the manager, it also keeps

+ 10 - 0
engine/resource/ResourcePackage.h

@@ -97,11 +97,21 @@ public:
 		{
 			m_resource_manager->load(GUI_TYPE, m_package->get_gui_id(i));
 		}
+
+		for (uint32_t i = 0; i < m_package->num_fonts(); i++)
+		{
+			m_resource_manager->load(FONT_TYPE, m_package->get_font_id(i));
+		}
 	}
 
 	/// Unloads all the resources in the package.
 	void unload()
 	{
+		for (uint32_t i = 0; i < m_package->num_fonts(); i++)
+		{
+			m_resource_manager->unload(m_package->get_font_id(i));
+		}
+
 		for (uint32_t i = 0; i < m_package->num_guis(); i++)
 		{
 			m_resource_manager->unload(m_package->get_gui_id(i));

+ 2 - 0
engine/resource/ResourceRegistry.cpp

@@ -35,6 +35,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PhysicsResource.h"
 #include "MaterialResource.h"
 #include "GuiResource.h"
+#include "FontResource.h"
 
 namespace crown
 {
@@ -52,6 +53,7 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ MATERIAL_TYPE, MaterialResource::load, MaterialResource::unload, MaterialResource::online, MaterialResource::offline },
 	{ GUI_TYPE, GuiResource::load, GuiResource::unload, GuiResource::online, GuiResource::offline },
 	{ PHYSICS_CONFIG_TYPE, PhysicsConfigResource::load, PhysicsConfigResource::unload, PhysicsConfigResource::online, PhysicsConfigResource::offline },
+	{ FONT_TYPE, FontResource::load, FontResource::unload, FontResource::online, FontResource::offline },
 	{ 0, NULL, NULL, NULL, NULL }
 };