Optimize arena rendering and simulation loop to 60 FPS

This commit is contained in:
Meta Vens 2026-12-04 22:42:21 +01:00
commit 0f36eed3a0
12 changed files with 1810 additions and 231 deletions

View file

@ -55,8 +55,11 @@ public partial class InventoryGridControl : Control
private HardpointSpec?[,] _hardpointLookup = new HardpointSpec?[0, 0];
private float[,] _prevArmor = new float[0, 0];
private float[,] _prevTemp = new float[0, 0];
private float[,] _hitPulse = new float[0, 0];
private int _lastSelectedCargoIndex = int.MinValue;
private int _lastMountedSig = int.MinValue;
private Vector2I? _hoveredCell;
private bool _thermalEnabled;
private float _thermalBlend;
@ -217,17 +220,42 @@ public partial class InventoryGridControl : Control
if (gridIdentityChanged)
{
EnsureBackingBuffers(grid);
}
else
{
DetectHits(grid);
_lastSelectedCargoIndex = selectedCargoIndex;
_lastMountedSig = ComputeMountedSignature(grid);
SnapshotTemperatures(grid);
PackDataTexture(grid);
PackTempTexture(grid);
FlushTextures();
SyncRectUniform();
_labelsOverlay.QueueRedraw();
return;
}
PackDataTexture(grid);
PackTempTexture(grid);
FlushTextures();
SyncRectUniform();
_labelsOverlay.QueueRedraw();
bool dataChanged = DetectHits(grid);
int mountedSig = ComputeMountedSignature(grid);
if (_lastSelectedCargoIndex != selectedCargoIndex || _lastMountedSig != mountedSig)
{
_lastSelectedCargoIndex = selectedCargoIndex;
_lastMountedSig = mountedSig;
dataChanged = true;
}
bool tempChanged = HasTemperatureChanged(grid);
if (dataChanged)
{
PackDataTexture(grid);
_labelsOverlay.QueueRedraw();
}
if (tempChanged)
{
SnapshotTemperatures(grid);
PackTempTexture(grid);
}
if (dataChanged || tempChanged)
{
FlushTextures();
}
}
public void TriggerHitPulse(int x, int y, float intensity = 1f)
@ -332,12 +360,14 @@ public partial class InventoryGridControl : Control
}
_prevArmor = new float[w, h];
_prevTemp = new float[w, h];
_hitPulse = new float[w, h];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
_prevArmor[x, y] = grid.Armor[x, y];
_prevTemp[x, y] = grid.Temperature[x, y];
}
}
_lastHitSequence = grid.HitSequence;
@ -365,8 +395,9 @@ public partial class InventoryGridControl : Control
}
}
private void DetectHits(ChassisGrid grid)
private bool DetectHits(ChassisGrid grid)
{
bool changed = false;
if (grid.HitSequence != _lastHitSequence)
{
_lastHitSequence = grid.HitSequence;
@ -374,6 +405,7 @@ public partial class InventoryGridControl : Control
{
_hitPulse[grid.LastHitX, grid.LastHitY] = 1f;
}
changed = true;
}
for (int y = 0; y < _gridHeight; y++)
@ -384,8 +416,55 @@ public partial class InventoryGridControl : Control
if (arm < _prevArmor[x, y] - 0.05f)
{
_hitPulse[x, y] = 1f;
_prevArmor[x, y] = arm;
changed = true;
}
_prevArmor[x, y] = arm;
else if (MathF.Abs(arm - _prevArmor[x, y]) > 0.01f)
{
_prevArmor[x, y] = arm;
changed = true;
}
}
}
return changed;
}
private static int ComputeMountedSignature(ChassisGrid grid)
{
var hc = new HashCode();
hc.Add(grid.Mounted.Count);
for (int i = 0; i < grid.Mounted.Count; i++)
{
var m = grid.Mounted[i];
hc.Add(m.HardpointId);
hc.Add(m.Spec.Id);
hc.Add(m.CurrentPerformance > 0.05f);
}
return hc.ToHashCode();
}
private bool HasTemperatureChanged(ChassisGrid grid)
{
for (int y = 0; y < _gridHeight; y++)
{
for (int x = 0; x < _gridWidth; x++)
{
if (MathF.Abs(grid.Temperature[x, y] - _prevTemp[x, y]) >= 0.25f)
{
return true;
}
}
}
return false;
}
private void SnapshotTemperatures(ChassisGrid grid)
{
for (int y = 0; y < _gridHeight; y++)
{
for (int x = 0; x < _gridWidth; x++)
{
_prevTemp[x, y] = grid.Temperature[x, y];
}
}
}