HGE中文解决方案hgeTTF

来源:互联网 发布:淘宝二手电脑怎么检测 编辑:程序博客网 时间:2024/05/01 12:45

 这是一个比较好的解决方案,直接用字体来,不用png图片渲染。

下面是下载来的hgeTTF里附带的demo(我自己稍微修改了下):

Code:
  1. #include <Windows.h>  
  2. #include <hge.h>  
  3. #include "hgeTTF/FontManager.h"  
  4. #include "hgeTTF/Font.h"  
  5. #include <cassert>  
  6.   
  7. static const int ScreenWidth = 800;  
  8. static const int ScreenHeight = 600;  
  9. static const unsigned char ScreenBpp = 32;  
  10. static const char* WindowTitle = "hgeTTF Test";  
  11. static const char* LogFile = "Log.txt";  
  12.   
  13. HGE* hge = hgeCreate(HGE_VERSION);  
  14. hgeTTF::FontManager FontMgr;  
  15. hgeTTF::Font* Font = NULL;  
  16. float Rotation = 0;  
  17.   
  18. bool Update();  
  19. bool Render();  
  20.   
  21. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  
  22. {  
  23.     hge->System_SetState(HGE_FRAMEFUNC, Update);  
  24.     hge->System_SetState(HGE_RENDERFUNC, Render);  
  25.     hge->System_SetState(HGE_WINDOWED, true);  
  26.     hge->System_SetState(HGE_SCREENWIDTH, ScreenWidth);  
  27.     hge->System_SetState(HGE_SCREENHEIGHT, ScreenHeight);  
  28.     hge->System_SetState(HGE_SCREENBPP, ScreenBpp);  
  29.     hge->System_SetState(HGE_TITLE, WindowTitle);  
  30.     hge->System_SetState(HGE_SHOWSPLASH, false);  
  31.     hge->System_SetState(HGE_LOGFILE, LogFile);  
  32.     hge->System_SetState(HGE_USESOUND, false);  
  33.     hge->System_SetState(HGE_HIDEMOUSE, false);  
  34.     hge->System_SetState(HGE_FPS, HGEFPS_UNLIMITED);  
  35.   
  36.     if (!hge->System_Initiate())  
  37.         return false;  
  38.   
  39.     if (!FontMgr.Initialize())  
  40.         return 1;  
  41.   
  42.     if (!FontMgr.loadFont("monafont-ttf-2.90/mona.ttf"))  
  43.         return 2;  
  44.   
  45.     Font = FontMgr.getFont("monafont-ttf-2.90/mona.ttf");  
  46.     Font->setSize(32);  
  47.     if (!Font->cacheChars('a''z'))  
  48.         return 3;  
  49.   
  50.     if (!Font->cacheChars(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))  
  51.         return 4;  
  52.   
  53.     hge->System_Start();  
  54.     hge->Release();  
  55.     return 0;  
  56. }  
  57.   
  58. bool Update()  
  59. {  
  60.     hgeInputEvent Event;  
  61.     while (hge->Input_GetEvent(&Event))  
  62.     {  
  63.         switch (Event.type)  
  64.         {  
  65.         case INPUT_KEYDOWN:  
  66.             switch (Event.key)  
  67.             {  
  68.             case HGEK_ESCAPE:  
  69.                 return true;  
  70.             case HGEK_K:  
  71.                 //Toggle kerning  
  72.                 Font->useKerning(!Font->usingKerning());  
  73.                 break;  
  74.             }  
  75.             break;  
  76.         }  
  77.     }  
  78.     Rotation += 5.0f * hge->Timer_GetDelta();  
  79.     return false;  
  80. }  
  81.   
  82. bool Render()  
  83. {  
  84.     hge->Gfx_BeginScene();  
  85.     hge->Gfx_Clear(0);  
  86.   
  87.     Font->setRotation(0.0f);  
  88.     Font->setColor(0xffffffff);  
  89.     Font->setSize(22);  
  90.     Font->printf(5, 30, L"FPS: %d", hge->Timer_GetFPS());  
  91.     float mouseX = 0.0f, mouseY = 0.0f;  
  92.     hge->Input_GetMousePos(&mouseX, &mouseY);  
  93.     Font->printf(5, 60, L"%d x %d", (int)mouseX, (int)mouseY);  
  94.   
  95.     Font->setColor(0xffffff00);  
  96.     Font->setSize(42);  
  97.     Font->drawString(L"一个好的解决方案", 400, 200);  
  98.     hge->Gfx_RenderLine(400, 210, 400 + Font->getStringWidth(L"一个好的解决方案"), 210);  
  99.   
  100.     Font->setSize(22);  
  101.     Font->printf(300, 50, L"%s", L"printf");  
  102.     hge->Gfx_RenderLine(300, 55, 300 + Font->getStringWidth(L"printf"), 55);  
  103.   
  104.     Font->setColor(0xffcc0000);  
  105.     Font->drawString(L"Multi/nLine", 150, 200);  
  106.   
  107.     Font->setColor(0xffff0000, 1);  
  108.     Font->setColor(0xffff0000, 2);  
  109.     Font->setColor(0xff00ff00, 3);  
  110.     Font->setColor(0xff00ff00, 4);  
  111.     Font->drawString(L"Horizontal/t Tab", 50, 400);  
  112.     Font->drawString(L"Vertical/v Tab", 50, 450);  
  113.   
  114.     Font->setSize(32);  
  115.     Font->setColor(0xff00ff00);  
  116.     Font->setRotation(Rotation);  
  117.     Font->drawString(L"Rotation", 500, 400);  
  118.   
  119.     hge->Gfx_EndScene();  
  120.     return false;  
  121. }  

 

原创粉丝点击