prosperon/source/engine/script.c

67 lines
1.2 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "script.h"
2022-08-01 13:32:58 -05:00
#include "stdio.h"
2022-08-03 17:00:00 -05:00
#include "log.h"
2022-06-21 12:48:19 -05:00
2022-08-07 01:43:45 -05:00
2022-08-01 13:32:58 -05:00
#include "mruby/compile.h"
2022-08-03 17:00:00 -05:00
#include "mrbffi.h"
2022-06-21 12:48:19 -05:00
2022-08-03 17:00:00 -05:00
mrb_value obj;
2021-11-30 21:29:18 -06:00
2022-08-03 17:00:00 -05:00
mrb_state *mrb;
mrbc_context *c;
2021-11-30 21:29:18 -06:00
2022-08-01 13:32:58 -05:00
void script_init() {
mrb = mrb_open();
2022-08-03 17:00:00 -05:00
c = mrbc_context_new(mrb);
ffi_load();
2021-11-30 21:29:18 -06:00
}
2022-08-01 13:32:58 -05:00
void script_run(const char *script) {
mrb_load_string(mrb, script);
2022-02-06 10:14:57 -06:00
}
2022-08-01 13:32:58 -05:00
2022-11-20 15:50:14 -06:00
int script_dofile(const char *file) {
2022-08-01 13:32:58 -05:00
FILE *mrbf = fopen(file, "r");
2022-08-03 17:00:00 -05:00
if (mrbf == NULL) {
2022-11-20 15:50:14 -06:00
YughError("Could not find script %s.", file);
return 1;
2022-08-03 17:00:00 -05:00
}
mrbc_filename(mrb, c, file);
obj = mrb_load_file_cxt(mrb, mrbf, c);
mrb_print_error(mrb);
2022-08-01 13:32:58 -05:00
fclose(mrbf);
2022-11-20 15:50:14 -06:00
return 0;
2022-08-03 17:00:00 -05:00
}
2022-08-07 01:43:45 -05:00
void script_update(double dt) {
mrb_funcall(mrb, obj, "update", 1, mrb_float_value(mrb, dt));
2022-08-05 14:23:39 -05:00
mrb_print_error(mrb);
2022-08-03 17:00:00 -05:00
}
void script_draw() {
mrb_funcall(mrb, obj, "draw", 0);
2022-08-05 14:23:39 -05:00
mrb_print_error(mrb);
2022-08-03 20:49:56 -05:00
}
void script_editor() {
mrb_funcall(mrb, obj, "editor", 0);
2022-08-05 14:23:39 -05:00
mrb_print_error(mrb);
}
void script_call(const char *f) {
2022-08-07 01:43:45 -05:00
script_call_sym(mrb_intern_cstr(mrb, f));
}
void script_call_sym(mrb_sym sym)
{
if (mrb_respond_to(mrb, obj, sym)) mrb_funcall_argv(mrb, obj, sym, 0, NULL);
2022-08-05 14:23:39 -05:00
mrb_print_error(mrb);
2022-08-07 01:43:45 -05:00
}
int script_has_sym(mrb_sym sym) {
return mrb_respond_to(mrb, obj, sym);
2022-08-01 13:32:58 -05:00
}