Godot.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #ifndef GODOT_H
  2. #define GODOT_H
  3. #include <cstdlib>
  4. #include <godot.h>
  5. #include <godot_cpp/core/CoreTypes.hpp>
  6. #include <godot_cpp/core/Variant.hpp>
  7. namespace godot {
  8. #define GODOT_CLASS(Name, Base) \
  9. public: inline static char *___get_type_name() { return (char *) #Name; } \
  10. inline static char *___get_base_type_name() { return (char *) #Base; } \
  11. Base *self; \
  12. inline Name(godot_object *o) { self = (Base *) o; } \
  13. private:
  14. // inline Name(Object o) { this = (Name *) godot_dlinstance_get_userdata(o.__core_object);\
  15. // memcpy(this, p, sizeof(Name)); } \
  16. // inline Name(const Variant& obj) { __core_object = ((Object) obj).__core_object; } \
  17. template<class T>
  18. T *as(Object *obj)
  19. {
  20. return (T *) godot_dlinstance_get_userdata(obj);
  21. }
  22. // instance and destroy funcs
  23. template<class T>
  24. void *_godot_class_instance_func(godot_object *p, void *method_data)
  25. {
  26. T *d = new T(p);
  27. d->_init();
  28. return d;
  29. }
  30. template<class T>
  31. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data)
  32. {
  33. T *d = (T *) data;
  34. delete d;
  35. }
  36. template<class T>
  37. void register_class()
  38. {
  39. godot_instance_create_func create = {};
  40. create.create_func = _godot_class_instance_func<T>;
  41. godot_instance_destroy_func destroy = {};
  42. destroy.destroy_func = _godot_class_destroy_func<T>;
  43. godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  44. T::_register_methods();
  45. }
  46. template<class T>
  47. void register_tool_class()
  48. {
  49. godot_instance_create_func create = {};
  50. create.create_func = _godot_class_instance_func<T>;
  51. godot_instance_destroy_func destroy = {};
  52. destroy.destroy_func = _godot_class_destroy_func<T>;
  53. godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  54. T::_register_methods();
  55. }
  56. // method registering
  57. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  58. template<class T, class R, class ...args>
  59. char *___get_method_class_name(R (T::*p)(args... a))
  60. {
  61. return T::___get_type_name();
  62. }
  63. // wohooo, let the fun begin.
  64. template<class T, class R>
  65. struct _WrappedMethod0 {
  66. R (T::*f)();
  67. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  68. {
  69. godot_variant v;
  70. godot_variant_new_nil(&v);
  71. T *obj = (T *) user_data;
  72. _WrappedMethod0<T, R> *method = (_WrappedMethod0<T, R>*) method_data;
  73. Variant *var = (Variant *) &v;
  74. *var = (obj->*(method->f))();
  75. return v;
  76. }
  77. };
  78. template<class T>
  79. struct _WrappedMethod0<T, void> {
  80. void (T::*f)();
  81. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  82. {
  83. godot_variant v;
  84. godot_variant_new_nil(&v);
  85. T *obj = (T *) user_data;
  86. _WrappedMethod0<T, void> *method = (_WrappedMethod0<T, void>*) method_data;
  87. (obj->*(method->f))();
  88. return v;
  89. }
  90. };
  91. template<class T, class R>
  92. void *___make_wrapper_function(R (T::*f)())
  93. {
  94. _WrappedMethod0<T, R> *p = (_WrappedMethod0<T, R> *) malloc(sizeof(_WrappedMethod0<T, R>));
  95. p->f = f;
  96. return (void *) p;
  97. }
  98. template<class T, class R>
  99. __godot_wrapper_method ___get_wrapper_function(R (T::*f)())
  100. {
  101. return (__godot_wrapper_method) &_WrappedMethod0<T, R>::__wrapped_method;
  102. }
  103. template<class T, class R, class A0>
  104. struct _WrappedMethod1 {
  105. R (T::*f)(A0);
  106. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  107. {
  108. godot_variant v;
  109. godot_variant_new_nil(&v);
  110. T *obj = (T *) user_data;
  111. _WrappedMethod1<T, R, A0> *method = (_WrappedMethod1<T, R, A0>*) method_data;
  112. Variant *var = (Variant *) &v;
  113. Variant **arg = (Variant **) args;
  114. *var = (obj->*(method->f))(*arg[0]);
  115. return v;
  116. }
  117. };
  118. template<class T, class A0>
  119. struct _WrappedMethod1<T, void, A0> {
  120. void (T::*f)(A0);
  121. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  122. {
  123. godot_variant v;
  124. godot_variant_new_nil(&v);
  125. T *obj = (T *) user_data;
  126. _WrappedMethod1<T, void, A0> *method = (_WrappedMethod1<T, void, A0>*) method_data;
  127. Variant **arg = (Variant **) args;
  128. (obj->*(method->f))(*arg[0]);
  129. return v;
  130. }
  131. };
  132. template<class T, class R, class A0>
  133. void *___make_wrapper_function(R (T::*f)(A0))
  134. {
  135. _WrappedMethod1<T, R, A0> *p = (_WrappedMethod1<T, R, A0> *) malloc(sizeof(_WrappedMethod1<T, R, A0>));
  136. p->f = f;
  137. return (void *) p;
  138. }
  139. template<class T, class R, class A0>
  140. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0))
  141. {
  142. return (__godot_wrapper_method) &_WrappedMethod1<T, R, A0>::__wrapped_method;
  143. }
  144. template<class T, class R, class A0, class A1>
  145. struct _WrappedMethod2 {
  146. R (T::*f)(A0, A1);
  147. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  148. {
  149. godot_variant v;
  150. godot_variant_new_nil(&v);
  151. T *obj = (T *) user_data;
  152. _WrappedMethod2<T, R, A0, A1> *method = (_WrappedMethod2<T, R, A0, A1>*) method_data;
  153. Variant *var = (Variant *) &v;
  154. Variant **arg = (Variant **) args;
  155. *var = (obj->*(method->f))(*arg[0], *arg[1]);
  156. return v;
  157. }
  158. };
  159. template<class T, class A0, class A1>
  160. struct _WrappedMethod2<T, void, A0, A1> {
  161. void (T::*f)(A0, A1);
  162. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  163. {
  164. godot_variant v;
  165. godot_variant_new_nil(&v);
  166. T *obj = (T *) user_data;
  167. _WrappedMethod2<T, void, A0, A1> *method = (_WrappedMethod2<T, void, A0, A1>*) method_data;
  168. Variant **arg = (Variant **) args;
  169. (obj->*(method->f))(*arg[0], *arg[1]);
  170. return v;
  171. }
  172. };
  173. template<class T, class R, class A0, class A1>
  174. void *___make_wrapper_function(R (T::*f)(A0, A1))
  175. {
  176. _WrappedMethod2<T, R, A0, A1> *p = (_WrappedMethod2<T, R, A0, A1> *) malloc(sizeof(_WrappedMethod2<T, R, A0, A1>));
  177. p->f = f;
  178. return (void *) p;
  179. }
  180. template<class T, class R, class A0, class A1>
  181. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1))
  182. {
  183. return (__godot_wrapper_method) &_WrappedMethod2<T, R, A0, A1>::__wrapped_method;
  184. }
  185. template<class T, class R, class A0, class A1, class A2>
  186. struct _WrappedMethod3 {
  187. R (T::*f)(A0, A1, A2);
  188. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  189. {
  190. godot_variant v;
  191. godot_variant_new_nil(&v);
  192. T *obj = (T *) user_data;
  193. _WrappedMethod3<T, R, A0, A1, A2> *method = (_WrappedMethod3<T, R, A0, A1, A2>*) method_data;
  194. Variant *var = (Variant *) &v;
  195. Variant **arg = (Variant **) args;
  196. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  197. return v;
  198. }
  199. };
  200. template<class T, class A0, class A1, class A2>
  201. struct _WrappedMethod3<T, void, A0, A1, A2> {
  202. void (T::*f)(A0, A1, A2);
  203. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  204. {
  205. godot_variant v;
  206. godot_variant_new_nil(&v);
  207. T *obj = (T *) user_data;
  208. _WrappedMethod3<T, void, A0, A1, A2> *method = (_WrappedMethod3<T, void, A0, A1, A2>*) method_data;
  209. Variant **arg = (Variant **) args;
  210. (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  211. return v;
  212. }
  213. };
  214. template<class T, class R, class A0, class A1, class A2>
  215. void *___make_wrapper_function(R (T::*f)(A0, A1, A2))
  216. {
  217. _WrappedMethod3<T, R, A0, A1, A2> *p = (_WrappedMethod3<T, R, A0, A1, A2> *) malloc(sizeof(_WrappedMethod3<T, R, A0, A1, A2>));
  218. p->f = f;
  219. return (void *) p;
  220. }
  221. template<class T, class R, class A0, class A1, class A2>
  222. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2))
  223. {
  224. return (__godot_wrapper_method) &_WrappedMethod3<T, R, A0, A1, A2>::__wrapped_method;
  225. }
  226. template<class T, class R, class A0, class A1, class A2, class A3>
  227. struct _WrappedMethod4 {
  228. R (T::*f)(A0, A1, A2, A3);
  229. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  230. {
  231. godot_variant v;
  232. godot_variant_new_nil(&v);
  233. T *obj = (T *) user_data;
  234. _WrappedMethod4<T, R, A0, A1, A2, A3> *method = (_WrappedMethod4<T, R, A0, A1, A2, A3>*) method_data;
  235. Variant *var = (Variant *) &v;
  236. Variant **arg = (Variant **) args;
  237. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  238. return v;
  239. }
  240. };
  241. template<class T, class A0, class A1, class A2, class A3>
  242. struct _WrappedMethod4<T, void, A0, A1, A2, A3> {
  243. void (T::*f)(A0, A1, A2, A3);
  244. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  245. {
  246. godot_variant v;
  247. godot_variant_new_nil(&v);
  248. T *obj = (T *) user_data;
  249. _WrappedMethod4<T, void, A0, A1, A2, A3> *method = (_WrappedMethod4<T, void, A0, A1, A2, A3>*) method_data;
  250. Variant **arg = (Variant **) args;
  251. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  252. return v;
  253. }
  254. };
  255. template<class T, class R, class A0, class A1, class A2, class A3>
  256. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  257. {
  258. _WrappedMethod4<T, R, A0, A1, A2, A3> *p = (_WrappedMethod4<T, R, A0, A1, A2, A3> *) malloc(sizeof(_WrappedMethod4<T, R, A0, A1, A2, A3>));
  259. p->f = f;
  260. return (void *) p;
  261. }
  262. template<class T, class R, class A0, class A1, class A2, class A3>
  263. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  264. {
  265. return (__godot_wrapper_method) &_WrappedMethod4<T, R, A0, A1, A2, A3>::__wrapped_method;
  266. }
  267. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  268. struct _WrappedMethod5 {
  269. R (T::*f)(A0, A1, A2, A3, A4);
  270. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  271. {
  272. godot_variant v;
  273. godot_variant_new_nil(&v);
  274. T *obj = (T *) user_data;
  275. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4>*) method_data;
  276. Variant *var = (Variant *) &v;
  277. Variant **arg = (Variant **) args;
  278. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  279. return v;
  280. }
  281. };
  282. template<class T, class A0, class A1, class A2, class A3, class A4>
  283. struct _WrappedMethod5<T, void, A0, A1, A2, A3, A4> {
  284. void (T::*f)(A0, A1, A2, A3, A4);
  285. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  286. {
  287. godot_variant v;
  288. godot_variant_new_nil(&v);
  289. T *obj = (T *) user_data;
  290. _WrappedMethod5<T, void, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, void, A0, A1, A2, A3, A4>*) method_data;
  291. Variant **arg = (Variant **) args;
  292. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  293. return v;
  294. }
  295. };
  296. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  297. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  298. {
  299. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *p = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4> *) malloc(sizeof(_WrappedMethod5<T, R, A0, A1, A2, A3, A4>));
  300. p->f = f;
  301. return (void *) p;
  302. }
  303. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  304. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  305. {
  306. return (__godot_wrapper_method) &_WrappedMethod5<T, R, A0, A1, A2, A3, A4>::__wrapped_method;
  307. }
  308. template<class M>
  309. void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED)
  310. {
  311. godot_instance_method method = {};
  312. method.method_data = ___make_wrapper_function(method_ptr);
  313. method.free_func = free;
  314. method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr);
  315. godot_method_attributes attr = {};
  316. attr.rpc_type = rpc_type;
  317. godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method);
  318. }
  319. template<class T, class P>
  320. struct _PropertySetFunc {
  321. void (T::*f)(P);
  322. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value)
  323. {
  324. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *) method_data;
  325. T *obj = (T *) user_data;
  326. Variant *v = (Variant *) &value;
  327. (obj->*(set_func->f))(*v);
  328. }
  329. };
  330. template<class T, class P>
  331. struct _PropertyGetFunc {
  332. P (T::*f)();
  333. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  334. {
  335. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *) method_data;
  336. T *obj = (T *) user_data;
  337. godot_variant var;
  338. godot_variant_new_nil(&var);
  339. Variant *v = (Variant *) &var;
  340. *v = (obj->*(get_func->f))();
  341. return var;
  342. }
  343. };
  344. template<class T, class P>
  345. void register_property(char *name, void (T::*setter)(P), P (T::*getter)(), P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE)
  346. {
  347. Variant def_val = default_value;
  348. godot_property_attributes attr = {};
  349. attr.type = def_val.get_type();
  350. attr.default_value = *(godot_variant *) &def_val;
  351. attr.hint = hint;
  352. attr.rset_type = rpc_mode;
  353. attr.usage = usage;
  354. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *) malloc(sizeof(_PropertySetFunc<T, P>));
  355. wrapped_set->f = setter;
  356. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *) malloc(sizeof(_PropertyGetFunc<T, P>));
  357. wrapped_get->f = getter;
  358. godot_property_set_func set_func = {};
  359. set_func.method_data = (void *) wrapped_set;
  360. set_func.free_func = free;
  361. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  362. godot_property_get_func get_func = {};
  363. get_func.method_data = (void *) wrapped_get;
  364. get_func.free_func = free;
  365. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  366. godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func);
  367. }
  368. }
  369. #endif // GODOT_H