5 Commits

Author SHA1 Message Date
4ce265e722 fixed unnecessary write to texture if no changes happened 2025-04-27 13:49:35 +02:00
572e02956c fixed benchmark placement 2025-04-27 13:18:18 +02:00
de99d54ad6 fixed brush Y dimension 2025-04-27 13:04:06 +02:00
2bcdeecb81 Merge branch 'performance-improvements' 2025-04-21 18:57:20 +02:00
7b2845ee01 added benchmark function
added .txt for benchmark results
fixed generating level message
2024-10-27 14:08:19 +01:00
2 changed files with 53 additions and 35 deletions

View File

@@ -18,6 +18,7 @@ public class Element {
protected int lastMove = 0; protected int lastMove = 0;
private bool active = false; private bool active = false;
private bool wasMovedThisTick = false;
private int lastUpdate = -1; private int lastUpdate = -1;
private Color originalColor; private Color originalColor;
@@ -42,6 +43,7 @@ public class Element {
active = value; active = value;
Chunk.SetElementActive(this, value); Chunk.SetElementActive(this, value);
Moved();
// SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f)); // SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f));
} }
} }
@@ -68,6 +70,7 @@ public class Element {
protected virtual void Tick() { protected virtual void Tick() {
Vector2I randomDirection = RandomDirectionDown(); Vector2I randomDirection = RandomDirectionDown();
wasMovedThisTick = false;
if (Chunk.IsEmpty(Position + Vector2I.Down)) if (Chunk.IsEmpty(Position + Vector2I.Down))
Chunk.Swap(this, Position + Vector2I.Down); Chunk.Swap(this, Position + Vector2I.Down);
@@ -109,9 +112,15 @@ public class Element {
if (!Main.Instance.DebugVisualization) return; if (!Main.Instance.DebugVisualization) return;
this.color = color; this.color = color;
wasMovedThisTick = true;
} }
public void Moved() { public void Moved() {
lastMove = Engine.GetFramesDrawn(); lastMove = Engine.GetFramesDrawn();
wasMovedThisTick = true;
}
public bool WasMoved() {
return wasMovedThisTick;
} }
} }

View File

@@ -69,7 +69,7 @@ public class Level {
GD.Print("benchmark"); GD.Print("benchmark");
for (int x = 0; x < chunksPerX; x++) { for (int x = 0; x < chunksPerX; x++) {
for (int y = 0; y < chunksPerY; y++) { for (int y = 0; y < chunksPerY; y++) {
WritePixel<Dirt>(x * chunkResX/2, y * chunkResY/2, 100); WritePixel<Dirt>((x * chunkResX) + chunkResX/2, (y * chunkResY) + chunkResY/2, 100);
} }
} }
} }
@@ -92,63 +92,72 @@ public class Level {
public void WritePixel<T>(int x, int y, int size) { public void WritePixel<T>(int x, int y, int size) {
int halfsize = size/2; int halfsize = size/2;
int startPtX = x % chunkResX;
int startPtY = y % chunkResY;
for (int i = -halfsize; i <= halfsize; i++) { for (int i = -halfsize; i <= halfsize; i++) {
for (int j = 0; j < 1; j++) { for (int j = -halfsize; j <= halfsize; j++) {
// for (int j = -halfsize; j <= halfsize; j++) {
// calculate in-chunk coordinates // calculate in-chunk coordinates
int inChunkX = x % chunkResX; int iteratorX = i;
int inChunkY = y % chunkResY; int iteratorY = j;
int brushX = i; int ptX = startPtX;
int brushY = j; int ptY = startPtY;
// calculate chunk to write to // calculate chunk to write to
int chunkX = x/chunkResX; int chunkX = x/chunkResX;
if (inChunkX + i < 0) {
chunkX--;
// 320 -22
brushX = chunkResX + inChunkX + i;
inChunkX = chunkResX - inChunkX;
} else if (inChunkX + i >= chunkResX) {
chunkX++;
inChunkX = 0;
brushX = i % chunkResX;
}
inChunkX += brushX;
if (chunkX < 0 || chunkX >= chunksPerX) { // left of chunk
GD.PrintErr($"Trying to write out of bounds: {x}:{y}"); if (startPtX + iteratorX < 0) {
return; chunkX--;
iteratorX = chunkResX + startPtX + iteratorX;
ptX = startPtX + iteratorX;
// right of chunk
} else if (startPtX + iteratorX >= chunkResX) {
chunkX++;
ptX = startPtX + iteratorX - chunkResX;
} else {
ptX = startPtX + iteratorX;
} }
int chunkY = y/chunkResY; int chunkY = y/chunkResY;
if (inChunkY + j < 0) {
// above chunk
if (startPtY + iteratorY < 0) {
chunkY--; chunkY--;
brushY = chunkResY - (inChunkY + j); ptY = chunkResY + (startPtY + iteratorY);
inChunkY = chunkResY - inChunkY;
} else if (inChunkY + j >= chunkResY) { // left of chunk
} else if (startPtY + iteratorY >= chunkResY) {
chunkY++; chunkY++;
inChunkY = 0; ptY = iteratorY % chunkResY;
brushY = j % chunkResY; } else {
} ptY = startPtY + iteratorY;
inChunkY += brushY;
if (chunkY < 0 || chunkY >= chunksPerY) {
GD.PrintErr($"Trying to write out of bounds: {x}:{y}");
return;
} }
chunks[chunkX, chunkY].WritePixel<T>(inChunkX, inChunkY); // ignore everything outside
if (chunkX < 0) continue;
if (chunkX > chunksPerX) continue;
if (chunkY < 0) continue;
if (chunkY > chunksPerY) continue;
chunks[chunkX, chunkY].WritePixel<T>(ptX, ptY);
} }
} }
} }
public Image DrawLevel() { public Image DrawLevel() {
// chunk
for (int cx = 0; cx < chunksPerX; cx++) { for (int cx = 0; cx < chunksPerX; cx++) {
for (int cy = 0; cy < chunksPerY; cy++) { for (int cy = 0; cy < chunksPerY; cy++) {
// pixel in chunk
for (int x = 0; x < chunkResX; x++) { for (int x = 0; x < chunkResX; x++) {
for (int y = 0; y < chunkResY; y++) { for (int y = 0; y < chunkResY; y++) {
// TODO: multithreading here! use Chunk.DrawLevel() and stitch images together // TODO: multithreading here! use Chunk.DrawLevel() and stitch images together
if (chunks[cx, cy].Elements[x, y].WasMoved())
image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx, cy].Elements[x, y].Color); image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx, cy].Elements[x, y].Color);
} }
} }