Ogre引擎源码——资源之Font

来源:互联网 发布:mac拆卸软件 编辑:程序博客网 时间:2024/05/29 03:44

http://blog.csdn.net/hunter8777/article/details/6231284

 

 

Resource部分源码已经解读过了,对Ogre如何管理Resource的流程有了一定的了解。下面就来具体看下每种资源的实现。

Ogre中的Resource共用如下几种:

Texture、Compositor、Font、GpuProgram、Material、Mesh、Skeleton、BspLevel

Resource种类

本文的主角是Font

相关的头文件有:

OgreFont.h

OgreFontManager.h

 

(1)Font

Ogre之Font

首先来看下Font的成员变量

[cpp] view plaincopyprint?
  1. /// The type of font   
  2. FontType mType;  
  3.   
  4. /// Source of the font (either an image name or a truetype font)  
  5. String mSource;  
  6.   
  7. /// Size of the truetype font, in points  
  8. Real mTtfSize;  
  9.   
  10. /// Resolution (dpi) of truetype font   
  11. uint mTtfResolution;  
  12.   
  13. /// Max distance to baseline of this (truetype) font  
  14. int mTtfMaxBearingY;  

第一个变量类FontType是一个枚举,标示当前字体类型。Ogre中Font只有两种类型:truetype和image。

[cpp] view plaincopyprint?
  1. enum FontType  
  2. {  
  3.     /// Generated from a truetype (.ttf) font  
  4.     FT_TRUETYPE = 1,  
  5.     /// Loaded from an image created by an artist  
  6.     FT_IMAGE = 2  
  7. };  

其余四个变量顾名思义。其中后三个和truetype类型字体有关。

 

Font中还有一个重要的结构体定义GlyphInfo,即字体轮廓信息。

[cpp] view plaincopyprint?
  1. struct GlyphInfo   
  2. {  
  3.     CodePoint codePoint;  
  4.     UVRect uvRect;  
  5.     Real aspectRatio;  
  6.   
  7.     GlyphInfo(CodePoint id, const UVRect& rect, Real aspect)  
  8.         : codePoint(id), uvRect(rect), aspectRatio(aspect)  
  9.     {  
  10.     }  
  11. };  

CodePoint是一个无符号32位整型,用来表示一个unicode。

UVRect是一个模板参数类型为浮点数的矩形TRect<float>,用来表示字体轮廓大小。

Real则是长宽比例。

 

由于一个Font类通常表示一段编码范围的字体,所以该类中主要存储的数据类型定义如下

[cpp] view plaincopyprint?
  1. /// A range of code points, inclusive on both ends  
  2. typedef std::pair<CodePoint, CodePoint> CodePointRange;  
  3. typedef vector<CodePointRange>::type CodePointRangeList;  
  4.   
  5. /// Range of code points to generate glyphs for (truetype only)  
  6. CodePointRangeList mCodePointRangeList;  
  7.   
  8. /// Map from unicode code point to texture coordinates  
  9. typedef map<CodePoint, GlyphInfo>::type CodePointMap;  
  10. CodePointMap mCodePointMap;  

RangeList是以CodePoint的范围成对存储;map则是将每个CodePoint与相应的字体轮廓信息对应起来。

 

剩下的成员变量是Material和Texture,即保存字体在内存中以便显示的形式。

Material和Texture也都是Resource的一种,留以后解读源码。

 

再来关注下Font的成员函数

从UML图中可以看出,Font类不仅继承了Resource,还继承了ManualResourceLoader。继承自后者表示Font类支持自定义load方法,以手动方式导入资源。

所以,除去标准的getter/setter函数,Font主要实现了从Resource接口继承来的loadImpl和unloadImpl方法以及ManualResourceLoader类的loadResource方法。

 

首先是loadResource函数。

这个函数作用是导入字体文件,它封装了FreeType库load字体文件的操作,主要完成以下流程:

  • 通过FT库导入fft文件
  • 获取所有字体中最大宽度和高度
  • 计算所需要的纹理大小总数
  • 分别计算每个字体的轮廓信息

当导入了字体文件后,装载资源的流程就主要表现为设置material和texture,这就需要在loadImpl和unloadImpl函数中设置。

 

(2)FontManager

fontManager

如同在资源管理一文中写到的意义,FontManager负责创建Font(ResourceManager负责创建Resource)。除此之外,在构造函数中,FontManager还要将自己注册入ResourceManager,方便后者进行管理。

FontManager中最主要的两个函数为

[cpp] view plaincopyprint?
  1. void parseScript(DataStreamPtr& stream, const String& groupName);  
  2.   
  3. void parseAttribute(const String& line, FontPtr& pFont);  

parseScript函数负责解析后缀为fontdef的脚本。

parseAttribute则将parseScript解析出来的相应属性,对Font类进行设置。设置方法是通过StringInterface中的Cmd模式。