AtomicEngine.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using AtomicPlayer;
  5. namespace AtomicEngine
  6. {
  7. public static class Atomic
  8. {
  9. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  10. private static extern int atomicsharp_initialize ();
  11. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  12. private static extern bool atomicsharp_runframe ();
  13. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  14. private static extern IntPtr csb_AtomicEngine_GetSubsystem(string name);
  15. static Atomic()
  16. {
  17. Initialize ();
  18. }
  19. public static void Run()
  20. {
  21. while (Atomic.RunFrame ()) {
  22. }
  23. }
  24. public static bool RunFrame()
  25. {
  26. GC.Collect();
  27. GC.WaitForPendingFinalizers();
  28. GC.Collect();
  29. NativeCore.ReleaseExpiredNativeReferences ();
  30. return atomicsharp_runframe ();
  31. }
  32. public static void Initialize()
  33. {
  34. ContainerModule.Initialize ();
  35. CoreModule.Initialize ();
  36. IOModule.Initialize ();
  37. ResourceModule.Initialize ();
  38. GraphicsModule.Initialize ();
  39. SceneModule.Initialize ();
  40. AtomicPlayer.PlayerModule.Initialize ();
  41. atomicsharp_initialize ();
  42. initSubsystems ();
  43. }
  44. static Dictionary<Type, RefCounted> subSystems = new Dictionary<Type, RefCounted>();
  45. static private void registerSubsystem (RefCounted subsystem)
  46. {
  47. subSystems[subsystem.GetType()] = subsystem;
  48. }
  49. static public T GetSubsystem<T>() where T : RefCounted
  50. {
  51. return (T) subSystems [typeof(T)];
  52. }
  53. static private void initSubsystems()
  54. {
  55. registerSubsystem (NativeCore.WrapNative<Player> (csb_AtomicEngine_GetSubsystem("Player")));
  56. }
  57. }
  58. public static partial class Constants
  59. {
  60. public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
  61. }
  62. public partial class RefCounted
  63. {
  64. protected RefCounted (IntPtr native)
  65. {
  66. nativeInstance = native;
  67. }
  68. public IntPtr nativeInstance;
  69. static public void _AddRef(IntPtr native)
  70. {
  71. csb_Atomic_RefCounted_AddRef(native);
  72. }
  73. static public void _ReleaseRef(IntPtr native)
  74. {
  75. csb_Atomic_RefCounted_ReleaseRef(native);
  76. }
  77. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  78. public static extern IntPtr csb_RefCounted_GetClassID (IntPtr self);
  79. }
  80. static class NativeCore
  81. {
  82. // given an existing instance classID, construct the managed instance, with downcast support (ask for Component, get StaticModel for example)
  83. public static Dictionary<IntPtr, Func<IntPtr, RefCounted>> nativeClassIDToManagedConstructor = new Dictionary<IntPtr, Func<IntPtr, RefCounted>>();
  84. public static Dictionary<IntPtr, WeakReference> nativeLookup = new Dictionary<IntPtr, WeakReference> ();
  85. public static IntPtr RegisterNative (IntPtr native, RefCounted r)
  86. {
  87. var w = new WeakReference (r);
  88. NativeCore.nativeLookup [native] = w;
  89. RefCounted._AddRef (native);
  90. return native;
  91. }
  92. public static void ReleaseExpiredNativeReferences()
  93. {
  94. List<IntPtr> released = new List<IntPtr> ();
  95. foreach(KeyValuePair<IntPtr, WeakReference> entry in nativeLookup)
  96. {
  97. if (entry.Value.Target == null || !entry.Value.IsAlive) {
  98. released.Add (entry.Key);
  99. } else {
  100. }
  101. }
  102. foreach (IntPtr native in released) {
  103. RefCounted._ReleaseRef(native);
  104. nativeLookup.Remove (native);
  105. }
  106. }
  107. // wraps an existing native instance, with downcast support
  108. public static T WrapNative<T> (IntPtr native) where T:RefCounted
  109. {
  110. if (native == IntPtr.Zero)
  111. return null;
  112. WeakReference w;
  113. // first see if we're already available
  114. if (nativeLookup.TryGetValue (native, out w)) {
  115. if (w.IsAlive) {
  116. // we're alive!
  117. return (T)w.Target;
  118. } else {
  119. // we were seen before, but have since been GC'd, remove!
  120. nativeLookup.Remove (native);
  121. }
  122. }
  123. IntPtr classID = RefCounted.csb_RefCounted_GetClassID (native);
  124. // and store, with downcast support for instance Component -> StaticModel
  125. w = new WeakReference (nativeClassIDToManagedConstructor[classID](native));
  126. NativeCore.nativeLookup [native] = w;
  127. // store a ref, so native side will not be released while we still have a reference in managed code
  128. RefCounted._AddRef (native);
  129. return (T) w.Target;
  130. }
  131. }
  132. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  133. public struct Vector3
  134. {
  135. public Vector3 (float x, float y, float z)
  136. {
  137. this.x = x;
  138. this.y = y;
  139. this.z = z;
  140. }
  141. public override string ToString()
  142. {
  143. return x + ", " + y + ", " + z;
  144. }
  145. public float x;
  146. public float y;
  147. public float z;
  148. }
  149. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  150. public struct Vector4
  151. {
  152. }
  153. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  154. public struct Vector2
  155. {
  156. }
  157. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  158. public struct Quaternion
  159. {
  160. }
  161. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  162. public struct Color
  163. {
  164. }
  165. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  166. public struct IntRect
  167. {
  168. }
  169. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  170. public struct IntVector2
  171. {
  172. }
  173. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  174. public struct BoundingBox
  175. {
  176. }
  177. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  178. public struct Rect
  179. {
  180. }
  181. }