Skip to content

Commit

Permalink
Convert lambdas in MapRenderer to local functions. Reduces allocation…
Browse files Browse the repository at this point in the history
…s that need to be collected by GC
  • Loading branch information
ethanmoffat committed May 5, 2023
1 parent 5363746 commit 7c5cda4
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions EndlessClient/Rendering/Map/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private void DrawToSpriteBatch(SpriteBatch spriteBatch, GameTime gameTime)
spriteBatch.Begin();

var drawLoc = _gridDrawCoordinateCalculator.CalculateGroundLayerRenderTargetDrawCoordinates();
var offset = _quakeState.Map(qs => qs.Offset).Match(some: o => o, none: () => 0);
var offset = _quakeState.Map(GetOffset).ValueOr(0);

lock (_rt_locker_)
{
Expand All @@ -378,11 +378,13 @@ private void DrawToSpriteBatch(SpriteBatch spriteBatch, GameTime gameTime)

spriteBatch.End();
}

static float GetOffset(MapQuakeState quakeState) => quakeState.Offset;
}

private void DrawBaseLayers(SpriteBatch spriteBatch)
{
var offset = _quakeState.Map(qs => qs.Offset).Match(some: o => o, none: () => 0);
var offset = _quakeState.Map(GetOffset).ValueOr(0);

var renderBounds = _mapRenderDistanceCalculator.CalculateRenderBounds(_characterProvider.MainCharacter, _currentMapProvider.CurrentMap);

Expand All @@ -399,6 +401,8 @@ private void DrawBaseLayers(SpriteBatch spriteBatch)
}
}
}

static float GetOffset(MapQuakeState quakeState) => quakeState.Offset;
}

private int GetAlphaForCoordinates(int objX, int objY, EOLib.Domain.Character.Character character)
Expand All @@ -422,20 +426,22 @@ private int GetAlphaForCoordinates(int objX, int objY, EOLib.Domain.Character.Ch
}
else if (metric == _mapTransitionState.TransitionMetric)
{
_mapTransitionState.StartTime
.MatchSome(startTime =>
{
var ms = (DateTime.Now - startTime).TotalMilliseconds;
alpha = (int)Math.Round(ms / TRANSITION_TIME_MS * 255);

if (ms / TRANSITION_TIME_MS >= 1)
_mapTransitionState = new MapTransitionState(Option.Some(DateTime.Now), _mapTransitionState.TransitionMetric + 1);
});
alpha = _mapTransitionState.StartTime.Map(HandleStartTime).ValueOr(alpha);
}
else
alpha = 0;

return alpha;

int HandleStartTime(DateTime startTime)
{
var ms = (DateTime.Now - startTime).TotalMilliseconds;

if (ms / TRANSITION_TIME_MS >= 1)
_mapTransitionState = new MapTransitionState(Option.Some(DateTime.Now), _mapTransitionState.TransitionMetric + 1);

return (int)Math.Round(ms / TRANSITION_TIME_MS * 255);
}
}

private void ResizeGameWindow(object sender, EventArgs e)
Expand Down

0 comments on commit 7c5cda4

Please sign in to comment.