Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RLBotCS/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Channels;
using System.Threading.Channels;
using Bridge;
using Bridge.TCP;
using Microsoft.Extensions.Logging;
Expand Down
36 changes: 22 additions & 14 deletions RLBotCS/ManagerTools/Rendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bridge.Controller;
using Bridge.State;
using Bridge.TCP;
using Microsoft.Extensions.Logging;
using RLBot.Flat;
using RLBotCS.Conversion;
using Color = System.Drawing.Color;
Expand All @@ -16,6 +17,9 @@ public class Rendering(TcpMessenger tcpMessenger)
public const int ResolutionHeightPixels = 1080;
public const int FontWidthPixels = 10;
public const int FontHeightPixels = 20;
public const int RectangleStringMaxLength = 30_000;

private readonly ILogger Logger = Logging.GetLogger("Rendering");

private readonly RenderingCommandQueue _renderingCommandQueue = new(tcpMessenger);
private readonly Dictionary<int, Dictionary<int, List<ushort>>> _clientRenderTracker = [];
Expand Down Expand Up @@ -130,7 +134,7 @@ private ushort SendRect3D(Rect3DT rect3Dt, GameState gameState)
/// scaled the string is scaled with returned scaling factor. We use this as a hack to created filled rectangles
/// for rectangle rendering.
/// </summary>
/// <returns></returns>
/// <returns>The rectangle string and the font scaling</returns>
private (string, float) MakeFakeRectangleString(int width, int height)
{
int Gcd(int a, int b)
Expand All @@ -147,27 +151,31 @@ int Gcd(int a, int b)
return a | b;
}

// We use the greatest common divisor to simplify the fraction (width/height)
// minimizing the characters needed for the rectangle.
int gcd = Gcd(width, height);
int cols = (width / gcd) * (FontHeightPixels / FontWidthPixels);
int rows = height / gcd;
float scale = gcd / (float)FontHeightPixels;

StringBuilder str = new StringBuilder(cols * rows + rows);
for (int r = 0; r < rows; r++)
if (cols + rows > RectangleStringMaxLength)
{
for (int c = 0; c < cols; c++)
{
str.Append(' ');
}
// The width-height ratio has resulting in a very long string.
// TODO: Consider an approximate solution as backup. Do we ever hit this case though?
Logger.LogWarning(
"A rendered rectangle requires more characters than budget allows. Consider different width-height ratio."
);
}

if (r + 1 < rows)
{
str.Append('\n');
}
StringBuilder str = new StringBuilder(cols + rows);
for (int c = 0; c < cols; c++)
{
str.Append(' ');
}
for (int r = 0; r < rows - 1; r++)
{
str.Append('\n');
}

return (str.ToString(), gcd / (float)FontHeightPixels);
return (str.ToString(), scale);
}

public void AddRenderGroup(
Expand Down
Loading