callrb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Callbacks from the C side into the mruby side.
  2. * Used to signal several events such as collisions or sprite
  3. * animation.
  4. */
  5. #include "state.h"
  6. #include "rh.h"
  7. #include "spritestate.h"
  8. #include "callrb.h"
  9. /* Collision handler for things. Calls an mruby callback depending on the kind
  10. * of hulls that are coll. */
  11. int callrb_collide_things(Thing * t1, Thing * t2, int kind, void * data) {
  12. mrb_value res;
  13. State * state;
  14. int ti1, ti2;
  15. Ruby * ruby;
  16. ti1 = thing_id(t1);
  17. ti2 = thing_id(t2);
  18. state = state_get();
  19. ruby = state_ruby(state);
  20. res = rh_run_toplevel(ruby, "eruta_on_bump", "iii", ti1, ti2, kind);
  21. (void) data;
  22. return rh_tobool(res);
  23. }
  24. /* Collision handler for hulls. Calls an mruby callback depending on the kind
  25. * of the hulls. */
  26. int callrb_collide_hulls(BumpHull * h1, BumpHull * h2, int kind, void * data) {
  27. mrb_value res;
  28. State * state;
  29. int hi1, hi2, ti1, ti2, k1, k2;
  30. Ruby * ruby;
  31. char * mename = "eruta_on_bump";
  32. hi1 = bumphull_id(h1);
  33. hi2 = bumphull_id(h2);
  34. ti1 = thing_id(bumphull_body_data(h1));
  35. ti2 = thing_id(bumphull_body_data(h2));
  36. k1 = bumphull_kind(h1);
  37. k2 = bumphull_kind(h2);
  38. state = state_get();
  39. ruby = state_ruby(state);
  40. res = rh_run_toplevel(ruby, "eruta_on_bump", "iiiii", ti1, ti2, hi1, hi2, kind);
  41. (void) data;
  42. return rh_tobool(res);
  43. }
  44. /* Sprite event handler. Calls an mruby callback. */
  45. int callrb_sprite_event(SpriteState * spritestate, int kind, void * data) {
  46. mrb_value res;
  47. Sprite * sprite;
  48. State * state;
  49. Thing * thing;
  50. int spriteid, thingid, pose, direction;
  51. Ruby * ruby;
  52. sprite = spritestate_sprite(spritestate);
  53. spriteid = sprite_id(sprite);
  54. thing = spritestate_data(spritestate);
  55. pose = spritestate_pose(spritestate);
  56. direction = spritestate_direction(spritestate);
  57. thingid = thing_id(thing);
  58. state = state_get();
  59. ruby = state_ruby(state);
  60. res = rh_run_toplevel(ruby, "eruta_on_sprite", "iiiii",
  61. spriteid, thingid, pose, direction, kind);
  62. (void) data;
  63. return rh_tobool(res);
  64. }
  65. /* Calls the eruta_on_start function */
  66. int callrb_on_start() {
  67. mrb_value res;
  68. State * state = state_get();
  69. Ruby * ruby = state_ruby(state);
  70. res = rh_run_toplevel(ruby, "eruta_on_start", "");
  71. return rh_tobool(res);
  72. }
  73. /* Calls the eruta_on_reload function */
  74. int callrb_on_reload() {
  75. mrb_value res;
  76. State * state = state_get();
  77. Ruby * ruby = state_ruby(state);
  78. res = rh_run_toplevel(ruby, "eruta_on_reload", "");
  79. return rh_tobool(res);
  80. }
  81. /* Calls the eruta_on_update function. */
  82. int callrb_on_update(State * self) {
  83. mrb_value res;
  84. mrb_value mval = mrb_float_value(state_ruby(self), state_frametime(self));
  85. res = rh_run_toplevel_args(state_ruby(self), "eruta_on_update", 1, &mval);
  86. return rh_tobool(res);
  87. }
  88. /* GUI vent handler. Calls the mruby callback eruta_on_gui. */
  89. int callrb_on_gui(zori_id widget_id, int kind, void * data) {
  90. mrb_value res;
  91. State * state = state_get();
  92. Ruby * ruby = state_ruby(state);
  93. res = rh_run_toplevel(ruby, "eruta_on_gui", "ii",
  94. widget_id, kind);
  95. (void) data;
  96. return rh_tobool(res);
  97. }