AtomicEngine.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. IOModule.Initialize ();
  55. ResourceModule.Initialize ();
  56. GraphicsModule.Initialize ();
  57. SceneModule.Initialize ();
  58. AtomicPlayer.PlayerModule.Initialize ();
  59. AtomicInterop.Initialize ();
  60. atomicsharp_initialize ();
  61. initSubsystems ();
  62. }
  63. static Dictionary<Type, RefCounted> subSystems = new Dictionary<Type, RefCounted>();
  64. static private void registerSubsystem (RefCounted subsystem)
  65. {
  66. subSystems[subsystem.GetType()] = subsystem;
  67. }
  68. static public T GetSubsystem<T>() where T : RefCounted
  69. {
  70. return (T) subSystems [typeof(T)];
  71. }
  72. static private void initSubsystems()
  73. {
  74. registerSubsystem (NativeCore.WrapNative<Player> (csb_AtomicEngine_GetSubsystem("Player")));
  75. registerSubsystem (NativeCore.WrapNative<Graphics> (csb_AtomicEngine_GetSubsystem("Graphics")));
  76. registerSubsystem (NativeCore.WrapNative<Renderer> (csb_AtomicEngine_GetSubsystem("Renderer")));
  77. }
  78. }
  79. public static partial class Constants
  80. {
  81. public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
  82. public const uint M_MAX_UNSIGNED = 0xffffffff;
  83. }
  84. public partial class RefCounted
  85. {
  86. public RefCounted()
  87. {
  88. }
  89. public void AllocGCHandle()
  90. {
  91. // if we're not a native type, we need to not be kept alive
  92. // as we need to save managed state, native types don't
  93. // save managed state and can be trivially regenerated
  94. // by creating a GCHandle this managed instance will
  95. // be used whenever we get a native ptr back from engine
  96. if (!handle.IsAllocated)
  97. {
  98. handle = GCHandle.Alloc (this);
  99. }
  100. }
  101. public void FreeGCHandle()
  102. {
  103. if (handle.IsAllocated)
  104. {
  105. handle.Free();
  106. }
  107. }
  108. protected RefCounted (IntPtr native)
  109. {
  110. nativeInstance = native;
  111. }
  112. public IntPtr nativeInstance;
  113. public static void SafeAddRef(uint id)
  114. {
  115. csb_Atomic_RefCounted_SafeAddRef (id);
  116. }
  117. public static void SafeReleaseRef(uint id)
  118. {
  119. csb_Atomic_RefCounted_SafeReleaseRef (id);
  120. }
  121. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  122. public static extern void csb_Atomic_RefCounted_SafeAddRef (uint id);
  123. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  124. public static extern void csb_Atomic_RefCounted_SafeReleaseRef (uint id);
  125. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  126. public static extern IntPtr csb_Atomic_RefCounted_GetClassID (IntPtr self);
  127. GCHandle handle;
  128. }
  129. public partial class Component : Animatable
  130. {
  131. public void Destroy()
  132. {
  133. if (Node != null)
  134. Node.RemoveComponent (this);
  135. // if we're a CSComponent notify ComponentCore
  136. ComponentCore.DestroyComponent (this);
  137. }
  138. }
  139. public partial class Node : Animatable
  140. {
  141. public T GetComponent<T> (bool recursive = false) where T:Component
  142. {
  143. return (T) GetComponent (typeof(T).Name, recursive);
  144. }
  145. public T AddComponent<T> () where T:Component, new()
  146. {
  147. T component = new T ();
  148. AddComponent( component, 0, CreateMode.REPLICATED);
  149. return component;
  150. }
  151. }
  152. }