mikymod 12 лет назад
Родитель
Сommit
23040c72c4
3 измененных файлов с 63 добавлено и 15 удалено
  1. 14 12
      engine/resource/FontResource.cpp
  2. 48 2
      engine/resource/FontResource.h
  3. 1 1
      engine/resource/PackageResource.cpp

+ 14 - 12
engine/resource/FontResource.cpp

@@ -48,16 +48,16 @@ void parse_glyph(JSONElement e, FontGlyphData& glyph)
 	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();
+	JSONElement x_advance = e.key("x_advance");
+
+	glyph.id = id.to_int();
+	glyph.x = x.to_int();
+	glyph.y = y.to_int();
+	glyph.width = width.to_int();
+	glyph.height = height.to_int();
+	glyph.x_offset = x_offset.to_float();
+	glyph.y_offset = y_offset.to_float();
+	glyph.x_advance = x_advance.to_float();
 }
 
 //-----------------------------------------------------------------------------
@@ -79,10 +79,12 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	JSONElement glyphs = root.key("glyphs");
 
 	DynamicString material_name;
-	mat.string_value(material_name);
+	mat.to_string(material_name);
 	material_name += ".material";
+
+	uint32_t num_glyphs = count.to_int();
 	
-	for (uint32_t i = 0; i < count.int_value(); i++)
+	for (uint32_t i = 0; i < num_glyphs; i++)
 	{
 		FontGlyphData data;
 		parse_glyph(glyphs[i], data);

+ 48 - 2
engine/resource/FontResource.h

@@ -32,10 +32,41 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Allocator.h"
 #include "File.h"
 #include "Assert.h"
+#include "Log.h"
 
 namespace crown
 {
 
+// Glyph metrics:
+// --------------
+//
+//                   x_min|<-------- width -------->|x_max
+//                        |                         |
+//              |   	  +-------------------------+--------------- y_max
+//              |         |    ggggggggg   ggggg    |     ^        ^
+//              |         |   g:::::::::ggg::::g    |     |        |
+//              |         |  g:::::::::::::::::g    |     |        |
+//              |         | g::::::ggggg::::::gg    |     |        |
+//              |         | g:::::g     g:::::g     |     |        |
+//    x_offset -|-------->| g:::::g     g:::::g     |  y_offset    |
+//              |         | g:::::g     g:::::g     |     |        |
+//              |         | g::::::g    g:::::g     |     |        |
+//              |         | g:::::::ggggg:::::g     |     |        |
+//              |         |  g::::::::::::::::g     |     |      height
+//              |         |   gg::::::::::::::g     |     |        |
+//  baseline ---*---------|---- gggggggg::::::g-----*--------      |
+//            / |         |             g:::::g     |              |
+//     origin   |         | gggggg      g:::::g     |              |
+//              |         | g:::::gg   gg:::::g     |              |
+//              |         |  g::::::ggg:::::::g     |              |
+//              |         |   gg:::::::::::::g      |              |
+//              |         |     ggg::::::ggg        |              |
+//              |         |         gggggg          |              v
+//              |         +-------------------------+--------------- y_min
+//              |                                   |
+//              |------------- x_advance ---------->|
+
+
 //-----------------------------------------------------------------------------
 struct FontHeader
 {
@@ -95,6 +126,12 @@ public:
 		// TODO
 	}
 
+	//-----------------------------------------------------------------------------
+	ResourceId material() const
+	{
+		return ((FontHeader*) this)->material;
+	}
+
 	//-----------------------------------------------------------------------------
 	uint32_t num_glyphs() const
 	{
@@ -102,12 +139,21 @@ public:
 	}
 
 	//-----------------------------------------------------------------------------
-	FontGlyphData get_glyph(uint32_t index) const
+	FontGlyphData get_glyph(const uint8_t index) const
 	{
 		CE_ASSERT(index < num_glyphs(), "Index out of bounds");
 
 		FontGlyphData* begin = (FontGlyphData*) (((char*) this) + sizeof(FontHeader));
-		return begin[index];
+
+		for (uint32_t i = 0; i < num_glyphs(); i++)
+		{
+			if (begin[i].id == index)
+			{
+				return begin[i];
+			}
+		}
+
+		CE_FATAL("Glyph not found");
 	}
 
 private:

+ 1 - 1
engine/resource/PackageResource.cpp

@@ -277,7 +277,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 		for (uint32_t i = 0; i < fonts_array_size; i++)
 		{
 			DynamicString font_name;
-			fonts_array[i].string_value(font_name); font_name += ".font";
+			fonts_array[i].to_string(font_name); font_name += ".font";
 
 			if (!fs.is_file(font_name.c_str()))
 			{