javascript

This commit is contained in:
John Alanbrook 2023-01-10 20:02:24 +00:00
parent 8e8af65125
commit bdde403a66
12 changed files with 278 additions and 759 deletions

View file

@ -479,7 +479,7 @@ static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
struct gameobject *g2 = cpBodyGetUserData(body2);
//script_call_sym_args(go->cbs->begin, s7_make_integer(s7, g2->editor.id));
s7_call(s7, go->cbs->begin, s7_list(s7, 2, s7_make_integer(s7, g2->editor.id), cpvec2s7(cpArbiterGetNormal(arb))));
//s7_call(s7, go->cbs->begin, s7_list(s7, 2, s7_make_integer(s7, g2->editor.id), cpvec2s7(cpArbiterGetNormal(arb))));
return 1;
@ -494,11 +494,11 @@ static void s7_phys_cb_postsolve(cpArbiter *arb, cpSpace *space, void *data) {
}
static void s7_phys_cb_separate(cpArbiter *Arb, cpSpace *space, void *data) {
struct gameobject *go = data;
script_call_sym(go->cbs->separate);
//struct gameobject *go = data;
//script_call_sym(go->cbs->separate);
}
void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb) {
void phys2d_add_handler_type(int cmd, struct gameobject *go, void *cb) {
cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go);
if (!go->cbs)
@ -508,8 +508,8 @@ void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb) {
switch (cmd) {
case 0:
handler->beginFunc = s7_phys_cb_begin;
go->cbs->begin = cb;
//handler->beginFunc = s7_phys_cb_begin;
//go->cbs->begin = cb;
break;
case 1:
@ -519,8 +519,8 @@ void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb) {
break;
case 3:
handler->separateFunc = s7_phys_cb_separate;
go->cbs->separate = cb;
//handler->separateFunc = s7_phys_cb_separate;
//go->cbs->separate = cb;
break;
}
}

View file

@ -2,7 +2,6 @@
#define TWODPHYSICS_H
#include <chipmunk/chipmunk.h>
#include "s7.h"
struct gameobject;
@ -12,8 +11,8 @@ extern int physOn;
extern cpSpace *space;
struct phys_cbs {
s7_pointer begin;
s7_pointer separate;
void *begin;
void *separate;
};
struct phys2d_shape {
@ -100,7 +99,7 @@ void phys2d_init();
void phys2d_update(float deltaT);
void phys2d_apply();
void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb);
void phys2d_add_handler_type(int cmd, struct gameobject *go, void *cb);
void phys2d_set_gravity(float x, float y);
void shape_gui(struct phys2d_shape *shape);

View file

@ -2,12 +2,27 @@
#include "script.h"
#include "string.h"
#include "window.h"
#include "editor.h"
#include "engine.h"
#include "log.h"
#include "input.h"
#include "gameobject.h"
#include "openglrender.h"
#include "2dphysics.h"
#include "sprite.h"
#include "anim.h"
#include "yugine.h"
#include "nuke.h"
cpVect duk2vec2(duk_context *duk, int p) {
cpVect pos;
duk_get_prop_index(duk, p, 0);
pos.x = duk_to_number(duk, -1);
duk_get_prop_index(duk, p, 1);
pos.x = duk_to_number(duk, -2);
pos.y = duk_to_number(duk, -1);
return pos;
@ -20,6 +35,7 @@ duk_ret_t duk_gui_text(duk_context *duk) {
float fpos[2] = {pos.x, pos.y};
float size = duk_to_number(duk, 2);
const float white[3] = {1.f, 1.f, 1.f};
renderText(s, fpos, size, white, 1800);
return 0;
@ -35,6 +51,26 @@ duk_ret_t duk_win_make(duk_context *duk) {
return 1;
}
duk_ret_t duk_cmd(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
switch(cmd) {
case 0:
script_dofile(duk_to_string(duk, 1));
break;
case 1:
set_pawn(duk_get_heapptr(duk, 1));
break;
case 2:
register_gui(duk_get_heapptr(duk, 1));
break;
}
return 0;
}
duk_ret_t duk_sys_cmd(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
@ -84,7 +120,18 @@ duk_ret_t duk_sys_cmd(duk_context *duk) {
return 0;
}
duk_ret_t duk_
duk_ret_t duk_register(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
void *obj = duk_get_pointer(duk, 1);
void *fn = duk_get_pointer(duk, 2);
/* Test a call ... */
duk_push_pointer(duk, fn);
duk_push_pointer(duk, obj);
duk_call_method(duk, 0);
return 0;
}
duk_ret_t duk_make_gameobject(duk_context *duk) {
int g = MakeGameobject();
@ -95,16 +142,108 @@ duk_ret_t duk_make_gameobject(duk_context *duk) {
go->mass = duk_to_number(duk, 2);
go->f = duk_to_number(duk, 3);
go->e = duk_to_number(duk, 4);
go->flipx = duk_to_boolean(5);
go->flipy = duk_to_boolean(6);
go->flipx = duk_to_boolean(duk, 5);
go->flipy = duk_to_boolean(duk, 6);
gameobject_apply(go);
duk_push_int(g);
duk_push_int(duk, g);
return 1;
}
void ffi_load()
{
duk_ret_t duk_loginfo(duk_context *duk) {
const char *s = duk_to_string(duk,0);
YughInfo("%s", s);
return 0;
}
duk_ret_t duk_make_sprite(duk_context *duk) {
int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1);
cpVect pos = duk2vec2(duk, 2);
struct sprite *sp = make_sprite(get_gameobject_from_id(go));
sprite_loadtex(sp, path);
sp->pos[0] = pos.x;
sp->pos[1] = pos.y;
return 0;
}
duk_ret_t duk_set_body(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
int id = duk_to_int(duk, 1);
struct gameobject *go = get_gameobject_from_id(id);
switch (cmd) {
case 0:
gameobject_setangle(go, duk_to_number(duk, 2));
break;
case 1:
cpBodySetType(go->body, duk_to_int(duk, 2));
break;
case 2:
cpBodySetPosition(go->body, duk2vec2(duk, 2));
break;
case 3:
gameobject_move(go, duk2vec2(duk, 2));
break;
case 4:
cpBodyApplyImpulseAtWorldPoint(go->body, duk2vec2(duk, 2), cpBodyGetPosition(go->body));
break;
case 5:
go->flipx = duk_to_boolean(duk, 2);
break;
case 6:
go->flipy = duk_to_boolean(duk, 2);
break;
}
return 0;
}
duk_ret_t duk_q_body(duk_context *duk) {
int q = duk_to_int(duk, 0);
struct gameobject *go = get_gameobject_from_id(duk_to_int(duk, 1));
switch(q) {
case 0:
duk_push_int(duk, cpBodyGetType(go->body));
return 1;
case 1:
vect2duk(cpBodyGetPosition(go->body));
return 1;
case 2:
duk_push_number(duk, cpBodyGetAngle(go->body));
return 1;
}
return 0;
}
#define DUK_FUNC(NAME, ARGS) duk_push_c_function(duk, duk_##NAME, ARGS); duk_put_global_string(duk, #NAME);
void ffi_load()
{
DUK_FUNC(loginfo, 1);
DUK_FUNC(make_gameobject, 7);
DUK_FUNC(set_body, 3);
DUK_FUNC(q_body, 2);
DUK_FUNC(register, 3);
DUK_FUNC(sys_cmd, 1);
DUK_FUNC(win_make, 3);
DUK_FUNC(gui_text, 3);
DUK_FUNC(make_sprite, 3);
DUK_FUNC(cmd, 2);
}

View file

@ -22,6 +22,39 @@ static uint32_t VAO = 0;
struct sFont *font;
static struct shader *shader;
char *slurp_file(const char *filename) {
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *slurp = malloc(fsize);
size_t r = fread(slurp, fsize, 1, f);
fclose(f);
return slurp;
}
char *slurp_text(const char *filename) {
FILE *f = fopen(filename, "r'");
if (!f) return NULL;
char *buf;
long int fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
buf = malloc(fsize+1);
rewind(f);
size_t r = fread(buf, sizeof(char), fsize, f);
buf[r] = '\0';
fclose(f);
return buf;
}
void font_init(struct shader *textshader) {
shader = textshader;

View file

@ -31,5 +31,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
void text_settype(struct sFont *font);
void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw);
char *slurp_file(const char *filename);
char *slurp_text(const char *filename);
#endif

View file

@ -19,10 +19,10 @@ static int *downkeys = NULL;
static int mquit = 0;
static s7_pointer *pawns = NULL;
static void **pawns = NULL;
void set_pawn(s7_pointer menv) {
arrput(pawns, menv);
void set_pawn(void *pawn) {
arrput(pawns, pawn);
YughInfo("Now controling %d pawns.", arrlen(pawns));
}

View file

@ -30,6 +30,6 @@ struct inputaction
int scancode;
};
void set_pawn(s7_pointer env);
void set_pawn(void *pawn);
#endif

View file

@ -1,624 +0,0 @@
#include "mrbffi.h"
#include "s7.h"
#include "font.h"
#include "script.h"
#include "string.h"
#include "window.h"
#include "editor.h"
#include "engine.h"
#include "log.h"
#include "input.h"
#include "gameobject.h"
#include "openglrender.h"
#include "2dphysics.h"
#include "sprite.h"
#include "anim.h"
#include "yugine.h"
#include "s7.h"
#include "nuke.h"
cpVect s7tovec2(s7_scheme *sc, s7_pointer s7vec) {
cpVect ret;
ret.x = s7_real(s7_vector_ref(sc, s7vec, 0));
ret.y = s7_real(s7_vector_ref(sc, s7vec, 1));
return ret;
}
extern s7_scheme *s7;
/* FFI */
s7_pointer s7_ui_label(s7_scheme *sc, s7_pointer args) {
if (s7_is_string(s7_car(args))) {
nuke_label(s7_string(s7_car(args)));
return s7_make_boolean(sc, 1);
}
return s7_wrong_type_arg_error(sc, "ui_label", 1, args, "Should be a string.");
}
s7_pointer s7_ui_btn(s7_scheme *sc, s7_pointer args) {
return s7_make_boolean(sc, nuke_btn(s7_string(s7_car(args))));
}
s7_pointer s7_ui_nel(s7_scheme *sc, s7_pointer args) {
nuke_nel(s7_integer(s7_cadr(args)));
return s7_make_boolean(sc, 1);
}
s7_pointer s7_ui_prop(s7_scheme *sc, s7_pointer args) {
float val = s7_real(s7_cadr(args));
nuke_prop_float(s7_string(s7_car(args)), (float)s7_real(s7_caddr(args)), &val, s7_real(s7_cadddr(args)), s7_real(s7_car(s7_cddddr(args))), s7_real(s7_car(s7_cdr(s7_cddddr(args)))));
return s7_make_real(sc, val);
}
s7_pointer s7_ui_text(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
int len = s7_integer(s7_cadr(args));
char str[len+1];
strncpy(str,s,len);
nuke_edit_str(str);
return s7_make_string(sc, str);
}
s7_pointer s7_gui_text(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
cpVect pos = s7tovec2(sc, s7_cadr(args));
float fpos[2] = {pos.x, pos.y};
float size = s7_real(s7_caddr(args));
const float white[3] = {1.f, 1.f, 1.f};
renderText(s, fpos, size, white, 1800);
return s7_car(args);
}
s7_pointer s7_gui_img(s7_scheme *sc, s7_pointer args) {
const char *img = s7_string(s7_car(args));
cpVect pos = s7tovec2(sc, s7_cadr(args));
gui_draw_img(img, pos.x, pos.y);
return args;
}
s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
double val = s7_real(s7_cadr(args));
switch(cmd) {
case 0: // render fps
renderMS = val;
break;
case 1:
updateMS = val;
break;
case 2:
physMS = val;
break;
case 3:
debug_draw_phys(val);
break;
case 4:
set_timescale(val);
break;
case 5:
add_zoom(val);
break;
}
return args;
}
s7_pointer s7_log(s7_scheme *sc, s7_pointer args) {
int lvl = s7_integer(s7_car(args));
const char *msg = s7_string(s7_cadr(args));
const char *file = s7_string(s7_caddr(args));
int line = s7_integer(s7_cadddr(args));
mYughLog(1, lvl, line, file, msg);
return args;
}
/* Call like (ui_rendertext "string" (xpos ypos) size) */
s7_pointer s7_ui_rendertext(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
s7_pointer s7pos = s7_cadr(args);
cpVect cpos = s7tovec2(sc, s7_cadr(args));
double pos[2] = { cpos.x, cpos.y };
double size = s7_real(s7_caddr(args));
double white[3] = {1.f, 1.f, 1.f};
renderText(s, pos, size, white, 0);
return args;
}
s7_pointer s7_win_cmd(s7_scheme *sc, s7_pointer args) {
int win = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
struct window *w = window_i(win);
/*
3: return win width
4: return win height
*/
switch (cmd) {
case 0: /* toggle fullscreen */
window_togglefullscreen(w);
break;
case 1: /* Fullscreen on */
window_makefullscreen(w);
break;
case 2: /* Fullscreen off */
window_unfullscreen(w);
break;
case 3:
return s7_make_integer(sc, w->width);
break;
case 4:
return s7_make_integer(sc, w->height);
break;
}
return args;
}
s7_pointer s7_win_make(s7_scheme *sc, s7_pointer args) {
const char *title = s7_string(s7_car(args));
int w = s7_integer(s7_cadr(args));
int h = s7_integer(s7_caddr(args));
struct window *win = MakeSDLWindow(title, w, h, 0);
return s7_make_integer(sc, win->id);
}
s7_pointer s7_gen_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
const char *s = s7_string(s7_cadr(args));
/* Branch table for general commands from scheme */
/* 0 : load level */
/* 1: load prefab */
int response = 0;
switch (cmd) {
case 0:
load_level(s);
break;
case 1:
response = gameobject_makefromprefab(s);
break;
}
return s7_make_integer(sc, response);
}
s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
switch (cmd) {
case 0:
quit();
break;
case 1:
sim_start();
break;
case 2:
sim_stop();
break;
case 3:
sim_pause();
break;
case 4:
sim_step();
break;
case 5:
return s7_make_boolean(sc, sim_playing());
case 6:
return s7_make_boolean(sc, sim_paused());
case 7:
return s7_make_integer(sc, MakeGameobject());
case 8:
return s7_make_integer(sc, frame_fps());
case 9: /* Clear the level out */
new_level();
break;
}
return args;
}
s7_pointer s7_sound_cmd(s7_scheme *sc, s7_pointer args) {
int sound = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
switch (cmd) {
case 0: // play
break;
case 1: // pause
break;
case 2: // stop
break;
case 3: // play from beginning
break;
}
return args;
}
s7_pointer s7_gui_hook(s7_scheme *sc, s7_pointer args) {
s7_pointer cb = s7_car(args);
script_call_sym(cb);
return cb;
}
s7_pointer s7_register(s7_scheme *sc, s7_pointer args) {
int hook = s7_integer(s7_car(args));
s7_pointer sym = s7_cadr(args);
s7_gc_protect(sc, sym);
/* 0 : update */
/* 1 : gui */
/* 2 : physics */
switch (hook) {
case 0:
register_update(sym);
break;
case 1:
register_gui(sym);
break;
case 2:
register_physics(sym);
break;
}
return sym;
}
s7_pointer s7_obregister(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
s7_pointer sym = s7_cadr(args);
s7_pointer env = s7_caddr(args);
s7_gc_protect(sc, sym);
s7_gc_protect(sc, env);
register_obupdate(env, sym);
}
s7_pointer s7_set_pawn(s7_scheme *sc, s7_pointer args) {
s7_pointer pawn = s7_car(args);
s7_gc_protect(sc, pawn);
set_pawn(pawn);
return args;
}
s7_pointer s7_set_body(s7_scheme *sc, s7_pointer args) {
int id = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
struct gameobject *go = get_gameobject_from_id(id);
switch (cmd) {
case 0:
gameobject_setangle(go, s7_real(s7_caddr(args)));
break;
case 1:
cpBodySetType(go->body, s7_integer(s7_caddr(args)));
break;
case 2:
cpBodySetPosition(go->body, s7tovec2(sc, s7_caddr(args)));
break;
case 3:
gameobject_move(go, s7tovec2(sc, s7_caddr(args)));
break;
case 4:
cpBodyApplyImpulseAtWorldPoint(go->body, s7tovec2(sc, s7_caddr(args)), cpBodyGetPosition(go->body));
break;
case 5:
go->flipx = s7_boolean(sc, s7_caddr(args)) ? -1 : 1;
break;
case 6:
go->flipy = s7_boolean(sc, s7_caddr(args)) ? -1 : 1;
break;
}
return args;
}
s7_pointer s7_phys_cmd(s7_scheme *sc, s7_pointer args) {
int go = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
s7_pointer env = s7_caddr(args);
if (go == -1) return s7_nil(sc);
phys2d_add_handler_type(cmd, get_gameobject_from_id(go), env);
}
/* Query physics bodies */
s7_pointer s7_phys_q(s7_scheme *sc, s7_pointer args) {
struct gameobject * go = get_gameobject_from_id(s7_integer(s7_car(args)));
int q = s7_integer(s7_cadr(args));
s7_pointer ret;
/* Queries about a body
0: body type of static, dynamic, kinematic
1: body position
2: body rotation
*/
switch(q) {
case 0:
return s7_make_integer(sc, cpBodyGetType(go->body));
case 1:
ret = s7_make_vector(sc, 2);
s7_vector_set(sc, ret, 0, s7_make_real(sc, cpBodyGetPosition(go->body).x));
s7_vector_set(sc, ret, 1, s7_make_real(sc, cpBodyGetPosition(go->body).y));
return ret;
case 2:
return s7_make_real(sc, cpBodyGetAngle(go->body));
}
}
s7_pointer s7_phys_set(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
double x = s7_real(s7_cadr(args));
double y = s7_real(s7_caddr(args));
phys2d_set_gravity(x, y);
}
s7_pointer s7_int_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
int val = s7_integer(s7_cadr(args));
switch (cmd) {
case 0:
set_cam_body(get_gameobject_from_id(val)->body);
break;
}
}
s7_pointer s7_yield(s7_scheme *sc, s7_pointer args) {
/* arg 1: condition
arg 2: function to run
*/
s7_pointer cond = s7_car(args);
s7_pointer func = s7_cadr(args);
}
void timer_s7_call(s7_pointer sym) {
s7_call(s7, sym, s7_nil(s7));
}
s7_pointer s7_timer(s7_scheme *sc, s7_pointer args) {
double delay = s7_real(s7_car(args));
s7_pointer sym = s7_cadr(args);
struct timer *timer = timer_make(delay, timer_s7_call, sym);
timer_start(timer);
return args;
}
s7_pointer s7_timer_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
int id = s7_integer(s7_cadr(args));
struct timer *t = NULL;
switch (cmd) {
case 0:
timer_pause(t);
break;
case 1:
timer_start(t);
break;
case 2:
timer_stop(t);
break;
}
return args;
}
s7_pointer s7_anim(s7_scheme *sc, s7_pointer args) {
s7_pointer prop = s7_car(args);
s7_pointer keyframes = s7_cadr(args);
YughInfo("Animating property %s.", s7_symbol_name(prop));
struct anim a = make_anim();
for (int i = 0; i < s7_list_length(sc, keyframes); i++) {
struct keyframe k;
s7_pointer kf = s7_list_ref(sc, keyframes, i);
k.time = s7_real(s7_car(kf));
k.val = s7_real(s7_cadr(kf));
a = anim_add_keyframe(a, k);
}
for (double i = 0; i < 3.0; i = i + 0.1) {
YughInfo("Val is now %f at time %f", anim_val(a, i), i);
s7_symbol_set_value(sc, prop, s7_make_real(sc, anim_val(a, i)));
}
free_anim(a);
}
s7_pointer s7_anim_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
int body = s7_integer(s7_cadr(args));
s7_pointer sym = s7_caddr(args);
switch (cmd) {
case 0:
YughInfo("Playing animation called %s.", s7_symbol_name(sym));
break;
}
return args;
}
s7_pointer s7_make_gameobject(s7_scheme *sc, s7_pointer args) {
int g = MakeGameobject();
struct gameobject *go = get_gameobject_from_id(g);
go->scale = s7_real(s7_car(args));
go->bodytype = s7_integer(s7_cadr(args));
go->mass = s7_real(s7_caddr(args));
go->f = s7_real(s7_cadddr(args));
go->e = s7_real(s7_list_ref(sc, args, 4));
go->flipx = s7_boolean(sc, s7_list_ref(sc, args, 5)) ? -1 : 1;
go->flipy = s7_boolean(sc, s7_list_ref(sc, args, 6)) ? -1 : 1;
gameobject_apply(go);
return s7_make_integer(sc, g);
}
s7_pointer s7_make_sprite(s7_scheme *sc, s7_pointer args) {
int go = s7_integer(s7_car(args));
const char *path = s7_string(s7_cadr(args));
cpVect pos = s7tovec2(sc, s7_caddr(args));
struct sprite *sp = make_sprite(get_gameobject_from_id(go));
sprite_loadtex(sp, path);
sp->pos[0] = pos.x;
sp->pos[1] = pos.y;
return args;
}
s7_pointer s7_make_box2d(s7_scheme *sc, s7_pointer args) {
int go = s7_integer(s7_car(args));
cpVect size = s7tovec2(sc, s7_cadr(args));
cpVect offset = s7tovec2(sc, s7_caddr(args));
struct phys2d_box *box = Make2DBox(get_gameobject_from_id(go));
box->w = size.x;
box->h = size.y;
box->offset[0] = offset.x;
box->offset[1] = offset.y;
phys2d_boxinit(box, get_gameobject_from_id(go));
return args;
}
s7_pointer s7_make_circ2d(s7_scheme *sc, s7_pointer args) {
int go = s7_integer(s7_car(args));
double radius = s7_real(s7_cadr(args));
cpVect offset = s7tovec2(sc, s7_caddr(args));
struct phys2d_circle *circle = Make2DCircle(get_gameobject_from_id(go));
circle->radius = radius;
circle->offset[0] = offset.x;
circle->offset[1] = offset.y;
phys2d_circleinit(circle, get_gameobject_from_id(go));
return s7_make_integer(sc, get_gameobject_from_id(go));
}
#define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "")
void ffi_load() {
S7_FUNC(ui_label, 1);
S7_FUNC(ui_btn, 1);
S7_FUNC(ui_nel, 1);
S7_FUNC(ui_prop, 6);
S7_FUNC(ui_text, 2);
S7_FUNC(ui_rendertext, 3);
S7_FUNC(gui_text, 3);
S7_FUNC(gui_img, 2);
S7_FUNC(gen_cmd, 2);
S7_FUNC(sys_cmd, 1);
S7_FUNC(settings_cmd, 2);
S7_FUNC(win_cmd, 2);
S7_FUNC(win_make, 3);
S7_FUNC(sound_cmd, 2);
S7_FUNC(gui_hook, 1);
S7_FUNC(register, 2);
S7_FUNC(obregister, 3);
S7_FUNC(set_pawn, 1);
S7_FUNC(set_body, 3);
S7_FUNC(phys_cmd, 3);
S7_FUNC(phys_q, 2);
S7_FUNC(phys_set, 3);
S7_FUNC(int_cmd, 2);
S7_FUNC(log, 4);
S7_FUNC(yield, 2);
S7_FUNC(timer, 2);
S7_FUNC(timer_cmd, 2);
S7_FUNC(anim, 2);
S7_FUNC(anim_cmd, 3);
S7_FUNC(make_gameobject, 7);
S7_FUNC(make_sprite, 3);
S7_FUNC(make_box2d, 3);
S7_FUNC(make_circ2d, 3);
}

View file

@ -1,7 +0,0 @@
#ifndef MRBFFI_H
#define MRBFFI_H
void ffi_load();
#endif

View file

@ -3,7 +3,8 @@
#include "stdio.h"
#include "log.h"
#include "mrbffi.h"
#include "ffi.h"
#include "font.h"
#include "ftw.h"
@ -11,165 +12,144 @@
duk_context *duk = NULL;
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));
duk_idx_t vect2duk(cpVect v) {
duk_idx_t arr = duk_push_array(duk);
duk_push_number(duk, v.x);
duk_put_prop_index(duk, arr, 0);
duk_push_number(duk, v.y);
duk_put_prop_index(duk, arr, 1);
return ret;
return arr;
}
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;
if (c != '\n' && p != &buffer[1023]) {
*p = c;
p++;
} else {
*p = '\0';
YughInfo(buffer);
p = buffer;
}
}
static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
if (typeflag != FTW_F)
return 0;
if (!strcmp(".prefab", strrchr(fpath, '.')))
s7_load(s7, fpath);
script_dofile(fpath);
return 0;
}
void script_init() {
duk = duk_create_heap_default();
/*
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));
*/
ffi_load();
/* Load all prefabs into memory */
script_dofile("scripts/engine.scm");
script_dofile("config.scm");
ftw(".", load_prefab, 10);
script_dofile("scripts/engine.js");
script_dofile("config.js");
//ftw(".", load_prefab, 10);
}
void script_run(const char *script) {
s7_eval_c_string(s7, script);
duk_eval_string(duk, script);
}
int script_dofile(const char *file) {
if (!s7_load(s7, file)) {
const char *script = slurp_text(file);
if (!script) {
YughError("Can't find file %s.", file);
return 1;
}
duk_push_string(duk, script);
free(script);
if (duk_peval(duk) != 0) {
printf("ERROR: %s\n", duk_safe_to_string(duk, -1));
return 1;
}
duk_pop(duk);
return 0;
}
/* Call the "update" function in the master game script */
void script_update(double dt) {
}
/* Call the "draw" function in master game script */
void script_draw() {
}
/* Call "editor" function in master game script */
void script_editor() {
}
/* Call the given function name */
void script_call(const char *f) {
s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
//s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
}
void script_eval_w_env(const char *s, s7_pointer env) {
char buffer[512];
snprintf(buffer, 512-1, "(%s)", s);
void script_eval_w_env(const char *s, void *env) {
duk_push_heapptr(duk, env);
duk_push_string(duk, s);
s7_set_current_error_port(s7, s7_open_output_function(s7, null_port));
YughInfo("pressed %s", s);
s7_pointer oldenv = s7_curlet(s7);
s7_set_curlet(s7, env);
s7_eval_c_string(s7, buffer);
s7_set_curlet(s7, oldenv);
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
if (!duk_has_prop(duk, -2)) {
duk_pop(duk);
return;
}
duk_push_string(duk, s);
duk_call_prop(duk, -2, 0);
duk_pop(duk);
duk_pop(duk);
}
void script_call_sym(s7_pointer sym)
void script_call_sym(void *sym)
{
s7_call(s7, sym, s7_nil(s7));
duk_push_heapptr(duk, sym);
duk_call(duk, 0);
duk_pop(duk);
}
void script_call_sym_args(s7_pointer sym, s7_pointer args)
void script_call_sym_args(void *sym, void *args)
{
s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
}
int script_has_sym(s7_pointer sym) {
return 1;
//s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
}
s7_pointer *updates;
s7_pointer *guis;
s7_pointer *physics;
void **updates;
void **guis;
void **physics;
struct obupdate {
s7_pointer obj;
s7_pointer sym;
void *obj;
void *sym;
};
struct obupdate *obupdates = NULL;
void register_update(s7_pointer sym) {
void register_update(void *sym) {
arrput(updates, sym);
}
void register_obupdate(s7_pointer obj, s7_pointer sym) {
void register_obupdate(void *obj, void *sym) {
struct obupdate ob = {obj, sym};
arrput(obupdates, ob);
}
void call_updates(double dt) {
for (int i = 0; i < arrlen(updates); i++)
s7_call(s7, updates[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
for (int i = 0; i < arrlen(updates); i++) {
duk_push_heapptr(duk, updates[i]);
duk_push_number(duk, dt);
duk_call(duk, 1);
}
for (int i = 0; i < arrlen(obupdates); i++) {
s7_pointer curlet = s7_curlet(s7);
s7_set_curlet(s7, obupdates[i].obj);
s7_call(s7, obupdates[i].sym, s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
s7_set_curlet(s7, curlet);
duk_push_heapptr(duk, obupdates[i].sym);
duk_push_heapptr(duk, obupdates[i].obj);
duk_push_number(duk, dt);
duk_call_method(duk, 1);
}
}
}
void register_gui(s7_pointer sym) {
void register_gui(void *sym) {
arrput(guis, sym);
}
@ -178,11 +158,14 @@ void call_gui() {
script_call_sym(guis[i]);
}
void register_physics(s7_pointer sym) {
void register_physics(void *sym) {
arrput(physics, sym);
}
void call_physics(double dt) {
for (int i = 0; i < arrlen(physics); i++)
s7_call(s7, physics[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
for (int i = 0; i < arrlen(physics); i++) {
duk_push_pointer(duk, physics[i]);
duk_push_number(duk, dt);
duk_call(duk, 1);
}
}

View file

@ -13,21 +13,21 @@ void script_update(double dt);
void script_draw();
void script_editor();
void script_call(const char *f);
void script_call_sym(s7_pointer sym);
void script_call_sym_args(s7_pointer sym, s7_pointer args);
int script_has_sym(s7_pointer sym);
void script_eval_w_env(const char *s, s7_pointer env);
void script_call_sym(void *sym);
void script_call_sym_args(void *sym, void *args);
int script_has_sym(void *sym);
void script_eval_w_env(const char *s, void *env);
void register_update(s7_pointer sym);
void register_obupdate(s7_pointer obj, s7_pointer sym);
void register_update(void *sym);
void register_obupdate(void *obj, void *sym);
void call_updates(double dt);
void register_gui(s7_pointer sym);
void register_gui(void *sym);
void call_gui();
void register_physics(s7_pointer sym);
void register_physics(void *sym);
void call_physics(double dt);
s7_pointer cpvec2s7(cpVect v);
duk_idx_t vec2duk(cpVect v);
#endif

View file

@ -120,12 +120,6 @@ int main(int argc, char **args) {
}
duk_context *duk = duk_create_heap_default();
duk_eval_string(duk, "1+2");
printf("DUK RESULT: %d", duk_get_int(duk, -1));
#if DBG
if (logout) {
time_t now = time(NULL);
@ -169,7 +163,7 @@ int main(int argc, char **args) {
if (ed) {
editor_init(MakeSDLWindow("Editor", 600, 600, 0));
} else {
script_dofile("game.scm");
script_dofile("game.js");
}
openglInit();