2
0

fCursorD.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. unit fCursorD;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.OpenGL,
  6. System.SysUtils,
  7. System.Classes,
  8. Vcl.Graphics,
  9. Vcl.Controls,
  10. Vcl.Forms,
  11. Vcl.Dialogs,
  12. Vcl.ExtDlgs,
  13. Vcl.Menus,
  14. Vcl.ComCtrls,
  15. Vcl.ExtCtrls,
  16. Vcl.Imaging.Jpeg,
  17. GLS.Scene,
  18. GLS.Objects,
  19. GLScene.VectorTypes,
  20. GLS.PersistentClasses,
  21. GLS.Particles,
  22. GLS.Texture,
  23. GLS.Cadencer,
  24. GLS.HUDObjects,
  25. GLS.SceneViewer,
  26. GLS.Material,
  27. GLS.Coordinates,
  28. GLS.BaseClasses,
  29. GLScene.Utils;
  30. type
  31. TFormCursor = class(TForm)
  32. GLSceneViewer1: TGLSceneViewer;
  33. GLScene1: TGLScene;
  34. GLCamera1: TGLCamera;
  35. GLParticles1: TGLParticles;
  36. HSBitmap: TGLHUDSprite;
  37. StatusBar1: TStatusBar;
  38. MainMenu1: TMainMenu;
  39. MIFile: TMenuItem;
  40. MILoadImage: TMenuItem;
  41. OpenPictureDialog1: TOpenPictureDialog;
  42. HSCursor: TGLHUDSprite;
  43. GLCadencer1: TGLCadencer;
  44. Bevel1: TBevel;
  45. HSParticle: TGLHUDSprite;
  46. GLMaterialLibrary1: TGLMaterialLibrary;
  47. Timer1: TTimer;
  48. O1: TMenuItem;
  49. MITrail: TMenuItem;
  50. N1: TMenuItem;
  51. MIExit: TMenuItem;
  52. miFPS: TMenuItem;
  53. procedure FormCreate(Sender: TObject);
  54. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  55. X, Y: Integer);
  56. procedure GLParticles1ActivateParticle(Sender: TObject;
  57. particle: TGLBaseSceneObject);
  58. procedure HSParticleProgress(Sender: TObject;
  59. const deltaTime, newTime: Double);
  60. procedure Timer1Timer(Sender: TObject);
  61. procedure MITrailClick(Sender: TObject);
  62. procedure MILoadImageClick(Sender: TObject);
  63. procedure MIExitClick(Sender: TObject);
  64. procedure GLSceneViewer1AfterRender(Sender: TObject);
  65. procedure GLCadencer1Progress(Sender: TObject;
  66. const deltaTime, newTime: Double);
  67. private
  68. handleMouseMoves: Boolean;
  69. public
  70. end;
  71. var
  72. FormCursor: TFormCursor;
  73. implementation
  74. {$R *.DFM}
  75. procedure TFormCursor.FormCreate(Sender: TObject);
  76. begin
  77. var Path: TFileName := GetCurrentAssetPath();
  78. SetCurrentDir(Path + '\button');
  79. // hide the Windows cursor for the GLSceneViewer
  80. GLSceneViewer1.Cursor := crNone;
  81. // and load an ugly cursor (size adjusted in design props)
  82. with GLMaterialLibrary1.Materials[0] do
  83. Material.Texture.Image.LoadFromFile('cursor.bmp');
  84. end;
  85. procedure TFormCursor.MILoadImageClick(Sender: TObject);
  86. begin
  87. if OpenPictureDialog1.Execute then
  88. begin
  89. // use the hourglass cursor, it may take some time to load the bitmap,
  90. // rescale it and generate mipmaps before sending it to OpenGL
  91. Screen.Cursor := crHourGlass;
  92. with (HSBitmap.Material.Texture.Image as TGLPictureImage).Picture do
  93. begin
  94. LoadFromFile(OpenPictureDialog1.FileName);
  95. // adjust hud sprite size to match that of the picture
  96. HSBitmap.SetSize(Width, Height);
  97. // adjust position, hudsprites are centered on their x, y coords
  98. HSBitmap.Position.X := Width / 2;
  99. HSBitmap.Position.Y := Height / 2;
  100. end;
  101. Screen.Cursor := crDefault;
  102. end;
  103. end;
  104. procedure TFormCursor.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  105. X, Y: Integer);
  106. var
  107. color: TColor;
  108. begin
  109. // Prevents event floods on slow hardware
  110. if not handleMouseMoves then
  111. Exit;
  112. handleMouseMoves := False;
  113. // Mouse moved, adjust the position of our cursor
  114. HSCursor.Position.X := X;
  115. HSCursor.Position.Y := Y;
  116. // Update the status bar with some misc. info
  117. color := GLSceneViewer1.Buffer.GetPixelColor(X, Y);
  118. StatusBar1.SimpleText := Format('X:%4d Y:%4d, R:%3d G:%3d B:%3d',
  119. [X, Y, GetRValue(color), GetGValue(color), GetBValue(color)]);
  120. // Add a trail particle
  121. if MITrail.Checked then
  122. GLParticles1.CreateParticle();
  123. // Update things now
  124. GLCadencer1.Progress();
  125. end;
  126. procedure TFormCursor.GLSceneViewer1AfterRender(Sender: TObject);
  127. begin
  128. handleMouseMoves := True;
  129. end;
  130. procedure TFormCursor.GLCadencer1Progress(Sender: TObject;
  131. const deltaTime, newTime: Double);
  132. begin
  133. GLSceneViewer1.Invalidate();
  134. end;
  135. procedure TFormCursor.HSParticleProgress(Sender: TObject;
  136. const deltaTime, newTime: Double);
  137. begin
  138. with (Sender as TGLHUDSprite) do
  139. begin
  140. // decrease life time / alpha
  141. TagFloat := TagFloat - deltaTime;
  142. // update alpha channel, but if no more life is left, then suicide
  143. if TagFloat < 0 then
  144. GLParticles1.KillParticle(TGLProxyObject(Sender))
  145. else
  146. AlphaChannel := TagFloat * 0.2;
  147. end;
  148. end;
  149. procedure TFormCursor.GLParticles1ActivateParticle(Sender: TObject;
  150. particle: TGLBaseSceneObject);
  151. begin
  152. with (particle as TGLHUDSprite) do
  153. begin
  154. // we are cadencing real-time, so these are 5 seconds
  155. TagFloat := 5;
  156. // new particle stands where cursor is
  157. Position.AsVector := HSCursor.Position.AsVector;
  158. end;
  159. end;
  160. procedure TFormCursor.Timer1Timer(Sender: TObject);
  161. begin
  162. // update FPS and sprite count
  163. miFPS.Caption := Format('%.1f FPS - %d Cursor Sprites',
  164. [GLSceneViewer1.FramesPerSecond, GLParticles1.Count]);
  165. GLSceneViewer1.ResetPerformanceMonitor;
  166. end;
  167. procedure TFormCursor.MITrailClick(Sender: TObject);
  168. begin
  169. // turn trails on/off
  170. MITrail.Checked := not MITrail.Checked;
  171. end;
  172. procedure TFormCursor.MIExitClick(Sender: TObject);
  173. begin
  174. Close;
  175. end;
  176. end.