Append allocation and generics fixes

This commit is contained in:
Simon Lübeß
2025-01-31 13:17:12 +01:00
parent 577cca5886
commit 2f0505241f
6 changed files with 15 additions and 20 deletions
+3 -10
View File
@@ -8,7 +8,7 @@ namespace GlitchyEngine
*/ */
public class GameTime public class GameTime
{ {
private Stopwatch _stopwatch; private append Stopwatch _stopwatch = .() ~ delete:append _;
private uint64 _frameCount; private uint64 _frameCount;
@@ -31,22 +31,15 @@ namespace GlitchyEngine
/// The interval in seconds from the last frame to the current one. /// The interval in seconds from the last frame to the current one.
public float DeltaTime => (float)_frameTime.TotalSeconds; 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. * Initializes a new instance of a GameTime.
* @param startNow If set to true, the timer will start immediately. * @param startNow If set to true, the timer will start immediately.
* If set to false, the timer has to be started manually. * If set to false, the timer has to be started manually.
*/ */
[AllowAppend] [AllowAppend]
public this(bool startNow) public this(bool startNow = false)
{ {
let sw = append Stopwatch(startNow); _stopwatch.Restart();
_stopwatch = sw;
} }
/** /**
+5 -3
View File
@@ -10,7 +10,7 @@ using internal GlitchyEngine.Scripting;
class SharpClass : RefCounter class SharpClass : RefCounter
{ {
protected String _fullName ~ delete:append _; protected String _fullName ~ delete _; // TODO APPEND
protected StringView _namespace; protected StringView _namespace;
protected StringView _className; protected StringView _className;
@@ -24,7 +24,10 @@ class SharpClass : RefCounter
[AllowAppend] [AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image) 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; _fullName = fullName;
_namespace = _fullName.Substring(0, classNamespace.Length); _namespace = _fullName.Substring(0, classNamespace.Length);
@@ -66,7 +69,6 @@ class ScriptClass : SharpClass
_onCreate = (OnCreateMethod)GetMethodThunk("OnCreate"); _onCreate = (OnCreateMethod)GetMethodThunk("OnCreate");
_onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1); _onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1);
_onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy"); _onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy");
_onCollisionEnter2D = (.)GetMethodThunk("OnCollisionEnter2D", 1); _onCollisionEnter2D = (.)GetMethodThunk("OnCollisionEnter2D", 1);
_onCollisionLeave2D = (.)GetMethodThunk("OnCollisionLeave2D", 1); _onCollisionLeave2D = (.)GetMethodThunk("OnCollisionLeave2D", 1);
+2 -2
View File
@@ -14,7 +14,7 @@ using internal GlitchyEngine.Scripting;
class EngineClasses 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_ComponentRoot ~ _?.ReleaseRef();
private ScriptClass s_EntityRoot ~ _?.ReleaseRef(); private ScriptClass s_EntityRoot ~ _?.ReleaseRef();
@@ -90,7 +90,7 @@ static class ScriptEngine
private static Scene s_Context ~ _?.ReleaseRef(); private static Scene s_Context ~ _?.ReleaseRef();
private static append EngineClasses _classes = .(); private static EngineClasses _classes = new .() ~ delete _;
public static EngineClasses Classes => _classes; public static EngineClasses Classes => _classes;
+2 -2
View File
@@ -176,7 +176,7 @@ namespace GlitchyEngine.World
/** /**
* Assigns a component of type T to the specified entity and returns it. * Assigns a component of type T to the specified entity and returns it.
*/ */
public T* AssignComponent<T>(EcsEntity entity, T value = T()) where T : struct, new public T* AssignComponent<T>(EcsEntity entity, T? value = null) where T : struct, new
{ {
Log.EngineLogger.AssertDebug(IsValid(entity)); Log.EngineLogger.AssertDebug(IsValid(entity));
@@ -207,7 +207,7 @@ namespace GlitchyEngine.World
T* component = (T*)entry.Pool.Get(entity.Index); T* component = (T*)entry.Pool.Get(entity.Index);
*component = value; *component = value ?? T();
return component; return component;
} }
+2 -2
View File
@@ -92,11 +92,11 @@ namespace GlitchyEngine.World
} }
} }
public T* AddComponent<T>(T value = T()) where T: struct, new public T* AddComponent<T>(T? value = null) where T: struct, new
{ {
Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component."); Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component.");
T* component = _scene._ecsWorld.AssignComponent<T>(_entity, value); T* component = _scene._ecsWorld.AssignComponent<T>(_entity, value ?? T());
_scene.[Friend]OnComponentAdded(this, typeof(T), component); _scene.[Friend]OnComponentAdded(this, typeof(T), component);
+1 -1
View File
@@ -6,7 +6,7 @@ namespace GlitchyEngine.World
protected TransformComponent* transform => GetComponent<TransformComponent>(); protected TransformComponent* transform => GetComponent<TransformComponent>();
public T* AddComponent<T>(T value = T()) where T: struct, new public T* AddComponent<T>(T? value = null) where T: struct, new
{ {
return _entity.AddComponent<T>(value); return _entity.AddComponent<T>(value);
} }