glue.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifndef _MSC_VER
  4. # include <unistd.h>
  5. #endif
  6. #include <stdlib.h>
  7. #include "AllUrho.h"
  8. #include "glue.h"
  9. #include "interop.h"
  10. using namespace Urho3D;
  11. //
  12. // This is just an implemention of EventHandler that can be used with function
  13. // pointers, so we can register delegates from C#
  14. //
  15. extern "C" {
  16. DllExport
  17. void * urho_map_get_ptr (VariantMap &map, int hash)
  18. {
  19. StringHash h (hash);
  20. return map [h].GetVoidPtr ();
  21. }
  22. DllExport
  23. void * urho_map_get_object(VariantMap &map, int hash, int* objHash)
  24. {
  25. StringHash h(hash);
  26. void* ptr = map[h].GetVoidPtr();
  27. Object * object = static_cast<Object *>(ptr);
  28. *objHash = object->GetType().Value(); //GetType is virtual
  29. return ptr;
  30. }
  31. DllExport
  32. const char * urho_map_get_String (VariantMap& map, int hash)
  33. {
  34. StringHash h (hash);
  35. return stringdup(map [h].GetString ().CString());
  36. }
  37. DllExport
  38. int urho_map_get_StringHash (VariantMap& map, int hash)
  39. {
  40. StringHash h (hash);
  41. return map [h].GetStringHash ().Value ();
  42. }
  43. DllExport
  44. Variant urho_map_get_Variant (VariantMap& map, int hash)
  45. {
  46. StringHash h (hash);
  47. return map [h];
  48. }
  49. DllExport
  50. Vector3 urho_map_get_Vector3 (VariantMap& map, int hash)
  51. {
  52. StringHash h (hash);
  53. return map [h].GetVector3 ();
  54. }
  55. DllExport
  56. int urho_map_get_bool (VariantMap& map, int hash)
  57. {
  58. StringHash h (hash);
  59. return map [h].GetBool ();
  60. }
  61. DllExport
  62. float urho_map_get_float (VariantMap& map, int hash)
  63. {
  64. StringHash h (hash);
  65. return map [h].GetFloat ();
  66. }
  67. DllExport
  68. int urho_map_get_int (VariantMap& map, int hash)
  69. {
  70. StringHash h (hash);
  71. return map [h].GetInt ();
  72. }
  73. DllExport
  74. unsigned int urho_map_get_uint (VariantMap& map, int hash)
  75. {
  76. StringHash h (hash);
  77. return map [h].GetUInt ();
  78. }
  79. DllExport unsigned char *
  80. urho_map_get_buffer (VariantMap &map, int hash, unsigned *size)
  81. {
  82. StringHash h (hash);
  83. PODVector<unsigned char> p (map [h].GetBuffer ());
  84. *size = p.Size();
  85. unsigned char * result = new unsigned char[p.Size()];
  86. for (int i = 0; i < p.Size(); i++) {
  87. result[i] = p[i];
  88. }
  89. return result;
  90. }
  91. DllExport
  92. void urho_unsubscribe (NotificationProxy *proxy)
  93. {
  94. proxy->Unsub ();
  95. }
  96. DllExport void
  97. UI_LoadLayoutToElement(Urho3D::UI *_target, Urho3D::UIElement *to, Urho3D::ResourceCache *cache, const char * name)
  98. {
  99. SharedPtr<UIElement> layoutRoot = _target->LoadLayout(cache->GetResource<XMLFile>(name));
  100. to->AddChild(layoutRoot);
  101. }
  102. DllExport int
  103. Scene_LoadXMLFromCache(Urho3D::Scene *_target, Urho3D::ResourceCache *cache, const char * name)
  104. {
  105. SharedPtr<File> file = cache->GetFile(name);
  106. return _target->LoadXML(*file);
  107. }
  108. DllExport
  109. void *TouchState_GetTouchedElement (TouchState *state)
  110. {
  111. return (void *) state->GetTouchedElement ();
  112. }
  113. DllExport
  114. const char *Urho_GetPlatform ()
  115. {
  116. return stringdup (GetPlatform().CString ());
  117. }
  118. DllExport
  119. unsigned urho_stringhash_from_string (const char *p)
  120. {
  121. StringHash foo (p);
  122. return foo.Value ();
  123. }
  124. DllExport
  125. Skeleton *AnimatedModel_GetSkeleton (AnimatedModel *model)
  126. {
  127. return &model->GetSkeleton ();
  128. }
  129. DllExport
  130. unsigned Controls_GetButtons (Controls *controls)
  131. {
  132. return controls->buttons_;
  133. }
  134. DllExport
  135. void* Graphics_GetSdlWindow(Graphics* target)
  136. {
  137. return target->GetWindow();
  138. }
  139. DllExport
  140. void Controls_SetButtons (Controls *controls, unsigned value)
  141. {
  142. controls->buttons_ = value;
  143. }
  144. DllExport
  145. float Controls_GetYaw (Controls *controls)
  146. {
  147. return controls->yaw_;
  148. }
  149. DllExport
  150. void Controls_SetYaw (Controls *controls, float value)
  151. {
  152. controls->yaw_ = value;
  153. }
  154. DllExport
  155. float Controls_GetPitch (Controls *controls)
  156. {
  157. return controls->pitch_;
  158. }
  159. DllExport
  160. void Controls_SetPitch (Controls *controls, float value)
  161. {
  162. controls->pitch_ = value;
  163. }
  164. DllExport void
  165. Controls_Reset (Urho3D::Controls *_target)
  166. {
  167. _target->Reset ();
  168. }
  169. DllExport void
  170. Controls_Set (Urho3D::Controls *_target, unsigned int buttons, int down)
  171. {
  172. _target->Set (buttons, down);
  173. }
  174. DllExport int
  175. Controls_IsDown (Urho3D::Controls *_target, unsigned int button)
  176. {
  177. return _target->IsDown (button);
  178. }
  179. #if !defined(UWP)
  180. DllExport int
  181. Network_Connect(Network *net, const char *ptr, short port, Scene *scene)
  182. {
  183. String s(ptr);
  184. return net->Connect(s, port, scene) ? 1 : 0;
  185. }
  186. DllExport const Controls *
  187. Connection_GetControls (Connection *conn)
  188. {
  189. return &conn->GetControls ();
  190. }
  191. DllExport void
  192. Connection_SetControls(Connection *conn, Controls *ctl)
  193. {
  194. conn->SetControls(*ctl);
  195. }
  196. #endif
  197. DllExport Controls *
  198. Controls_Create ()
  199. {
  200. return new Controls ();
  201. }
  202. DllExport void
  203. Controls_Destroy (Controls *controls)
  204. {
  205. delete controls;
  206. }
  207. DllExport RayQueryResult *
  208. Octree_Raycast(Octree *octree, const Urho3D::Ray & ray, const Urho3D::RayQueryLevel & level, float maxDistance, unsigned int flags, unsigned int viewMask, bool single, int *count) {
  209. PODVector<RayQueryResult> results;
  210. auto size = sizeof(RayQueryResult);
  211. RayOctreeQuery query(results, ray, level, maxDistance, flags, viewMask);
  212. if (single)
  213. octree->RaycastSingle(query);
  214. else
  215. octree->Raycast(query);
  216. if (results.Size() == 0)
  217. return NULL;
  218. RayQueryResult * result = new RayQueryResult[results.Size()];
  219. *count = results.Size();
  220. for (int i = 0; i < results.Size(); i++) {
  221. result[i] = results[i];
  222. }
  223. return result;
  224. }
  225. DllExport void
  226. Console_OpenConsoleWindow()
  227. {
  228. OpenConsoleWindow();
  229. }
  230. DllExport const char *
  231. Console_GetConsoleInput()
  232. {
  233. return stringdup(GetConsoleInput().CString());
  234. }
  235. //
  236. // returns: null on no matches
  237. // otherwise, a pointer that should be released with free() that
  238. // contains a first element (pointer sized) with the number of elements
  239. // followed by the number of pointers
  240. //
  241. DllExport
  242. void *urho_node_get_components (Node *node, int code, int recursive, int *count)
  243. {
  244. PODVector<Node*> dest;
  245. node->GetChildrenWithComponent (dest, StringHash(code), recursive);
  246. if (dest.Size () == 0)
  247. return NULL;
  248. *count = dest.Size ();
  249. void **t = (void **) malloc (sizeof(Node*)*dest.Size());
  250. for (int i = 0; i < dest.Size (); i++){
  251. t [i] = dest [i];
  252. }
  253. return t;
  254. }
  255. DllExport Interop::Vector3 *
  256. urho_navigationmesh_findpath(NavigationMesh * navMesh, const class Urho3D::Vector3 & start, const class Urho3D::Vector3 & end, int *count)
  257. {
  258. PODVector<Vector3> dest;
  259. navMesh->FindPath(dest, start, end);
  260. if (dest.Size() == 0)
  261. return NULL;
  262. *count = dest.Size();
  263. Interop::Vector3 * results = new Interop::Vector3[dest.Size()];
  264. for (int i = 0; i < dest.Size(); i++) {
  265. auto vector = *((Interop::Vector3 *) &(dest[i]));
  266. results[i] = vector;
  267. }
  268. return results;
  269. }
  270. DllExport MemoryBuffer* MemoryBuffer_MemoryBuffer(void* data, int size)
  271. {
  272. auto buffer = new MemoryBuffer(data, size);
  273. return buffer;
  274. }
  275. DllExport unsigned char* MemoryBuffer_GetData(MemoryBuffer* target, int *count)
  276. {
  277. *count = target->GetSize();
  278. return target->GetData();
  279. }
  280. DllExport void MemoryBuffer_Dispose(MemoryBuffer* target)
  281. {
  282. delete target;
  283. }
  284. }