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