You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.4 KiB

  1. unit uMainForm;
  2. {$mode objfpc}{$H+}
  3. {.$DEFINE USE_OLD_TS}
  4. interface
  5. uses
  6. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, uglcContext, TextSuite, uglcTypes,
  7. utsTextSuite, utsTypes, utsFontCreatorGDI, utsRendererOpenGL, utsPostProcess;
  8. type
  9. TMainForm = class(TForm)
  10. ApplicationProperties: TApplicationProperties;
  11. procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  12. procedure FormCreate(Sender: TObject);
  13. procedure FormDestroy(Sender: TObject);
  14. procedure FormPaint(Sender: TObject);
  15. private
  16. fFrameTime: QWord;
  17. fFrameCount: Integer;
  18. fSecTime: QWord;
  19. fContext: TglcContext;
  20. {$IFDEF USE_OLD_TS}
  21. ftsContext: tsContextID;
  22. ftsFont: tsFontID;
  23. {$ELSE}
  24. ftsContext: TtsContext;
  25. ftsRenderer: TtsRendererOpenGL;
  26. ftsGenerator: TtsFontGeneratorGDI;
  27. ftsFont1: TtsFont;
  28. ftsFont2: TtsFont;
  29. {$ENDIF}
  30. procedure Render;
  31. public
  32. { public declarations }
  33. end;
  34. var
  35. MainForm: TMainForm;
  36. implementation
  37. {$R *.lfm}
  38. uses
  39. dglOpenGL;
  40. const
  41. TEST_STRING = 'orem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
  42. procedure TMainForm.FormCreate(Sender: TObject);
  43. var
  44. pf: TglcContextPixelFormatSettings;
  45. pp: TtsPostProcessStep;
  46. pa: TtsImage;
  47. const
  48. data: array[0..63] of Byte = (
  49. $FF, $AA, $88, $44, $44, $88, $AA, $FF,
  50. $AA, $88, $44, $22, $22, $44, $88, $AA,
  51. $88, $44, $22, $11, $11, $22, $44, $88,
  52. $44, $22, $11, $00, $00, $11, $22, $44,
  53. $44, $22, $11, $00, $00, $11, $22, $44,
  54. $88, $44, $22, $11, $11, $22, $44, $88,
  55. $AA, $88, $44, $22, $22, $44, $88, $AA,
  56. $FF, $AA, $88, $44, $44, $88, $AA, $FF);
  57. begin
  58. pf := TglcContext.MakePF();
  59. fContext := TglcContext.GetPlatformClass.Create(self, pf);
  60. fContext.BuildContext;
  61. {$IFDEF USE_OLD_TS}
  62. tsInit(TS_INIT_TEXTSUITE or TS_INIT_OPENGL or TS_INIT_GDI);
  63. tsContextCreate(@ftsContext);
  64. tsSetParameteri(TS_RENDERER, TS_RENDERER_OPENGL);
  65. tsSetParameteri(TS_CREATOR, TS_CREATOR_GDI_FACENAME);
  66. tsContextBind(ftsContext);
  67. tsFontCreateCreatorA('Calibri', 25, 0, TS_ANTIALIASING_NORMAL, TS_DEFAULT, @ftsFont);
  68. tsFontBind(ftsFont);
  69. {$ELSE}
  70. ftsContext := TtsContext.Create;
  71. ftsRenderer := TtsRendererOpenGL.Create(ftsContext, tsFormatRGBA8);
  72. ftsGenerator := TtsFontGeneratorGDI.Create(ftsContext);
  73. pp := TtsPostProcessFillColor.Create(tsColor4f(0.0, 0.0, 0.0, 1.0), TS_MODES_MODULATE_ALPHA, TS_CHANNELS_RGBA);
  74. pp.AddUsageRange(tsUsageInclude, #$0000, #$FFFF);
  75. ftsGenerator.AddPostProcessStep(pp);
  76. pp := TtsPostProcessShadow.Create(3, 0, 2, 2, tsColor4f(1.0, 0.0, 1.0, 0.05));
  77. pp.AddUsageRange(tsUsageInclude, #$0000, #$FFFF);
  78. ftsGenerator.AddPostProcessStep(pp);
  79. ftsFont1 := ftsGenerator.GetFontByName('Calibri', ftsRenderer, 100, [tsStyleBold, tsStyleItalic], tsAANormal);
  80. ftsFont2 := ftsGenerator.GetFontByName('Calibri', ftsRenderer, 20, [], tsAANormal);
  81. {$ENDIF}
  82. end;
  83. procedure TMainForm.FormDestroy(Sender: TObject);
  84. begin
  85. {$IFDEF USE_OLD_TS}
  86. tsFontDestroy(ftsFont);
  87. tsContextDestroy(ftsContext);
  88. {$ELSE}
  89. FreeAndNil(ftsFont1);
  90. FreeAndNil(ftsFont2);
  91. FreeAndNil(ftsGenerator);
  92. FreeAndNil(ftsRenderer);
  93. FreeAndNil(ftsContext);
  94. {$ENDIF}
  95. end;
  96. procedure TMainForm.FormPaint(Sender: TObject);
  97. begin
  98. Render;
  99. end;
  100. procedure TMainForm.Render;
  101. var
  102. block: TtsTextBlock;
  103. t: QWord;
  104. dif: Integer;
  105. begin
  106. t := GetTickCount64;
  107. if (fFrameTime <> 0) then begin
  108. dif := t - fFrameTime;
  109. inc(fFrameCount, 1);
  110. inc(fSecTime, dif);
  111. if (fSecTime > 1000) then begin
  112. Caption := IntToStr(fFrameCount) + ' FPS';
  113. fFrameCount := 0;
  114. dec(fSecTime, 1000);
  115. end;
  116. end;
  117. fFrameTime := t;
  118. glViewport(0, 0, ClientWidth, ClientHeight);
  119. glClearColor(1, 1, 1, 0);
  120. glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  121. glMatrixMode(GL_PROJECTION);
  122. glLoadIdentity;
  123. glOrtho(0, ClientWidth, ClientHeight, 0, -10, 10);
  124. glMatrixMode(GL_MODELVIEW);
  125. glLoadIdentity;
  126. glEnable(GL_BLEND);
  127. glcBlendFunc(TglcBlendMode.bmAlphaBlend);
  128. {$IFDEF USE_OLD_TS}
  129. tsTextBeginBlock(0, 0, ClientWidth, ClientHeight, TS_ALIGN_BLOCK);
  130. tsTextOutA(TEST_STRING);
  131. tsTextEndBlock;
  132. {$ELSE}
  133. block := ftsRenderer.BeginBlock(10, 10, ClientWidth-20, ClientHeight-20, [tsBlockFlagWordWrap]);
  134. try
  135. block.HorzAlign := tsHorzAlignJustify;
  136. block.ChangeFont(ftsFont1);
  137. block.ChangeColor(tsColor4f(1.0, 1.0, 1.0, 1.0));
  138. block.TextOutW('L');
  139. block.ChangeFont(ftsFont2);
  140. block.ChangeColor(tsColor4f(1.0, 1.0, 1.0, 1.0));
  141. block.TextOutW(TEST_STRING + sLineBreak);
  142. block.ChangeFont(ftsFont1);
  143. block.ChangeColor(tsColor4f(1.0, 1.0, 1.0, 1.0));
  144. block.TextOutW('L');
  145. block.ChangeFont(ftsFont2);
  146. block.ChangeColor(tsColor4f(1.0, 1.0, 1.0, 1.0));
  147. block.TextOutW(TEST_STRING);
  148. finally
  149. ftsRenderer.EndBlock(block);
  150. end;
  151. {$ENDIF}
  152. fContext.SwapBuffers;
  153. end;
  154. procedure TMainForm.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  155. begin
  156. Render;
  157. Done := false;
  158. end;
  159. end.