Compare commits
1 Commits
performanc
...
7b2845ee01
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b2845ee01 |
@@ -1,9 +1,6 @@
|
|||||||
<Project Sdk="Godot.NET.Sdk/4.4.1">
|
<Project Sdk="Godot.NET.Sdk/4.2.2">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="benchmark.txt" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -2,8 +2,7 @@ using Godot;
|
|||||||
|
|
||||||
public partial class FPSLabel : Label
|
public partial class FPSLabel : Label
|
||||||
{
|
{
|
||||||
[Export]
|
bool ShowFPS = true;
|
||||||
public bool ShowFPS = true;
|
|
||||||
|
|
||||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
public override void _Process(double delta) {
|
public override void _Process(double delta) {
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://d3cbk8f7lckbr
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using FOU.Scripts;
|
|
||||||
using Godot;
|
|
||||||
|
|
||||||
public partial class PerfDetails : Label
|
|
||||||
{
|
|
||||||
[Export]
|
|
||||||
public bool ShowDetails = true;
|
|
||||||
|
|
||||||
public override void _Process(double delta)
|
|
||||||
{
|
|
||||||
Text = "";
|
|
||||||
if (!ShowDetails) return;
|
|
||||||
|
|
||||||
int activeElements = 0;
|
|
||||||
foreach (Chunk c in Main.Instance.Level.GetChunks())
|
|
||||||
activeElements += c.ActiveElementsCount();
|
|
||||||
|
|
||||||
Text += $"Active Elements: {activeElements}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
uid://52grhydeyy0t
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
uid://dxmkbb5f1t368
|
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
[gd_scene load_steps=5 format=3 uid="uid://cf34vk5r055dx"]
|
[gd_scene load_steps=4 format=3 uid="uid://cf34vk5r055dx"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://cmvvubxfvdca7" path="res://Scripts/Main.cs" id="1_k1i8e"]
|
[ext_resource type="Script" path="res://Scripts/Main.cs" id="1_k1i8e"]
|
||||||
[ext_resource type="Script" uid="uid://d3cbk8f7lckbr" path="res://Scenes/FPSLabel.cs" id="2_8cb7y"]
|
[ext_resource type="Script" path="res://Scenes/FPSLabel.cs" id="2_8cb7y"]
|
||||||
[ext_resource type="Script" uid="uid://dxmkbb5f1t368" path="res://Scenes/SettingsController.cs" id="3_a4w6m"]
|
[ext_resource type="Script" path="res://Scenes/SettingsController.cs" id="3_a4w6m"]
|
||||||
[ext_resource type="Script" uid="uid://52grhydeyy0t" path="res://Scenes/PerfDetails.cs" id="3_u2p48"]
|
|
||||||
|
|
||||||
[node name="Main" type="Node2D"]
|
[node name="Main" type="Node2D"]
|
||||||
script = ExtResource("1_k1i8e")
|
script = ExtResource("1_k1i8e")
|
||||||
DebugVisualization = true
|
DebugVisualization = true
|
||||||
BrushSize = 2
|
BrushSize = 2
|
||||||
RainAmount = 1.0
|
TextureResolution = 0.35
|
||||||
|
|
||||||
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
||||||
|
|
||||||
@@ -35,15 +34,6 @@ text = "FPS: 0
|
|||||||
"
|
"
|
||||||
script = ExtResource("2_8cb7y")
|
script = ExtResource("2_8cb7y")
|
||||||
|
|
||||||
[node name="PerfDetails" type="Label" parent="CanvasLayer/TopUI"]
|
|
||||||
layout_mode = 0
|
|
||||||
offset_top = 30.0
|
|
||||||
offset_right = 76.0
|
|
||||||
offset_bottom = 67.0
|
|
||||||
theme_override_font_sizes/font_size = 10
|
|
||||||
text = "Active Elements: 0"
|
|
||||||
script = ExtResource("3_u2p48")
|
|
||||||
|
|
||||||
[node name="Settings" type="VBoxContainer" parent="CanvasLayer/TopUI"]
|
[node name="Settings" type="VBoxContainer" parent="CanvasLayer/TopUI"]
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
anchors_preset = 1
|
anchors_preset = 1
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using FOU.Scripts.Elements;
|
using FOU.Scripts.Elements;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
@@ -16,9 +15,6 @@ public class Chunk {
|
|||||||
private readonly Image image;
|
private readonly Image image;
|
||||||
private readonly int sizeX;
|
private readonly int sizeX;
|
||||||
private readonly int sizeY;
|
private readonly int sizeY;
|
||||||
private readonly HashSet<Element> activeElements;
|
|
||||||
private readonly HashSet<Element> toAddElements = new();
|
|
||||||
private readonly HashSet<Element> toRemoveElements = new();
|
|
||||||
|
|
||||||
public Chunk(int x, int y, int index) {
|
public Chunk(int x, int y, int index) {
|
||||||
Index = index;
|
Index = index;
|
||||||
@@ -32,62 +28,43 @@ public class Chunk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
activeElements = new HashSet<Element>(sizeX * sizeY);
|
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
||||||
|
|
||||||
image = Image.CreateEmpty(sizeX, sizeY, false, Image.Format.Rgb8);
|
|
||||||
image.Fill(Colors.Black);
|
image.Fill(Colors.Black);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update() {
|
public void Update() {
|
||||||
foreach (Element e in activeElements)
|
for (int x = 0; x < sizeX; x++) {
|
||||||
e.Update();
|
for (int y = 0; y < sizeY; y++) {
|
||||||
|
if (Elements[x,y] != null)
|
||||||
// apply changes:
|
Elements[x,y].Update();
|
||||||
if (toRemoveElements.Count > 0) {
|
|
||||||
foreach (Element removeElement in toRemoveElements) {
|
|
||||||
activeElements.Remove(removeElement);
|
|
||||||
removeElement.ResetColor();
|
|
||||||
}
|
}
|
||||||
toRemoveElements.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toAddElements.Count > 0) {
|
|
||||||
foreach (Element addElement in toAddElements) {
|
|
||||||
activeElements.Add(addElement);
|
|
||||||
addElement.SetDebugColor(Colors.Green);
|
|
||||||
}
|
|
||||||
toAddElements.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetElementActive(Element e, bool active) {
|
public void WritePixel<T>(int x, int y, int size) {
|
||||||
if (e.GetType() == typeof(Element) && active) return;
|
int halfsize = size/2;
|
||||||
|
|
||||||
if (active) AddToChunk(e);
|
|
||||||
else RemoveFromChunk(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void WritePixel<T>(int x, int y) {
|
|
||||||
if (x < 0 || x >= sizeX || y < 0 || y >= sizeY) {
|
|
||||||
GD.PrintErr($"Out of bounds for chunk: {x}:{y}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Type type = typeof(T);
|
Type type = typeof(T);
|
||||||
object o = Activator.CreateInstance(type, x, y, this);
|
|
||||||
|
|
||||||
// check if not empty:
|
for (int i = -halfsize; i <= halfsize; i++) {
|
||||||
if (Elements[x,y].GetType() != typeof(Element)) {
|
for (int j = -halfsize; j <= halfsize; j++) {
|
||||||
return;
|
int X = Mathf.Clamp(x + i, 0, sizeX-1);
|
||||||
|
int Y = Mathf.Clamp(y + j, 0, sizeY-1);
|
||||||
|
object o = Activator.CreateInstance(type, X, Y, this);
|
||||||
|
|
||||||
|
if (Elements[X,Y].GetType() != type)
|
||||||
|
Elements[X,Y] = o as Element;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Elements[x, y].Chunk.RemoveFromChunk(Elements[x, y]);
|
public Image DrawLevel() {
|
||||||
Elements[x, y] = o as Element;
|
for (int x = 0; x < sizeX; x++) {
|
||||||
Elements[x, y].Active = true;
|
for (int y = 0; y < sizeY; y++) {
|
||||||
|
image.SetPixel(x,y, Elements[x,y].Color);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public int ActiveElementsCount() {
|
return image;
|
||||||
return activeElements.Count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Swap(Element what, Vector2I pos) {
|
public void Swap(Element what, Vector2I pos) {
|
||||||
@@ -134,12 +111,6 @@ public class Chunk {
|
|||||||
GD.PrintErr("Trying to swap null");
|
GD.PrintErr("Trying to swap null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (what.Chunk != swapTo.Chunk) {
|
|
||||||
what.Chunk.RemoveFromChunk(what);
|
|
||||||
swapTo.Chunk.AddToChunk(what);
|
|
||||||
}
|
|
||||||
|
|
||||||
Element temp = new Element(what);
|
Element temp = new Element(what);
|
||||||
|
|
||||||
what.Position = swapTo.Position;
|
what.Position = swapTo.Position;
|
||||||
@@ -151,20 +122,6 @@ public class Chunk {
|
|||||||
swapTo.Chunk.Elements[swapTo.Position.X, swapTo.Position.Y] = swapTo;
|
swapTo.Chunk.Elements[swapTo.Position.X, swapTo.Position.Y] = swapTo;
|
||||||
|
|
||||||
what.Active = true;
|
what.Active = true;
|
||||||
what.Moved();
|
|
||||||
swapTo.Active = true;
|
swapTo.Active = true;
|
||||||
swapTo.Moved();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() {
|
|
||||||
return $"Chunk {Index}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RemoveFromChunk(Element e) {
|
|
||||||
toRemoveElements.Add(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddToChunk(Element e) {
|
|
||||||
toAddElements.Add(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://r17upkbkmfiy
|
|
||||||
@@ -5,7 +5,7 @@ namespace FOU.Scripts.Elements;
|
|||||||
public class Dirt : Solid {
|
public class Dirt : Solid {
|
||||||
|
|
||||||
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
||||||
color = AddColorVariance(Colors.Brown);
|
Color = AddColorVariance(Colors.Brown);
|
||||||
Density = 10;
|
Density = 10;
|
||||||
DiffuseSpeed = 50;
|
DiffuseSpeed = 50;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://80esqy0cardd
|
|
||||||
@@ -3,50 +3,48 @@
|
|||||||
namespace FOU.Scripts.Elements;
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
public class Element {
|
public class Element {
|
||||||
|
public Color Color = Colors.Black;
|
||||||
|
|
||||||
public Chunk Chunk;
|
|
||||||
public Vector2I Position;
|
public Vector2I Position;
|
||||||
|
public Chunk Chunk;
|
||||||
|
public int Density;
|
||||||
|
public int LastUpdate = -1;
|
||||||
|
public int LastMove = 0;
|
||||||
|
|
||||||
protected int Density;
|
public int DiffuseSpeed = 10;
|
||||||
protected int DiffuseSpeed = 10;
|
public const int MAX_DIFFUSE_SPEED = 100;
|
||||||
protected Color color = Colors.Black;
|
public const int STEPS_UNTIL_INACTIVE = 500;
|
||||||
protected const int MAX_DIFFUSE_SPEED = 100;
|
|
||||||
protected const int STEPS_UNTIL_INACTIVE = 500;
|
|
||||||
|
|
||||||
protected const float MAX_COLOR_VARIANCE = 0.1f;
|
protected const float MAX_COLOR_VARIANCE = 0.1f;
|
||||||
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
||||||
protected int lastMove = 0;
|
|
||||||
|
|
||||||
private bool active = false;
|
private bool active = true;
|
||||||
private int lastUpdate = -1;
|
|
||||||
private Color originalColor;
|
private Color originalColor;
|
||||||
|
|
||||||
public Element(Element e) {
|
public Element(Element e) {
|
||||||
Position = e.Position;
|
Position = e.Position;
|
||||||
Chunk = e.Chunk;
|
Chunk = e.Chunk;
|
||||||
lastMove = Engine.GetFramesDrawn();
|
LastMove = Engine.GetFramesDrawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element(int x, int y, Chunk chunk) {
|
public Element(int x, int y, Chunk chunk) {
|
||||||
Position.X = x;
|
Position.X = x;
|
||||||
Position.Y = y;
|
Position.Y = y;
|
||||||
Chunk = chunk;
|
Chunk = chunk;
|
||||||
lastMove = Engine.GetFramesDrawn();
|
LastMove = Engine.GetFramesDrawn();
|
||||||
Active = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Active {
|
public bool Active {
|
||||||
get => active;
|
get => active;
|
||||||
set {
|
set {
|
||||||
if (active == value) return;
|
|
||||||
|
|
||||||
active = value;
|
active = value;
|
||||||
Chunk.SetElementActive(this, value);
|
|
||||||
// SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color Color => color;
|
if (active)
|
||||||
|
Color = originalColor;
|
||||||
|
else if (Main.Instance.DebugVisualization)
|
||||||
|
Color = new Color(0.2f, 0.2f, 0.2f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// base update method, checks if anything is to do at all
|
/// base update method, checks if anything is to do at all
|
||||||
@@ -56,14 +54,14 @@ public class Element {
|
|||||||
public virtual bool Update() {
|
public virtual bool Update() {
|
||||||
if (!Active) return false;
|
if (!Active) return false;
|
||||||
|
|
||||||
if (lastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
|
if (LastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
|
||||||
lastUpdate = Engine.GetFramesDrawn();
|
LastUpdate = Engine.GetFramesDrawn();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return $"{GetType()} {Position}[{Chunk.Index}] {active}";
|
return $"{GetType()} {Position}[{Chunk.Index}]";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Tick() {
|
protected virtual void Tick() {
|
||||||
@@ -101,17 +99,4 @@ public class Element {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetColor() {
|
|
||||||
color = originalColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetDebugColor(Color color) {
|
|
||||||
if (!Main.Instance.DebugVisualization) return;
|
|
||||||
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Moved() {
|
|
||||||
lastMove = Engine.GetFramesDrawn();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://b5lme1c4rx15n
|
|
||||||
@@ -9,7 +9,7 @@ public abstract class Liquid : Element {
|
|||||||
|
|
||||||
public override bool Update() {
|
public override bool Update() {
|
||||||
if (!base.Update()) return false;
|
if (!base.Update()) return false;
|
||||||
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
||||||
|
|
||||||
Tick();
|
Tick();
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://bqx36wisy7127
|
|
||||||
@@ -7,7 +7,7 @@ public abstract class Solid : Element {
|
|||||||
|
|
||||||
public override bool Update() {
|
public override bool Update() {
|
||||||
if (!base.Update()) return false;
|
if (!base.Update()) return false;
|
||||||
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
||||||
|
|
||||||
Tick();
|
Tick();
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://ur56t06r7n4l
|
|
||||||
@@ -5,7 +5,7 @@ namespace FOU.Scripts.Elements;
|
|||||||
public class Water : Liquid {
|
public class Water : Liquid {
|
||||||
|
|
||||||
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
||||||
color = AddColorVariance(Colors.Blue);
|
Color = AddColorVariance(Colors.Blue);
|
||||||
Density = 1;
|
Density = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://k8id5egjc7x8
|
|
||||||
102
Scripts/Level.cs
102
Scripts/Level.cs
@@ -11,45 +11,46 @@ public class Level {
|
|||||||
private Chunk[,] chunks;
|
private Chunk[,] chunks;
|
||||||
private Image image;
|
private Image image;
|
||||||
|
|
||||||
private int chunksPerX;
|
private int chunksX;
|
||||||
private int chunksPerY;
|
private int chunksY;
|
||||||
|
|
||||||
// per chunk:
|
// per chunk:
|
||||||
private int chunkResX;
|
private int chunkResX;
|
||||||
private int chunkResY;
|
private int chunkResY;
|
||||||
|
|
||||||
|
private bool enableRain = false;
|
||||||
private float rainAmount = 0;
|
private float rainAmount = 0;
|
||||||
|
|
||||||
public Level(Main main, int sizeX, int sizeY) {
|
public Level(Main main, int sizeX, int sizeY) {
|
||||||
|
GD.Print($"Generating level ({sizeX}:{sizeY}) with {chunksX*chunksY} chunks ({chunkResX} * {chunkResY})");
|
||||||
|
|
||||||
Resolution = new Vector2I(sizeX, sizeY);
|
Resolution = new Vector2I(sizeX, sizeY);
|
||||||
chunksPerX = main.ChunksPerAxis;
|
chunksX = main.ChunksPerAxis;
|
||||||
chunksPerY = main.ChunksPerAxis;
|
chunksY = main.ChunksPerAxis;
|
||||||
|
|
||||||
chunkResX = sizeX / chunksPerX;
|
chunkResX = sizeX / chunksX;
|
||||||
chunkResY = sizeY / chunksPerY;
|
chunkResY = sizeY / chunksY;
|
||||||
|
|
||||||
GD.Print($"Generating level ({sizeX}:{sizeY}) with {chunksPerX*chunksPerY} chunks ({chunkResX} * {chunkResY})");
|
|
||||||
|
|
||||||
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
||||||
|
|
||||||
chunks = new Chunk[chunksPerX, chunksPerY];
|
chunks = new Chunk[chunksX, chunksY];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
// create all chunks
|
// create all chunks
|
||||||
for (int x = 0; x < chunksPerX; x++) {
|
for (int x = 0; x < chunksX; x++) {
|
||||||
for (int y = 0; y < chunksPerY; y++) {
|
for (int y = 0; y < chunksY; y++) {
|
||||||
chunks[x, y] = new Chunk(chunkResX, chunkResY, index++);
|
chunks[x, y] = new Chunk(chunkResX, chunkResY, index++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign neighbors
|
// assign neighbors
|
||||||
for (int x = 0; x < chunksPerX; x++) {
|
for (int x = 0; x < chunksX; x++) {
|
||||||
for (int y = 0; y < chunksPerY; y++) {
|
for (int y = 0; y < chunksY; y++) {
|
||||||
|
|
||||||
if (y > 0) chunks[x, y].NeighborN = chunks[x, y-1];
|
if (y > 0) chunks[x, y].NeighborN = chunks[x, y-1];
|
||||||
if (y < chunksPerY-1) chunks[x, y].NeighborS = chunks[x, y+1];
|
if (y < chunksY-1) chunks[x, y].NeighborS = chunks[x, y+1];
|
||||||
|
|
||||||
if (x > 0) chunks[x, y].NeighborE = chunks[x-1, y];
|
if (x > 0) chunks[x, y].NeighborE = chunks[x-1, y];
|
||||||
if (x < chunksPerX-1) chunks[x, y].NeighborW = chunks[x+1, y];
|
if (x < chunksX-1) chunks[x, y].NeighborW = chunks[x+1, y];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,19 +66,6 @@ public class Level {
|
|||||||
rainAmount = amount;
|
rainAmount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartBenchmark() {
|
|
||||||
GD.Print("benchmark");
|
|
||||||
for (int x = 0; x < chunksPerX; x++) {
|
|
||||||
for (int y = 0; y < chunksPerY; y++) {
|
|
||||||
WritePixel<Dirt>(x * chunkResX/2, y * chunkResY/2, 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Chunk[,] GetChunks() {
|
|
||||||
return chunks;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MakeItRain() {
|
private void MakeItRain() {
|
||||||
if (rainAmount < .1f) return;
|
if (rainAmount < .1f) return;
|
||||||
|
|
||||||
@@ -85,70 +73,20 @@ public class Level {
|
|||||||
|
|
||||||
for (int i = 0; i <= rainDrops; i++) {
|
for (int i = 0; i <= rainDrops; i++) {
|
||||||
if (GD.Randf() < rainAmount)
|
if (GD.Randf() < rainAmount)
|
||||||
WritePixel<Water>((int)(GD.Randi() % (chunkResX * chunksPerX)), 0, 1);
|
WritePixel<Water>((int)(GD.Randi() % (chunkResX * chunksX)), 0, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WritePixel<T>(int x, int y, int size) {
|
public void WritePixel<T>(int x, int y, int size) {
|
||||||
int halfsize = size/2;
|
chunks[x/chunkResX, y/chunkResY].WritePixel<T>(x % chunkResX, y % chunkResY, size);
|
||||||
|
|
||||||
for (int i = -halfsize; i <= halfsize; i++) {
|
|
||||||
for (int j = 0; j < 1; j++) {
|
|
||||||
// for (int j = -halfsize; j <= halfsize; j++) {
|
|
||||||
// calculate in-chunk coordinates
|
|
||||||
int inChunkX = x % chunkResX;
|
|
||||||
int inChunkY = y % chunkResY;
|
|
||||||
int brushX = i;
|
|
||||||
int brushY = j;
|
|
||||||
|
|
||||||
// calculate chunk to write to
|
|
||||||
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) {
|
|
||||||
GD.PrintErr($"Trying to write out of bounds: {x}:{y}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int chunkY = y/chunkResY;
|
|
||||||
if (inChunkY + j < 0) {
|
|
||||||
chunkY--;
|
|
||||||
brushY = chunkResY - (inChunkY + j);
|
|
||||||
inChunkY = chunkResY - inChunkY;
|
|
||||||
} else if (inChunkY + j >= chunkResY) {
|
|
||||||
chunkY++;
|
|
||||||
inChunkY = 0;
|
|
||||||
brushY = j % chunkResY;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Image DrawLevel() {
|
public Image DrawLevel() {
|
||||||
for (int cx = 0; cx < chunksPerX; cx++) {
|
for (int cx = 0; cx < chunksX; cx++) {
|
||||||
for (int cy = 0; cy < chunksPerY; cy++) {
|
for (int cy = 0; cy < chunksY; cy++) {
|
||||||
|
|
||||||
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
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://bkwbjagt8s01s
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Reflection;
|
||||||
using FOU.Scripts;
|
using FOU.Scripts;
|
||||||
using FOU.Scripts.Elements;
|
using FOU.Scripts.Elements;
|
||||||
using Godot;
|
using Godot;
|
||||||
@@ -8,31 +9,35 @@ public partial class Main : Node2D {
|
|||||||
[Export] public int BrushSize = 5;
|
[Export] public int BrushSize = 5;
|
||||||
[Export] public float TextureResolution = 0.5f;
|
[Export] public float TextureResolution = 0.5f;
|
||||||
[Export] public int ChunksPerAxis = 2;
|
[Export] public int ChunksPerAxis = 2;
|
||||||
[Export] public float RainAmount = 0;
|
|
||||||
|
[Export]
|
||||||
|
public float RainAmount {
|
||||||
|
get => rainAmount;
|
||||||
|
set {
|
||||||
|
rainAmount = value;
|
||||||
|
mLevel.SetRainAmount(rainAmount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Main Instance;
|
public static Main Instance;
|
||||||
public Level Level;
|
|
||||||
|
|
||||||
private TextureRect mLevelDrawer;
|
private TextureRect mLevelDrawer;
|
||||||
|
private Level mLevel;
|
||||||
private bool enableRain;
|
private bool enableRain;
|
||||||
private float rainAmount;
|
private float rainAmount;
|
||||||
|
|
||||||
public override void _Ready() {
|
public override void _Ready() {
|
||||||
Level = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution),
|
mLevel = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution),
|
||||||
(int)(GetViewportRect().Size.Y * TextureResolution));
|
(int)(GetViewportRect().Size.Y * TextureResolution));
|
||||||
Level.SetRainAmount(rainAmount);
|
mLevel.SetRainAmount(rainAmount);
|
||||||
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
|
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
|
||||||
|
|
||||||
Instance = this;
|
Instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _PhysicsProcess(double delta) {
|
|
||||||
base._PhysicsProcess(delta);
|
|
||||||
Level.Update();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _Process(double delta) {
|
public override void _Process(double delta) {
|
||||||
mLevelDrawer.Texture = ImageTexture.CreateFromImage(Level.DrawLevel());
|
mLevel.Update();
|
||||||
|
mLevelDrawer.Texture = ImageTexture.CreateFromImage(mLevel.DrawLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _UnhandledInput(InputEvent @event) {
|
public override void _UnhandledInput(InputEvent @event) {
|
||||||
@@ -41,15 +46,11 @@ public partial class Main : Node2D {
|
|||||||
if (eventMouseButton.IsPressed()) {
|
if (eventMouseButton.IsPressed()) {
|
||||||
Vector2 mouse = GetViewport().GetMousePosition();
|
Vector2 mouse = GetViewport().GetMousePosition();
|
||||||
|
|
||||||
float mappedX = mouse.X / (GetViewportRect().Size.X / Level.Resolution.X);
|
float mappedX = mouse.X / (GetViewportRect().Size.X / mLevel.Resolution.X);
|
||||||
float mappedY = mouse.Y / (GetViewportRect().Size.Y / Level.Resolution.Y);
|
float mappedY = mouse.Y / (GetViewportRect().Size.Y / mLevel.Resolution.Y);
|
||||||
|
|
||||||
Level.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize);
|
mLevel.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize);
|
||||||
}
|
}
|
||||||
} else if (@event is InputEventKey keyEvent && keyEvent.Pressed) {
|
} else base._UnhandledInput(@event);
|
||||||
if (keyEvent.Keycode == Key.F9)
|
|
||||||
Level.StartBenchmark();
|
|
||||||
}
|
|
||||||
else base._UnhandledInput(@event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
uid://cmvvubxfvdca7
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# Benchmark
|
|
||||||
|
|
||||||
Start from editor not rider!
|
|
||||||
|
|
||||||
## Settings
|
|
||||||
|
|
||||||
- Texture Resolution: 0.5
|
|
||||||
- CHunks per Axis: 2
|
|
||||||
- Rain Amount: 0
|
|
||||||
|
|
||||||
## Measurements:
|
|
||||||
|
|
||||||
### original (no optimization):
|
|
||||||
|
|
||||||
- idle: 42 - 44 fps
|
|
||||||
- benchmark: 24 fps minimum
|
|
||||||
|
|
||||||
### performance-improvements (71db6513f22094e7dd67bd32c7ab86b326f9ad9e)
|
|
||||||
|
|
||||||
- idle before: 89
|
|
||||||
- benchmark: 33
|
|
||||||
- idle after: 75
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ config_version=5
|
|||||||
|
|
||||||
config/name="FOU"
|
config/name="FOU"
|
||||||
run/main_scene="res://Scenes/main.tscn"
|
run/main_scene="res://Scenes/main.tscn"
|
||||||
config/features=PackedStringArray("4.4", "C#", "Forward Plus")
|
config/features=PackedStringArray("4.2", "C#", "Forward Plus")
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
[dotnet]
|
[dotnet]
|
||||||
|
|||||||
Reference in New Issue
Block a user