Skip to content

Commit

Permalink
Fixed unnecessary event raising on sorting
Browse files Browse the repository at this point in the history
Removed foreach.
  • Loading branch information
vchelaru committed Dec 29, 2023
1 parent b337244 commit d6b358b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/FlatRedBallServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ private static void Window_ClientSizeChanged(object sender, EventArgs e)
// If we're setting the mClientWith and mClientHeight, we need
// to adjust the cameras:
// Update cameras
foreach (Camera camera in SpriteManager.Cameras)
for (int i = 0; i < SpriteManager.Cameras.Count; i++)
{
Camera camera = SpriteManager.Cameras[i];
camera.UpdateOnResize();
}
}
Expand Down
104 changes: 52 additions & 52 deletions Engines/FlatRedBallXNA/FlatRedBall/Math/PositionedObjectList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,21 +483,21 @@ public void Shuffle()
for (int i = count - 1; i > -1; i--)
{
T objectAtI = this[i];
this.RemoveAtOneWay(i);
mInternalList.RemoveAt(i);
// count - 1 because we removed an object.
index = FlatRedBallServices.Random.Next(count - 1);
this.InsertOneWay(index, objectAtI);
mInternalList.Insert(index, objectAtI);
}

// We do this twice because this will essentially start at the last index
// and skip every other one. Doing it twice gives us much better results
for (int i = count - 1; i > -1; i--)
{
T objectAtI = this[i];
this.RemoveAtOneWay(i);
mInternalList.RemoveAt(i);
// count - 1 because we removed an object.
index = FlatRedBallServices.Random.Next(count - 1);
this.InsertOneWay(index, objectAtI);
mInternalList.Insert(index, objectAtI);
}

}
Expand Down Expand Up @@ -551,8 +551,8 @@ public void SortCameraDistanceInsersionDescending(Camera camera)
{
if (i == 1)
{
base.Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

Expand All @@ -564,14 +564,14 @@ public void SortCameraDistanceInsersionDescending(Camera camera)
if (cameraDistanceSquared <= ((this[whereCameraBelongs]).Position - camera.Position).LengthSquared())
#endif
{
base.Insert(whereCameraBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereCameraBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereCameraBelongs == 0 && cameraDistanceSquared > ((this[0]).Position - camera.Position).LengthSquared())
{
base.Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -593,23 +593,23 @@ public void SortXInsertionAscending(int firstObject, int lastObjectExclusive)
{
if (i == firstObject + 1)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > firstObject - 1; whereObjectBelongs--)
{
if ((this[i]).Position.X >= (this[whereObjectBelongs]).Position.X)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == firstObject && (this[i]).Position.X < (this[firstObject]).Position.X)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -631,23 +631,23 @@ public void SortXInsertionDescending(int firstObject, int lastObjectExclusive)
{
if (i == firstObject + 1)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > firstObject - 1; whereObjectBelongs--)
{
if ((this[i]).Position.X <= (this[whereObjectBelongs]).Position.X)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == firstObject && (this[i]).Position.X > (this[firstObject]).Position.X)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -669,23 +669,23 @@ public void SortYInsertionAscending(int firstObject, int lastObjectExclusive)
{
if (i == firstObject + 1)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > firstObject - 1; whereObjectBelongs--)
{
if ((this[i]).Position.Y >= (this[whereObjectBelongs]).Position.Y)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == firstObject && (this[i]).Position.Y < (this[firstObject]).Position.Y)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -707,23 +707,23 @@ public void SortYInsertionDescending(int firstObject, int lastObjectExclusive)
{
if (i == firstObject + 1)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > firstObject - 1; whereObjectBelongs--)
{
if ((this[i]).Position.Y <= (this[whereObjectBelongs]).Position.Y)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == firstObject && (this[i]).Position.Y > (this[firstObject]).Position.Y)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -747,23 +747,23 @@ public void SortParentYInsertionDescending(int firstObject, int lastObjectExclus
{
if (i == firstObject + 1)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > firstObject - 1; whereObjectBelongs--)
{
if (yAtI <= (this[whereObjectBelongs]).TopParent.Position.Y)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == firstObject && yAtI > (this[firstObject]).TopParent.Position.Y)
{
Insert(firstObject, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(firstObject, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand Down Expand Up @@ -824,23 +824,23 @@ public void SortZInsertionAscending()
{
if (i == 1)
{
Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > -1; whereObjectBelongs--)
{
if ((this[i]).Position.Z >= (this[whereObjectBelongs]).Position.Z)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == 0 && (this[i]).Position.Z < (this[0]).Position.Z)
{
Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand All @@ -861,23 +861,23 @@ public void SortZInsertionDescending()
{
if (i == 1)
{
Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
continue;
}

for (whereObjectBelongs = i - 2; whereObjectBelongs > -1; whereObjectBelongs--)
{
if ((this[i]).Position.Z <= (this[whereObjectBelongs]).Position.Z)
{
Insert(whereObjectBelongs + 1, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(whereObjectBelongs + 1, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
else if (whereObjectBelongs == 0 && (this[i]).Position.Z > (this[0]).Position.Z)
{
Insert(0, this[i]);
base.RemoveAtOneWay(i + 1);
mInternalList.Insert(0, this[i]);
mInternalList.RemoveAt(i + 1);
break;
}
}
Expand Down

0 comments on commit d6b358b

Please sign in to comment.