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-03 17:00:00 -05:00
|
|
|
#include "mrbffi.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_scheme *s7 = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-12-20 19:34:22 -06:00
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
|
2022-12-20 08:16:26 -06:00
|
|
|
static void null_port(s7_scheme *sc, uint8_t c, s7_pointer port) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void my_err(s7_scheme *sc, uint8_t c, s7_pointer port) {
|
|
|
|
static char buffer[1024];
|
|
|
|
static char *p = buffer;
|
|
|
|
if (c != '\n' && p != &buffer[1023]) {
|
|
|
|
*p = c;
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
*p = '\0';
|
|
|
|
if (buffer[0] != '\n')
|
|
|
|
YughError(buffer);
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
//YughInfo("File %s, line %d", s7_port_filename(sc, port), s7_port_line_number(sc, port));
|
|
|
|
|
|
|
|
int lost = s7_flush_output_port(sc, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 17:30:22 -06:00
|
|
|
static void my_print(s7_scheme *sc, uint8_t c, s7_pointer port) {
|
|
|
|
static char buffer[1024];
|
|
|
|
static char *p = buffer;
|
2022-12-20 08:16:26 -06:00
|
|
|
if (c != '\n' && p != &buffer[1023]) {
|
2022-12-15 17:30:22 -06:00
|
|
|
*p = c;
|
|
|
|
p++;
|
|
|
|
} else {
|
2022-12-20 08:16:26 -06:00
|
|
|
*p = '\0';
|
2022-12-15 17:30:22 -06:00
|
|
|
YughInfo(buffer);
|
|
|
|
p = buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_init() {
|
2022-12-12 10:48:21 -06:00
|
|
|
s7 = s7_init();
|
2022-12-20 08:16:26 -06:00
|
|
|
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
|
2022-12-15 17:30:22 -06:00
|
|
|
s7_set_current_output_port(s7, s7_open_output_function(s7, my_print));
|
2022-08-03 17:00:00 -05:00
|
|
|
ffi_load();
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_run(const char *script) {
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_eval_c_string(s7, 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-12-15 17:30:22 -06:00
|
|
|
if (!s7_load(s7, file)) {
|
|
|
|
YughError("Can't find file %s.", file);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-11-20 15:50:14 -06:00
|
|
|
return 0;
|
2022-08-03 17:00:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call the "update" function in the master game script */
|
2022-08-07 01:43:45 -05:00
|
|
|
void script_update(double dt) {
|
2022-08-03 17:00:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call the "draw" function in master game script */
|
2022-08-03 17:00:00 -05:00
|
|
|
void script_draw() {
|
2022-08-03 20:49:56 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call "editor" function in master game script */
|
2022-08-03 20:49:56 -05:00
|
|
|
void script_editor() {
|
2022-08-05 14:23:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void script_call(const char *f) {
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-19 18:15:38 -06:00
|
|
|
void script_eval_w_env(const char *s, s7_pointer env) {
|
|
|
|
char buffer[512];
|
|
|
|
snprintf(buffer, 512-1, "(%s)", s);
|
2022-12-20 08:16:26 -06:00
|
|
|
|
|
|
|
s7_set_current_error_port(s7, s7_open_output_function(s7, null_port));
|
2022-12-19 18:15:38 -06:00
|
|
|
s7_eval_c_string_with_environment(s7, buffer, env);
|
2022-12-20 08:16:26 -06:00
|
|
|
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
|
2022-12-19 18:15:38 -06:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
void script_call_sym(s7_pointer sym)
|
2022-08-07 01:43:45 -05:00
|
|
|
{
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_call(s7, sym, s7_nil(s7));
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
int script_has_sym(s7_pointer sym) {
|
|
|
|
return 1;
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
2022-12-19 09:12:34 -06:00
|
|
|
|
2022-12-20 19:34:22 -06:00
|
|
|
|
|
|
|
s7_pointer *updates;
|
|
|
|
s7_pointer *guis;
|
|
|
|
s7_pointer *physics;
|
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
void register_update(s7_pointer sym) {
|
|
|
|
arrput(updates, sym);
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_updates(double dt) {
|
2022-12-19 09:12:34 -06:00
|
|
|
for (int i = 0; i < arrlen(updates); i++)
|
2022-12-22 16:58:06 -06:00
|
|
|
s7_call(s7, updates[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
|
2022-12-20 19:34:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_gui(s7_pointer sym) {
|
|
|
|
arrput(guis, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_gui() {
|
|
|
|
for (int i = 0; i < arrlen(guis); i++)
|
|
|
|
script_call_sym(guis[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_physics(s7_pointer sym) {
|
|
|
|
arrput(physics, sym);
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_physics(double dt) {
|
2022-12-20 19:34:22 -06:00
|
|
|
for (int i = 0; i < arrlen(physics); i++)
|
2022-12-22 16:58:06 -06:00
|
|
|
s7_call(s7, physics[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
|
2022-12-19 09:12:34 -06:00
|
|
|
}
|