AtomicEngine.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. static Atomic()
  17. {
  18. try {
  19. Initialize ();
  20. }
  21. catch (Exception e)
  22. {
  23. Console.WriteLine(e.ToString());
  24. throw;
  25. }
  26. }
  27. public static void Run()
  28. {
  29. while (Atomic.RunFrame ()) {
  30. }
  31. }
  32. public static bool RunFrame()
  33. {
  34. GC.Collect();
  35. GC.WaitForPendingFinalizers();
  36. GC.Collect();
  37. NativeCore.ReleaseExpiredNativeReferences ();
  38. return atomicsharp_runframe ();
  39. }
  40. public static void RegisterAssemblyComponents(Assembly assembly)
  41. {
  42. ComponentCore.RegisterAssemblyComponents (assembly);
  43. }
  44. public static void Initialize()
  45. {
  46. ContainerModule.Initialize ();
  47. CoreModule.Initialize ();
  48. IOModule.Initialize ();
  49. ResourceModule.Initialize ();
  50. GraphicsModule.Initialize ();
  51. SceneModule.Initialize ();
  52. AtomicPlayer.PlayerModule.Initialize ();
  53. AtomicInterop.Initialize ();
  54. atomicsharp_initialize ();
  55. initSubsystems ();
  56. }
  57. static Dictionary<Type, RefCounted> subSystems = new Dictionary<Type, RefCounted>();
  58. static private void registerSubsystem (RefCounted subsystem)
  59. {
  60. subSystems[subsystem.GetType()] = subsystem;
  61. }
  62. static public T GetSubsystem<T>() where T : RefCounted
  63. {
  64. return (T) subSystems [typeof(T)];
  65. }
  66. static private void initSubsystems()
  67. {
  68. registerSubsystem (NativeCore.WrapNative<Player> (csb_AtomicEngine_GetSubsystem("Player")));
  69. }
  70. }
  71. public static partial class Constants
  72. {
  73. public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
  74. }
  75. public partial class Node
  76. {
  77. public T AddComponent<T> () where T:Component, new()
  78. {
  79. T component = new T ();
  80. AddComponent( component, 0, CreateMode.REPLICATED);
  81. return component;
  82. }
  83. // /new MyComponent (), 0, CreateMode.REPLICATED
  84. }
  85. public partial class RefCounted
  86. {
  87. protected RefCounted (IntPtr native)
  88. {
  89. nativeInstance = native;
  90. }
  91. public IntPtr nativeInstance;
  92. static public void _AddRef(IntPtr native)
  93. {
  94. csb_Atomic_RefCounted_AddRef(native);
  95. }
  96. static public void _ReleaseRef(IntPtr native)
  97. {
  98. csb_Atomic_RefCounted_ReleaseRef(native);
  99. }
  100. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  101. public static extern IntPtr csb_RefCounted_GetClassID (IntPtr self);
  102. }
  103. public partial class Node : Animatable
  104. {
  105. public T GetComponent<T> (bool recursive = false) where T:Component
  106. {
  107. return (T) GetComponent (typeof(T).Name, recursive);
  108. }
  109. }
  110. }