Unit1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLCadencer"
  10. #pragma link "GLCoordinates"
  11. #pragma link "GLCrossPlatform"
  12. #pragma link "GLHUDObjects"
  13. #pragma link "GLMaterial"
  14. #pragma link "GLObjects"
  15. #pragma link "GLParticles"
  16. #pragma link "GLScene"
  17. #pragma link "GLWin32Viewer"
  18. #pragma resource "*.dfm"
  19. TForm1 *Form1;
  20. //---------------------------------------------------------------------------
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22. : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. void __fastcall TForm1::FormCreate(TObject *Sender)
  27. {
  28. SetGLSceneMediaDir();
  29. // hide the Windows cursor for the GLSceneViewer
  30. GLSceneViewer1->Cursor = crNone;
  31. // and load my ugly cursor (size adjusted in design props)
  32. GLMaterialLibrary1->Materials->Items[0]->Material->Texture->Image->LoadFromFile("cursor.bmp");
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::MILoadImageClick(TObject *Sender)
  36. {
  37. if (OpenPictureDialog1->Execute())
  38. {
  39. // use the hourglass cursor, it may take some time to load the bitmap,
  40. // rescale it and generate mipmaps before sending it to OpenGL
  41. Screen->Cursor = crHourGlass;
  42. HSBitmap->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  43. // adjust hud sprite size to match that of the picture
  44. HSBitmap->SetSize(Width, Height);
  45. // adjust position, hudsprites are centered on their x, y coords
  46. HSBitmap->Position->X = Width/2;
  47. HSBitmap->Position->Y = Height/2;
  48. Screen->Cursor = crDefault;
  49. }
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  53. int X, int Y)
  54. {
  55. TColor color;
  56. // Prevents event floods on slow hardware
  57. if (!handleMouseMoves) exit;
  58. handleMouseMoves = false;
  59. // Mouse moved, adjust the position of our cursor
  60. HSCursor->Position->X = X;
  61. HSCursor->Position->Y = Y;
  62. // Update the status bar with some misc. info
  63. color =GLSceneViewer1->Buffer->GetPixelColor(X, Y);
  64. StatusBar1->SimpleText = Format("X:%4d Y:%4d, R:%3d G:%3d B:%3d",
  65. ARRAYOFCONST ((X, Y, GetRValue(color), GetGValue(color), GetBValue(color))));
  66. // Add a trail particle
  67. if (MITrail->Checked)
  68. GLParticles1->CreateParticle();
  69. // Update things now
  70. GLCadencer1->Progress();
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TForm1::GLSceneViewer1AfterRender(TObject *Sender)
  74. {
  75. handleMouseMoves = true;
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  79. const double newTime)
  80. {
  81. GLSceneViewer1->Invalidate();
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TForm1::HSParticleProgress(TObject *Sender, const double deltaTime,
  85. const double newTime)
  86. {
  87. // decrease life time / alpha
  88. HSParticle->TagFloat = HSParticle->TagFloat + deltaTime;
  89. // update alpha channel, but if no more life is left, then suicide
  90. if (HSParticle->TagFloat<0)
  91. GLParticles1->KillParticle(HSParticle);
  92. else
  93. HSParticle->AlphaChannel = HSParticle->TagFloat*0.2;
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TForm1::GLParticles1ActivateParticle(TObject *Sender, TGLBaseSceneObject *particle)
  97. {
  98. // with (particle as TGLHUDSprite) do begin
  99. // we are cadencing real-time, so these are 5 seconds
  100. particle->TagFloat = 5;
  101. // new particle stands where cursor is
  102. particle->Position->AsVector = HSCursor->Position->AsVector;
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  106. {
  107. // update FPS and sprite count
  108. miFPS->Caption = Format("%.1f FPS - %d Cursor Sprites",
  109. ARRAYOFCONST ((GLSceneViewer1->FramesPerSecond(), GLParticles1->Count)));
  110. GLSceneViewer1->ResetPerformanceMonitor();
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::MITrailClick(TObject *Sender)
  114. {
  115. // turn trails on/off
  116. MITrail->Checked = !MITrail->Checked;
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::MIExitClick(TObject *Sender)
  120. {
  121. Close();
  122. }
  123. //---------------------------------------------------------------------------