fPongC.cpp 4.0 KB

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