demo06.c 3.5 KB

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