glue.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 _strdup(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 _strdup (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->GetImpl()->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_RaycastSingle(Octree *octree, const Urho3D::Ray & ray, const Urho3D::RayQueryLevel & level, float maxDistance, unsigned int flags, unsigned int viewMask, int *count) {
  209. PODVector<RayQueryResult> results;
  210. auto size = sizeof(RayQueryResult);
  211. RayOctreeQuery query(results, ray, RAY_TRIANGLE, maxDistance, flags, viewMask);
  212. octree->RaycastSingle(query);
  213. if (results.Size() == 0)
  214. return NULL;
  215. RayQueryResult * result = new RayQueryResult[results.Size()];
  216. *count = results.Size();
  217. for (int i = 0; i < results.Size(); i++) {
  218. result[i] = results[i];
  219. }
  220. return result;
  221. }
  222. DllExport void
  223. Console_OpenConsoleWindow()
  224. {
  225. OpenConsoleWindow();
  226. }
  227. DllExport const char *
  228. Console_GetConsoleInput()
  229. {
  230. return _strdup(GetConsoleInput().CString());
  231. }
  232. //
  233. // returns: null on no matches
  234. // otherwise, a pointer that should be released with free() that
  235. // contains a first element (pointer sized) with the number of elements
  236. // followed by the number of pointers
  237. //
  238. DllExport
  239. void *urho_node_get_components (Node *node, int code, int recursive, int *count)
  240. {
  241. PODVector<Node*> dest;
  242. node->GetChildrenWithComponent (dest, StringHash(code), recursive);
  243. if (dest.Size () == 0)
  244. return NULL;
  245. *count = dest.Size ();
  246. void **t = (void **) malloc (sizeof(Node*)*dest.Size());
  247. for (int i = 0; i < dest.Size (); i++){
  248. t [i] = dest [i];
  249. }
  250. return t;
  251. }
  252. DllExport Interop::Vector3 *
  253. urho_navigationmesh_findpath(NavigationMesh * navMesh, const class Urho3D::Vector3 & start, const class Urho3D::Vector3 & end, int *count)
  254. {
  255. PODVector<Vector3> dest;
  256. navMesh->FindPath(dest, start, end);
  257. if (dest.Size() == 0)
  258. return NULL;
  259. *count = dest.Size();
  260. Interop::Vector3 * results = new Interop::Vector3[dest.Size()];
  261. for (int i = 0; i < dest.Size(); i++) {
  262. auto vector = *((Interop::Vector3 *) &(dest[i]));
  263. results[i] = vector;
  264. }
  265. return results;
  266. }
  267. #if false
  268. // Stuff to check interop
  269. void check1 (Context *app)
  270. {
  271. ResourceCache* cache = app->GetSubsystem<ResourceCache>();
  272. Texture2D* logoTexture = cache->GetResource<Texture2D>("Textures/LogoLarge.png");
  273. printf ("LOGOTEXTURE %p\n", logoTexture);
  274. auto x = app->GetSubsystems ();
  275. for (auto i = x.Begin(); i != x.End(); ++i){
  276. printf ("got %s\n", i->second_.Get ()->GetTypeName().CString ());
  277. }
  278. void *g = app->GetSubsystem<Graphics>();
  279. printf ("GGGG->%p\n", g);
  280. }
  281. class Vector3i {
  282. public:
  283. Vector3i(int a, int b, int c):x(a),y(b),z(c) {}
  284. int x, y, z;
  285. };
  286. struct Vector2i {
  287. public:
  288. int a, b;
  289. Vector2i (int a1, int b1):a(a1),b(b1) {}
  290. };
  291. Vector3i a (0xdeadbeef,0xcafebabe,0xfeed8001);
  292. Vector3i &Readl_getVector3 ()
  293. {
  294. return a;
  295. }
  296. Vector3i getVector3 ()
  297. {
  298. return Readl_getVector3 ();
  299. }
  300. void check2 (Vector3& vec)
  301. {
  302. printf ("Got %g %g %g\n", vec.x_, vec.y_, vec.z_);
  303. }
  304. Urho3D::IntVector2
  305. Input_GetMousePosition (Input *_target);
  306. Vector2i Foo1 ()
  307. {
  308. return Vector2i(0xb00bf00d, 0xfeedc0de);
  309. }
  310. Urho3D::IntVector2 Foo2 ()
  311. {
  312. return Urho3D::IntVector2(0xdeadbeef,0xdecafbab);
  313. }
  314. Vector2i Test2 (Input *inp)
  315. {
  316. return *((Vector2i *)&inp->GetMouseMove ());
  317. }
  318. //void check2 (ResourceCache *rc)
  319. //{
  320. // auto x = rc->GetSubsystems ();
  321. // for (auto i = x.Begin(); i != x.End(); ++i){
  322. // printf ("got %s\n", i->second_.Get ()->GetTypeName().CString ());
  323. // }
  324. // void *g = rc->GetSubsystem<Graphics>();
  325. // printf ("GGGG->%x\n", g);
  326. //}
  327. #endif
  328. }