glue.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. int Network_Connect (Network *net, const char *ptr, short port, Scene *scene)
  110. {
  111. String s(ptr);
  112. return net->Connect (s, port, scene) ? 1 : 0;
  113. }
  114. DllExport
  115. void *TouchState_GetTouchedElement (TouchState *state)
  116. {
  117. return (void *) state->GetTouchedElement ();
  118. }
  119. DllExport
  120. const char *Urho_GetPlatform ()
  121. {
  122. return strdup (GetPlatform().CString ());
  123. }
  124. DllExport
  125. unsigned urho_stringhash_from_string (const char *p)
  126. {
  127. StringHash foo (p);
  128. return foo.Value ();
  129. }
  130. DllExport
  131. Skeleton *AnimatedModel_GetSkeleton (AnimatedModel *model)
  132. {
  133. return &model->GetSkeleton ();
  134. }
  135. DllExport
  136. unsigned Controls_GetButtons (Controls *controls)
  137. {
  138. return controls->buttons_;
  139. }
  140. DllExport
  141. void Controls_SetButtons (Controls *controls, unsigned value)
  142. {
  143. controls->buttons_ = value;
  144. }
  145. DllExport
  146. float Controls_GetYaw (Controls *controls)
  147. {
  148. return controls->yaw_;
  149. }
  150. DllExport
  151. void Controls_SetYaw (Controls *controls, float value)
  152. {
  153. controls->yaw_ = value;
  154. }
  155. DllExport
  156. float Controls_GetPitch (Controls *controls)
  157. {
  158. return controls->pitch_;
  159. }
  160. DllExport
  161. void Controls_SetPitch (Controls *controls, float value)
  162. {
  163. controls->pitch_ = value;
  164. }
  165. DllExport void
  166. Controls_Reset (Urho3D::Controls *_target)
  167. {
  168. _target->Reset ();
  169. }
  170. DllExport void
  171. Controls_Set (Urho3D::Controls *_target, unsigned int buttons, int down)
  172. {
  173. _target->Set (buttons, down);
  174. }
  175. DllExport int
  176. Controls_IsDown (Urho3D::Controls *_target, unsigned int button)
  177. {
  178. return _target->IsDown (button);
  179. }
  180. DllExport const Controls *
  181. Connection_GetControls (Connection *conn)
  182. {
  183. return &conn->GetControls ();
  184. }
  185. DllExport void
  186. Connection_SetControls (Connection *conn, Controls *ctl)
  187. {
  188. conn->SetControls (*ctl);
  189. }
  190. DllExport Controls *
  191. Controls_Create ()
  192. {
  193. return new Controls ();
  194. }
  195. DllExport void
  196. Controls_Destroy (Controls *controls)
  197. {
  198. delete controls;
  199. }
  200. DllExport RayQueryResult *
  201. Octree_RaycastSingle(Octree *octree, const Urho3D::Ray & ray, const Urho3D::RayQueryLevel & level, float maxDistance, unsigned int flags, unsigned int viewMask, int *count) {
  202. PODVector<RayQueryResult> results;
  203. auto size = sizeof(RayQueryResult);
  204. RayOctreeQuery query(results, ray, RAY_TRIANGLE, maxDistance, flags, viewMask);
  205. octree->RaycastSingle(query);
  206. if (results.Size() == 0)
  207. return NULL;
  208. RayQueryResult * result = new RayQueryResult[results.Size()];
  209. *count = results.Size();
  210. for (int i = 0; i < results.Size(); i++) {
  211. result[i] = results[i];
  212. }
  213. return result;
  214. }
  215. DllExport void
  216. Console_OpenConsoleWindow()
  217. {
  218. OpenConsoleWindow();
  219. }
  220. DllExport const char *
  221. Console_GetConsoleInput()
  222. {
  223. return strdup(GetConsoleInput().CString());
  224. }
  225. //
  226. // returns: null on no matches
  227. // otherwise, a pointer that should be released with free() that
  228. // contains a first element (pointer sized) with the number of elements
  229. // followed by the number of pointers
  230. //
  231. DllExport
  232. void *urho_node_get_components (Node *node, int code, int recursive, int *count)
  233. {
  234. PODVector<Node*> dest;
  235. node->GetChildrenWithComponent (dest, StringHash(code), recursive);
  236. if (dest.Size () == 0)
  237. return NULL;
  238. *count = dest.Size ();
  239. void **t = (void **) malloc (sizeof(Node*)*dest.Size());
  240. for (int i = 0; i < dest.Size (); i++){
  241. t [i] = dest [i];
  242. }
  243. return t;
  244. }
  245. DllExport Interop::Vector3 *
  246. urho_navigationmesh_findpath(NavigationMesh * navMesh, const class Urho3D::Vector3 & start, const class Urho3D::Vector3 & end, int *count)
  247. {
  248. PODVector<Vector3> dest;
  249. navMesh->FindPath(dest, start, end);
  250. if (dest.Size() == 0)
  251. return NULL;
  252. *count = dest.Size();
  253. Interop::Vector3 * results = new Interop::Vector3[dest.Size()];
  254. for (int i = 0; i < dest.Size(); i++) {
  255. auto vector = *((Interop::Vector3 *) &(dest[i]));
  256. results[i] = vector;
  257. }
  258. return results;
  259. }
  260. #if false
  261. // Stuff to check interop
  262. void check1 (Context *app)
  263. {
  264. ResourceCache* cache = app->GetSubsystem<ResourceCache>();
  265. Texture2D* logoTexture = cache->GetResource<Texture2D>("Textures/LogoLarge.png");
  266. printf ("LOGOTEXTURE %p\n", logoTexture);
  267. auto x = app->GetSubsystems ();
  268. for (auto i = x.Begin(); i != x.End(); ++i){
  269. printf ("got %s\n", i->second_.Get ()->GetTypeName().CString ());
  270. }
  271. void *g = app->GetSubsystem<Graphics>();
  272. printf ("GGGG->%p\n", g);
  273. }
  274. class Vector3i {
  275. public:
  276. Vector3i(int a, int b, int c):x(a),y(b),z(c) {}
  277. int x, y, z;
  278. };
  279. struct Vector2i {
  280. public:
  281. int a, b;
  282. Vector2i (int a1, int b1):a(a1),b(b1) {}
  283. };
  284. Vector3i a (0xdeadbeef,0xcafebabe,0xfeed8001);
  285. Vector3i &Readl_getVector3 ()
  286. {
  287. return a;
  288. }
  289. Vector3i getVector3 ()
  290. {
  291. return Readl_getVector3 ();
  292. }
  293. void check2 (Vector3& vec)
  294. {
  295. printf ("Got %g %g %g\n", vec.x_, vec.y_, vec.z_);
  296. }
  297. Urho3D::IntVector2
  298. Input_GetMousePosition (Input *_target);
  299. Vector2i Foo1 ()
  300. {
  301. return Vector2i(0xb00bf00d, 0xfeedc0de);
  302. }
  303. Urho3D::IntVector2 Foo2 ()
  304. {
  305. return Urho3D::IntVector2(0xdeadbeef,0xdecafbab);
  306. }
  307. Vector2i Test2 (Input *inp)
  308. {
  309. return *((Vector2i *)&inp->GetMouseMove ());
  310. }
  311. //void check2 (ResourceCache *rc)
  312. //{
  313. // auto x = rc->GetSubsystems ();
  314. // for (auto i = x.Begin(); i != x.End(); ++i){
  315. // printf ("got %s\n", i->second_.Get ()->GetTypeName().CString ());
  316. // }
  317. // void *g = rc->GetSubsystem<Graphics>();
  318. // printf ("GGGG->%x\n", g);
  319. //}
  320. #endif
  321. }