Added scripting

This commit is contained in:
John Alanbrook 2022-08-03 22:00:00 +00:00
parent d016554841
commit 2781f09626
2 changed files with 24 additions and 13 deletions

View file

@ -1,26 +1,21 @@
#include "script.h" #include "script.h"
#include "stdio.h" #include "stdio.h"
#include "log.h"
#include "mruby.h" #include "mruby.h"
#include "mruby/compile.h" #include "mruby/compile.h"
#include "mrbffi.h"
static mrb_state *mrb; mrb_value obj;
int fib(int n) { mrb_state *mrb;
if (n < 2) return n; mrbc_context *c;
return fib(n-1) + fib(n-2);
}
mrb_value mrb_fib(mrb_state *mrb, mrb_value self) {
int n;
mrb_get_args(mrb, "i", &n);
return mrb_fixnum_value(fib(n));
}
void script_init() { void script_init() {
mrb = mrb_open(); mrb = mrb_open();
mrb_define_method(mrb, mrb->object_class, "fib", mrb_fib, MRB_ARGS_REQ(1)); c = mrbc_context_new(mrb);
ffi_load();
} }
void script_run(const char *script) { void script_run(const char *script) {
@ -29,6 +24,20 @@ void script_run(const char *script) {
void script_dofile(const char *file) { void script_dofile(const char *file) {
FILE *mrbf = fopen(file, "r"); FILE *mrbf = fopen(file, "r");
mrb_load_file(mrb, mrbf); if (mrbf == NULL) {
YughError("Could not find game.rb in root folder.",0);
return;
}
mrbc_filename(mrb, c, file);
obj = mrb_load_file_cxt(mrb, mrbf, c);
mrb_print_error(mrb);
fclose(mrbf); fclose(mrbf);
} }
void script_update() {
mrb_funcall(mrb, obj, "update", 0);
}
void script_draw() {
mrb_funcall(mrb, obj, "draw", 0);
}

View file

@ -4,5 +4,7 @@
void script_init(); void script_init();
void script_run(const char *script); void script_run(const char *script);
void script_dofile(const char *file); void script_dofile(const char *file);
void script_update();
void script_draw();
#endif #endif