toruby.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. BBConsole * console = NULL;
  128. (void) self; (void) mrb;
  129. state = state_get();
  130. mrb_value text = mrb_nil_value();
  131. mrb_get_args(mrb, "S", &text);
  132. monolog_enable_level(RSTRING_PTR(text));
  133. return self;
  134. }
  135. /** Disables a certain log level */
  136. static mrb_value tr_log_disable(mrb_state * mrb, mrb_value self) {
  137. State * state = NULL;
  138. BBConsole * console = NULL;
  139. (void) self; (void) mrb;
  140. state = state_get();
  141. mrb_value text = mrb_nil_value();
  142. mrb_get_args(mrb, "S", &text);
  143. monolog_disable_level(RSTRING_PTR(text));
  144. return self;
  145. }
  146. /* Loads another script from the script directory. Reports to the
  147. console or stderr if no console available. */
  148. static mrb_value tr_script(mrb_state * mrb, mrb_value self) {
  149. int res;
  150. char * command;
  151. State * state = state_get();
  152. (void) self;
  153. mrb_value text = mrb_nil_value();
  154. mrb_get_args(mrb, "S", &text);
  155. command = mrb_str_to_cstr(mrb, text);
  156. res = rh_run_script(mrb, command);
  157. return mrb_fixnum_value(res);
  158. }
  159. /* Sets the active map for the engine */
  160. static mrb_value tr_active_map_(mrb_state * mrb, mrb_value self) {
  161. Tilemap * map = NULL;
  162. int id;
  163. State * state = state_get();
  164. (void) self; (void) mrb;
  165. mrb_int index = -1;
  166. mrb_get_args(mrb, "i", &index);
  167. // Negative index means "disable the map"
  168. id = state_active_map_id_(state, index);
  169. if (index < 0) {
  170. return mrb_nil_value();
  171. }
  172. return mrb_fixnum_value(id);
  173. }
  174. /* Gets the active map for the state */
  175. static mrb_value tr_active_map(mrb_state * mrb, mrb_value self) {
  176. int id;
  177. (void) self; (void) mrb;
  178. State * state = state_get();
  179. id = state_active_map_id(state);
  180. return mrb_fixnum_value(id);
  181. }
  182. /* Wraps an Allegro event for use in ruby into an mruby hash.
  183. static mrb_value tr_eventvalues(mrb_state * mrb , ALLEGRO_EVENT * event,
  184. mrb_value * values, int size) {
  185. int result;
  186. mrb_value aid;
  187. aid = mrb_hash_new(mrb);
  188. // mrb_hash_set(mrb, aid, mrb_intern(mrb, "type"), );
  189. return aid;
  190. }
  191. */
  192. static mrb_value tr_camera_track(mrb_state * mrb, mrb_value self) {
  193. State * state = state_get();
  194. int result;
  195. mrb_int thing_index;
  196. (void) self; (void) mrb;
  197. mrb_get_args(mrb, "i", &thing_index);
  198. result = state_camera_track_(state, thing_index);
  199. return mrb_fixnum_value(result);
  200. }
  201. static mrb_value tr_lockin_maplayer(mrb_state * mrb, mrb_value self) {
  202. State * state = state_get();
  203. int result;
  204. mrb_int layer;
  205. (void) self; (void) mrb;
  206. mrb_get_args(mrb, "i", &layer);
  207. result = state_lockin_maplayer(state, layer);
  208. return mrb_fixnum_value(result);
  209. }
  210. static mrb_value tr_camera_x(mrb_state * mrb, mrb_value self) {
  211. State * state = state_get();
  212. Camera * camera = state_camera(state);
  213. int result;
  214. (void) self; (void) mrb;
  215. result = camera_at_x(camera);
  216. return mrb_fixnum_value(result);
  217. }
  218. static mrb_value tr_camera_y(mrb_state * mrb, mrb_value self) {
  219. State * state = state_get();
  220. Camera * camera = state_camera(state);
  221. int result;
  222. (void) self; (void) mrb;
  223. result = camera_at_y(camera);
  224. return mrb_fixnum_value(result);
  225. }
  226. static mrb_value tr_camera_w(mrb_state * mrb, mrb_value self) {
  227. State * state = state_get();
  228. Camera * camera = state_camera(state);
  229. int result;
  230. (void) self; (void) mrb;
  231. result = camera_w(camera);
  232. return mrb_fixnum_value(result);
  233. }
  234. static mrb_value tr_camera_h(mrb_state * mrb, mrb_value self) {
  235. State * state = state_get();
  236. Camera * camera = state_camera(state);
  237. int result;
  238. (void) self; (void) mrb;
  239. result = camera_w(camera);
  240. return mrb_fixnum_value(result);
  241. }
  242. /* Obsolete, tile maps will be loaded through store.
  243. static mrb_value tr_loadtilemap_vpath(mrb_state * mrb, mrb_value self) {
  244. State * state = state_get();
  245. int result;
  246. mrb_value rvpath;
  247. mrb_get_args(mrb, "S", &rvpath);
  248. result = state_loadtilemap_vpath(state, mrb_str_to_cstr(mrb, rvpath));
  249. return mrb_fixnum_value(result);
  250. }
  251. */
  252. /*
  253. int state_newthingindex(State * state, int kind,
  254. int x, int y, int z, int w, int h);
  255. int state_thing_sprite_(State * state, int thing_index, int sprite_index);
  256. int state_thing_pose_(State * state, int thing_index, int pose);
  257. int state_thing_direction_(State * state, int thing_index, int direction);
  258. int state_actorindex_(State * self, int thing_index);
  259. Thing * state_actor(State * self);
  260. */
  261. /*
  262. tr_getornewsprite
  263. tr_newsprite
  264. tr_sprite
  265. tr_sprite_loadulpcss
  266. tr_newthing
  267. tr_camera_track
  268. tr_lockin_maplayer
  269. tr_loadtilemap_vpath
  270. tr_thing_sprite_
  271. tr_thing_pose_
  272. tr_thing_direction_
  273. tr_actorindex_
  274. tr_actorindex
  275. */
  276. TORUBY_0_FGETTER(tr_get_time, al_get_time)
  277. TR_WRAP_NOARG_BOOL(tr_show_fps, global_state_show_fps)
  278. TR_WRAP_NOARG_BOOL(tr_show_graph, global_state_show_graph)
  279. TR_WRAP_NOARG_BOOL(tr_show_area, global_state_show_area)
  280. TR_WRAP_NOARG_BOOL(tr_show_physics, global_state_show_physics)
  281. TR_WRAP_B_BOOL(tr_show_fps_, global_state_show_fps_)
  282. TR_WRAP_B_BOOL(tr_show_graph_, global_state_show_graph_)
  283. TR_WRAP_B_BOOL(tr_show_area_, global_state_show_area_)
  284. TR_WRAP_B_BOOL(tr_show_physics_, global_state_show_physics_)
  285. TR_WRAP_B_BOOL(tr_show_mouse_cursor_, scegra_show_system_mouse_cursor)
  286. /* Initializes the functionality that Eruta exposes to Ruby. */
  287. int tr_init(mrb_state * mrb) {
  288. // luaL_dostring(lua, "print 'Hello!' ");
  289. struct RClass *krn;
  290. struct RClass *pth;
  291. struct RClass *sto;
  292. struct RClass *gra;
  293. struct RClass *spr;
  294. struct RClass *thi;
  295. struct RClass *eru;
  296. struct RClass *aud;
  297. eru = mrb_define_module(mrb, "Eruta");
  298. pth = mrb_define_class_under(mrb, eru, "Path" , mrb->object_class);
  299. MRB_SET_INSTANCE_TT(pth, MRB_TT_DATA);
  300. /* Define some constants. */
  301. TR_CONST_INT(mrb, eru, "FLIP_HORIZONTAL", ALLEGRO_FLIP_HORIZONTAL);
  302. TR_CONST_INT(mrb, eru, "FLIP_VERTICAL" , ALLEGRO_FLIP_VERTICAL);
  303. TR_CONST_INT(mrb, eru, "ALIGN_LEFT" , ALLEGRO_ALIGN_LEFT);
  304. TR_CONST_INT(mrb, eru, "ALIGN_CENTRE" , ALLEGRO_ALIGN_CENTRE);
  305. TR_CONST_INT(mrb, eru, "ALIGN_CENTER" , ALLEGRO_ALIGN_CENTER);
  306. TR_CONST_INT(mrb, eru, "ALIGN_RIGHT" , ALLEGRO_ALIGN_RIGHT);
  307. TR_CONST_INT(mrb, eru, "ALIGN_INTEGER" , ALLEGRO_ALIGN_INTEGER);
  308. TR_CLASS_METHOD_NOARG(mrb, eru, "quit" , tr_state_done);
  309. krn = mrb_module_get(mrb, "Kernel");
  310. if(!krn) return -1;
  311. TR_METHOD_ARGC(mrb, krn, "test" , tr_test , 1);
  312. TR_METHOD_ARGC(mrb, krn, "warn" , tr_warn , 1);
  313. TR_METHOD_ARGC(mrb, krn, "warning" , tr_warn , 1);
  314. TR_METHOD_ARGC(mrb, krn, "log" , tr_log , 1);
  315. TR_METHOD_ARGC(mrb, krn, "log_to" , tr_log_to , 2);
  316. TR_METHOD_ARGC(mrb, krn, "log_enable" , tr_log_disable , 1);
  317. TR_METHOD_ARGC(mrb, krn, "log_disable" , tr_log_enable , 1);
  318. TR_METHOD_ARGC(mrb, krn, "script" , tr_script , 1);
  319. TR_METHOD_ARGC(mrb, krn, "camera_track" , tr_camera_track, 1);
  320. TR_METHOD_ARGC(mrb, krn, "camera_lockin", tr_lockin_maplayer, 1);
  321. TR_METHOD_NOARG(mrb, krn, "camera_x" , tr_camera_x);
  322. TR_METHOD_NOARG(mrb, krn, "camera_y" , tr_camera_y);
  323. TR_METHOD_NOARG(mrb, krn, "camera_w" , tr_camera_w);
  324. TR_METHOD_NOARG(mrb, krn, "camera_h" , tr_camera_h);
  325. /*
  326. */
  327. TR_METHOD_NOARG(mrb, krn, "active_map", tr_active_map);
  328. TR_METHOD_ARGC(mrb, krn, "active_map_", tr_active_map_, 1);
  329. TR_CLASS_METHOD_NOARG(mrb, eru, "active_map", tr_active_map);
  330. TR_CLASS_METHOD_ARGC(mrb, eru, "active_map_", tr_active_map_, 1);
  331. TR_CLASS_METHOD_NOARG(mrb, eru, "show_fps", tr_show_fps);
  332. TR_CLASS_METHOD_NOARG(mrb, eru, "show_area", tr_show_area);
  333. TR_CLASS_METHOD_NOARG(mrb, eru, "show_graph", tr_show_graph);
  334. TR_CLASS_METHOD_NOARG(mrb, eru, "show_physics", tr_show_physics);
  335. TR_CLASS_METHOD_ARGC(mrb, eru, "show_fps=" , tr_show_fps_, 1);
  336. TR_CLASS_METHOD_ARGC(mrb, eru, "show_area=" , tr_show_area_, 1);
  337. TR_CLASS_METHOD_ARGC(mrb, eru, "show_graph=", tr_show_graph_, 1);
  338. TR_CLASS_METHOD_NOARG(mrb, eru, "show_physics=", tr_show_physics_);
  339. TR_CLASS_METHOD_ARGC(mrb, eru, "show_mouse_cursor=", tr_show_mouse_cursor_, 1);
  340. TR_CLASS_METHOD_NOARG(mrb, eru, "time", tr_get_time);
  341. /* Set up submodules. */
  342. tr_sprite_init(mrb, eru);
  343. tr_thing_init(mrb, eru);
  344. tr_store_init(mrb, eru);
  345. tr_graph_init(mrb, eru);
  346. tr_audio_init(mrb, eru);
  347. // must restore gc area here ????
  348. mrb_gc_arena_restore(mrb, 0);
  349. return 0;
  350. }