AtomicEngine.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using AtomicPlayer;
  6. namespace AtomicEngine
  7. {
  8. public static class Atomic
  9. {
  10. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  11. private static extern int atomicsharp_initialize ();
  12. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  13. private static extern bool atomicsharp_runframe ();
  14. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  15. private static extern IntPtr csb_AtomicEngine_GetSubsystem(string name);
  16. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  17. private static extern uint csb_Atomic_StringToStringHash(string name);
  18. public static uint StringToStringHash(string value)
  19. {
  20. return csb_Atomic_StringToStringHash (value);
  21. }
  22. static Atomic()
  23. {
  24. try {
  25. Initialize ();
  26. }
  27. catch (Exception e)
  28. {
  29. Console.WriteLine(e.ToString());
  30. throw;
  31. }
  32. }
  33. public static void Run()
  34. {
  35. while (Atomic.RunFrame ()) {
  36. }
  37. }
  38. public static bool RunFrame()
  39. {
  40. GC.Collect();
  41. GC.WaitForPendingFinalizers();
  42. GC.Collect();
  43. NativeCore.ReleaseExpiredNativeReferences ();
  44. return atomicsharp_runframe ();
  45. }
  46. public static void RegisterAssemblyComponents(Assembly assembly)
  47. {
  48. ComponentCore.RegisterAssemblyComponents (assembly);
  49. }
  50. public static void Initialize()
  51. {
  52. ContainerModule.Initialize ();
  53. CoreModule.Initialize ();
  54. MathModule.Initialize ();
  55. EngineModule.Initialize ();
  56. InputModule.Initialize ();
  57. IOModule.Initialize ();
  58. ResourceModule.Initialize ();
  59. AudioModule.Initialize ();
  60. GraphicsModule.Initialize ();
  61. SceneModule.Initialize ();
  62. Atomic2DModule.Initialize ();
  63. Atomic3DModule.Initialize ();
  64. NavigationModule.Initialize ();
  65. NetworkModule.Initialize ();
  66. PhysicsModule.Initialize ();
  67. EnvironmentModule.Initialize ();
  68. UIModule.Initialize ();
  69. AtomicPlayer.PlayerModule.Initialize ();
  70. AtomicInterop.Initialize ();
  71. atomicsharp_initialize ();
  72. initSubsystems ();
  73. }
  74. static Dictionary<Type, RefCounted> subSystems = new Dictionary<Type, RefCounted>();
  75. static private void registerSubsystem (RefCounted subsystem)
  76. {
  77. subSystems[subsystem.GetType()] = subsystem;
  78. }
  79. static public T GetSubsystem<T>() where T : RefCounted
  80. {
  81. return (T) subSystems [typeof(T)];
  82. }
  83. static private void initSubsystems()
  84. {
  85. registerSubsystem (NativeCore.WrapNative<Player> (csb_AtomicEngine_GetSubsystem("Player")));
  86. registerSubsystem (NativeCore.WrapNative<Graphics> (csb_AtomicEngine_GetSubsystem("Graphics")));
  87. registerSubsystem (NativeCore.WrapNative<Renderer> (csb_AtomicEngine_GetSubsystem("Renderer")));
  88. registerSubsystem (NativeCore.WrapNative<ResourceCache> (csb_AtomicEngine_GetSubsystem("ResourceCache")));
  89. }
  90. }
  91. public static partial class Constants
  92. {
  93. public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
  94. }
  95. public partial class RefCounted
  96. {
  97. public RefCounted()
  98. {
  99. }
  100. public void AllocGCHandle()
  101. {
  102. // if we're not a native type, we need to not be kept alive
  103. // as we need to save managed state, native types don't
  104. // save managed state and can be trivially regenerated
  105. // by creating a GCHandle this managed instance will
  106. // be used whenever we get a native ptr back from engine
  107. if (!handle.IsAllocated)
  108. {
  109. handle = GCHandle.Alloc (this);
  110. }
  111. }
  112. public void FreeGCHandle()
  113. {
  114. if (handle.IsAllocated)
  115. {
  116. handle.Free();
  117. }
  118. }
  119. protected RefCounted (IntPtr native)
  120. {
  121. nativeInstance = native;
  122. }
  123. public IntPtr nativeInstance;
  124. public static void SafeAddRef(uint id)
  125. {
  126. csb_Atomic_RefCounted_SafeAddRef (id);
  127. }
  128. public static void SafeReleaseRef(uint id)
  129. {
  130. csb_Atomic_RefCounted_SafeReleaseRef (id);
  131. }
  132. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  133. public static extern void csb_Atomic_RefCounted_SafeAddRef (uint id);
  134. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  135. public static extern void csb_Atomic_RefCounted_SafeReleaseRef (uint id);
  136. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  137. public static extern IntPtr csb_Atomic_RefCounted_GetClassID (IntPtr self);
  138. GCHandle handle;
  139. }
  140. public partial class Component : Animatable
  141. {
  142. public void Destroy()
  143. {
  144. if (Node != null)
  145. Node.RemoveComponent (this);
  146. // if we're a CSComponent notify ComponentCore
  147. ComponentCore.DestroyComponent (this);
  148. }
  149. }
  150. public partial class Node : Animatable
  151. {
  152. public T GetComponent<T> (bool recursive = false) where T:Component
  153. {
  154. return (T) GetComponent (typeof(T).Name, recursive);
  155. }
  156. public T AddComponent<T> () where T:Component, new()
  157. {
  158. T component = new T ();
  159. AddComponent( component, 0, CreateMode.REPLICATED);
  160. return component;
  161. }
  162. }
  163. public class InspectorAttribute : Attribute
  164. {
  165. public InspectorAttribute(string defaultValue = "")
  166. {
  167. DefaultValue = defaultValue;
  168. }
  169. public string DefaultValue;
  170. }
  171. }