Skip to content

Commit 4690bd8

Browse files
basic svg working
1 parent eb1ad56 commit 4690bd8

28 files changed

+1102
-395
lines changed

Flamui.Components/Ui.Input.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Runtime.CompilerServices;
22
using Flamui.UiElements;
3-
using Silk.NET.Input;
43

54
namespace Flamui.Components;
65

@@ -37,7 +36,7 @@ public static UiText Input(this Ui ui, ref string text, bool hasFocus = false, I
3736
{
3837
var offsets = l.CharOffsets;
3938

40-
for (int i = 0; i < offsets.Count; i++)
39+
for (int i = 0; i < offsets.Length; i++)
4140
{
4241
if (offsets[i] > pos.X)
4342
{

Flamui.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flamui.Tests", "Flamui.Test
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.LayoutTest", "Sample.LayoutTest\Sample.LayoutTest.csproj", "{6D448843-4468-4B7E-9E65-D075861C3AA7}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground", "Playground\Playground.csproj", "{A0FB0B21-F3A3-4D96-A5E4-96DCF4EEF151}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
5860
{6D448843-4468-4B7E-9E65-D075861C3AA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{6D448843-4468-4B7E-9E65-D075861C3AA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{6D448843-4468-4B7E-9E65-D075861C3AA7}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{A0FB0B21-F3A3-4D96-A5E4-96DCF4EEF151}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{A0FB0B21-F3A3-4D96-A5E4-96DCF4EEF151}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{A0FB0B21-F3A3-4D96-A5E4-96DCF4EEF151}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{A0FB0B21-F3A3-4D96-A5E4-96DCF4EEF151}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(NestedProjects) = preSolution
6369
{6D448843-4468-4B7E-9E65-D075861C3AA7} = {9C94658C-B59B-4FC7-AAC4-837C0D481C45}

Flamui/Arena.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,28 @@
33

44
namespace Flamui;
55

6-
public class Arena : IDisposable
6+
7+
public interface IAllocator
8+
{
9+
public unsafe T* Allocate<T>(T value) where T : unmanaged;
10+
11+
public Slice<T> AllocateSlice<T>(int count) where T : unmanaged;
12+
}
13+
14+
// public class GCAllocator : IAllocator
15+
// {
16+
// public unsafe T* Allocate<T>(T value) where T : unmanaged
17+
// {
18+
//
19+
// }
20+
//
21+
// public Slice<T> AllocateSlice<T>(int count) where T : unmanaged
22+
// {
23+
// throw new NotImplementedException();
24+
// }
25+
// }
26+
27+
public class Arena : IAllocator, IDisposable
728
{
829
public VirtualBuffer VirtualBuffer;
930

@@ -75,7 +96,7 @@ public unsafe Slice<T> AllocateSlice<T>(int count) where T : unmanaged
7596
return new Slice<T>
7697
{
7798
Items = a,
78-
Count = count
99+
Length = count
79100
};
80101
}
81102
}

Flamui/ArenaChunkedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct Chunk
185185

186186
public bool IsFull()
187187
{
188-
return Count >= Items.Count;
188+
return Count >= Items.Length;
189189
}
190190
}
191191

Flamui/ArenaList.cs

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System.Collections;
2+
using System.Diagnostics;
23

34
namespace Flamui;
45

@@ -8,32 +9,39 @@ namespace Flamui;
89
/// Basic dynamic array on the arena
910
/// </summary>
1011
/// <typeparam name="T"></typeparam>
11-
public struct ArenaList<T> where T : unmanaged
12+
public struct ArenaList<T> : IEnumerable<T> where T : unmanaged
1213
{
13-
private Arena? _arena;
1414
private Slice<T> _backingSlice;
1515
private int _backingSliceAllocateNum;
16+
private int _arenaHash;
1617

1718
public int Count;
18-
public int Capacity => _backingSlice.Count;
19+
public int Capacity => _backingSlice.Length;
1920

20-
public ArenaList(Arena arena, int initialCapacity)
21+
public ArenaList(Arena arena, int initialCapacity = 1)
2122
{
23+
_arenaHash = arena.GetHashCode();
24+
2225
ArgumentNullException.ThrowIfNull(arena);
2326
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(initialCapacity);
2427

25-
_arena = arena;
26-
_backingSlice = _arena.AllocateSlice<T>(initialCapacity);
27-
_backingSliceAllocateNum = _arena.AllocNum;
28+
_backingSlice = arena.AllocateSlice<T>(initialCapacity);
29+
_backingSliceAllocateNum = arena.AllocNum;
2830
}
2931

3032
public unsafe void Add(T value)
3133
{
32-
_arena ??= Ui.Arena;
34+
var arena = Ui.Arena;
35+
36+
if (_arenaHash == 0)
37+
_arenaHash = arena.GetHashCode();
38+
39+
Debug.Assert(arena.GetHashCode() == _arenaHash);
40+
3341
if (Capacity == 0) //in case the ArenaList isn't initialized via the constructor
3442
{
35-
_backingSlice = _arena.AllocateSlice<T>(1);
36-
_backingSliceAllocateNum = _arena.AllocNum;
43+
_backingSlice = arena.AllocateSlice<T>(1);
44+
_backingSliceAllocateNum = arena.AllocNum;
3745
}
3846

3947
Debug.Assert(Count <= Capacity);
@@ -44,16 +52,16 @@ public unsafe void Add(T value)
4452

4553
//todo, this isn't really elegant or easy to understand, would be better to have a method directly on the
4654
//arena, that can tell you if a slice is at the end of the arena, and therefore allows *zero* cost resize
47-
if (_backingSliceAllocateNum == _arena.AllocNum)
55+
if (_backingSliceAllocateNum == arena.AllocNum)
4856
{
49-
_arena.AllocateSlice<T>(_backingSlice.Count);
50-
_backingSlice = new Slice<T>(_backingSlice.Items, _backingSlice.Count * 2);
51-
_backingSliceAllocateNum = _arena.AllocNum;
57+
arena.AllocateSlice<T>(_backingSlice.Length);
58+
_backingSlice = new Slice<T>(_backingSlice.Items, _backingSlice.Length * 2);
59+
_backingSliceAllocateNum = arena.AllocNum;
5260
}
5361
else
5462
{
55-
var newSlice = _arena.AllocateSlice<T>(Capacity * 2);
56-
_backingSliceAllocateNum = _arena.AllocNum;
63+
var newSlice = arena.AllocateSlice<T>(Capacity * 2);
64+
_backingSliceAllocateNum = arena.AllocNum;
5765

5866
_backingSlice.Span.CopyTo(newSlice.Span);
5967
_backingSlice = newSlice;
@@ -64,26 +72,81 @@ public unsafe void Add(T value)
6472
Count++;
6573
}
6674

67-
public T this[int index]
75+
public ref T this[int index]
6876
{
6977
get
7078
{
7179
if (index >= Count)
7280
throw new IndexOutOfRangeException($"Index {index} isn't inside Count {Count}");
7381

74-
return _backingSlice[index];
82+
return ref _backingSlice[index];
7583
}
76-
set
77-
{
78-
if (index >= Count)
79-
throw new IndexOutOfRangeException();
84+
}
8085

81-
_backingSlice[index] = value;
86+
public ref T Last()
87+
{
88+
if (Count == 0)
89+
{
90+
throw new Exception("Cannot get last element of empty ArenaList");
8291
}
92+
93+
return ref this[Count - 1];
8394
}
8495

8596
public Slice<T> AsSlice()
8697
{
8798
return _backingSlice.SubSlice(0, Count);
8899
}
100+
101+
public Enumerator GetEnumerator()
102+
{
103+
return new Enumerator(this);
104+
}
105+
106+
IEnumerator<T> IEnumerable<T>.GetEnumerator()
107+
{
108+
return GetEnumerator();
109+
}
110+
111+
IEnumerator IEnumerable.GetEnumerator()
112+
{
113+
return GetEnumerator();
114+
}
115+
116+
public struct Enumerator : IEnumerator<T>
117+
{
118+
private readonly ArenaList<T> _arenaList;
119+
private T _current;
120+
private int _currentIndex;
121+
122+
public T Current => _current;
123+
124+
object IEnumerator.Current => _current;
125+
126+
public Enumerator(ArenaList<T> arenaList)
127+
{
128+
_arenaList = arenaList;
129+
}
130+
131+
public void Dispose()
132+
{
133+
}
134+
135+
public bool MoveNext()
136+
{
137+
if (_currentIndex == _arenaList.Count - 1)
138+
return false;
139+
140+
_currentIndex++;
141+
_current = _arenaList[_currentIndex];
142+
143+
return true;
144+
}
145+
146+
public void Reset()
147+
{
148+
_current = default;
149+
_currentIndex = 0;
150+
}
151+
}
89152
}

Flamui/ArenaString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public struct ArenaString : IEquatable<ArenaString> //todo implement IEnumerable
8888
// private ArenaStringBuilder _arenaStringBuilder;
8989
private Slice<char> _slice;
9090

91-
public int Length => _slice.Count;
91+
public int Length => _slice.Length;
9292

9393
public ArenaString(Slice<char> slice)
9494
{

Flamui/Drawing/FontLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public record struct GlyphCacheHash
111111
public class FontAtlas
112112
{
113113
public required ScaledFont Font;
114-
public required int AtlasWidth;
115-
public required int AtlasHeight;
114+
public required uint AtlasWidth;
115+
public required uint AtlasHeight;
116116

117117
public GpuTexture GpuTexture;
118118

0 commit comments

Comments
 (0)