From 2f0505241f743dd67a579ede2cd2240b465c1a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Fri, 31 Jan 2025 13:17:12 +0100 Subject: [PATCH] Append allocation and generics fixes --- GlitchyEngine/src/GameTime.bf | 13 +++---------- GlitchyEngine/src/Scripting/ScriptClass.bf | 8 +++++--- GlitchyEngine/src/Scripting/ScriptEngine.bf | 4 ++-- GlitchyEngine/src/World/EcsWorld.bf | 4 ++-- GlitchyEngine/src/World/Entity.bf | 4 ++-- GlitchyEngine/src/World/ScriptableEntity.bf | 2 +- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/GlitchyEngine/src/GameTime.bf b/GlitchyEngine/src/GameTime.bf index f623d6b..2a620c8 100644 --- a/GlitchyEngine/src/GameTime.bf +++ b/GlitchyEngine/src/GameTime.bf @@ -8,7 +8,7 @@ namespace GlitchyEngine */ public class GameTime { - private Stopwatch _stopwatch; + private append Stopwatch _stopwatch = .() ~ delete:append _; private uint64 _frameCount; @@ -31,22 +31,15 @@ namespace GlitchyEngine /// The interval in seconds from the last frame to the current one. public float DeltaTime => (float)_frameTime.TotalSeconds; - /** - * Initializes a new instance of a GameTime. - */ - [AllowAppend] - public this() : this(false) {} - /** * Initializes a new instance of a GameTime. * @param startNow If set to true, the timer will start immediately. * If set to false, the timer has to be started manually. */ [AllowAppend] - public this(bool startNow) + public this(bool startNow = false) { - let sw = append Stopwatch(startNow); - _stopwatch = sw; + _stopwatch.Restart(); } /** diff --git a/GlitchyEngine/src/Scripting/ScriptClass.bf b/GlitchyEngine/src/Scripting/ScriptClass.bf index cbbf039..6276f90 100644 --- a/GlitchyEngine/src/Scripting/ScriptClass.bf +++ b/GlitchyEngine/src/Scripting/ScriptClass.bf @@ -10,7 +10,7 @@ using internal GlitchyEngine.Scripting; class SharpClass : RefCounter { - protected String _fullName ~ delete:append _; + protected String _fullName ~ delete _; // TODO APPEND protected StringView _namespace; protected StringView _className; @@ -24,7 +24,10 @@ class SharpClass : RefCounter [AllowAppend] public this(StringView classNamespace, StringView className, MonoImage* image) { - String fullName = append $"{classNamespace}.{className}"; + // TODO APPEND + String fullName = new String(classNamespace.Length + 1 + className.Length); + //append $"{classNamespace}.{className}"; + fullName.AppendF($"{classNamespace}.{className}"); _fullName = fullName; _namespace = _fullName.Substring(0, classNamespace.Length); @@ -66,7 +69,6 @@ class ScriptClass : SharpClass _onCreate = (OnCreateMethod)GetMethodThunk("OnCreate"); _onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1); _onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy"); - _onCollisionEnter2D = (.)GetMethodThunk("OnCollisionEnter2D", 1); _onCollisionLeave2D = (.)GetMethodThunk("OnCollisionLeave2D", 1); diff --git a/GlitchyEngine/src/Scripting/ScriptEngine.bf b/GlitchyEngine/src/Scripting/ScriptEngine.bf index 52bdfa9..bfbe278 100644 --- a/GlitchyEngine/src/Scripting/ScriptEngine.bf +++ b/GlitchyEngine/src/Scripting/ScriptEngine.bf @@ -14,7 +14,7 @@ using internal GlitchyEngine.Scripting; class EngineClasses { - // TODO: Not all of there are actually ScriptClasses (actually none of them are...) + // TODO: Not all of these are actually ScriptClasses (actually none of them are...) private ScriptClass s_ComponentRoot ~ _?.ReleaseRef(); private ScriptClass s_EntityRoot ~ _?.ReleaseRef(); @@ -90,7 +90,7 @@ static class ScriptEngine private static Scene s_Context ~ _?.ReleaseRef(); - private static append EngineClasses _classes = .(); + private static EngineClasses _classes = new .() ~ delete _; public static EngineClasses Classes => _classes; diff --git a/GlitchyEngine/src/World/EcsWorld.bf b/GlitchyEngine/src/World/EcsWorld.bf index 5477a40..ebaf4b6 100644 --- a/GlitchyEngine/src/World/EcsWorld.bf +++ b/GlitchyEngine/src/World/EcsWorld.bf @@ -176,7 +176,7 @@ namespace GlitchyEngine.World /** * Assigns a component of type T to the specified entity and returns it. */ - public T* AssignComponent(EcsEntity entity, T value = T()) where T : struct, new + public T* AssignComponent(EcsEntity entity, T? value = null) where T : struct, new { Log.EngineLogger.AssertDebug(IsValid(entity)); @@ -207,7 +207,7 @@ namespace GlitchyEngine.World T* component = (T*)entry.Pool.Get(entity.Index); - *component = value; + *component = value ?? T(); return component; } diff --git a/GlitchyEngine/src/World/Entity.bf b/GlitchyEngine/src/World/Entity.bf index 8961ac0..38b3e6b 100644 --- a/GlitchyEngine/src/World/Entity.bf +++ b/GlitchyEngine/src/World/Entity.bf @@ -92,11 +92,11 @@ namespace GlitchyEngine.World } } - public T* AddComponent(T value = T()) where T: struct, new + public T* AddComponent(T? value = null) where T: struct, new { Log.EngineLogger.AssertDebug(!HasComponent(), scope $"Entity already has component."); - T* component = _scene._ecsWorld.AssignComponent(_entity, value); + T* component = _scene._ecsWorld.AssignComponent(_entity, value ?? T()); _scene.[Friend]OnComponentAdded(this, typeof(T), component); diff --git a/GlitchyEngine/src/World/ScriptableEntity.bf b/GlitchyEngine/src/World/ScriptableEntity.bf index df06cd4..22cbfdc 100644 --- a/GlitchyEngine/src/World/ScriptableEntity.bf +++ b/GlitchyEngine/src/World/ScriptableEntity.bf @@ -6,7 +6,7 @@ namespace GlitchyEngine.World protected TransformComponent* transform => GetComponent(); - public T* AddComponent(T value = T()) where T: struct, new + public T* AddComponent(T? value = null) where T: struct, new { return _entity.AddComponent(value); }