Fonts
hgeFont是Hge的辅助类,用来渲染 bitmap 字体. 有两个必须的组件来实现该功能:
- Font bitmap file:图像文件(通常是png文件),用来存储每一个字符。
- Font description file:描述文件,定义每一个字符在图像文件中的位置,具体的定义细节你可以参考如下连接: Font description files in HGE
定义Fonts在res文件中
此处有hge默认的字体图像: Media:HGE_font1.png [link appears broken...however they can be obtained from the HGE website]
该字体文件的描述文件: Media:HGE_font1.fnt
将两个文件 font1.png and font1.fnt,放置在程序的同一目录下。
定义一个字体文件,注意使用字体描述文件来定义,而不是bitmap文件!!!!
Font font1 { filename=font1.fnt }
显示字体文本
#include <hgefont.h> hgeFont* font1;
WinMain函数中加入:
font1 = myRes->GetFont("font1");
渲染字体,用 hgeFont的 Render() 或者 printf() 函数都可以。
Render()函数带有4个参数:(x,y)的屏幕坐标,你要显示的文本, 对齐方式. 对齐方式包括: HGETEXT_LEFT, HGETEXT_RIGHT, HGETEXT_CENTER, . printf()函数参见 : printf statement
在FrameFunc函数体中加入:
font1->SetScale(1.0); //set text size to normal font1->SetColor(ARGB(255,0,0,0)); //set color of text to black font1->Render(5, 5, "This is some text"); //render text at coordinates 5, 5 int someNumber = 50; font1->SetScale(2.0); //set text size to twice its normal size font1->printf(5, 30, "Here is a number: %d", someNumber); //render text using printf-style formatting
整个的FrameFunc函数如下:
bool FrameFunc() { float dt=hge->Timer_GetDelta(); //get the time since the last call to FrameFunc star->Update(dt); //update the animation hge->Gfx_BeginScene(); hge->Gfx_Clear(0); //clear the screen, filling it with black bgSprite->RenderStretch(0, 0, 800, 600); //render the background sprite stretched playerSprite->Render(200, 200); //render the player sprite star->Render(400, 300); //render the animation of a star font1->SetScale(1.0); //set text size to normal font1->SetColor(ARGB(255,0,0,0)); //set color of text to black font1->Render(5, 5, HGETEXT_LEFT, "This is some text"); //render text at coordinates 5, 5 int someNumber = 50; font1->SetScale(2.0); //set text size to twice its normal size font1->printf(5, 30, HGETEXT_LEFT, "Here is a number: %d", someNumber); //render text using printf-style formatting hge->Gfx_EndScene(); return false; }