callrb.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /* Sprite event handler. Calls an mruby callback. */
  10. int callrb_sprite_event(SpriteState * spritestate, int kind, void * data) {
  11. mrb_value res;
  12. Sprite * sprite;
  13. State * state;
  14. int spriteid, thingid, pose, direction;
  15. Ruby * ruby;
  16. void * thing;
  17. sprite = spritestate_sprite(spritestate);
  18. spriteid = sprite_id(sprite);
  19. thing = spritestate_data(spritestate);
  20. pose = spritestate_pose(spritestate);
  21. direction = spritestate_direction(spritestate);
  22. state = state_get();
  23. ruby = state_ruby(state);
  24. res = rh_run_toplevel(ruby, "eruta_on_sprite", "iiii",
  25. spriteid, pose, direction, kind);
  26. (void) data;
  27. return rh_tobool(res);
  28. }
  29. /* Calls the eruta_on_start function */
  30. int callrb_on_start() {
  31. mrb_value res;
  32. State * state = state_get();
  33. Ruby * ruby = state_ruby(state);
  34. res = rh_run_toplevel(ruby, "woe_on_start", "");
  35. return rh_tobool(res);
  36. }
  37. /* Calls the eruta_on_reload function */
  38. int callrb_on_reload() {
  39. mrb_value res;
  40. State * state = state_get();
  41. Ruby * ruby = state_ruby(state);
  42. res = rh_run_toplevel(ruby, "woe_on_reload", "");
  43. return rh_tobool(res);
  44. }
  45. /* Calls the eruta_on_update function. */
  46. int callrb_on_update(mrb_ruby * self, double dt) {
  47. mrb_value res;
  48. mrb_value mval = mrb_float_value(self, dt);
  49. res = rh_run_toplevel_args(state_ruby(self), "woe_on_update", 1, &mval);
  50. return rh_tobool(res);
  51. }