glue.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 void *urho_subscribe_event(void *_receiver, HandlerFunctionPtr callback, void *data, int eventNameHash)
  17. {
  18. StringHash h(eventNameHash);
  19. Urho3D::Object *receiver = (Urho3D::Object *) _receiver;
  20. NotificationProxy *proxy = new NotificationProxy(receiver, callback, data, h);
  21. receiver->SubscribeToEvent(receiver, h, proxy);
  22. return proxy;
  23. }
  24. DllExport
  25. void * urho_map_get_ptr (VariantMap &map, int hash)
  26. {
  27. StringHash h (hash);
  28. return map [h].GetVoidPtr ();
  29. }
  30. DllExport
  31. void * urho_map_get_object(VariantMap &map, int hash, int* objHash)
  32. {
  33. StringHash h(hash);
  34. void* ptr = map[h].GetVoidPtr();
  35. Object * object = static_cast<Object *>(ptr);
  36. *objHash = object->GetType().Value(); //GetType is virtual
  37. return ptr;
  38. }
  39. DllExport
  40. const char * urho_map_get_String (VariantMap& map, int hash)
  41. {
  42. StringHash h (hash);
  43. return stringdup(map [h].GetString ().CString());
  44. }
  45. DllExport
  46. int urho_map_get_StringHash (VariantMap& map, int hash)
  47. {
  48. StringHash h (hash);
  49. return map [h].GetStringHash ().Value ();
  50. }
  51. DllExport
  52. Variant urho_map_get_Variant (VariantMap& map, int hash)
  53. {
  54. StringHash h (hash);
  55. return map [h];
  56. }
  57. DllExport
  58. Interop::Vector3 urho_map_get_Vector3 (VariantMap& map, int hash)
  59. {
  60. StringHash h (hash);
  61. return *((Interop::Vector3 *) &(map [h].GetVector3 ()));
  62. }
  63. DllExport
  64. int urho_map_get_bool (VariantMap& map, int hash)
  65. {
  66. StringHash h (hash);
  67. return map [h].GetBool ();
  68. }
  69. DllExport
  70. float urho_map_get_float (VariantMap& map, int hash)
  71. {
  72. StringHash h (hash);
  73. return map [h].GetFloat ();
  74. }
  75. DllExport
  76. int urho_map_get_int (VariantMap& map, int hash)
  77. {
  78. StringHash h (hash);
  79. return map [h].GetInt ();
  80. }
  81. DllExport
  82. unsigned int urho_map_get_uint (VariantMap& map, int hash)
  83. {
  84. StringHash h (hash);
  85. return map [h].GetUInt ();
  86. }
  87. DllExport unsigned char *
  88. urho_map_get_buffer (VariantMap &map, int hash, unsigned *size)
  89. {
  90. StringHash h (hash);
  91. PODVector<unsigned char> p (map [h].GetBuffer ());
  92. *size = p.Size();
  93. unsigned char * result = new unsigned char[p.Size()];
  94. for (int i = 0; i < p.Size(); i++) {
  95. result[i] = p[i];
  96. }
  97. return result;
  98. }
  99. DllExport
  100. void urho_unsubscribe (NotificationProxy *proxy)
  101. {
  102. proxy->Unsub ();
  103. }
  104. DllExport void
  105. UI_LoadLayoutToElement(Urho3D::UI *_target, Urho3D::UIElement *to, Urho3D::ResourceCache *cache, const char * name)
  106. {
  107. SharedPtr<UIElement> layoutRoot = _target->LoadLayout(cache->GetResource<XMLFile>(name));
  108. to->AddChild(layoutRoot);
  109. }
  110. DllExport int
  111. Scene_LoadXMLFromCache(Urho3D::Scene *_target, Urho3D::ResourceCache *cache, const char * name)
  112. {
  113. SharedPtr<File> file = cache->GetFile(name);
  114. return _target->LoadXML(*file);
  115. }
  116. DllExport
  117. void *TouchState_GetTouchedElement (TouchState *state)
  118. {
  119. return (void *) state->GetTouchedElement ();
  120. }
  121. DllExport
  122. const char *Urho_GetPlatform ()
  123. {
  124. return stringdup (GetPlatform().CString ());
  125. }
  126. DllExport
  127. unsigned urho_stringhash_from_string (const char *p)
  128. {
  129. StringHash foo (p);
  130. return foo.Value ();
  131. }
  132. DllExport
  133. Skeleton *AnimatedModel_GetSkeleton (AnimatedModel *model)
  134. {
  135. return &model->GetSkeleton ();
  136. }
  137. DllExport
  138. unsigned Controls_GetButtons (Controls *controls)
  139. {
  140. return controls->buttons_;
  141. }
  142. DllExport
  143. void* Graphics_GetSdlWindow(Graphics* target)
  144. {
  145. return target->GetWindow();
  146. }
  147. DllExport
  148. void Controls_SetButtons (Controls *controls, unsigned value)
  149. {
  150. controls->buttons_ = value;
  151. }
  152. DllExport
  153. float Controls_GetYaw (Controls *controls)
  154. {
  155. return controls->yaw_;
  156. }
  157. DllExport
  158. void Controls_SetYaw (Controls *controls, float value)
  159. {
  160. controls->yaw_ = value;
  161. }
  162. DllExport
  163. float Controls_GetPitch (Controls *controls)
  164. {
  165. return controls->pitch_;
  166. }
  167. DllExport
  168. void Controls_SetPitch (Controls *controls, float value)
  169. {
  170. controls->pitch_ = value;
  171. }
  172. DllExport void
  173. Controls_Reset (Urho3D::Controls *_target)
  174. {
  175. _target->Reset ();
  176. }
  177. DllExport void
  178. Controls_Set (Urho3D::Controls *_target, unsigned int buttons, int down)
  179. {
  180. _target->Set (buttons, down);
  181. }
  182. DllExport int
  183. Controls_IsDown (Urho3D::Controls *_target, unsigned int button)
  184. {
  185. return _target->IsDown (button);
  186. }
  187. #if !defined(UWP)
  188. DllExport int
  189. Network_Connect(Network *net, const char *ptr, short port, Scene *scene)
  190. {
  191. String s(ptr);
  192. return net->Connect(s, port, scene) ? 1 : 0;
  193. }
  194. DllExport const Controls *
  195. Connection_GetControls (Connection *conn)
  196. {
  197. return &conn->GetControls ();
  198. }
  199. DllExport void
  200. Connection_SetControls(Connection *conn, Controls *ctl)
  201. {
  202. conn->SetControls(*ctl);
  203. }
  204. #endif
  205. DllExport Controls *
  206. Controls_Create ()
  207. {
  208. return new Controls ();
  209. }
  210. DllExport void
  211. Controls_Destroy (Controls *controls)
  212. {
  213. delete controls;
  214. }
  215. DllExport RayQueryResult *
  216. Octree_Raycast(Octree *octree, const Urho3D::Ray & ray, const Urho3D::RayQueryLevel & level, float maxDistance, unsigned int flags, unsigned int viewMask, bool single, int *count) {
  217. PODVector<RayQueryResult> results;
  218. auto size = sizeof(RayQueryResult);
  219. RayOctreeQuery query(results, ray, level, maxDistance, flags, viewMask);
  220. if (single)
  221. octree->RaycastSingle(query);
  222. else
  223. octree->Raycast(query);
  224. if (results.Size() == 0)
  225. return NULL;
  226. RayQueryResult * result = new RayQueryResult[results.Size()];
  227. *count = results.Size();
  228. for (int i = 0; i < results.Size(); i++) {
  229. result[i] = results[i];
  230. }
  231. return result;
  232. }
  233. DllExport void
  234. Console_OpenConsoleWindow()
  235. {
  236. OpenConsoleWindow();
  237. }
  238. DllExport const char *
  239. Console_GetConsoleInput()
  240. {
  241. return stringdup(GetConsoleInput().CString());
  242. }
  243. //
  244. // returns: null on no matches
  245. // otherwise, a pointer that should be released with free() that
  246. // contains a first element (pointer sized) with the number of elements
  247. // followed by the number of pointers
  248. //
  249. DllExport
  250. void *urho_node_get_components (Node *node, int code, int recursive, int *count)
  251. {
  252. PODVector<Node*> dest;
  253. node->GetChildrenWithComponent (dest, StringHash(code), recursive);
  254. if (dest.Size () == 0)
  255. return NULL;
  256. *count = dest.Size ();
  257. void **t = (void **) malloc (sizeof(Node*)*dest.Size());
  258. for (int i = 0; i < dest.Size (); i++){
  259. t [i] = dest [i];
  260. }
  261. return t;
  262. }
  263. DllExport Interop::Vector3 *
  264. urho_navigationmesh_findpath(NavigationMesh * navMesh, const class Urho3D::Vector3 & start, const class Urho3D::Vector3 & end, int *count)
  265. {
  266. PODVector<Vector3> dest;
  267. navMesh->FindPath(dest, start, end);
  268. if (dest.Size() == 0)
  269. return NULL;
  270. *count = dest.Size();
  271. Interop::Vector3 * results = new Interop::Vector3[dest.Size()];
  272. for (int i = 0; i < dest.Size(); i++) {
  273. auto vector = *((Interop::Vector3 *) &(dest[i]));
  274. results[i] = vector;
  275. }
  276. return results;
  277. }
  278. DllExport MemoryBuffer* MemoryBuffer_MemoryBuffer(void* data, int size)
  279. {
  280. auto buffer = new MemoryBuffer(data, size);
  281. return buffer;
  282. }
  283. DllExport unsigned char* MemoryBuffer_GetData(MemoryBuffer* target, int *count)
  284. {
  285. *count = target->GetSize();
  286. return target->GetData();
  287. }
  288. DllExport void MemoryBuffer_Dispose(MemoryBuffer* target)
  289. {
  290. delete target;
  291. }
  292. DllExport unsigned MemoryBuffer_GetSize(MemoryBuffer* target)
  293. {
  294. return target->GetSize();
  295. }
  296. DllExport unsigned File_GetSize(File* target)
  297. {
  298. return target->GetSize();
  299. }
  300. DllExport Interop::Vector3 *
  301. Frustum_GetVertices(Frustum * frustum, int* count)
  302. {
  303. int size = NUM_FRUSTUM_VERTICES;
  304. Interop::Vector3 * results = new Interop::Vector3[size];
  305. for (int i = 0; i < size; i++) {
  306. auto vector = *((Interop::Vector3 *) &(frustum->vertices_[i]));
  307. results[i] = vector;
  308. }
  309. *count = size;
  310. return results;
  311. }
  312. DllExport Interop::Plane *
  313. Frustum_GetPlanes(Frustum * frustum, int* count)
  314. {
  315. int size = NUM_FRUSTUM_PLANES;
  316. Interop::Plane * results = new Interop::Plane[size];
  317. for (int i = 0; i < size; i++) {
  318. auto plane = *((Interop::Plane *) &(frustum->planes_[i]));
  319. results[i] = plane;
  320. }
  321. *count = size;
  322. return results;
  323. }
  324. DllExport Interop::Color
  325. Material_GetShaderParameterColor(Urho3D::Material *_target, const char* paramName)
  326. {
  327. return *((Interop::Color *) &(_target->GetShaderParameter(Urho3D::String(paramName)).GetColor()));
  328. }
  329. DllExport unsigned char*
  330. Image_GetDataBytes(Urho3D::Image *_target, int* len)
  331. {
  332. *len = _target->GetWidth() * _target->GetHeight() * _target->GetDepth() * _target->GetComponents();
  333. return _target->GetData();
  334. }
  335. DllExport unsigned char*
  336. Image_SavePNG2(Urho3D::Image *_target, int* len)
  337. {
  338. return _target->SavePNG(len);
  339. }
  340. DllExport
  341. void FreeBuffer(unsigned char* myBuffer)
  342. {
  343. delete myBuffer;
  344. }
  345. DllExport
  346. void RenderPathCommand_SetShaderParameter(RenderPathCommand* rpc, const char* parameter, float value)
  347. {
  348. rpc->SetShaderParameter(String(parameter), value);
  349. }
  350. DllExport
  351. void RenderPathCommand_SetOutput(RenderPathCommand* rpc, int index, const char* name)
  352. {
  353. rpc->SetOutput(index, String(name));
  354. }
  355. }