From 4ce265e7224b470432250cda9993a879b187fb62 Mon Sep 17 00:00:00 2001 From: rogo Date: Sun, 27 Apr 2025 13:49:35 +0200 Subject: [PATCH] fixed unnecessary write to texture if no changes happened --- Scripts/Elements/Element.cs | 9 +++++++++ Scripts/Level.cs | 5 ++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 62fe24c..1a1a77f 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -18,6 +18,7 @@ public class Element { protected int lastMove = 0; private bool active = false; + private bool wasMovedThisTick = false; private int lastUpdate = -1; private Color originalColor; @@ -42,6 +43,7 @@ public class Element { active = value; Chunk.SetElementActive(this, value); + Moved(); // SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f)); } } @@ -68,6 +70,7 @@ public class Element { protected virtual void Tick() { Vector2I randomDirection = RandomDirectionDown(); + wasMovedThisTick = false; if (Chunk.IsEmpty(Position + Vector2I.Down)) Chunk.Swap(this, Position + Vector2I.Down); @@ -109,9 +112,15 @@ public class Element { if (!Main.Instance.DebugVisualization) return; this.color = color; + wasMovedThisTick = true; } public void Moved() { lastMove = Engine.GetFramesDrawn(); + wasMovedThisTick = true; + } + + public bool WasMoved() { + return wasMovedThisTick; } } diff --git a/Scripts/Level.cs b/Scripts/Level.cs index 666b7ed..8beb952 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -157,9 +157,8 @@ public class Level { for (int y = 0; y < chunkResY; y++) { // TODO: multithreading here! use Chunk.DrawLevel() and stitch images together - Element e = chunks[cx, cy].Elements[x, y]; - if (e.WasMoved()) - image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, e.Color); + if (chunks[cx, cy].Elements[x, y].WasMoved()) + image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx, cy].Elements[x, y].Color); } } }