FrameMetrics.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /** FrameMetrics.cpp */
  24. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  25. #include "GameNetwork/FrameMetrics.h"
  26. #include "GameClient/Display.h"
  27. #include "GameNetwork/NetworkUtil.h"
  28. FrameMetrics::FrameMetrics()
  29. {
  30. //Added By Sadullah Nader
  31. //Initializations missing and needed
  32. m_averageFps = 0.0f;
  33. m_averageLatency = 0.0f;
  34. m_cushionIndex = 0;
  35. m_fpsListIndex = 0;
  36. m_lastFpsTimeThing = 0;
  37. m_minimumCushion = 0;
  38. m_pendingLatencies = NEW time_t[MAX_FRAMES_AHEAD];
  39. for(Int i = 0; i < MAX_FRAMES_AHEAD; i++)
  40. m_pendingLatencies[i] = 0;
  41. //
  42. m_fpsList = NEW Real[TheGlobalData->m_networkFPSHistoryLength];
  43. m_latencyList = NEW Real[TheGlobalData->m_networkLatencyHistoryLength];
  44. }
  45. FrameMetrics::~FrameMetrics() {
  46. if (m_fpsList != NULL) {
  47. delete m_fpsList;
  48. m_fpsList = NULL;
  49. }
  50. if (m_latencyList != NULL) {
  51. delete m_latencyList;
  52. m_latencyList = NULL;
  53. }
  54. if (m_pendingLatencies)
  55. {
  56. delete[] m_pendingLatencies;
  57. m_pendingLatencies = NULL;
  58. }
  59. }
  60. void FrameMetrics::init() {
  61. m_averageFps = 30;
  62. m_averageLatency = (Real)0.2;
  63. m_minimumCushion = -1;
  64. for (Int i = 0; i < TheGlobalData->m_networkFPSHistoryLength; ++i) {
  65. m_fpsList[i] = 30.0;
  66. }
  67. m_fpsListIndex = 0;
  68. for (i = 0; i < TheGlobalData->m_networkLatencyHistoryLength; ++i) {
  69. m_latencyList[i] = (Real)0.2;
  70. }
  71. m_cushionIndex = 0;
  72. }
  73. void FrameMetrics::reset() {
  74. init();
  75. }
  76. void FrameMetrics::doPerFrameMetrics(UnsignedInt frame) {
  77. // Do the measurement of the fps.
  78. time_t curTime = timeGetTime();
  79. if ((curTime - m_lastFpsTimeThing) >= 1000) {
  80. // if ((m_fpsListIndex % 16) == 0) {
  81. // DEBUG_LOG(("FrameMetrics::doPerFrameMetrics - adding %f to fps history. average before: %f ", m_fpsList[m_fpsListIndex], m_averageFps));
  82. // }
  83. m_averageFps -= ((m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // subtract out the old value from the average.
  84. m_fpsList[m_fpsListIndex] = TheDisplay->getAverageFPS();
  85. // m_fpsList[m_fpsListIndex] = TheGameClient->getFrame() - m_fpsStartingFrame;
  86. m_averageFps += ((Real)(m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // add the new value to the average.
  87. // DEBUG_LOG(("average after: %f\n", m_averageFps));
  88. ++m_fpsListIndex;
  89. m_fpsListIndex %= TheGlobalData->m_networkFPSHistoryLength;
  90. m_lastFpsTimeThing = curTime;
  91. }
  92. Int pendingLatenciesIndex = frame % MAX_FRAMES_AHEAD;
  93. m_pendingLatencies[pendingLatenciesIndex] = curTime;
  94. }
  95. void FrameMetrics::processLatencyResponse(UnsignedInt frame) {
  96. time_t curTime = timeGetTime();
  97. Int pendingIndex = frame % MAX_FRAMES_AHEAD;
  98. time_t timeDiff = curTime - m_pendingLatencies[pendingIndex];
  99. Int latencyListIndex = frame % TheGlobalData->m_networkLatencyHistoryLength;
  100. m_averageLatency -= m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
  101. m_latencyList[latencyListIndex] = (Real)timeDiff / (Real)1000; // convert to seconds from milliseconds.
  102. m_averageLatency += m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
  103. if (frame % 16 == 0) {
  104. // DEBUG_LOG(("ConnectionManager::processFrameInfoAck - average latency = %f\n", m_averageLatency));
  105. }
  106. }
  107. void FrameMetrics::addCushion(Int cushion) {
  108. ++m_cushionIndex;
  109. m_cushionIndex %= TheGlobalData->m_networkCushionHistoryLength;
  110. if (m_cushionIndex == 0) {
  111. m_minimumCushion = -1;
  112. }
  113. if ((cushion < m_minimumCushion) || (m_minimumCushion == -1)) {
  114. m_minimumCushion = cushion;
  115. }
  116. }
  117. Int FrameMetrics::getAverageFPS() {
  118. return (Int)m_averageFps;
  119. }
  120. Real FrameMetrics::getAverageLatency() {
  121. return m_averageLatency;
  122. }
  123. Int FrameMetrics::getMinimumCushion() {
  124. return m_minimumCushion;
  125. }