glue.cpp 10 KB

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