Godot.hpp 13 KB

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