demo06.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #define ZGL_IMPORT
  2. #include <memory.h>
  3. #include "zglHeader.h"
  4. zglPFont fntMain;
  5. char resource[256];
  6. char* GetResource( char* FileName )
  7. {
  8. sprintf_s( resource, "../data/%s", FileName );
  9. return resource;
  10. }
  11. void Init()
  12. {
  13. // RU: Загружаем данные о шрифте.
  14. // EN: Load the font.
  15. fntMain = font_LoadFromFile( GetResource( "font.zfi" ) );
  16. // RU: Если же текстуры именуются без использования маски вида "$(имя_шрифта)FontName-page$(номер).$(расширение)", то загрузку можно произвести следующим образом(для png):
  17. // EN: If textures were named without special mask - "$(font_name)-page$(number).$(extension)", then use this method to load them(for png):
  18. //for ( int i = 0; i < fntMain.Count.Pages; i++ )
  19. //{
  20. // char texture[32];
  21. // sprintf_s( texture, "font-page%i.png", i );
  22. // fntMain->Pages[ i ] = tex_LoadFromFile( GetResource( texture ) );
  23. //}
  24. }
  25. void Draw()
  26. {
  27. zglTRect r;
  28. char str[256];
  29. batch2d_Begin();
  30. // RU: ZenGL работает исключительно с кодировкой UTF-8, поэтому весь текст должен быть в UTF-8.
  31. // EN: ZenGL works only with UTF-8 encoding, so all text should be encoded with UTF-8.
  32. text_Draw( fntMain, 400, 25, "String with center alignment", TEXT_HALIGN_CENTER );
  33. text_DrawEx( fntMain, 400, 65, 2, 0, "Scaling", 255, 0xFFFFFF, TEXT_HALIGN_CENTER );
  34. fx2d_SetVCA( 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF, 255, 255, 255, 255 );
  35. text_Draw( fntMain, 400, 125, "Gradient color for every symbol", TEXT_FX_VCA | TEXT_HALIGN_CENTER );
  36. r.X = 0;
  37. r.Y = 300 - 128;
  38. r.W = 192;
  39. r.H = 256;
  40. text_DrawInRect( fntMain, r, "Simple text rendering in rectangle", 0 );
  41. pr2d_Rect( r.X, r.Y, r.W, r.H, 0xFF0000, 255 );
  42. r.X = 800 - 192;
  43. r.Y = 300 - 128;
  44. r.W = 192;
  45. r.H = 256;
  46. text_DrawInRect( fntMain, r, "Text rendering using horizontal right alignment and vertical bottom alignment", TEXT_HALIGN_RIGHT | TEXT_VALIGN_BOTTOM );
  47. pr2d_Rect( r.X, r.Y, r.W, r.H, 0xFF0000, 255 );
  48. r.X = 400 - 192;
  49. r.Y = 300 - 128;
  50. r.W = 384;
  51. r.H = 256;
  52. text_DrawInRect( fntMain, r, "This text uses justify alignment and centered vertically. Text which doesn't fit inside the rectangle will be cropped.", TEXT_HALIGN_JUSTIFY | TEXT_VALIGN_CENTER );
  53. pr2d_Rect( r.X, r.Y, r.W, r.H, 0xFF0000, 255 );
  54. r.X = 400 - 320;
  55. r.Y = 300 + 160;
  56. r.W = 640;
  57. r.H = 128;
  58. text_DrawInRect( fntMain, r, "For starting new line LF symbol can be used\ncode of which is equal to 10 and named in Unicode as \"Line Feed\"", TEXT_HALIGN_CENTER | TEXT_VALIGN_CENTER );
  59. pr2d_Rect( r.X, r.Y, r.W, r.H, 0xFF0000, 255 );
  60. // RU: Выводим количество FPS в правом углу, используя text_GetWidth.
  61. // EN: Render frames per second in the top right corner using text_GetWidth.
  62. sprintf_s( str, "FPS: %i", (int)zgl_Get( RENDER_FPS ) );
  63. text_Draw( fntMain, 800 - text_GetWidth( fntMain, str ), 0, str );
  64. batch2d_End();
  65. }
  66. void Timer()
  67. {
  68. if ( key_Press( K_ESCAPE ) ) zgl_Exit();
  69. key_ClearState();
  70. }
  71. int CALLBACK WinMain (
  72. __in HINSTANCE hInstance,
  73. __in_opt HINSTANCE hPrevInstance,
  74. __in_opt LPSTR lpCmdLine,
  75. __in int nShowCmd
  76. )
  77. {
  78. if ( !zglLoad( libZenGL ) ) return 0;
  79. timer_Add( (void*)&Timer, 16 );
  80. zgl_Reg( SYS_LOAD, (void*)&Init );
  81. zgl_Reg( SYS_DRAW, (void*)&Draw );
  82. wnd_SetCaption( "06 - Text" );
  83. wnd_ShowCursor( TRUE );
  84. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );
  85. zgl_Init();
  86. zglFree();
  87. return 0;
  88. }