prosperon/source/engine/script.c

143 lines
3.1 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-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"
s7_scheme *s7 = NULL;
2021-11-30 21:29:18 -06:00
2022-12-28 16:50:54 -06:00
s7_pointer cpvec2s7(cpVect v) {
s7_pointer ret = s7_make_vector(s7, 2);
s7_vector_set(s7, ret, 0, s7_make_real(s7, v.x));
s7_vector_set(s7, ret, 1, s7_make_real(s7, v.y));
2022-12-20 19:34:22 -06:00
2022-12-28 16:50:54 -06:00
return ret;
}
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);
}
}
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]) {
*p = c;
p++;
} else {
2022-12-20 08:16:26 -06:00
*p = '\0';
YughInfo(buffer);
p = buffer;
}
}
2022-08-01 13:32:58 -05:00
void script_init() {
s7 = s7_init();
2022-12-20 08:16:26 -06:00
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
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) {
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) {
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
}
/* 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
}
/* 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
}
/* 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) {
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
}
void script_call_sym(s7_pointer sym)
2022-08-07 01:43:45 -05:00
{
s7_call(s7, sym, s7_nil(s7));
2022-08-07 01:43:45 -05:00
}
2022-12-26 20:57:45 -06:00
void script_call_sym_args(s7_pointer sym, s7_pointer args)
{
2022-12-27 12:52:25 -06:00
s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
2022-12-26 20:57:45 -06:00
}
int script_has_sym(s7_pointer sym) {
return 1;
}
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
}