tr_macro.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * This file contains macros to help generate the bindings
  3. * of functionality to MRuby.
  4. */
  5. #ifndef TR_MACRO_H_INCLUDED
  6. #define TR_MACRO_H_INCLUDED
  7. #define RH_WRAP(RUBY, TYPE, OBJECT) \
  8. mrb_obj_value(Data_Wrap_Struct(RUBY, \
  9. RUBY->object_class, &toruby_##TYPE, OBJECT));
  10. #define TR_NAME(NAME) tr_##NAME
  11. #define TR_PAIR_HELPER(NAME) TR_NAME(NAME), NAME
  12. #define TR_PAIR(NAME) TR_PAIR_HELPER(NAME)
  13. #define TR_MACRO_AID(MACRO, ARGS) MACRO ARGS
  14. #define TR_PAIR_DO_AID(MACRO, NAME) TR_MACRO_AID(MACRO, (TR_PAIR(NAME)))
  15. #define TR_PAIR_DO(MACRO, NAME) TR_PAIR_DO_AID(MACRO, NAME)
  16. #define TORUBY_0_ICALLER(NAME, TOCALL) \
  17. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  18. (void) self; (void) mrb; \
  19. return mrb_fixnum_value(TOCALL()); \
  20. }
  21. #define TORUBY_0_FGETTER(NAME, TOCALL) \
  22. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  23. (void) self; \
  24. return mrb_float_value(mrb, TOCALL()); \
  25. }
  26. #define SCEGRA_ICALLER(NAME, TOCALL) \
  27. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  28. int index; \
  29. (void) self; \
  30. mrb_get_args(mrb, "i", &index); \
  31. return mrb_fixnum_value(TOCALL(index)); \
  32. }
  33. #define SCEGRA_BCALLER(NAME, TOCALL) \
  34. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  35. int index; \
  36. (void) self; \
  37. mrb_get_args(mrb, "i", &index); \
  38. return rh_bool_value(TOCALL(index)); \
  39. }
  40. #define SCEGRA_FCALLER(NAME, TOCALL) \
  41. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  42. mrb_int index; \
  43. (void) self; \
  44. mrb_get_args(mrb, "i", &index); \
  45. return mrb_float_value(mrb, TOCALL(index)); \
  46. }
  47. #define SCEGRA_ISETTER(NAME, TOCALL) \
  48. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  49. int index, value; \
  50. (void) self; \
  51. mrb_get_args(mrb, "ii", &index, &value); \
  52. return mrb_fixnum_value(TOCALL(index, value)); \
  53. }
  54. #define SCEGRA_BSETTER(NAME, TOCALL) \
  55. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  56. int index; mrb_value value; \
  57. (void) self; \
  58. mrb_get_args(mrb, "io", &index, &value); \
  59. return mrb_fixnum_value(TOCALL(index, rh_tobool(value))); \
  60. }
  61. #define SCEGRA_PSETTER(NAME, TOCALL) \
  62. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  63. int index, x, y; \
  64. (void) self; \
  65. mrb_get_args(mrb, "iii", &index, &x, &y); \
  66. return mrb_fixnum_value(TOCALL(index, x, y)); \
  67. }
  68. #define SCEGRA_CSETTER(NAME, TOCALL) \
  69. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  70. int index, r, g, b, a; \
  71. (void) self; \
  72. mrb_get_args(mrb, "iiiii", &index, &r, &g, &b, &a); \
  73. return mrb_fixnum_value(TOCALL(index, r, g, b, a)); \
  74. }
  75. #define SCEGRA_FSETTER(NAME, TOCALL) \
  76. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  77. int index; mrb_float value; \
  78. (void) self; \
  79. mrb_get_args(mrb, "if", &index, &value); \
  80. return mrb_fixnum_value(TOCALL(index, value)); \
  81. }
  82. #define TR_WRAP_NOARG_BOOL(NAME, TOCALL) \
  83. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  84. (void) self; (void) mrb; \
  85. return rh_bool_value(TOCALL()); \
  86. }
  87. #define TR_WRAP_I_BOOL(NAME, TOCALL) \
  88. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  89. mrb_int i1; \
  90. (void) self; \
  91. mrb_get_args(mrb, "i", &i1); \
  92. return rh_bool_value(TOCALL(i1)); \
  93. }
  94. #define TR_WRAP_B_BOOL(NAME, TOCALL) \
  95. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  96. mrb_value b1; \
  97. (void) self; \
  98. mrb_get_args(mrb, "o", &b1); \
  99. return rh_bool_value(TOCALL(rh_tobool(b1))); \
  100. }
  101. #define TR_WRAP_IZ_INT(NAME, TOCALL) \
  102. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  103. mrb_int i1 = -1; \
  104. char * str = NULL; \
  105. (void) self; \
  106. mrb_get_args(mrb, "iz", &i1, &str); \
  107. return mrb_fixnum_value(TOCALL(i1, str)); \
  108. }
  109. #define TR_WRAP_I_INT(NAME, TOCALL) \
  110. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  111. mrb_int i1; \
  112. mrb_get_args(mrb, "i", &i1); \
  113. (void) self; \
  114. return mrb_fixnum_value(TOCALL(i1)); \
  115. }
  116. #define TR_WRAP_II_INT(NAME, TOCALL) \
  117. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  118. mrb_int i1, i2; \
  119. mrb_get_args(mrb, "ii", &i1, &i2); \
  120. (void) self; \
  121. return mrb_fixnum_value(TOCALL(i1, i2)); \
  122. }
  123. #define TR_WRAP_III_INT(NAME, TOCALL) \
  124. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  125. mrb_int i1, i2, i3; \
  126. mrb_get_args(mrb, "iii", &i1, &i2, &i3) \
  127. (void) self; \
  128. return mrb_fixnum_value(TOCALL(i1, i2, i3)); \
  129. }
  130. #define TR_WRAP_IIII_INT(NAME, TOCALL) \
  131. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  132. mrb_int i1, i2, i3, i4; \
  133. mrb_get_args(mrb, "iiii", &i1, &i2, &i3, &i4); \
  134. (void) self; \
  135. return mrb_fixnum_value(TOCALL(i1, i2, i3, i4)); \
  136. }
  137. #define TR_WRAP_IIIIB_INT(NAME, TOCALL) \
  138. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  139. mrb_int i1, i2, i3, i4; \
  140. mrb_value b5; \
  141. (void) self; \
  142. mrb_get_args(mrb, "iiiib", &i1, &i2, &i3, &i4, &b5); \
  143. return mrb_fixnum_value(TOCALL(i1, i2, i3, i4, rh_tobool(b5))); \
  144. }
  145. #define TR_WRAP_NOARG_INT(NAME, TOCALL) \
  146. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  147. (void) self; (void) mrb; \
  148. return mrb_fixnum_value(TOCALL()); \
  149. }
  150. #define TR_SPRITE_GET(SPRITE, STATE, SPRITEID) \
  151. SPRITE = state_sprite(STATE, SPRITEID); \
  152. if (!SPRITE) { \
  153. return mrb_nil_value(); \
  154. }
  155. #define TR_SPRITE_FUNC_INIT(SPRITE, STATE, SPRITEID) \
  156. mrb_int SPRITEID = -1; \
  157. State * STATE = state_get(); \
  158. Sprite * sprite = NULL; \
  159. (void) self;
  160. #define TR_SPRITE_II_INT(NAME, TOCALL) \
  161. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  162. TR_SPRITE_FUNC_INIT(sprite, state, index) \
  163. mrb_int result; \
  164. mrb_int i1, i2; \
  165. mrb_get_args(mrb, "iii", &index, &i1, &i2); \
  166. TR_SPRITE_GET(sprite, state, index); \
  167. result = TOCALL(sprite, i1, i2); \
  168. return mrb_fixnum_value(result); \
  169. }
  170. #define TR_METHOD(MRB, CLASS, NAME, IMPL, FLAGS) \
  171. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
  172. #define TR_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
  173. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
  174. #define TR_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
  175. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
  176. #define TR_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
  177. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
  178. #define TR_CLASS_METHOD(MRB, CLASS, NAME, IMPL, FLAGS)\
  179. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
  180. #define TR_CLASS_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
  181. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
  182. #define TR_CLASS_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
  183. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
  184. #define TR_CLASS_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
  185. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
  186. #define TR_CONST_INT(MRB, CLASS, NAME, VALUE) \
  187. mrb_define_const((MRB), (CLASS), (NAME), mrb_fixnum_value(VALUE))
  188. #define TR_CONST_INT_EASY(MRB, CLASS, PREFIX, NAME) \
  189. TR_CONST_INT(MRB, CLASS, #NAME, PREFIX##NAME)
  190. #endif // TR_H_INCLUDED