MyClass.cs 4.6 KB

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