Unit1.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.Material"
  11. #pragma link "GLS.Objects"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.ShadowVolume"
  14. #pragma link "GLS.SpaceText"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::ResetGame()
  25. {
  26. float angle;
  27. // places the ball in the mat center, resets score and ball speed
  28. angle = DegToRad((float)(45+Random(90)));
  29. MakeVector(ballVector, 4*cos(angle), 4*sin(angle), 0);
  30. score = 0;
  31. gameOver = false;
  32. Ball->Position->AsVector = NullHmgPoint;
  33. }
  34. void __fastcall TForm1::FormCreate(TObject *Sender)
  35. {
  36. Randomize();
  37. GLSceneViewer1->Cursor = crNone;
  38. ResetGame();
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  42. int X, int Y)
  43. {
  44. const float
  45. cPadMinMax = 6.25;
  46. float px;
  47. // the pad's position is directly calculated from the mouse position
  48. px = (X-(GLSceneViewer1->Width/2))*0.035;
  49. if (px<-cPadMinMax)
  50. px = -cPadMinMax;
  51. else
  52. if (px>cPadMinMax)
  53. px = cPadMinMax;
  54. Pad->Position->X = px;
  55. // GLCadencer1.Reset;
  56. // update the whole stuff now!
  57. GLCadencer1->Progress();
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  61. const double newTime)
  62. {
  63. Gls::Vectorgeometry::TVector newBallPos;
  64. // gameOver is True as soon as the ball is behind the pad, but we don't end
  65. // the game immediately so the user can realize he has lost
  66. if ((!gameOver) && (deltaTime>0))
  67. {
  68. // calc expected new ball pos (if no bump occurs)
  69. // ( note : VectorCombine(v1, v2, f1, f2)=v1*f1+v2*f2 )
  70. newBallPos = VectorCombine(Ball->Position->AsVector, ballVector, 1, deltaTime);
  71. // check collision with edges
  72. if (newBallPos.X<-7.05)
  73. ballVector.X = -ballVector.X;
  74. else
  75. if (newBallPos.X>7.05)
  76. ballVector.X = -ballVector.X;
  77. else
  78. if (newBallPos.Y>4.55)
  79. ballVector.Y = -ballVector.Y;
  80. // check collision with pad
  81. if (newBallPos.Y<-4)
  82. {
  83. if ((newBallPos.X>Pad->Position->X-1.25) && (newBallPos.X<Pad->Position->X+1.25))
  84. {
  85. // when ball bumps the pad, it is accelerated and the vector
  86. // is slightly randomized
  87. ballVector.Y = -ballVector.Y;
  88. ballVector.X = ballVector.X+(Random(100)-50)/50;
  89. ballVector.Y = ballVector.Y+0.1;
  90. // ...and of course a point is scored !
  91. score++;
  92. SpaceText1->Text = Format("%.3d", ARRAYOFCONST((score)));
  93. }
  94. else
  95. {
  96. // ball missed !
  97. gameOver = true;
  98. exit;
  99. }
  100. }
  101. }
  102. // move the ball
  103. Ball->Position->AsVector = VectorCombine(Ball->Position->AsVector, ballVector, 1, deltaTime);
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  107. {
  108. // update performance monitor
  109. //%s : Name,
  110. Caption = Format("%.2f FPS", ARRAYOFCONST((GLSceneViewer1->FramesPerSecond())));
  111. GLSceneViewer1->ResetPerformanceMonitor();
  112. // display score window when game is over and the ball is well out of the board
  113. if (gameOver && (Ball->Position->Y<-6))
  114. {
  115. // stop the timer to avoid stacking up Timer events
  116. // while the user makes up his mind...
  117. Timer1->Enabled = false;
  118. if (MessageDlg("Score : "+IntToStr(score)+ " Play again ?",
  119. mtInformation, TMsgDlgButtons() << mbYes, 0)==mrYes) //esc - mbNo ?
  120. {
  121. ResetGame();
  122. Timer1->Enabled = true;
  123. }
  124. else Close();
  125. }
  126. }
  127. //---------------------------------------------------------------------------