Skip to content

Commit 8869eb3

Browse files
committed
create renderer label type
1 parent fdf9fb4 commit 8869eb3

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

source/Components/IsDestination.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Numerics;
3-
using Unmanaged;
43

54
namespace Rendering.Components
65
{
@@ -10,7 +9,7 @@ public struct IsDestination
109
public uint height;
1110
public Vector4 region;
1211
public Vector4 clearColor;
13-
public ASCIIText256 rendererLabel;
12+
public RendererLabel rendererLabel;
1413

1514
public readonly uint Area => width * height;
1615

@@ -33,7 +32,7 @@ public IsDestination()
3332
throw new NotImplementedException();
3433
}
3534
#endif
36-
public IsDestination(Vector2 size, ASCIIText256 rendererLabel)
35+
public IsDestination(Vector2 size, RendererLabel rendererLabel)
3736
{
3837
width = (uint)size.X;
3938
height = (uint)size.Y;
@@ -42,7 +41,7 @@ public IsDestination(Vector2 size, ASCIIText256 rendererLabel)
4241
this.rendererLabel = rendererLabel;
4342
}
4443

45-
public IsDestination(uint width, uint height, ASCIIText256 rendererLabel, Vector4 region, Vector4 clearColor)
44+
public IsDestination(uint width, uint height, RendererLabel rendererLabel, Vector4 region, Vector4 clearColor)
4645
{
4746
this.width = width;
4847
this.height = height;
@@ -51,18 +50,13 @@ public IsDestination(uint width, uint height, ASCIIText256 rendererLabel, Vector
5150
this.rendererLabel = rendererLabel;
5251
}
5352

54-
public IsDestination(Vector2 size, ASCIIText256 rendererLabel, Vector4 region, Vector4 clearColor)
53+
public IsDestination(Vector2 size, RendererLabel rendererLabel, Vector4 region, Vector4 clearColor)
5554
{
5655
width = (uint)size.X;
5756
height = (uint)size.Y;
5857
this.region = region;
5958
this.clearColor = clearColor;
6059
this.rendererLabel = rendererLabel;
6160
}
62-
63-
public readonly Vector2 GetSizeAsVector2()
64-
{
65-
return new(width, height);
66-
}
6761
}
6862
}

source/Destination.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,11 @@ public readonly float AspectRatio
6262

6363
public readonly ref Vector4 Region => ref GetComponent<IsDestination>().region;
6464
public readonly ref Vector4 ClearColor => ref GetComponent<IsDestination>().clearColor;
65-
public readonly ref ASCIIText256 RendererLabel => ref GetComponent<IsDestination>().rendererLabel;
65+
public readonly ref RendererLabel RendererLabel => ref GetComponent<IsDestination>().rendererLabel;
6666
public readonly int ExtensionCount => GetArrayLength<DestinationExtension>();
6767
public readonly ReadOnlySpan<DestinationExtension> Extensions => GetArray<DestinationExtension>().AsSpan();
6868

69-
public Destination(World world, Vector2 size, ASCIIText256 rendererLabel)
70-
{
71-
this.world = world;
72-
value = world.CreateEntity(new IsDestination(size, rendererLabel));
73-
CreateArray<DestinationExtension>();
74-
}
75-
76-
public Destination(World world, Vector2 size, ReadOnlySpan<char> rendererLabel)
69+
public Destination(World world, Vector2 size, RendererLabel rendererLabel)
7770
{
7871
this.world = world;
7972
value = world.CreateEntity(new IsDestination(size, rendererLabel));

source/RendererLabel.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Unmanaged;
3+
4+
namespace Rendering
5+
{
6+
public readonly struct RendererLabel : IEquatable<RendererLabel>
7+
{
8+
public readonly long hash;
9+
10+
public RendererLabel(ReadOnlySpan<char> text)
11+
{
12+
hash = text.GetLongHashCode();
13+
}
14+
15+
public readonly override bool Equals(object? obj)
16+
{
17+
return obj is RendererLabel label && Equals(label);
18+
}
19+
20+
public readonly bool Equals(RendererLabel other)
21+
{
22+
return hash == other.hash;
23+
}
24+
25+
public override int GetHashCode()
26+
{
27+
return (int)hash;
28+
}
29+
30+
public static bool operator ==(RendererLabel left, RendererLabel right)
31+
{
32+
return left.Equals(right);
33+
}
34+
35+
public static bool operator !=(RendererLabel left, RendererLabel right)
36+
{
37+
return !(left == right);
38+
}
39+
40+
public static implicit operator RendererLabel(string text)
41+
{
42+
return new RendererLabel(text.AsSpan());
43+
}
44+
}
45+
}

tests/DestinationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void VerifyCompliance()
1111
using World world = CreateWorld();
1212
Destination destination = new(world, new Vector2(1920, 1080), "Renderer");
1313
Assert.That(destination.IsCompliant, Is.True);
14-
Assert.That(destination.RendererLabel.ToString(), Is.EqualTo("Renderer"));
14+
Assert.That(destination.RendererLabel, Is.EqualTo(new RendererLabel("Renderer")));
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)