toruby.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * toruby.c helps expose functionality from C to Mruby for Eruta.
  3. * All functions are prefixed with tr_.
  4. * Look at the tr_*.c files.
  5. * */
  6. #include "eruta.h"
  7. #include "toruby.h"
  8. #include "rh.h"
  9. #include "state.h"
  10. #include "image.h"
  11. #include "fifi.h"
  12. #include "store.h"
  13. #include "scegra.h"
  14. #include "sound.h"
  15. #include "camera.h"
  16. #include "monolog.h"
  17. #include <mruby/hash.h>
  18. #include <mruby/class.h>
  19. #include <mruby/data.h>
  20. #include <mruby/array.h>
  21. #include "tr_macro.h"
  22. #include "tr_audio.h"
  23. #include "tr_graph.h"
  24. #include "tr_store.h"
  25. #include "tr_sprite.h"
  26. #include "tr_thing.h"
  27. /* Documentation of mrb_get_args:
  28. retrieve arguments from mrb_state.
  29. mrb_get_args(mrb, format, ...)
  30. returns number of arguments parsed.
  31. format specifiers:
  32. o: Object [mrb_value]
  33. S: String [mrb_value]
  34. A: Array [mrb_value]
  35. H: Hash [mrb_value]
  36. s: String [char*,int]
  37. z: String [char*] nul terminated
  38. a: Array [mrb_value*,mrb_int]
  39. f: Float [mrb_float]
  40. i: Integer [mrb_int]
  41. b: Binary [int]
  42. n: Symbol [mrb_sym]
  43. &: Block [mrb_value]
  44. *: rest argument [mrb_value*,int]
  45. |: optional
  46. */
  47. /**
  48. * helper macros
  49. */
  50. void tr_Font_free(mrb_state * mrb, void * ptr) {
  51. (void) mrb;
  52. LOG("Freeing font %p\n", ptr);
  53. font_free((Font*)ptr);
  54. }
  55. struct mrb_data_type toruby_Font_type = { "Font", tr_Font_free };
  56. /*
  57. mrb_value toruby_Font(mrb_state* mrb, mrb_value sel, mrb_value rname) {
  58. Fonr * font = RSTRING(name);
  59. }
  60. */
  61. void tr_Path_free(mrb_state * mrb, void * ptr) {
  62. (void) mrb;
  63. LOG("Freeing path %p\n", ptr);
  64. al_destroy_path((Path*)ptr);
  65. }
  66. struct mrb_data_type toruby_Path = { "Path", tr_Path_free };
  67. /* gets a data path based on a vpath. */
  68. mrb_value tr_Path(Ruby * ruby, mrb_value self, struct RClass * klass) {
  69. ALLEGRO_PATH * path = NULL;
  70. const char * vpath = NULL; int vlen = 0;
  71. (void) self;
  72. mrb_get_args(ruby, "s", &vpath, &vlen);
  73. LOG("Making path for : %s, %d", vpath, vlen);
  74. path = fifi_data_vpath(vpath);
  75. if(!path) return mrb_nil_value();
  76. return mrb_obj_value(Data_Wrap_Struct(ruby, klass, &toruby_Path, path));
  77. }
  78. /* Test method. */
  79. static mrb_value tr_test(mrb_state * mrb, mrb_value self) {
  80. (void) self; (void) mrb;
  81. LOG("Hello from a mruby test!\n");
  82. return self;
  83. }
  84. /* Stops the engine by calling state_done */
  85. static mrb_value tr_state_done(mrb_state * mrb, mrb_value self) {
  86. State * state = NULL;
  87. (void) self; (void) mrb;
  88. state = state_get();
  89. if (state) {
  90. state_done(state);
  91. return self;
  92. } else {
  93. return mrb_nil_value();
  94. }
  95. }
  96. /** Writes a NOTE message to to log. */
  97. static mrb_value tr_log(mrb_state * mrb, mrb_value self) {
  98. State * state = NULL;
  99. (void) self; (void) mrb;
  100. mrb_value text = mrb_nil_value();
  101. mrb_get_args(mrb, "S", &text);
  102. LOG_NOTE("%s\n", RSTRING_PTR(text));
  103. return self;
  104. }
  105. /** Writes a messageto a certain log level log. */
  106. static mrb_value tr_log_to(mrb_state * mrb, mrb_value self) {
  107. State * state = NULL;
  108. (void) self; (void) mrb;
  109. mrb_value level = mrb_nil_value();
  110. mrb_value text = mrb_nil_value();
  111. mrb_get_args(mrb, "SS", &level, &text);
  112. LOG_LEVEL(RSTRING_PTR(level), "%s\n", RSTRING_PTR(text));
  113. return self;
  114. }
  115. /** Cause a warning to be logged */
  116. static mrb_value tr_warn(mrb_state * mrb, mrb_value self) {
  117. State * state = NULL;
  118. (void) self; (void) mrb;
  119. mrb_value text = mrb_nil_value();
  120. mrb_get_args(mrb, "S", &text);
  121. LOG_WARNING("%s\n", RSTRING_PTR(text));
  122. return self;
  123. }
  124. /** Enables a certain log level */
  125. static mrb_value tr_log_enable(mrb_state * mrb, mrb_value self) {
  126. State * state = NULL;
  127. (void) self; (void) mrb;
  128. state = state_get();
  129. mrb_value text = mrb_nil_value();
  130. mrb_get_args(mrb, "S", &text);
  131. monolog_enable_level(RSTRING_PTR(text));
  132. return self;
  133. }
  134. /** Disables a certain log level */
  135. static mrb_value tr_log_disable(mrb_state * mrb, mrb_value self) {
  136. State * state = NULL;
  137. (void) self; (void) mrb;
  138. state = state_get();
  139. mrb_value text = mrb_nil_value();
  140. mrb_get_args(mrb, "S", &text);
  141. monolog_disable_level(RSTRING_PTR(text));
  142. return self;
  143. }
  144. /* Loads another script from the script directory. Reports to the
  145. console or stderr if no console available. */
  146. static mrb_value tr_script(mrb_state * mrb, mrb_value self) {
  147. int res;
  148. char * command;
  149. State * state = state_get();
  150. (void) self;
  151. mrb_value text = mrb_nil_value();
  152. mrb_get_args(mrb, "S", &text);
  153. command = mrb_str_to_cstr(mrb, text);
  154. res = rh_run_script(mrb, command);
  155. return mrb_fixnum_value(res);
  156. }
  157. /* Sets the active map for the engine */
  158. static mrb_value tr_active_map_(mrb_state * mrb, mrb_value self) {
  159. Tilemap * map = NULL;
  160. int id;
  161. State * state = state_get();
  162. (void) self; (void) mrb;
  163. mrb_int index = -1;
  164. mrb_get_args(mrb, "i", &index);
  165. // Negative index means "disable the map"
  166. id = state_active_map_id_(state, index);
  167. if (index < 0) {
  168. return mrb_nil_value();
  169. }
  170. return mrb_fixnum_value(id);
  171. }
  172. /* Gets the active map for the state */
  173. static mrb_value tr_active_map(mrb_state * mrb, mrb_value self) {
  174. int id;
  175. (void) self; (void) mrb;
  176. State * state = state_get();
  177. id = state_active_map_id(state);
  178. return mrb_fixnum_value(id);
  179. }
  180. /* Wraps an Allegro event for use in ruby into an mruby hash.
  181. static mrb_value tr_eventvalues(mrb_state * mrb , ALLEGRO_EVENT * event,
  182. mrb_value * values, int size) {
  183. int result;
  184. mrb_value aid;
  185. aid = mrb_hash_new(mrb);
  186. // mrb_hash_set(mrb, aid, mrb_intern(mrb, "type"), );
  187. return aid;
  188. }
  189. */
  190. static mrb_value tr_camera_track(mrb_state * mrb, mrb_value self) {
  191. State * state = state_get();
  192. int result;
  193. mrb_int thing_index;
  194. (void) self; (void) mrb;
  195. mrb_get_args(mrb, "i", &thing_index);
  196. result = state_camera_track_(state, thing_index);
  197. return mrb_fixnum_value(result);
  198. }
  199. static mrb_value tr_lockin_maplayer(mrb_state * mrb, mrb_value self) {
  200. State * state = state_get();
  201. int result;
  202. mrb_int layer;
  203. (void) self; (void) mrb;
  204. mrb_get_args(mrb, "i", &layer);
  205. result = state_lockin_maplayer(state, layer);
  206. return mrb_fixnum_value(result);
  207. }
  208. static mrb_value tr_camera_x(mrb_state * mrb, mrb_value self) {
  209. State * state = state_get();
  210. Camera * camera = state_camera(state);
  211. int result;
  212. (void) self; (void) mrb;
  213. result = camera_at_x(camera);
  214. return mrb_fixnum_value(result);
  215. }
  216. static mrb_value tr_camera_y(mrb_state * mrb, mrb_value self) {
  217. State * state = state_get();
  218. Camera * camera = state_camera(state);
  219. int result;
  220. (void) self; (void) mrb;
  221. result = camera_at_y(camera);
  222. return mrb_fixnum_value(result);
  223. }
  224. static mrb_value tr_camera_w(mrb_state * mrb, mrb_value self) {
  225. State * state = state_get();
  226. Camera * camera = state_camera(state);
  227. int result;
  228. (void) self; (void) mrb;
  229. result = camera_w(camera);
  230. return mrb_fixnum_value(result);
  231. }
  232. static mrb_value tr_camera_h(mrb_state * mrb, mrb_value self) {
  233. State * state = state_get();
  234. Camera * camera = state_camera(state);
  235. int result;
  236. (void) self; (void) mrb;
  237. result = camera_w(camera);
  238. return mrb_fixnum_value(result);
  239. }
  240. /* Obsolete, tile maps will be loaded through store.
  241. static mrb_value tr_loadtilemap_vpath(mrb_state * mrb, mrb_value self) {
  242. State * state = state_get();
  243. int result;
  244. mrb_value rvpath;
  245. mrb_get_args(mrb, "S", &rvpath);
  246. result = state_loadtilemap_vpath(state, mrb_str_to_cstr(mrb, rvpath));
  247. return mrb_fixnum_value(result);
  248. }
  249. */
  250. /*
  251. int state_newthingindex(State * state, int kind,
  252. int x, int y, int z, int w, int h);
  253. int state_thing_sprite_(State * state, int thing_index, int sprite_index);
  254. int state_thing_pose_(State * state, int thing_index, int pose);
  255. int state_thing_direction_(State * state, int thing_index, int direction);
  256. int state_actorindex_(State * self, int thing_index);
  257. Thing * state_actor(State * self);
  258. */
  259. /*
  260. tr_getornewsprite
  261. tr_newsprite
  262. tr_sprite
  263. tr_sprite_loadulpcss
  264. tr_newthing
  265. tr_camera_track
  266. tr_lockin_maplayer
  267. tr_loadtilemap_vpath
  268. tr_thing_sprite_
  269. tr_thing_pose_
  270. tr_thing_direction_
  271. tr_actorindex_
  272. tr_actorindex
  273. */
  274. TORUBY_0_FGETTER(tr_get_time, al_get_time)
  275. TR_WRAP_NOARG_BOOL(tr_show_fps, global_state_show_fps)
  276. TR_WRAP_NOARG_BOOL(tr_show_graph, global_state_show_graph)
  277. TR_WRAP_NOARG_BOOL(tr_show_area, global_state_show_area)
  278. TR_WRAP_NOARG_BOOL(tr_show_physics, global_state_show_physics)
  279. TR_WRAP_B_BOOL(tr_show_fps_, global_state_show_fps_)
  280. TR_WRAP_B_BOOL(tr_show_graph_, global_state_show_graph_)
  281. TR_WRAP_B_BOOL(tr_show_area_, global_state_show_area_)
  282. TR_WRAP_B_BOOL(tr_show_physics_, global_state_show_physics_)
  283. TR_WRAP_B_BOOL(tr_show_mouse_cursor_, scegra_show_system_mouse_cursor)
  284. /* Initializes the functionality that Eruta exposes to Ruby. */
  285. int tr_init(mrb_state * mrb) {
  286. // luaL_dostring(lua, "print 'Hello!' ");
  287. struct RClass *krn;
  288. struct RClass *pth;
  289. struct RClass *sto;
  290. struct RClass *gra;
  291. struct RClass *spr;
  292. struct RClass *thi;
  293. struct RClass *eru;
  294. struct RClass *aud;
  295. eru = mrb_define_module(mrb, "Eruta");
  296. pth = mrb_define_class_under(mrb, eru, "Path" , mrb->object_class);
  297. MRB_SET_INSTANCE_TT(pth, MRB_TT_DATA);
  298. /* Define some constants. */
  299. TR_CONST_INT(mrb, eru, "FLIP_HORIZONTAL", ALLEGRO_FLIP_HORIZONTAL);
  300. TR_CONST_INT(mrb, eru, "FLIP_VERTICAL" , ALLEGRO_FLIP_VERTICAL);
  301. TR_CONST_INT(mrb, eru, "ALIGN_LEFT" , ALLEGRO_ALIGN_LEFT);
  302. TR_CONST_INT(mrb, eru, "ALIGN_CENTRE" , ALLEGRO_ALIGN_CENTRE);
  303. TR_CONST_INT(mrb, eru, "ALIGN_CENTER" , ALLEGRO_ALIGN_CENTER);
  304. TR_CONST_INT(mrb, eru, "ALIGN_RIGHT" , ALLEGRO_ALIGN_RIGHT);
  305. TR_CONST_INT(mrb, eru, "ALIGN_INTEGER" , ALLEGRO_ALIGN_INTEGER);
  306. TR_CLASS_METHOD_NOARG(mrb, eru, "quit" , tr_state_done);
  307. krn = mrb_module_get(mrb, "Kernel");
  308. if(!krn) return -1;
  309. TR_METHOD_ARGC(mrb, krn, "test" , tr_test , 1);
  310. TR_METHOD_ARGC(mrb, krn, "warn" , tr_warn , 1);
  311. TR_METHOD_ARGC(mrb, krn, "warning" , tr_warn , 1);
  312. TR_METHOD_ARGC(mrb, krn, "log" , tr_log , 1);
  313. TR_METHOD_ARGC(mrb, krn, "log_to" , tr_log_to , 2);
  314. TR_METHOD_ARGC(mrb, krn, "log_enable" , tr_log_disable , 1);
  315. TR_METHOD_ARGC(mrb, krn, "log_disable" , tr_log_enable , 1);
  316. TR_METHOD_ARGC(mrb, krn, "script" , tr_script , 1);
  317. TR_METHOD_ARGC(mrb, krn, "camera_track" , tr_camera_track, 1);
  318. TR_METHOD_ARGC(mrb, krn, "camera_lockin", tr_lockin_maplayer, 1);
  319. TR_METHOD_NOARG(mrb, krn, "camera_x" , tr_camera_x);
  320. TR_METHOD_NOARG(mrb, krn, "camera_y" , tr_camera_y);
  321. TR_METHOD_NOARG(mrb, krn, "camera_w" , tr_camera_w);
  322. TR_METHOD_NOARG(mrb, krn, "camera_h" , tr_camera_h);
  323. /*
  324. */
  325. TR_METHOD_NOARG(mrb, krn, "active_map", tr_active_map);
  326. TR_METHOD_ARGC(mrb, krn, "active_map_", tr_active_map_, 1);
  327. TR_CLASS_METHOD_NOARG(mrb, eru, "active_map", tr_active_map);
  328. TR_CLASS_METHOD_ARGC(mrb, eru, "active_map_", tr_active_map_, 1);
  329. TR_CLASS_METHOD_NOARG(mrb, eru, "show_fps", tr_show_fps);
  330. TR_CLASS_METHOD_NOARG(mrb, eru, "show_area", tr_show_area);
  331. TR_CLASS_METHOD_NOARG(mrb, eru, "show_graph", tr_show_graph);
  332. TR_CLASS_METHOD_NOARG(mrb, eru, "show_physics", tr_show_physics);
  333. TR_CLASS_METHOD_ARGC(mrb, eru, "show_fps=" , tr_show_fps_, 1);
  334. TR_CLASS_METHOD_ARGC(mrb, eru, "show_area=" , tr_show_area_, 1);
  335. TR_CLASS_METHOD_ARGC(mrb, eru, "show_graph=", tr_show_graph_, 1);
  336. TR_CLASS_METHOD_NOARG(mrb, eru, "show_physics=", tr_show_physics_);
  337. TR_CLASS_METHOD_ARGC(mrb, eru, "show_mouse_cursor=", tr_show_mouse_cursor_, 1);
  338. TR_CLASS_METHOD_NOARG(mrb, eru, "time", tr_get_time);
  339. /* Set up submodules. */
  340. tr_sprite_init(mrb, eru);
  341. tr_thing_init(mrb, eru);
  342. tr_store_init(mrb, eru);
  343. tr_graph_init(mrb, eru);
  344. tr_audio_init(mrb, eru);
  345. // must restore gc area here ????
  346. mrb_gc_arena_restore(mrb, 0);
  347. return 0;
  348. }