bandwidthgraph.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. ** Command & Conquer Renegade(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/bandwidthgraph.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/02/02 2:29a $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "bandwidthgraph.h"
  36. #include "assets.h"
  37. #include "font3d.h"
  38. #include "render2d.h"
  39. #include "cnetwork.h"
  40. #include "devoptions.h"
  41. #include "packetmgr.h"
  42. #include "serversettings.h"
  43. #include "consolemode.h"
  44. //
  45. // Class statics
  46. //
  47. Render2DTextClass * cBandwidthGraph::PTextRenderer = NULL;
  48. Font3DInstanceClass * cBandwidthGraph::PFont = NULL;
  49. int cBandwidthGraph::BandwidthScaler = 50000;
  50. float cBandwidthGraph::YPosition = 0;
  51. float cBandwidthGraph::BarHeight = 0;
  52. float cBandwidthGraph::BarWidth = 0;
  53. float cBandwidthGraph::YIncrement = 0;
  54. #define COLOR_BLACK 0xFF000000
  55. #define COLOR_WHITE 0xFFFFFFFF
  56. #define COLOR_RED 0xFFFF0000
  57. #define COLOR_GREEN 0xFF48FF48
  58. //-----------------------------------------------------------------------------
  59. void
  60. cBandwidthGraph::Onetime_Init
  61. (
  62. void
  63. )
  64. {
  65. WWDEBUG_SAY(("cBandwidthGraph::Onetime_Init\n"));
  66. bool can_render = ConsoleBox.Is_Exclusive() ? false : true;
  67. if (can_render) {
  68. WWASSERT(WW3DAssetManager::Get_Instance() != NULL);
  69. PFont = WW3DAssetManager::Get_Instance()->Get_Font3DInstance("FONT6x8.TGA");
  70. WWASSERT(PFont != NULL);
  71. PFont->Set_Mono_Spaced();
  72. SET_REF_OWNER(PFont);
  73. PTextRenderer = new Render2DTextClass(PFont);
  74. PTextRenderer->Set_Coordinate_Range(Render2DClass::Get_Screen_Resolution());
  75. BarWidth = Render2DClass::Get_Screen_Resolution().Width() / 10.0f;
  76. BarHeight = PTextRenderer->Peek_Font()->Char_Height();
  77. YIncrement = BarHeight * 1.25f;
  78. }
  79. // This needs to be done somewhere else - we need a good value whether we have the graph or not.
  80. //PacketManager.Set_Stats_Sampling_Frequency_Delay(1000);
  81. //PacketManager.Set_Stats_Sampling_Frequency_Delay(250);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void
  85. cBandwidthGraph::Onetime_Shutdown
  86. (
  87. void
  88. )
  89. {
  90. WWDEBUG_SAY(("cBandwidthGraph::Onetime_Close\n"));
  91. if (PTextRenderer != NULL)
  92. {
  93. delete PTextRenderer;
  94. PTextRenderer = NULL;
  95. }
  96. if (PFont != NULL)
  97. {
  98. PFont->Release_Ref();
  99. PFont = NULL;
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. void
  104. cBandwidthGraph::Bandwidth_Graph
  105. (
  106. StringClass & label,
  107. int bps,
  108. int target_bps,
  109. float bandwidth_multiplier,
  110. float average_priority,
  111. bool is_loading
  112. )
  113. {
  114. if (PTextRenderer == NULL) {
  115. return;
  116. }
  117. WWASSERT(PTextRenderer != NULL);
  118. float x1 = 0;
  119. float x2 = bps / (float) BandwidthScaler * BarWidth;
  120. float y1 = YPosition;
  121. float y2 = y1 + BarHeight;
  122. //
  123. // Bandwidth used (graph)
  124. //
  125. PTextRenderer->Draw_Block(RectClass(x1, y1, x2, y2), COLOR_RED);
  126. PTextRenderer->Set_Location(Vector2(2, YPosition));
  127. PTextRenderer->Draw_Text(label);
  128. //
  129. // Bandwidth used (text)
  130. //
  131. StringClass text;
  132. text.Format("%d", bps);
  133. PTextRenderer->Set_Location(Vector2(200, YPosition));
  134. PTextRenderer->Draw_Text(text);
  135. //
  136. // Bandwidth budgeted (text)
  137. //
  138. if (target_bps >= 0)
  139. {
  140. StringClass text;
  141. text.Format("%d", target_bps);
  142. PTextRenderer->Set_Location(Vector2(245, YPosition));
  143. PTextRenderer->Draw_Text(text);
  144. float target_x = target_bps / (float) BandwidthScaler * BarWidth;
  145. PTextRenderer->Draw_Block(RectClass(target_x - 1, y1, target_x + 1, y2));
  146. }
  147. //
  148. // Bandwidth multiplier and average object priority
  149. //
  150. if (bandwidth_multiplier >= 0.0f)
  151. {
  152. //
  153. // Graph mark
  154. //
  155. //float tp = threshold_priority * Render2DClass::Get_Screen_Resolution().Width();
  156. //PTextRenderer->Draw_Block(RectClass(tp - 1, y1, tp + 1, y2), COLOR_GREEN);
  157. //
  158. // Text
  159. //
  160. // Bandwidth multiplier
  161. if (is_loading) {
  162. text.Format("BM:%-8.6f Ld", bandwidth_multiplier);
  163. } else {
  164. text.Format("BM:%-8.6f", bandwidth_multiplier);
  165. }
  166. PTextRenderer->Set_Location(Vector2(400, YPosition));
  167. PTextRenderer->Draw_Text(text);
  168. // Average object priority on this client
  169. text.Format("AP:%-8.6f", average_priority);
  170. PTextRenderer->Set_Location(Vector2(500, YPosition));
  171. PTextRenderer->Draw_Text(text);
  172. }
  173. YPosition += YIncrement;
  174. }
  175. //-----------------------------------------------------------------------------
  176. void
  177. cBandwidthGraph::Think
  178. (
  179. void
  180. )
  181. {
  182. bool bail = true;
  183. #ifdef WWDEBUG
  184. if (cDevOptions::ShowBandwidth.Is_True())
  185. {
  186. bail = false;
  187. }
  188. #endif // WWDEBUG
  189. if (bail)
  190. {
  191. return;
  192. }
  193. PacketManager.Update_Stats();
  194. if (PTextRenderer == NULL) {
  195. return;
  196. }
  197. WWASSERT(PTextRenderer != NULL);
  198. PTextRenderer->Reset();
  199. //
  200. // Draw the scale at the top
  201. //
  202. YPosition = 50;
  203. float x1 = 0;
  204. float x2 = 0;
  205. float y1 = YPosition;
  206. float y2 = y1 + BarHeight;
  207. int i = 0;
  208. do {
  209. x2 = x1 + BarWidth;
  210. PTextRenderer->Draw_Block(RectClass(x1, y1, x2, y2), (i % 2) ? COLOR_WHITE : COLOR_BLACK);
  211. x1 = x2;
  212. i++;
  213. } while (x2 < Render2DClass::Get_Screen_Resolution().Width());
  214. //
  215. // Label the scale
  216. //
  217. StringClass text;
  218. text.Format("BW / %d", BandwidthScaler);
  219. PTextRenderer->Set_Location(Vector2(2, YPosition - 10));
  220. PTextRenderer->Draw_Text(text);
  221. YPosition += YIncrement;
  222. DWORD bps = 0;
  223. DWORD target_bps = 0;
  224. //
  225. // Client bandwidth
  226. //
  227. if (cNetwork::I_Am_Client() && cNetwork::PClientConnection->Is_Established())
  228. {
  229. text.Format("c%d->s", cNetwork::Get_My_Id());
  230. bps = PacketManager.Get_Compressed_Bandwidth_Out(&cNetwork::Get_Client_Rhost()->Get_Address());
  231. target_bps = cNetwork::Get_Client_Rhost()->Get_Target_Bps();
  232. Bandwidth_Graph(text, bps, target_bps, -1.0f, -1.0f, false); //cNetwork::Get_Client_Rhost()->Get_Threshold_Priority());
  233. text.Format("c%d<-s", cNetwork::Get_My_Id());
  234. bps = PacketManager.Get_Compressed_Bandwidth_In(&cNetwork::Get_Client_Rhost()->Get_Address());
  235. Bandwidth_Graph(text, bps, -1, -1, -1.0f, false);
  236. }
  237. //
  238. // Server bandwidth
  239. //
  240. if (cNetwork::PServerConnection != NULL)
  241. {
  242. text.Format("s->c*");
  243. bps = PacketManager.Get_Total_Compressed_Bandwidth_Out();
  244. target_bps = cNetwork::PServerConnection->Get_Bandwidth_Budget_Out();
  245. Bandwidth_Graph(text, bps, target_bps, -1, -1.0f, false);
  246. text.Format("s<-c*");
  247. bps = PacketManager.Get_Total_Compressed_Bandwidth_In();
  248. Bandwidth_Graph(text, bps, -1, -1, -1.0f, false);
  249. for (int i = cNetwork::PServerConnection->Get_Min_RHost(); i <= cNetwork::PServerConnection->Get_Max_RHost(); i++)
  250. {
  251. cRemoteHost * p_rhost = cNetwork::Get_Server_Rhost(i);
  252. if (p_rhost != NULL)
  253. {
  254. bool is_loading = p_rhost->Get_Flood();
  255. text.Format("s->c%d", i);
  256. bps = PacketManager.Get_Compressed_Bandwidth_Out(&p_rhost->Get_Address());
  257. target_bps = p_rhost->Get_Target_Bps();
  258. Bandwidth_Graph(text, bps, target_bps, p_rhost->Get_Bandwidth_Multiplier(), p_rhost->Get_Average_Priority(), is_loading);
  259. text.Format("s<-c%d", i);
  260. bps = PacketManager.Get_Compressed_Bandwidth_In(&p_rhost->Get_Address());
  261. Bandwidth_Graph(text, bps, -1, -1, -1.0f, false);
  262. }
  263. }
  264. }
  265. }
  266. //-----------------------------------------------------------------------------
  267. void
  268. cBandwidthGraph::Render
  269. (
  270. void
  271. )
  272. {
  273. bool bail = true;
  274. #ifdef WWDEBUG
  275. if (cDevOptions::ShowBandwidth.Is_True())
  276. {
  277. bail = false;
  278. }
  279. #endif // WWDEBUG
  280. if (bail)
  281. {
  282. return;
  283. }
  284. if (PTextRenderer == NULL) {
  285. return;
  286. }
  287. WWASSERT(PTextRenderer != NULL);
  288. PTextRenderer->Render();
  289. }