Skip to content

Commit 67ceef1

Browse files
committed
Faster FlipVertically
1 parent 45eb5b5 commit 67ceef1

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Source/Client/Networking/SteamIntegration.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using LudeonTK;
45
using Multiplayer.Client.Networking;
56
using Multiplayer.Client.Util;
67
using Multiplayer.Client.Windows;
@@ -128,6 +129,14 @@ public static class SteamImages
128129
{
129130
private static readonly Dictionary<int, Texture2D> Cache = new();
130131

132+
[DebugAction(category = MpDebugActions.MultiplayerCategory, name = "Clear image cache",
133+
allowedGameStates = AllowedGameStates.Entry)]
134+
private static void ClearCache()
135+
{
136+
foreach (var tex in Cache.Values) UnityEngine.Object.Destroy(tex);
137+
Cache.Clear();
138+
}
139+
131140
public static Texture2D GetTexture(int id, bool force = false)
132141
{
133142
if (Cache.TryGetValue(id, out Texture2D tex) && !force)
@@ -162,16 +171,14 @@ public static Texture2D GetTexture(int id, bool force = false)
162171
private static void FlipVertically(Texture2D tex)
163172
{
164173
var pixels = tex.GetPixels32();
174+
var buf = new Color32[tex.width];
165175

166176
for (int y = 0; y < tex.height / 2; y++)
167177
{
168-
for (int x = 0; x < tex.width; x++)
169-
{
170-
int top = y * tex.width + x;
171-
int bottom = (tex.height - y - 1) * tex.width + x;
172-
173-
(pixels[top], pixels[bottom]) = (pixels[bottom], pixels[top]);
174-
}
178+
var reversedY = tex.height - y - 1;
179+
Array.Copy(pixels, y * tex.width, buf, 0, tex.width);
180+
Array.Copy(pixels, reversedY * tex.width, pixels, y * tex.width, tex.width);
181+
Array.Copy(buf, 0, pixels, reversedY * tex.width, tex.width);
175182
}
176183

177184
tex.SetPixels32(pixels);

0 commit comments

Comments
 (0)