prosperon/source/engine/jsffi.c

2031 lines
54 KiB
C
Raw Normal View History

2023-11-03 22:01:30 -05:00
#include "jsffi.h"
2023-01-10 07:13:00 -06:00
#include "script.h"
2023-05-12 13:22:05 -05:00
#include "anim.h"
#include "debugdraw.h"
2023-01-10 15:41:43 -06:00
#include "font.h"
2023-05-12 13:22:05 -05:00
#include "gameobject.h"
#include "input.h"
#include "log.h"
2023-11-27 14:29:55 -06:00
#include "dsp.h"
2023-05-12 13:22:05 -05:00
#include "music.h"
#include "2dphysics.h"
2023-11-27 14:29:55 -06:00
#include "datastream.h"
2023-05-12 13:22:05 -05:00
#include "sound.h"
#include "sprite.h"
2023-02-05 17:42:36 -06:00
#include "stb_ds.h"
2023-05-12 13:22:05 -05:00
#include "string.h"
#include "window.h"
2023-12-04 13:38:37 -06:00
#include "spline.h"
2023-05-12 13:22:05 -05:00
#include "yugine.h"
2024-01-01 17:30:42 -06:00
#include "particle.h"
2023-04-21 16:57:30 -05:00
#include <assert.h>
#include "resources.h"
2023-09-11 02:46:12 -05:00
#include <sokol/sokol_time.h>
2024-02-25 17:31:48 -06:00
#include <sokol/sokol_app.h>
2023-09-11 02:46:12 -05:00
#include <time.h>
#include <sys/time.h>
2024-02-23 16:05:30 -06:00
#include <sys/stat.h>
#include <sys/types.h>
2024-02-25 17:31:48 -06:00
#include <unistd.h>
#include <limits.h>
#if (defined(_WIN32) || defined(__WIN32__))
#include <direct.h>
#define mkdir(x,y) _mkdir(x)
#endif
2024-02-23 16:05:30 -06:00
2024-01-16 07:29:27 -06:00
#include "nota.h"
2023-05-24 20:45:50 -05:00
#include "render.h"
2023-05-12 13:22:05 -05:00
#include "model.h"
2023-05-24 20:45:50 -05:00
#include "HandmadeMath.h"
2024-01-14 10:24:31 -06:00
#define countof(x) (sizeof(x)/sizeof((x)[0]))
2023-05-24 20:45:50 -05:00
static JSValue globalThis;
static JSClassID js_ptr_id;
static JSClassDef js_ptr_class = { "POINTER" };
2023-11-29 07:32:32 -06:00
#define QJSCLASS(TYPE)\
static JSClassID js_ ## TYPE ## _id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
TYPE##_free(n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
2023-11-29 12:40:13 -06:00
.finalizer = js_##TYPE##_finalizer,\
2023-11-29 07:32:32 -06:00
};\
static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
2023-11-29 12:40:13 -06:00
static JSValue TYPE##2js(TYPE *n) { \
2023-11-29 07:32:32 -06:00
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
JS_SetOpaque(j,n);\
return j; }\
2023-11-29 12:40:13 -06:00
QJSCLASS(gameobject)
2024-01-14 10:24:31 -06:00
QJSCLASS(emitter)
QJSCLASS(dsp_node)
QJSCLASS(texture)
QJSCLASS(sprite)
2024-01-14 10:24:31 -06:00
QJSCLASS(warp_gravity)
QJSCLASS(warp_damp)
QJSCLASS(material)
QJSCLASS(mesh)
2024-03-13 16:30:55 -05:00
QJSCLASS(window)
2024-01-14 10:24:31 -06:00
/* qjs class colliders and constraints */
/* constraint works for all constraints - 2d or 3d */
static JSClassID js_constraint_id;
static void js_constraint_finalizer(JSRuntime *rt, JSValue val) {
constraint *c = JS_GetOpaque(val, js_constraint_id);
constraint_free(c);
}
static JSClassDef js_constraint_class = {
"constraint",
.finalizer = js_constraint_finalizer
};
static constraint *js2constraint(JSValue val) { return JS_GetOpaque(val, js_constraint_id); }
static JSValue constraint2js(constraint *c)
{
JSValue j = JS_NewObjectClass(js, js_constraint_id);
JS_SetOpaque(j, c);
return j;
}
static JSValue sound_proto;
sound *js2sound(JSValue v) { return js2dsp_node(v)->data; }
2023-11-29 12:40:13 -06:00
2024-02-25 17:31:48 -06:00
#define QJSGLOBALCLASS(NAME) \
JSValue NAME = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, NAME, js_##NAME##_funcs, countof(js_##NAME##_funcs)); \
JS_SetPropertyStr(js, globalThis, #NAME, NAME); \
2023-11-29 07:32:32 -06:00
#define QJSCLASSPREP(TYPE) \
JS_NewClassID(&js_##TYPE##_id);\
JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
/* Defines a class and uses its function list as its prototype */
2024-01-14 10:24:31 -06:00
#define QJSCLASSPREP_FUNCS(TYPE) \
QJSCLASSPREP(TYPE); \
JSValue TYPE##_proto = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYPE##_funcs)); \
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
2023-05-12 13:22:05 -05:00
#define BYTE_TO_BINARY(byte) \
2024-01-14 10:24:31 -06:00
(byte & 0x80 ? '1' : '0'), \
2023-05-12 13:22:05 -05:00
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
2023-12-18 06:45:27 -06:00
void js_setprop_str(JSValue obj, const char *prop, JSValue v) { JS_SetPropertyStr(js, obj, prop, v); }
2023-12-09 22:09:15 -06:00
int js2bool(JSValue v) { return JS_ToBool(js, v); }
JSValue bool2js(int b) { return JS_NewBool(js,b); }
2023-12-11 08:36:45 -06:00
void js_setprop_num(JSValue obj, uint32_t i, JSValue v) { JS_SetPropertyUint32(js, obj, i, v); }
2023-11-29 12:40:13 -06:00
2023-12-11 08:36:45 -06:00
JSValue gos2ref(gameobject **go)
2023-11-29 12:40:13 -06:00
{
JSValue array = JS_NewArray(js);
for (int i = 0; i < arrlen(go); i++)
2023-12-19 15:34:36 -06:00
js_setprop_num(array,i,JS_DupValue(js,go[i]->ref));
2023-11-29 12:40:13 -06:00
return array;
}
2023-05-24 20:45:50 -05:00
JSValue js_getpropstr(JSValue v, const char *str)
{
JSValue p = JS_GetPropertyStr(js,v,str);
JS_FreeValue(js,p);
return p;
}
JSValue js_getpropidx(JSValue v, uint32_t i)
{
2024-03-18 14:27:52 -05:00
JSValue p = JS_GetPropertyUint32(js, v, i);
2023-05-24 20:45:50 -05:00
JS_FreeValue(js,p);
return p;
}
2024-01-01 07:44:43 -06:00
static inline cpBody *js2body(JSValue v) { return js2gameobject(v)->body; }
2023-12-18 06:45:27 -06:00
JSValue str2js(const char *c) { return JS_NewString(js, c); }
const char *js2str(JSValue v) { return JS_ToCString(js, v); }
2023-10-10 17:37:58 -05:00
JSValue strarr2js(char **c)
2023-09-11 02:46:12 -05:00
{
JSValue arr = JS_NewArray(js);
for (int i = 0; i < arrlen(c); i++)
js_setprop_num(arr,i,JS_NewString(js, c[i]));
2023-09-11 02:46:12 -05:00
return arr;
}
2023-05-12 13:22:05 -05:00
double js2number(JSValue v) {
2023-04-18 17:58:44 -05:00
double g;
2023-05-12 13:22:05 -05:00
JS_ToFloat64(js, &g, v);
2023-04-18 17:58:44 -05:00
return g;
}
2024-01-01 17:30:42 -06:00
void *js2ptr(JSValue v) { return JS_GetOpaque(v,js_ptr_id); }
2024-03-18 14:27:52 -05:00
JSValue number2js(double g) { return JS_NewFloat64(js,g); }
2023-04-18 17:58:44 -05:00
2023-05-12 13:22:05 -05:00
JSValue ptr2js(void *ptr) {
JSValue obj = JS_NewObjectClass(js, js_ptr_id);
JS_SetOpaque(obj, ptr);
return obj;
2023-04-18 17:58:44 -05:00
}
2023-05-12 13:22:05 -05:00
int js_arrlen(JSValue v) {
2023-04-18 14:49:17 -05:00
int len;
2023-05-24 20:45:50 -05:00
JS_ToInt32(js, &len, js_getpropstr( v, "length"));
2023-04-18 14:49:17 -05:00
return len;
}
2023-05-16 01:31:13 -05:00
2024-03-18 08:16:25 -05:00
char *js_do_nota_decode(JSValue *tmp, char *nota)
2024-01-16 19:04:52 -06:00
{
int type = nota_type(nota);
JSValue ret2;
long long n;
double d;
int b;
2024-01-23 10:56:34 -06:00
char *str;
2024-01-16 19:04:52 -06:00
switch(type) {
case NOTA_BLOB:
break;
case NOTA_TEXT:
2024-01-23 10:56:34 -06:00
nota = nota_read_text(&str, nota);
2024-01-16 19:04:52 -06:00
*tmp = str2js(str);
2024-01-31 02:42:15 -06:00
/* TODO: Avoid malloc and free here */
free(str);
2024-01-23 10:56:34 -06:00
break;
2024-01-16 19:04:52 -06:00
case NOTA_ARR:
nota = nota_read_array(&n, nota);
2024-01-23 10:56:34 -06:00
*tmp = JS_NewArray(js);
2024-01-16 19:04:52 -06:00
for (int i = 0; i < n; i++) {
2024-03-18 08:16:25 -05:00
nota = js_do_nota_decode(&ret2, nota);
2024-01-23 10:56:34 -06:00
JS_SetPropertyInt64(js, *tmp, i, ret2);
2024-01-16 19:04:52 -06:00
}
break;
case NOTA_REC:
2024-01-23 10:56:34 -06:00
nota = nota_read_record(&n, nota);
*tmp = JS_NewObject(js);
for (int i = 0; i < n; i++) {
nota = nota_read_text(&str, nota);
2024-03-18 08:16:25 -05:00
nota = js_do_nota_decode(&ret2, nota);
2024-01-23 10:56:34 -06:00
JS_SetPropertyStr(js, *tmp, str, ret2);
2024-01-31 02:42:15 -06:00
free(str);
2024-01-23 10:56:34 -06:00
}
2024-01-16 19:04:52 -06:00
break;
case NOTA_INT:
nota = nota_read_int(&n, nota);
2024-03-18 14:27:52 -05:00
*tmp = number2js(n);
2024-01-23 10:56:34 -06:00
break;
2024-01-16 19:04:52 -06:00
case NOTA_SYM:
2024-01-31 02:42:15 -06:00
nota = nota_read_sym(&b, nota);
if (b == NOTA_NULL) *tmp = JS_UNDEFINED;
else
*tmp = bool2js(b);
2024-01-23 10:56:34 -06:00
break;
default:
case NOTA_FLOAT:
nota = nota_read_float(&d, nota);
*tmp = number2js(d);
break;
2024-01-16 19:04:52 -06:00
}
2024-01-23 10:56:34 -06:00
return nota;
2024-01-16 19:04:52 -06:00
}
2024-03-18 08:16:25 -05:00
char *js_do_nota_encode(JSValue v, char *nota)
2024-01-16 19:04:52 -06:00
{
int tag = JS_VALUE_GET_TAG(v);
2024-01-23 10:56:34 -06:00
char *str = NULL;
2024-01-16 19:04:52 -06:00
JSPropertyEnum *ptab;
uint32_t plen;
int n;
JSValue val;
switch(tag) {
case JS_TAG_FLOAT64:
return nota_write_float(JS_VALUE_GET_FLOAT64(v), nota);
case JS_TAG_INT:
return nota_write_int(JS_VALUE_GET_INT(v), nota);
case JS_TAG_STRING:
2024-01-23 10:56:34 -06:00
str = js2str(v);
nota = nota_write_text(str, nota);
JS_FreeCString(js, str);
return nota;
2024-01-16 19:04:52 -06:00
case JS_TAG_BOOL:
2024-01-31 02:42:15 -06:00
return nota_write_sym(JS_VALUE_GET_BOOL(v), nota);
case JS_TAG_UNDEFINED:
return nota_write_sym(NOTA_NULL, nota);
case JS_TAG_NULL:
return nota_write_sym(NOTA_NULL, nota);
2024-01-16 19:04:52 -06:00
case JS_TAG_OBJECT:
if (JS_IsArray(js, v)) {
int n = js_arrlen(v);
nota = nota_write_array(n, nota);
2024-01-31 02:42:15 -06:00
for (int i = 0; i < n; i++)
2024-03-18 14:27:52 -05:00
nota = js_do_nota_encode(js_getpropidx(v, i), nota);
2024-01-16 19:04:52 -06:00
return nota;
}
n = JS_GetOwnPropertyNames(js, &ptab, &plen, v, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK);
nota = nota_write_record(plen, nota);
2024-01-31 02:42:15 -06:00
2024-01-16 19:04:52 -06:00
for (int i = 0; i < plen; i++) {
2024-01-23 10:56:34 -06:00
val = JS_GetProperty(js,v,ptab[i].atom);
str = JS_AtomToCString(js, ptab[i].atom);
2024-03-11 22:23:02 -05:00
JS_FreeAtom(js, ptab[i].atom);
2024-01-31 02:42:15 -06:00
2024-01-23 10:56:34 -06:00
nota = nota_write_text(str, nota);
2024-03-11 22:23:02 -05:00
JS_FreeCString(js, str);
2024-01-31 02:42:15 -06:00
2024-03-18 08:16:25 -05:00
nota = js_do_nota_encode(val, nota);
2024-03-11 22:23:02 -05:00
JS_FreeValue(js,val);
2024-01-16 19:04:52 -06:00
}
2024-01-31 02:42:15 -06:00
js_free(js, ptab);
2024-01-16 19:04:52 -06:00
return nota;
2024-03-11 22:23:02 -05:00
default:
return nota;
2024-01-16 19:04:52 -06:00
}
2024-03-11 22:23:02 -05:00
return nota;
2024-01-16 19:04:52 -06:00
}
2023-05-16 01:31:13 -05:00
struct rgba js2color(JSValue v) {
2023-05-24 20:45:50 -05:00
JSValue c[4];
for (int i = 0; i < 4; i++)
2024-03-18 14:27:52 -05:00
c[i] = js_getpropidx(v,i);
float a = JS_IsUndefined(c[3]) ? 1.0 : js2number(c[3]);
2023-05-16 01:31:13 -05:00
struct rgba color = {
.r = js2number(c[0])*RGBA_MAX,
.g = js2number(c[1])*RGBA_MAX,
.b = js2number(c[2])*RGBA_MAX,
.a = a*RGBA_MAX,
2023-05-16 01:31:13 -05:00
};
2023-05-24 20:45:50 -05:00
for (int i = 0; i < 4; i++)
JS_FreeValue(js,c[i]);
2023-05-12 13:22:05 -05:00
return color;
2023-04-19 15:16:35 -05:00
}
2023-01-27 12:06:16 -06:00
JSValue color2js(struct rgba color)
{
JSValue arr = JS_NewArray(js);
2024-03-18 14:27:52 -05:00
js_setprop_num(arr,0,number2js((double)color.r/255));
js_setprop_num(arr,1,number2js((double)color.g/255));
js_setprop_num(arr,2,number2js((double)color.b/255));
js_setprop_num(arr,3,number2js((double)color.a/255));
return arr;
}
2023-10-04 08:18:09 -05:00
struct boundingbox js2bb(JSValue v)
{
struct boundingbox bb;
bb.t = js2number(js_getpropstr(v,"t"));
bb.b = js2number(js_getpropstr(v,"b"));
bb.r = js2number(js_getpropstr(v,"r"));
bb.l = js2number(js_getpropstr(v,"l"));
return bb;
}
2023-11-14 09:20:09 -06:00
HMM_Vec2 js2vec2(JSValue v)
2023-05-24 20:45:50 -05:00
{
HMM_Vec2 v2;
v2.X = js2number(js_getpropidx(v,0));
v2.Y = js2number(js_getpropidx(v,1));
return v2;
}
JSValue vec22js(HMM_Vec2 v)
{
JSValue array = JS_NewArray(js);
js_setprop_num(array,0,number2js(v.x));
js_setprop_num(array,1,number2js(v.y));
return array;
}
2024-01-14 10:24:31 -06:00
HMM_Vec3 js2vec3(JSValue v)
{
HMM_Vec3 v3;
v3.x = js2number(js_getpropidx(v,0));
v3.y = js2number(js_getpropidx(v,1));
v3.z = js2number(js_getpropidx(v,2));
return v3;
}
JSValue vec32js(HMM_Vec3 v)
{
JSValue array = JS_NewArray(js);
js_setprop_num(array,0,number2js(v.x));
js_setprop_num(array,1,number2js(v.y));
js_setprop_num(array,2,number2js(v.z));
return array;
}
2023-05-12 13:22:05 -05:00
cpBitmask js2bitmask(JSValue v) {
cpBitmask mask = 0;
int len = js_arrlen(v);
2023-05-12 13:22:05 -05:00
for (int i = 0; i < len; i++) {
2023-05-24 20:45:50 -05:00
int val = JS_ToBool(js, js_getpropidx( v, i));
2023-05-12 13:22:05 -05:00
if (!val) continue;
2023-05-12 13:22:05 -05:00
mask |= 1 << i;
}
2023-05-12 13:22:05 -05:00
return mask;
}
2023-11-14 09:20:09 -06:00
HMM_Vec2 *js2cpvec2arr(JSValue v) {
2023-12-12 19:35:34 -06:00
HMM_Vec2 *arr = NULL;
2023-04-18 14:49:17 -05:00
int n = js_arrlen(v);
2023-12-18 06:45:27 -06:00
arrsetlen(arr,n);
2023-04-18 14:49:17 -05:00
for (int i = 0; i < n; i++)
2023-12-18 06:45:27 -06:00
arr[i] = js2vec2(js_getpropidx( v, i));
return arr;
}
2023-02-08 15:30:12 -06:00
2023-12-18 06:45:27 -06:00
HMM_Vec2 *jsfloat2vec(JSValue v)
{
size_t s;
void *buf = JS_GetArrayBuffer(js, &s, v);
HMM_Vec2 *arr = NULL;
int n = s/2;
n /= sizeof(float);
2023-12-12 19:35:34 -06:00
return arr;
2023-02-08 15:30:12 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue bitmask2js(cpBitmask mask) {
2023-04-19 16:58:17 -05:00
JSValue arr = JS_NewArray(js);
for (int i = 0; i < 11; i++)
js_setprop_num(arr,i,JS_NewBool(js,mask & 1 << i));
2023-04-18 14:49:17 -05:00
return arr;
}
2023-11-14 09:20:09 -06:00
JSValue vecarr2js(HMM_Vec2 *points, int n) {
2023-04-19 16:58:17 -05:00
JSValue array = JS_NewArray(js);
2023-05-12 13:22:05 -05:00
for (int i = 0; i < n; i++)
js_setprop_num(array,i,vec22js(points[i]));
2023-04-18 14:49:17 -05:00
return array;
}
2023-05-12 13:22:05 -05:00
JSValue duk_ui_text(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
const char *s = JS_ToCString(js, argv[0]);
2023-11-14 09:20:09 -06:00
HMM_Vec2 pos = js2vec2(argv[1]);
2023-05-12 13:22:05 -05:00
float size = js2number(argv[2]);
2023-05-16 01:31:13 -05:00
struct rgba c = js2color(argv[3]);
2024-03-18 14:27:52 -05:00
int wrap = js2number(argv[4]);
int cursor = js2number(argv[5]);
2023-10-04 17:57:37 -05:00
JSValue ret = JS_NewInt64(js, renderText(s, pos, size, c, wrap, cursor, 1.0));
2023-05-12 13:22:05 -05:00
JS_FreeCString(js, s);
return ret;
2023-03-10 13:13:48 -06:00
}
2023-10-10 17:37:58 -05:00
int js_print_exception(JSValue v)
{
#ifndef NDEBUG
if (!JS_IsException(v))
return 0;
JSValue ex = JS_GetException(js);
const char *name = JS_ToCString(js, js_getpropstr(ex, "name"));
const char *msg = js2str(js_getpropstr(ex, "message"));
const char *stack = js2str(js_getpropstr(ex, "stack"));
int line = 0;
mYughLog(LOG_SCRIPT, LOG_ERROR, line, "script", "%s :: %s\n%s", name, msg, stack);
JS_FreeCString(js, name);
JS_FreeCString(js, msg);
JS_FreeCString(js, stack);
JS_FreeValue(js,ex);
return 1;
#endif
return 0;
}
2023-05-12 13:22:05 -05:00
JSValue duk_gui_img(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
const char *img = JS_ToCString(js, argv[0]);
2023-11-30 10:47:59 -06:00
transform2d t;
t.pos = js2vec2(argv[1]);
t.scale = js2vec2(argv[2]);
t.angle = js2number(argv[3]);
gui_draw_img(img, t, js2bool(argv[4]), js2vec2(argv[5]), 1.0, js2color(argv[6]));
2023-05-12 13:22:05 -05:00
JS_FreeCString(js, img);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 17:23:11 -06:00
}
2023-09-05 09:38:52 -05:00
struct rect js2rect(JSValue v) {
struct rect rect;
2023-05-24 20:45:50 -05:00
rect.x = js2number(js_getpropstr(v, "x"));
rect.y = js2number(js_getpropstr(v, "y"));
rect.w = js2number(js_getpropstr(v, "w"));
rect.h = js2number(js_getpropstr(v, "h"));
2023-04-18 17:58:44 -05:00
return rect;
}
2023-09-05 09:38:52 -05:00
JSValue rect2js(struct rect rect) {
2023-04-18 17:58:44 -05:00
JSValue obj = JS_NewObject(js);
2024-03-18 14:27:52 -05:00
js_setprop_str(obj, "x", number2js(rect.x));
js_setprop_str(obj, "y", number2js(rect.y));
js_setprop_str(obj, "w", number2js(rect.w));
js_setprop_str(obj, "h", number2js(rect.h));
2023-04-18 17:58:44 -05:00
return obj;
}
2023-05-30 13:07:18 -05:00
JSValue bb2js(struct boundingbox bb)
{
JSValue obj = JS_NewObject(js);
2024-03-18 14:27:52 -05:00
js_setprop_str(obj,"t", number2js(bb.t));
js_setprop_str(obj,"b", number2js(bb.b));
js_setprop_str(obj,"r", number2js(bb.r));
js_setprop_str(obj,"l", number2js(bb.l));
2023-05-30 13:07:18 -05:00
return obj;
}
2023-05-12 13:22:05 -05:00
JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-12-04 13:38:37 -06:00
/*
2023-12-13 19:53:09 -06:00
0: catmull-rom
1: bezier
2023-12-04 13:38:37 -06:00
*/
2023-12-13 19:53:09 -06:00
2024-03-18 14:27:52 -05:00
int type = js2number(argv[1]);
int d = js2number(argv[2]); /* dimensions: 1d, 2d, 3d ...*/
2023-12-12 08:46:27 -06:00
HMM_Vec2 *points = js2cpvec2arr(argv[3]);
float param = js2number(argv[4]);
2023-12-13 19:53:09 -06:00
HMM_Vec2 *samples = NULL;
switch(type) {
case 0:
samples = catmull_rom_ma_v2(points, param);
break;
case 1:
samples = bezier_cb_ma_v2(points, param);
break;
}
2023-12-12 19:35:34 -06:00
arrfree(points);
2023-12-12 08:46:27 -06:00
if (!samples)
return JS_UNDEFINED;
2023-01-27 12:06:16 -06:00
2023-12-12 08:46:27 -06:00
JSValue arr = vecarr2js(samples, arrlen(samples));
2023-12-12 19:35:34 -06:00
arrfree(samples);
2023-12-12 08:46:27 -06:00
return arr;
2023-01-27 12:06:16 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue ints2js(int *ints) {
2023-04-19 16:58:17 -05:00
JSValue arr = JS_NewArray(js);
2023-04-18 17:58:44 -05:00
for (int i = 0; i < arrlen(ints); i++)
2024-03-18 14:27:52 -05:00
js_setprop_num(arr,i, number2js(ints[i]));
2023-04-18 17:58:44 -05:00
return arr;
2023-02-05 17:42:36 -06:00
}
2023-11-14 09:20:09 -06:00
int vec_between(HMM_Vec2 p, HMM_Vec2 a, HMM_Vec2 b) {
HMM_Vec2 n;
2023-02-10 14:31:58 -06:00
n.x = b.x - a.x;
n.y = b.y - a.y;
2023-11-14 09:20:09 -06:00
n = HMM_NormV2(n);
2023-02-10 14:31:58 -06:00
2023-11-14 09:20:09 -06:00
return HMM_DotV2(n, HMM_SubV2(p,a)) > 0 && HMM_DotV2(HMM_MulV2F(n, -1), HMM_SubV2(p,b)) > 0;
2023-02-10 14:31:58 -06:00
}
/* Determines between which two points in 'segs' point 'p' falls.
0 indicates 'p' comes before the first point.
arrlen(segs) indicates it comes after the last point.
*/
2023-11-14 09:20:09 -06:00
int point2segindex(HMM_Vec2 p, HMM_Vec2 *segs, double slop) {
2023-02-10 14:31:58 -06:00
float shortest = slop < 0 ? INFINITY : slop;
2023-02-08 15:30:12 -06:00
int best = -1;
2023-05-12 13:22:05 -05:00
for (int i = 0; i < arrlen(segs) - 1; i++) {
float a = (segs[i + 1].y - segs[i].y) / (segs[i + 1].x - segs[i].x);
2023-02-08 15:30:12 -06:00
float c = segs[i].y - (a * segs[i].x);
float b = -1;
float dist = fabsf(a * p.x + b * p.y + c) / sqrt(pow(a, 2) + 1);
2023-02-08 15:30:12 -06:00
2023-02-10 14:31:58 -06:00
if (dist > shortest) continue;
2023-05-12 13:22:05 -05:00
int between = vec_between(p, segs[i], segs[i + 1]);
2023-02-10 14:31:58 -06:00
if (between) {
2023-02-08 15:30:12 -06:00
shortest = dist;
2023-05-12 13:22:05 -05:00
best = i + 1;
2023-02-10 14:31:58 -06:00
} else {
2023-11-14 09:20:09 -06:00
if (i == 0 && HMM_DistV2(p,segs[0]) < slop) {
2023-02-10 14:31:58 -06:00
shortest = dist;
2023-05-12 13:22:05 -05:00
best = i;
2023-11-14 09:20:09 -06:00
} else if (i == arrlen(segs) - 2 && HMM_DistV2(p,arrlast(segs)) < slop) {
2023-02-10 14:31:58 -06:00
shortest = dist;
2023-05-12 13:22:05 -05:00
best = arrlen(segs);
2023-02-10 14:31:58 -06:00
}
2023-02-08 15:30:12 -06:00
}
}
2023-02-10 14:31:58 -06:00
if (best == 1) {
2023-11-14 09:20:09 -06:00
HMM_Vec2 n;
2023-05-12 13:22:05 -05:00
n.x = segs[1].x - segs[0].x;
n.y = segs[1].y - segs[0].y;
2023-11-14 09:20:09 -06:00
n = HMM_NormV2(n);
if (HMM_DotV2(n, HMM_SubV2(p,segs[0])) < 0 ){
if (HMM_DistV2(p, segs[0]) >= slop)
2023-02-10 14:31:58 -06:00
best = -1;
else
best = 0;
}
2023-02-08 15:30:12 -06:00
}
2023-05-12 13:22:05 -05:00
if (best == arrlen(segs) - 1) {
2023-11-14 09:20:09 -06:00
HMM_Vec2 n;
2023-05-12 13:22:05 -05:00
n.x = segs[best - 1].x - segs[best].x;
n.y = segs[best - 1].y - segs[best - 1].y;
2023-11-14 09:20:09 -06:00
n = HMM_NormV2(n);
2023-02-08 15:30:12 -06:00
2023-11-14 09:20:09 -06:00
if (HMM_DotV2(n, HMM_SubV2(p, segs[best])) < 0) {
if (HMM_DistV2(p, segs[best]) >= slop)
2023-02-10 14:31:58 -06:00
best = -1;
else
best = arrlen(segs);
}
2023-02-08 15:30:12 -06:00
}
return best;
}
2023-05-12 13:22:05 -05:00
JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
const char *str = NULL;
const char *str2 = NULL;
void *d1 = NULL;
void *d2 = NULL;
void *v1 = NULL;
gameobject **ids = NULL;
int *intids = NULL;
2023-12-11 08:36:45 -06:00
gameobject *go = NULL;
2023-11-29 12:40:13 -06:00
JSValue ret = JS_UNDEFINED;
2024-01-31 02:42:15 -06:00
size_t plen = 0;
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
switch (cmd) {
case 18:
shape_set_sensor(js2ptr(argv[1]), JS_ToBool(js, argv[2]));
break;
case 21:
ret = JS_NewBool(js, shape_get_sensor(js2ptr(argv[1])));
break;
2023-05-12 13:22:05 -05:00
case 22:
shape_enabled(js2ptr(argv[1]), JS_ToBool(js, argv[2]));
break;
case 23:
ret = JS_NewBool(js, shape_is_enabled(js2ptr(argv[1])));
break;
2023-05-12 13:22:05 -05:00
case 44:
2023-12-19 17:28:45 -06:00
go = pos2gameobject(js2vec2(argv[1]), js2number(argv[2]));
2023-12-12 08:46:27 -06:00
ret = go ? JS_DupValue(js,go->ref) : JS_UNDEFINED;
break;
2023-05-12 13:22:05 -05:00
case 52:
2023-12-04 13:38:37 -06:00
ids = phys2d_query_box(js2vec2(argv[1]), js2vec2(argv[2]));
ret = gos2ref(ids);
arrfree(ids);
break;
2023-05-12 13:22:05 -05:00
case 53:
draw_box(js2vec2(argv[1]), js2vec2(argv[2]), js2color(argv[3]));
break;
2023-05-12 13:22:05 -05:00
case 59:
2023-12-12 19:35:34 -06:00
v1 = js2cpvec2arr(argv[2]);
ret = JS_NewInt64(js, point2segindex(js2vec2(argv[1]), v1, js2number(argv[3])));
break;
2023-05-12 13:22:05 -05:00
case 61:
2023-12-11 08:36:45 -06:00
set_cam_body(js2gameobject(argv[1])->body);
2023-05-12 13:22:05 -05:00
break;
case 62:
add_zoom(js2number(argv[1]));
break;
case 70:
ret = vec22js(world2go(js2gameobject(argv[1]), js2vec2(argv[2])));
break;
2023-05-12 13:22:05 -05:00
case 71:
ret = vec22js(go2world(js2gameobject(argv[1]), js2vec2(argv[2])));
break;
2023-05-12 13:22:05 -05:00
case 80:
2023-12-04 13:38:37 -06:00
ids = phys2d_query_shape(js2ptr(argv[1]));
ret = gos2ref(ids);
arrfree(ids);
break;
2023-05-12 13:22:05 -05:00
case 83:
2023-12-12 19:35:34 -06:00
v1 = js2cpvec2arr(argv[1]);
2023-12-19 17:28:45 -06:00
draw_edge(v1, js_arrlen(argv[1]), js2color(argv[2]), js2number(argv[3]), 0, js2color(argv[2]), 10);
break;
2023-05-12 13:22:05 -05:00
case 86:
2023-12-12 19:35:34 -06:00
v1 = js2cpvec2arr(argv[3]);
2024-03-18 14:27:52 -05:00
intids = phys2d_query_box_points(js2vec2(argv[1]), js2vec2(argv[2]), v1, js2number(argv[4]));
ret = ints2js(intids);
arrfree(intids);
break;
2024-03-14 09:33:15 -05:00
2023-05-25 21:55:55 -05:00
case 103:
ret = vec22js(js2gameobject(argv[1])->scale.XY);
break;
2023-05-27 07:01:17 -05:00
case 112:
2024-01-14 10:24:31 -06:00
ret = number2js(((struct phys2d_edge*)js2ptr(argv[1]))->thickness);
break;
2023-05-27 07:01:17 -05:00
case 113:
2023-12-18 17:12:05 -06:00
js2gameobject(argv[1])->ref = argv[2];
2023-05-27 07:01:17 -05:00
break;
2023-05-30 13:07:18 -05:00
case 118:
str = JS_ToCString(js,argv[1]);
ret = bb2js(text_bb(str, js2number(argv[2]), js2number(argv[3]), 1.0));
2023-05-30 13:07:18 -05:00
break;
2023-06-08 17:27:37 -05:00
case 123:
str = JS_ToCString(js, argv[1]);
str2 = JS_ToCString(js, argv[3]);
ret = eval_script_env(str, argv[2], str2);
break;
2023-09-11 15:07:36 -05:00
case 131:
2024-03-18 14:27:52 -05:00
gif_rec_start(js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4]));
2023-09-11 15:07:36 -05:00
break;
case 132:
str = JS_ToCString(js, argv[1]);
gif_rec_end(str);
break;
case 135:
ret = number2js(cam_zoom());
break;
case 136:
ret = vec22js(world2screen(js2vec2(argv[1])));
break;
case 137:
ret = vec22js(screen2world(js2vec2(argv[1])));
break;
case 139:
str = JS_ToCString(js,argv[1]);
ret = JS_NewInt64(js, gif_nframes(str));
break;
2023-10-04 08:18:09 -05:00
2023-11-03 08:31:06 -05:00
case 149:
((struct drawmodel *)js2ptr(argv[1]))->model = GetExistingModel(js2str(argv[2]));
break;
case 150:
draw_drawmodel(js2ptr(argv[1]));
break;
case 153:
2023-12-11 08:36:45 -06:00
cpBodySetTorque(js2gameobject(argv[1])->body, js2number(argv[2]));
break;
case 160:
ret = vec22js(mat_t_dir(t_world2go(js2gameobject(argv[1])), js2vec2(argv[2])));
break;
case 161:
ret = vec22js(mat_t_dir(t_go2world(js2gameobject(argv[1])), js2vec2(argv[2])));
break;
2023-11-27 14:29:55 -06:00
case 177:
2023-11-29 17:31:41 -06:00
plugin_node(js2dsp_node(argv[1]), js2dsp_node(argv[2]));
2023-11-27 14:29:55 -06:00
break;
case 180:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(masterbus);
2023-11-27 14:29:55 -06:00
break;
case 181:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(make_node(NULL,NULL,NULL));
2023-11-27 14:29:55 -06:00
break;
case 182:
str = js2str(argv[1]);
2023-11-29 17:31:41 -06:00
ret = dsp_node2js(dsp_source(str));
2024-01-14 10:24:31 -06:00
JS_SetPrototype(js, ret, sound_proto);
2023-11-27 14:29:55 -06:00
break;
case 185:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_delay(js2number(argv[1]), js2number(argv[2])));
2023-11-27 14:29:55 -06:00
break;
case 186:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_lpf(js2number(argv[1])));
2023-11-27 14:29:55 -06:00
break;
case 187:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_hpf(js2number(argv[1])));
2023-11-27 14:29:55 -06:00
break;
case 188:
str = js2str(argv[1]);
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_mod(str));
2023-11-27 14:29:55 -06:00
break;
case 189:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_bitcrush(js2number(argv[1]), js2number(argv[2])));
2023-11-27 14:29:55 -06:00
break;
case 190:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_compressor());
2023-11-27 14:29:55 -06:00
break;
case 191:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_limiter(js2number(argv[1])));
2023-11-27 14:29:55 -06:00
break;
case 192:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_noise_gate(js2number(argv[1])));
2023-11-27 14:29:55 -06:00
break;
2023-11-27 17:04:04 -06:00
case 197:
2024-01-14 10:24:31 -06:00
ret = number2js(js2sound(argv[1])->data->frames);
2023-11-27 17:04:04 -06:00
break;
case 198:
2024-01-14 10:24:31 -06:00
ret = number2js(SAMPLERATE);
2023-11-27 17:04:04 -06:00
break;
2023-11-28 22:48:32 -06:00
case 200:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_pitchshift(js2number(argv[1])));
2023-11-28 22:48:32 -06:00
break;
case 203:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_whitenoise());
2023-11-28 22:48:32 -06:00
break;
case 204:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_pinknoise());
2023-11-28 22:48:32 -06:00
break;
case 205:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_rednoise());
2023-11-28 22:48:32 -06:00
break;
case 206:
str = js2str(argv[1]);
str2 = js2str(argv[2]);
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_midi(str, make_soundfont(str2)));
2023-11-28 22:48:32 -06:00
break;
case 207:
2023-11-29 12:40:13 -06:00
ret = dsp_node2js(dsp_fwd_delay(js2number(argv[1]), js2number(argv[2])));
2023-11-29 07:32:32 -06:00
break;
2023-12-20 17:20:29 -06:00
case 213:
free_drawmodel(js2ptr(argv[1]));
break;
2023-12-28 17:38:17 -06:00
case 221:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpPivotJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body,js2vec2(argv[3]).cp)));
2023-12-28 17:38:17 -06:00
break;
case 222:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpPinJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, cpvzero,cpvzero)));
2023-12-28 17:38:17 -06:00
break;
case 223:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpGearJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2number(argv[3]), js2number(argv[4]))));
2023-12-28 17:38:17 -06:00
break;
2023-12-29 19:08:53 -06:00
case 224:
str = js2str(argv[1]);
ret = ints2js(gif_delays(str));
break;
2024-01-01 07:44:43 -06:00
case 225:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpRotaryLimitJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2number(argv[3]), js2number(argv[4]))));
2024-01-01 07:44:43 -06:00
break;
case 226:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpDampedRotarySpringNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2number(argv[3]), js2number(argv[4]), js2number(argv[5]))));
2024-01-01 07:44:43 -06:00
break;
case 227:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpDampedSpringNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2vec2(argv[3]).cp, js2vec2(argv[4]).cp, js2number(argv[5]), js2number(argv[6]), js2number(argv[7]))));
2024-01-01 07:44:43 -06:00
break;
case 228:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpGrooveJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2vec2(argv[3]).cp, js2vec2(argv[4]).cp, js2vec2(argv[5]).cp)));
2024-01-01 07:44:43 -06:00
break;
case 229:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpSlideJointNew(js2gameobject(argv[1])->body, js2gameobject(argv[2])->body, js2vec2(argv[3]).cp, js2vec2(argv[4]).cp, js2number(argv[5]), js2number(argv[6]))));
2024-01-01 07:44:43 -06:00
break;
case 230:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpRatchetJointNew(js2body(argv[1]), js2body(argv[2]), js2number(argv[3]), js2number(argv[4]))));
2024-01-01 07:44:43 -06:00
break;
case 231:
2024-01-14 10:24:31 -06:00
ret = constraint2js(constraint_make(cpSimpleMotorNew(js2body(argv[1]), js2body(argv[2]), js2number(argv[3]))));
2024-01-01 07:44:43 -06:00
break;
2024-01-14 10:24:31 -06:00
case 253:
ret = warp_gravity2js(warp_gravity_make());
2024-01-04 09:46:28 -06:00
break;
2024-01-14 10:24:31 -06:00
case 254:
ret = warp_damp2js(warp_damp_make());
2024-01-04 09:46:28 -06:00
break;
2024-02-23 16:05:30 -06:00
case 268:
if (js2bool(argv[1]))
ret = JS_GetClassProto(js, js_sprite_id);
else
ret = sprite2js(sprite_make());
break;
2023-05-12 13:22:05 -05:00
}
2024-02-01 10:11:09 -06:00
if (str) JS_FreeCString(js, str);
if (str2) JS_FreeCString(js, str2);
if (d1) free(d1);
if (d2) free(d2);
2023-12-12 19:35:34 -06:00
if (v1) arrfree(v1);
2024-02-01 10:11:09 -06:00
if (!JS_IsNull(ret)) return ret;
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
}
2024-01-03 17:19:13 -06:00
void gameobject_add_shape_collider(gameobject *go, JSValue fn, struct phys2d_shape *shape) {
2023-02-17 01:16:52 -06:00
struct shape_cb shapecb;
2023-03-10 13:13:48 -06:00
shapecb.shape = shape;
2024-01-03 17:19:13 -06:00
shapecb.cbs.begin = fn;
2023-12-11 08:36:45 -06:00
arrpush(go->shape_cbs, shapecb);
2023-02-17 01:16:52 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_register_collide(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2024-01-03 17:19:13 -06:00
gameobject *go = js2gameobject(argv[2]);
JSValue fn = argv[1];
2023-01-11 16:57:34 -06:00
2023-05-12 13:22:05 -05:00
switch (cmd) {
case 0:
2024-01-03 17:19:13 -06:00
go->cbs.begin = JS_DupValue(js,fn);
2023-05-12 13:22:05 -05:00
break;
case 1:
2024-01-03 17:19:13 -06:00
gameobject_add_shape_collider(go, JS_DupValue(js,fn), js2ptr(argv[3]));
2023-05-12 13:22:05 -05:00
break;
2023-01-11 16:57:34 -06:00
2023-05-12 13:22:05 -05:00
case 2:
phys2d_rm_go_handlers(go);
break;
2023-01-10 07:13:00 -06:00
2023-05-12 13:22:05 -05:00
case 3:
2024-01-03 17:19:13 -06:00
go->cbs.separate = JS_DupValue(js,fn);
2023-05-12 13:22:05 -05:00
break;
}
2023-01-10 07:13:00 -06:00
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-05-12 13:22:05 -05:00
}
2023-01-10 07:13:00 -06:00
2023-05-12 13:22:05 -05:00
JSValue duk_make_gameobject(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2023-11-29 12:40:13 -06:00
return gameobject2js(MakeGameobject());
2023-01-10 07:13:00 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[1]);
2023-11-29 12:40:13 -06:00
if (!go) return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
2023-01-13 13:07:44 -06:00
/* TODO: Possible that reindexing shapes only needs done for static shapes? */
2023-01-10 14:02:24 -06:00
switch (cmd) {
2023-05-12 13:22:05 -05:00
case 0:
gameobject_setangle(go, js2number(argv[2]));
break;
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 1:
2024-03-18 14:27:52 -05:00
go->bodytype = js2number(argv[2]);
2023-05-27 07:01:17 -05:00
cpBodySetType(go->body, go->bodytype);
gameobject_apply(go);
2023-05-12 13:22:05 -05:00
break;
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 2:
2023-11-14 09:20:09 -06:00
cpBodySetPosition(go->body, js2vec2(argv[2]).cp);
2023-05-12 13:22:05 -05:00
break;
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 4:
2023-11-14 09:20:09 -06:00
cpBodyApplyImpulseAtWorldPoint(go->body, js2vec2(argv[2]).cp, cpBodyGetPosition(go->body));
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 7:
2023-09-21 19:51:38 -05:00
go->mass = js2number(argv[2]);
2023-05-27 07:01:17 -05:00
if (go->bodytype == CP_BODY_TYPE_DYNAMIC)
2023-09-21 19:51:38 -05:00
cpBodySetMass(go->body, go->mass);
2023-05-12 13:22:05 -05:00
break;
2023-01-12 17:41:54 -06:00
2023-05-12 13:22:05 -05:00
case 8:
cpBodySetAngularVelocity(go->body, js2number(argv[2]));
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-13 13:07:44 -06:00
2023-05-12 13:22:05 -05:00
case 9:
2023-11-14 09:20:09 -06:00
cpBodySetVelocity(go->body, js2vec2(argv[2]).cp);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-13 13:07:44 -06:00
2023-05-12 13:22:05 -05:00
case 12:
2023-11-14 09:20:09 -06:00
cpBodyApplyForceAtWorldPoint(go->body, js2vec2(argv[2]).cp, cpBodyGetPosition(go->body));
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-06-28 11:35:41 -05:00
case 13:
cpBodySetMoment(go->body, js2number(argv[2]));
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
case 14:
2023-11-14 09:20:09 -06:00
cpBodyApplyForceAtLocalPoint(go->body, js2vec2(argv[2]).cp, js2vec2(argv[3]).cp);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
}
2023-01-25 21:32:58 -06:00
cpSpaceReindexShapesForBody(space, go->body);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_q_body(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int q = js2number(argv[0]);
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[1]);
2023-01-10 14:02:24 -06:00
2023-11-29 12:40:13 -06:00
if (!go) return JS_UNDEFINED;
2023-01-11 16:57:34 -06:00
2023-05-12 13:22:05 -05:00
switch (q) {
case 0:
return JS_NewInt64(js, cpBodyGetType(go->body));
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 1:
return vec22js((HMM_Vec2)cpBodyGetPosition(go->body));
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 2:
2024-03-18 14:27:52 -05:00
return number2js(cpBodyGetAngle(go->body));
2023-01-11 16:57:34 -06:00
2023-05-12 13:22:05 -05:00
case 3:
return vec22js((HMM_Vec2)cpBodyGetVelocity(go->body));
2023-01-11 16:57:34 -06:00
2023-05-12 13:22:05 -05:00
case 4:
2024-03-18 14:27:52 -05:00
return number2js(cpBodyGetAngularVelocity(go->body));
2023-03-10 13:13:48 -06:00
2023-05-12 13:22:05 -05:00
case 5:
2024-03-18 14:27:52 -05:00
return number2js(cpBodyGetMass(go->body));
2023-03-17 10:25:35 -05:00
2023-05-12 13:22:05 -05:00
case 6:
2024-03-18 14:27:52 -05:00
return number2js(cpBodyGetMoment(go->body));
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
case 7:
return JS_NewBool(js, phys2d_in_air(go->body));
}
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-10 14:02:24 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_make_circle2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[0]);
2023-01-10 17:23:11 -06:00
2023-04-21 16:57:30 -05:00
struct phys2d_circle *circle = Make2DCircle(go);
2023-05-12 13:22:05 -05:00
2023-04-18 17:58:44 -05:00
JSValue circleval = JS_NewObject(js);
js_setprop_str(circleval, "id", ptr2js(circle));
js_setprop_str(circleval, "shape", ptr2js(&circle->shape));
2023-04-18 17:58:44 -05:00
return circleval;
2023-01-10 17:23:11 -06:00
}
2023-11-03 08:31:06 -05:00
JSValue duk_make_model(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[0]);
2023-11-03 08:31:06 -05:00
struct drawmodel *dm = make_drawmodel(go);
JSValue ret = JS_NewObject(js);
js_setprop_str(ret, "id", ptr2js(dm));
return ret;
}
2023-05-12 13:22:05 -05:00
JSValue duk_cmd_circle2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-05-12 13:22:05 -05:00
struct phys2d_circle *circle = js2ptr(argv[1]);
2023-01-19 13:06:32 -06:00
2023-11-29 12:40:13 -06:00
if (!circle) return JS_UNDEFINED;
2023-01-19 13:06:32 -06:00
2023-05-12 13:22:05 -05:00
switch (cmd) {
case 0:
circle->radius = js2number(argv[2]);
break;
2023-01-19 13:06:32 -06:00
2023-05-12 13:22:05 -05:00
case 1:
circle->offset = js2vec2(argv[2]);
break;
2023-05-25 21:55:55 -05:00
case 2:
2024-01-14 10:24:31 -06:00
return number2js(circle->radius);
2023-05-25 21:55:55 -05:00
case 3:
return vec22js(circle->offset);
2023-05-12 13:22:05 -05:00
}
2023-12-28 17:38:17 -06:00
phys2d_shape_apply(&circle->shape);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-19 10:44:29 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_make_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[0]);
2023-01-25 21:32:58 -06:00
struct phys2d_poly *poly = Make2DPoly(go);
2023-10-10 17:37:58 -05:00
phys2d_poly_setverts(poly, NULL);
2023-04-18 17:58:44 -05:00
JSValue polyval = JS_NewObject(js);
js_setprop_str(polyval, "id", ptr2js(poly));
js_setprop_str(polyval, "shape", ptr2js(&poly->shape));
2023-04-18 17:58:44 -05:00
return polyval;
2023-01-25 21:32:58 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_cmd_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-04-18 17:58:44 -05:00
struct phys2d_poly *poly = js2ptr(argv[1]);
2023-12-12 19:35:34 -06:00
HMM_Vec2 *v1 = NULL;
2023-02-24 14:13:35 -06:00
2023-11-29 12:40:13 -06:00
if (!poly) return JS_UNDEFINED;
2023-05-12 13:22:05 -05:00
switch (cmd) {
case 0:
2023-12-12 19:35:34 -06:00
v1 = js2cpvec2arr(argv[2]);
phys2d_poly_setverts(poly, v1);
2023-05-12 13:22:05 -05:00
break;
2023-02-17 22:32:23 -06:00
}
2023-12-12 19:35:34 -06:00
if (v1) arrfree(v1);
2023-05-12 13:22:05 -05:00
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-25 21:32:58 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_make_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2023-12-11 08:36:45 -06:00
gameobject *go = js2gameobject(argv[0]);
2023-01-25 21:32:58 -06:00
struct phys2d_edge *edge = Make2DEdge(go);
2023-12-12 19:35:34 -06:00
HMM_Vec2 *points = js2cpvec2arr(argv[1]);
phys2d_edge_update_verts(edge, points);
arrfree(points);
2023-01-25 21:32:58 -06:00
2023-04-18 17:58:44 -05:00
JSValue edgeval = JS_NewObject(js);
js_setprop_str(edgeval, "id", ptr2js(edge));
js_setprop_str(edgeval, "shape", ptr2js(&edge->shape));
2023-04-18 17:58:44 -05:00
return edgeval;
2023-01-25 21:32:58 -06:00
}
2024-01-14 10:24:31 -06:00
#define GETSET_PAIR(ID, ENTRY, TYPE) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
js2##ID (this)->ENTRY = js2##TYPE (val); \
return JS_UNDEFINED; \
} \
\
JSValue ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
2024-03-13 16:30:55 -05:00
#define GETSET_PAIR_APPLY(ID, ENTRY, TYPE) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
ID *id = js2##ID (this); \
id->ENTRY = js2##TYPE (val); \
ID##_apply(id); \
return JS_UNDEFINED; \
} \
\
JSValue ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
2024-01-14 10:24:31 -06:00
#define GETSET_PAIR_HOOK(ID, ENTRY) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
ID *n = js2##ID (this); \
n->ENTRY = val; \
return JS_UNDEFINED; \
} \
\
JSValue ID##_get_##ENTRY (JSContext *js, JSValue this) { \
ID *n = js2##ID (this); \
return n->ENTRY; \
} \
2024-03-18 08:16:25 -05:00
#define DEF_FN_STR(NAME) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \
char *str = js2str(argv[0]); \
NAME (str); \
JS_FreeCString(js,str); \
return JS_UNDEFINED; \
} \
2024-01-14 10:24:31 -06:00
GETSET_PAIR(warp_gravity, strength, number)
GETSET_PAIR(warp_gravity, decay, number)
GETSET_PAIR(warp_gravity, spherical, bool)
GETSET_PAIR(warp_gravity, mask, bitmask)
GETSET_PAIR(warp_gravity, planar_force, vec3)
2024-02-09 01:49:52 -06:00
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define MIST_FUNC_DEF(TYPE, FN, LEN) MIST_CFUNC_DEF(#FN, LEN, js_##TYPE##_##FN)
2024-03-18 08:16:25 -05:00
#define MIST_FN_STR(NAME) MIST_CFUNC_DEF(#NAME, 1, js_##NAME)
2024-02-09 01:49:52 -06:00
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, ID##_get_##ENTRY, ID##_set_##ENTRY)
2024-01-14 10:24:31 -06:00
#define GGETSET_ADD(ENTRY)
2024-03-18 14:27:52 -05:00
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \
{FN;} \
return JS_UNDEFINED; \
} \
#define JSC_RET(NAME, FN) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \
FN; } \
2024-01-14 10:24:31 -06:00
static const JSCFunctionListEntry js_warp_gravity_funcs [] = {
CGETSET_ADD(warp_gravity, strength),
CGETSET_ADD(warp_gravity, decay),
CGETSET_ADD(warp_gravity, spherical),
CGETSET_ADD(warp_gravity, mask),
CGETSET_ADD(warp_gravity, planar_force),
};
GETSET_PAIR(warp_damp, damp, vec3)
static const JSCFunctionListEntry js_warp_damp_funcs [] = {
CGETSET_ADD(warp_damp, damp)
};
GETSET_PAIR(emitter, life, number)
GETSET_PAIR(emitter, life_var, number)
GETSET_PAIR(emitter, speed, number)
GETSET_PAIR(emitter, variation, number)
GETSET_PAIR(emitter, divergence, number)
GETSET_PAIR(emitter, scale, number)
GETSET_PAIR(emitter, scale_var, number)
GETSET_PAIR(emitter, grow_for, number)
GETSET_PAIR(emitter, shrink_for, number)
GETSET_PAIR(emitter, max, number)
GETSET_PAIR(emitter, explosiveness, number)
GETSET_PAIR(emitter, go, gameobject)
GETSET_PAIR(emitter, bounce, number)
GETSET_PAIR(emitter, collision_mask, bitmask)
GETSET_PAIR(emitter, die_after_collision, bool)
GETSET_PAIR(emitter, persist, number)
GETSET_PAIR(emitter, persist_var, number)
GETSET_PAIR(emitter, warp_mask, bitmask)
2024-03-18 08:16:25 -05:00
GETSET_PAIR(emitter, texture, texture)
2024-01-14 10:24:31 -06:00
JSValue js_emitter_start (JSContext *js, JSValue this, int argc, JSValue *argv)
2024-01-14 10:24:31 -06:00
{
emitter *n = js2emitter(this);
start_emitter(n);
return JS_UNDEFINED;
}
JSValue js_emitter_stop(JSContext *js, JSValue this, int argc, JSValue *argv)
2024-01-14 10:24:31 -06:00
{
emitter *n = js2emitter(this);
stop_emitter(n);
return JS_UNDEFINED;
}
JSValue js_emitter_emit(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
emitter *n = js2emitter(this);
emitter_emit(n, js2number(argv[0]));
return JS_UNDEFINED;
}
JSValue js_os_cwd(JSContext *js, JSValueConst this, int argc, JSValue *argv)
2024-02-25 17:31:48 -06:00
{
char cwd[PATH_MAX];
#ifndef __EMSCRIPTEN__
2024-02-25 17:31:48 -06:00
getcwd(cwd, sizeof(cwd));
#else
cwd[0] = '.';
cwd[1] = 0;
#endif
2024-02-25 17:31:48 -06:00
return str2js(cwd);
}
JSValue js_os_env(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *str = js2str(argv[0]);
JSValue ret = JS_UNDEFINED;
if (getenv(str)) ret = str2js(getenv(str));
JS_FreeCString(js,str);
return ret;
}
JSValue js_os_sys(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
#ifdef __linux__
return str2js("linux");
#elif defined(_WIN32) || defined(_WIN64)
return str2js("windows");
#elif defined(__APPLE__)
return str2js("macos");
#endif
return JS_UNDEFINED;
}
2024-03-18 14:27:52 -05:00
JSC_CCALL(os_quit, quit();)
JSC_CCALL(os_reindex_static, cpSpaceReindexStatic(space));
JSC_CCALL(os_gc, script_gc());
2024-03-15 11:21:36 -05:00
2024-03-18 08:16:25 -05:00
#define RETUN return JS_UNDEFINED
JSValue js_os_capture(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
char *str = js2str(argv[0]);
capture_screen(js2number(argv[1]), js2number(argv[2]), js2number(argv[4]), js2number(argv[5]), str);
JS_FreeCString(js,str);
RETUN;
}
2024-02-25 17:31:48 -06:00
static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, cwd, 0),
MIST_FUNC_DEF(os, env, 1),
2024-03-18 08:16:25 -05:00
MIST_FUNC_DEF(os, sys, 0),
MIST_FUNC_DEF(os, quit, 0),
MIST_FUNC_DEF(os, reindex_static, 0),
MIST_FUNC_DEF(os, gc, 0),
MIST_FUNC_DEF(os, capture, 5),
};
2024-03-18 14:27:52 -05:00
JSC_CCALL(render_normal, opengl_rendermode(LIT))
JSC_CCALL(render_wireframe, opengl_rendermode(WIREFRAME))
JSC_CCALL(render_grid, draw_grid(js2number(argv[0]), js2number(argv[1]), js2color(argv[2]));)
JSC_CCALL(render_point, draw_cppoint(js2vec2(argv[0]), js2number(argv[1]), js2color(argv[2])))
JSC_CCALL(render_circle, draw_circle(js2vec2(argv[0]), js2number(argv[1]), js2number(argv[2]), js2color(argv[3]), -1);)
JSC_CCALL(render_poly,
2024-03-18 08:16:25 -05:00
int n = js_arrlen(argv[0]);
HMM_Vec2 points[n];
for (int i = 0; i < n; i++)
2024-03-18 14:27:52 -05:00
points[i] = js2vec2(js_getpropidx(argv[0], i));
2024-03-18 08:16:25 -05:00
draw_poly(points, n, js2color(argv[1]));
2024-03-18 14:27:52 -05:00
)
JSC_CCALL(render_line,
2024-03-18 08:16:25 -05:00
void *v1 = js2cpvec2arr(argv[0]);
draw_edge(v1, js_arrlen(argv[0]), js2color(argv[1]), js2number(argv[2]), 0, js2color(argv[1]), 10);
2024-03-18 14:27:52 -05:00
)
JSC_CCALL(render_sprites, sprite_draw_all())
JSC_CCALL(render_models, model_draw_all())
JSC_CCALL(render_emitters, emitters_draw())
JSC_CCALL(render_flush, debug_flush(&projection); text_flush(&projection))
JSC_CCALL(render_flush_hud, debug_flush(&hudproj); debug_flush(&hudproj); sprite_flush();)
JSC_CCALL(render_pass, debug_nextpass())
JSC_CCALL(render_end_pass, sg_end_pass())
JSC_CCALL(render_commit, sg_commit(); debug_newframe();)
2024-03-18 08:16:25 -05:00
static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, normal, 0),
MIST_FUNC_DEF(render, wireframe, 0),
MIST_FUNC_DEF(render, grid, 3),
MIST_FUNC_DEF(render, point, 3),
MIST_FUNC_DEF(render, circle, 3),
MIST_FUNC_DEF(render, poly, 2),
MIST_FUNC_DEF(render, line, 3),
2024-03-18 14:27:52 -05:00
MIST_FUNC_DEF(render, sprites, 0),
MIST_FUNC_DEF(render, models, 0),
MIST_FUNC_DEF(render, emitters, 0),
MIST_FUNC_DEF(render, flush, 0),
MIST_FUNC_DEF(render, flush_hud, 0),
MIST_FUNC_DEF(render, pass, 0),
MIST_FUNC_DEF(render, end_pass, 0),
MIST_FUNC_DEF(render, commit, 0)
2024-03-18 08:16:25 -05:00
};
2024-03-18 14:27:52 -05:00
JSC_CCALL(gui_flush, text_flush(&hudproj));
JSC_CCALL(gui_scissor, sg_apply_scissor_rectf(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), 0))
2024-03-18 08:16:25 -05:00
DEF_FN_STR(font_set)
static const JSCFunctionListEntry js_gui_funcs[] = {
MIST_FUNC_DEF(gui, flush, 0),
MIST_FUNC_DEF(gui, scissor, 4),
MIST_FN_STR(font_set),
};
JSValue js_vector_dot(JSContext *js, JSValueConst this, int argc, JSValue *argv) { return number2js(HMM_DotV2(js2vec2(argv[0]), js2vec2(argv[1]))) ; };
2024-03-18 14:27:52 -05:00
JSC_RET(vector_project, return vec22js(HMM_ProjV2(js2vec2(argv[0]), js2vec2(argv[1])));)
2024-03-18 08:16:25 -05:00
static const JSCFunctionListEntry js_vector_funcs[] = {
MIST_FUNC_DEF(vector,dot,2),
2024-03-18 14:27:52 -05:00
MIST_FUNC_DEF(vector,project,2),
2024-03-18 08:16:25 -05:00
};
JSValue js_game_engine_start(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
2024-03-18 14:27:52 -05:00
engine_start(argv[0],argv[1]);
2024-03-18 08:16:25 -05:00
RETUN;
}
JSValue js_game_object_count(JSContext *js, JSValueConst this) { return number2js(go_count()); }
static const JSCFunctionListEntry js_game_funcs[] = {
MIST_FUNC_DEF(game, engine_start, 2),
MIST_FUNC_DEF(game, object_count, 0)
};
2024-03-18 14:27:52 -05:00
JSC_CCALL(input_show_keyboard, sapp_show_keyboard(js2bool(argv[0])))
2024-03-18 08:16:25 -05:00
JSValue js_input_keyboard_shown(JSContext *js, JSValueConst this) { return bool2js(sapp_keyboard_shown()); }
2024-03-18 14:27:52 -05:00
JSC_CCALL(input_mouse_mode, set_mouse_mode(js2number(argv[0])))
JSC_CCALL(input_mouse_cursor, sapp_set_mouse_cursor(js2number(argv[0])))
2024-03-18 08:16:25 -05:00
DEF_FN_STR(cursor_img)
static const JSCFunctionListEntry js_input_funcs[] = {
MIST_FUNC_DEF(input, show_keyboard, 1),
MIST_FUNC_DEF(input, keyboard_shown, 0),
MIST_FUNC_DEF(input, mouse_mode, 1),
MIST_FUNC_DEF(input, mouse_cursor, 1),
MIST_FN_STR(cursor_img)
};
#define GETSET_GLOBAL(ENTRY, TYPE) \
JSValue global_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
ENTRY = js2##TYPE (val); \
return JS_UNDEFINED; \
} \
\
JSValue global_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(ENTRY); \
} \
2024-03-18 14:27:52 -05:00
JSC_CCALL(prosperon_emitters_step, emitters_step(js2number(argv[0])))
JSC_CCALL(prosperon_phys2d_step, phys2d_update(js2number(argv[0])))
JSC_CCALL(prosperon_window_render, openglRender(&mainwin))
static const JSCFunctionListEntry js_prosperon_funcs[] = {
2024-03-15 11:21:36 -05:00
MIST_FUNC_DEF(prosperon, emitters_step, 1),
MIST_FUNC_DEF(prosperon, phys2d_step, 1),
MIST_FUNC_DEF(prosperon, window_render, 0)
};
JSValue js_time_now(JSContext *js, JSValue this) {
struct timeval ct;
gettimeofday(&ct, NULL);
return number2js((double)ct.tv_sec+(double)(ct.tv_usec/1000000.0));
}
JSValue js_time_computer_dst(JSContext *js, JSValue this) {
time_t t = time(NULL);
return bool2js(localtime(&t)->tm_isdst);
}
JSValue js_time_computer_zone(JSContext *js, JSValue this) {
time_t t = time(NULL);
time_t local_t = mktime(localtime(&t));
double diff = difftime(t, local_t);
return number2js(diff/3600);
}
static const JSCFunctionListEntry js_time_funcs[] = {
MIST_FUNC_DEF(time, now, 0),
MIST_FUNC_DEF(time, computer_dst, 0),
MIST_FUNC_DEF(time, computer_zone, 0)
};
JSValue js_console_print(JSContext *js, JSValue this, int argc, JSValue *argv)
{
char *s = JS_ToCString(js,argv[0]);
log_print(s);
JS_FreeCString(js,s);
return JS_UNDEFINED;
}
JSValue js_console_rec(JSContext *js, JSValue this, int argc, JSValue *argv)
{
2024-03-18 14:27:52 -05:00
int level = js2number(argv[0]);
const char *msg = JS_ToCString(js, argv[1]);
const char *file = JS_ToCString(js, argv[2]);
2024-03-18 14:27:52 -05:00
int line = js2number(argv[3]);
mYughLog(LOG_SCRIPT, level, line, file, msg);
JS_FreeCString(js, msg);
JS_FreeCString(js, file);
return JS_UNDEFINED;
}
GETSET_GLOBAL(stdout_lvl, number)
static const JSCFunctionListEntry js_console_funcs[] = {
MIST_FUNC_DEF(console,print,1),
MIST_FUNC_DEF(console,rec,4),
CGETSET_ADD(global, stdout_lvl)
};
JSValue js_profile_now(JSContext *js, JSValue this) { return number2js(stm_now()); }
static const JSCFunctionListEntry js_profile_funcs[] = {
MIST_FUNC_DEF(profile,now,0),
};
JSValue js_io_exists(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *file = JS_ToCString(js, argv[0]);
JSValue ret = JS_NewBool(js, fexists(file));
JS_FreeCString(js,file);
return ret;
}
JSValue js_io_ls(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
return strarr2js(ls("."));
}
JSValue js_io_cp(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f1, *f2;
f1 = JS_ToCString(js, argv[0]);
f2 = JS_ToCString(js, argv[1]);
2024-03-18 14:27:52 -05:00
JSValue ret = number2js(cp(f1,f2));
JS_FreeCString(js,f1);
JS_FreeCString(js,f2);
return ret;
}
JSValue js_io_mv(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f1, *f2;
f1 = JS_ToCString(js, argv[0]);
f2 = JS_ToCString(js, argv[1]);
2024-03-18 14:27:52 -05:00
JSValue ret = number2js(rename(f1,f2));
JS_FreeCString(js,f1);
JS_FreeCString(js,f2);
return ret;
}
JSValue js_io_chdir(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *path = JS_ToCString(js, argv[0]);
2024-03-18 14:27:52 -05:00
JSValue ret = number2js(chdir(path));
JS_FreeCString(js,path);
return ret;
}
JSValue js_io_rm(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *file = JS_ToCString(js, argv[0]);
2024-03-18 14:27:52 -05:00
JSValue ret = number2js(remove(file));
JS_FreeCString(js,file);
return JS_UNDEFINED;
}
JSValue js_io_mkdir(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f = js2str(argv[0]);
2024-03-18 14:27:52 -05:00
JSValue ret = number2js(mkdir(f,0777));
JS_FreeCString(js,f);
return ret;
}
JSValue js_io_slurpbytes(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f = js2str(argv[0]);
size_t len;
char *d = slurp_file(f,&len);
JSValue ret = JS_NewArrayBufferCopy(js,d,len);
JS_FreeCString(js,f);
free(d);
return ret;
}
JSValue js_io_slurp(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f = js2str(argv[0]);
size_t len;
char *s = slurp_text(f,&len);
JS_FreeCString(js,f);
if (!s) return JS_UNDEFINED;
JSValue ret = JS_NewStringLen(js, s, len);
free(s);
return ret;
}
JSValue js_io_slurpwrite(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f = js2str(argv[0]);
size_t len;
char *data;
JSValue ret;
if (JS_IsString(argv[1])) {
data = JS_ToCStringLen(js, &len, argv[1]);
2024-03-18 14:27:52 -05:00
ret = number2js(slurp_write(data, f, len));
JS_FreeCString(js,data);
} else {
data = JS_GetArrayBuffer(js, &len, argv[1]);
2024-03-18 14:27:52 -05:00
ret = number2js(slurp_write(data, f, len));
}
return ret;
}
JSValue js_io_chmod(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *f = js2str(argv[0]);
2024-03-18 14:27:52 -05:00
int mod = js2number(argv[1]);
chmod(f, mod);
return JS_UNDEFINED;
}
2024-03-18 08:16:25 -05:00
DEF_FN_STR(save_qoa)
JSValue js_io_compile(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
size_t len;
char *str = js2str(argv[0]);
void *d = script_compile(str, &len);
JSValue ret = JS_NewArrayBufferCopy(js,d,len);
JS_FreeCString(js,str);
free(d);
return ret;
}
JSValue js_io_run_bytecode(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
size_t len;
char *str = js2str(argv[0]);
void *d = slurp_file(str, &len);
JSValue ret = script_run_bytecode(d, len);
JS_FreeCString(js,str);
free(d);
return ret;
}
DEF_FN_STR(pack_engine)
static const JSCFunctionListEntry js_io_funcs[] = {
2024-03-18 08:16:25 -05:00
MIST_FUNC_DEF(io, exists,1),
MIST_FUNC_DEF(io, ls, 0),
MIST_FUNC_DEF(io, cp, 2),
MIST_FUNC_DEF(io, mv, 2),
MIST_FUNC_DEF(io, rm, 1),
MIST_FUNC_DEF(io, chdir, 1),
MIST_FUNC_DEF(io, mkdir, 1),
MIST_FUNC_DEF(io, chmod, 2),
MIST_FUNC_DEF(io, slurp, 1),
MIST_FUNC_DEF(io, slurpbytes, 1),
MIST_FUNC_DEF(io, slurpwrite, 2),
MIST_FN_STR(save_qoa),
MIST_FN_STR(pack_engine)
};
JSValue js_debug_draw_gameobject(JSContext *js, JSValueConst this, int argc, JSValue *argv) { gameobject_draw_debug(js2gameobject(argv[0])); RETUN; }
static const JSCFunctionListEntry js_debug_funcs[] = {
MIST_FUNC_DEF(debug, draw_gameobject, 1)
};
JSValue js_physics_sgscale(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
js2gameobject(argv[0])->scale.xy = js2vec2(argv[1]);
gameobject_apply(js2gameobject(argv[0]));
cpSpaceReindexShapesForBody(space, js2gameobject(argv[0])->body);
RETUN;
}
2024-03-18 14:27:52 -05:00
JSValue js_physics_set_cat_mask(JSContext *js, JSValueConst this, int argc, JSValue *argv) { set_cat_mask(js2number(argv[0]), js2bitmask(argv[1])); RETUN; }
2024-03-18 08:16:25 -05:00
static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics, sgscale, 2),
MIST_FUNC_DEF(physics, set_cat_mask, 2)
2024-02-23 16:05:30 -06:00
};
2024-01-14 10:24:31 -06:00
static const JSCFunctionListEntry js_emitter_funcs[] = {
CGETSET_ADD(emitter, life),
CGETSET_ADD(emitter, life_var),
CGETSET_ADD(emitter, speed),
CGETSET_ADD(emitter, variation),
CGETSET_ADD(emitter, divergence),
CGETSET_ADD(emitter, scale),
CGETSET_ADD(emitter, scale_var),
CGETSET_ADD(emitter, grow_for),
CGETSET_ADD(emitter, shrink_for),
CGETSET_ADD(emitter, max),
CGETSET_ADD(emitter, explosiveness),
CGETSET_ADD(emitter, go),
CGETSET_ADD(emitter, bounce),
CGETSET_ADD(emitter, collision_mask),
CGETSET_ADD(emitter, die_after_collision),
CGETSET_ADD(emitter, persist),
CGETSET_ADD(emitter, persist_var),
CGETSET_ADD(emitter, warp_mask),
2024-03-18 08:16:25 -05:00
MIST_FUNC_DEF(emitter, start, 0),
MIST_FUNC_DEF(emitter, stop, 0),
MIST_FUNC_DEF(emitter, emit, 1),
CGETSET_ADD(emitter, texture),
2024-01-14 10:24:31 -06:00
};
GETSET_PAIR(dsp_node, pass, bool)
GETSET_PAIR(dsp_node, off, bool)
GETSET_PAIR(dsp_node, gain, number)
GETSET_PAIR(dsp_node, pan, number)
JSValue js_dsp_node_plugin(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
plugin_node(js2dsp_node(this), js2dsp_node(argv[0]));
return JS_UNDEFINED;
}
JSValue js_dsp_node_unplug(JSContext *js, JSValueConst this, int argc, JSValue *argv)
2024-01-14 10:24:31 -06:00
{
unplug_node(js2dsp_node(this));
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_dsp_node_funcs[] = {
CGETSET_ADD(dsp_node, pass),
CGETSET_ADD(dsp_node, off),
CGETSET_ADD(dsp_node, gain),
CGETSET_ADD(dsp_node, pan),
2024-03-18 08:16:25 -05:00
MIST_FUNC_DEF(dsp_node, plugin, 1),
MIST_FUNC_DEF(dsp_node, unplug, 0),
2024-01-14 10:24:31 -06:00
};
GETSET_PAIR(sound, loop, bool)
GETSET_PAIR(sound, timescale, number)
GETSET_PAIR(sound, frame, number)
GETSET_PAIR_HOOK(sound, hook)
static const JSCFunctionListEntry js_sound_funcs[] = {
CGETSET_ADD(sound, loop),
CGETSET_ADD(sound, timescale),
CGETSET_ADD(sound, frame),
CGETSET_ADD(sound, hook)
};
2024-03-13 16:30:55 -05:00
static JSValue window_get_size(JSContext *js, JSValueConst this) { return vec22js(js2window(this)->size); }
static JSValue window_set_size(JSContext *js, JSValueConst this, JSValue v) {
window *w = js2window(this);
if (!w->start)
w->size = js2vec2(v);
return JS_UNDEFINED;
}
GETSET_PAIR_APPLY(window, rendersize, vec2)
GETSET_PAIR(window, mode, number)
static JSValue window_get_fullscreen(JSContext *js, JSValueConst this)
{
return number2js(js2window(this)->fullscreen);
}
static JSValue window_set_fullscreen(JSContext *js, JSValueConst this, JSValue v)
{
window_setfullscreen(js2window(this), js2bool(v));
return JS_UNDEFINED;
}
static JSValue window_set_title(JSContext *js, JSValueConst this, JSValue v)
{
window *w = js2window(this);
if (w->title) JS_FreeCString(js, w->title);
w->title = js2str(v);
if (w->start)
sapp_set_window_title(w->title);
return JS_UNDEFINED;
}
static JSValue window_get_title(JSContext *js, JSValueConst this, JSValue v)
{
if (!js2window(this)->title) return JS_UNDEFINED;
return str2js(js2window(this)->title);
}
2024-03-18 08:16:25 -05:00
DEF_FN_STR(set_icon)
JSValue js_window_icon(JSContext *js, JSValueConst this, int argc, JSValue *argv) {
char *str = js2str(argv[0]);
set_icon(str);
JS_FreeCString(js,str);
RETUN;
}
2024-03-13 16:30:55 -05:00
static const JSCFunctionListEntry js_window_funcs[] = {
CGETSET_ADD(window, size),
CGETSET_ADD(window, rendersize),
CGETSET_ADD(window, mode),
CGETSET_ADD(window, fullscreen),
CGETSET_ADD(window, title),
2024-03-18 08:16:25 -05:00
MIST_FN_STR(set_icon),
2024-03-13 16:30:55 -05:00
};
#define GETSET_PAIR_BODY(ID, ENTRY, TYPE) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
js2##ID (this)->ENTRY = js2##TYPE (val); \
ID##_apply(js2##ID (this)); \
return JS_UNDEFINED; \
} \
\
JSValue ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
GETSET_PAIR_BODY(gameobject, f, number)
GETSET_PAIR_BODY(gameobject, e, number)
GETSET_PAIR_BODY(gameobject, mass, number)
GETSET_PAIR(gameobject, damping, number)
GETSET_PAIR(gameobject, timescale, number)
GETSET_PAIR(gameobject, maxvelocity, number)
GETSET_PAIR(gameobject, maxangularvelocity, number)
GETSET_PAIR_BODY(gameobject, layer, number)
GETSET_PAIR(gameobject, warp_filter, bitmask)
GETSET_PAIR(gameobject, scale, vec3)
GETSET_PAIR(gameobject, drawlayer, number)
static const JSCFunctionListEntry js_gameobject_funcs[] = {
CGETSET_ADD(gameobject, f),
CGETSET_ADD(gameobject, e),
CGETSET_ADD(gameobject,mass),
CGETSET_ADD(gameobject,damping),
CGETSET_ADD(gameobject,timescale),
CGETSET_ADD(gameobject,maxvelocity),
CGETSET_ADD(gameobject,maxangularvelocity),
CGETSET_ADD(gameobject,layer),
CGETSET_ADD(gameobject,warp_filter),
CGETSET_ADD(gameobject,scale),
CGETSET_ADD(gameobject,drawlayer)
};
GETSET_PAIR(sprite, color, color)
GETSET_PAIR(sprite, emissive, color)
GETSET_PAIR(sprite, enabled, bool)
GETSET_PAIR(sprite, parallax, number)
GETSET_PAIR(sprite, tex, texture)
GETSET_PAIR(sprite, pos, vec2)
GETSET_PAIR(sprite, scale, vec2)
GETSET_PAIR(sprite, angle, number)
GETSET_PAIR(sprite, frame, rect)
GETSET_PAIR(sprite,go,gameobject)
static const JSCFunctionListEntry js_sprite_funcs[] = {
CGETSET_ADD(sprite,pos),
CGETSET_ADD(sprite,scale),
CGETSET_ADD(sprite,angle),
CGETSET_ADD(sprite,tex),
CGETSET_ADD(sprite,color),
CGETSET_ADD(sprite,emissive),
CGETSET_ADD(sprite,enabled),
CGETSET_ADD(sprite,parallax),
CGETSET_ADD(sprite,frame),
CGETSET_ADD(sprite,go)
};
#define GETFN(ID, ENTRY, TYPE) \
2024-03-18 08:16:25 -05:00
JSValue js_##ID##_##ENTRY (JSContext *js, JSValue this, JSValue val) {\
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
GETFN(texture,width,number)
GETFN(texture,height,number)
2024-03-18 08:16:25 -05:00
JSValue js_texture_path(JSContext *js, JSValue this, JSValue val)
{
return str2js(tex_get_path(js2texture(this)));
}
2024-03-18 08:16:25 -05:00
JSValue js_texture_find(JSContext *js, JSValue this, int argc, JSValue *argv) {
char *str = js2str(argv[0]);
JSValue ret = texture2js(texture_from_file(str));
JS_FreeCString(js,str);
return ret;
}
2024-03-18 14:27:52 -05:00
static const JSCFunctionListEntry js_texture_funcs[] = {
2024-03-18 08:16:25 -05:00
MIST_FUNC_DEF(texture, width, 0),
MIST_FUNC_DEF(texture, height, 0),
MIST_FUNC_DEF(texture, path, 0),
MIST_FUNC_DEF(texture, find, 1),
2024-03-18 14:27:52 -05:00
//MIST_FUNC_DEF(texture, dimensions, 1),
};
2024-01-14 10:24:31 -06:00
JSValue constraint_set_max_force (JSContext *js, JSValue this, JSValue val) {
cpConstraintSetMaxForce(js2constraint(this)->c, js2number(val));
return JS_UNDEFINED;
}
JSValue constraint_get_max_force(JSContext *js, JSValue this) { return number2js(cpConstraintGetMaxForce(js2constraint(this)->c));
}
JSValue constraint_set_collide (JSContext *js, JSValue this, JSValue val) {
cpConstraintSetCollideBodies(js2constraint(this)->c, js2bool(val));
return JS_UNDEFINED;
}
JSValue constraint_get_collide(JSContext *js, JSValue this) { return bool2js(cpConstraintGetCollideBodies(js2constraint(this)->c));
}
static const JSCFunctionListEntry js_constraint_funcs[] = {
CGETSET_ADD(constraint, max_force),
CGETSET_ADD(constraint, collide),
};
2023-05-12 13:22:05 -05:00
JSValue duk_cmd_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-04-18 17:58:44 -05:00
struct phys2d_edge *edge = js2ptr(argv[1]);
2023-02-08 15:30:12 -06:00
2023-10-26 11:48:02 -05:00
if (!edge) {
YughError("Attempted to do a cmd on edge %p. Not found.", edge);
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-10-26 11:48:02 -05:00
}
2023-02-24 14:13:35 -06:00
2023-12-12 19:35:34 -06:00
HMM_Vec2 *v1 = NULL;
2023-05-12 13:22:05 -05:00
switch (cmd) {
case 0:
2023-12-12 19:35:34 -06:00
v1 = js2cpvec2arr(argv[2]);
phys2d_edge_update_verts(edge,v1);
2023-05-12 13:22:05 -05:00
break;
case 1:
edge->thickness = js2number(argv[2]);
break;
2023-02-08 15:30:12 -06:00
}
2023-12-12 19:35:34 -06:00
if (v1) arrfree(v1);
2023-05-12 13:22:05 -05:00
2023-11-29 12:40:13 -06:00
return JS_UNDEFINED;
2023-01-25 21:32:58 -06:00
}
2023-05-12 13:22:05 -05:00
JSValue duk_inflate_cpv(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
2023-11-14 09:20:09 -06:00
HMM_Vec2 *points = js2cpvec2arr(argv[0]);
2024-03-18 14:27:52 -05:00
int n = js2number(argv[1]);
2023-04-18 17:58:44 -05:00
double d = js2number(argv[2]);
2023-11-14 09:20:09 -06:00
HMM_Vec2 *infl = inflatepoints(points,d,n);
JSValue arr = vecarr2js(infl,arrlen(infl));
2023-12-12 19:35:34 -06:00
arrfree(infl);
2023-12-12 19:35:34 -06:00
arrfree(points);
2023-04-18 17:58:44 -05:00
return arr;
2023-03-19 20:33:05 -05:00
}
2023-12-18 06:45:27 -06:00
const char *STRTEST = "TEST STRING";
2024-03-01 11:45:06 -06:00
JSValue duk_performance(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
2023-12-18 06:45:27 -06:00
{
2024-03-18 14:27:52 -05:00
int cmd = js2number(argv[0]);
2023-12-18 06:45:27 -06:00
switch(cmd) {
case 0:
break;
case 1:
js2number(argv[1]);
break;
case 2:
js2cpvec2arr(argv[1]);
break;
case 3:
2024-01-14 10:24:31 -06:00
return number2js(1.0);
2023-12-18 06:45:27 -06:00
case 4:
js2str(argv[1]);
break;
case 5:
jsfloat2vec(argv[1]);
break;
case 6:
return JS_NewStringLen(js, STRTEST, sizeof(*STRTEST));
2024-01-14 10:24:31 -06:00
case 7:
for (int i = 0; i < js2number(argv[2]); i++)
script_call_sym(argv[1],0,NULL);
script_call_sym(argv[3],0,NULL);
2024-01-14 10:24:31 -06:00
break;
2023-12-18 06:45:27 -06:00
}
return JS_UNDEFINED;
}
2024-03-18 08:16:25 -05:00
JSValue js_nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
2024-01-14 12:56:52 -06:00
{
2024-01-16 07:29:27 -06:00
if (argc < 1) return JS_UNDEFINED;
2024-01-14 12:56:52 -06:00
JSValue obj = argv[0];
2024-01-31 02:42:15 -06:00
char nota[1024*1024]; // 1MB
2024-03-18 08:16:25 -05:00
char *e = js_do_nota_encode(obj, nota);
2024-01-23 10:56:34 -06:00
return JS_NewArrayBufferCopy(js, nota, e-nota);
2024-01-14 12:56:52 -06:00
}
2024-03-18 08:16:25 -05:00
JSValue js_nota_decode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
2024-01-14 12:56:52 -06:00
{
if (argc < 1) return JS_UNDEFINED;
2024-01-23 10:56:34 -06:00
size_t len;
char *nota = JS_GetArrayBuffer(js, &len, argv[0]);
2024-01-16 19:04:52 -06:00
JSValue ret;
2024-03-18 08:16:25 -05:00
js_do_nota_decode(&ret, nota);
2024-01-16 19:04:52 -06:00
return ret;
2024-01-14 12:56:52 -06:00
}
2024-03-18 08:16:25 -05:00
static const JSCFunctionListEntry js_nota_funcs[] = {
MIST_FUNC_DEF(nota, encode, 1),
MIST_FUNC_DEF(nota, decode, 1)
2024-01-16 07:29:27 -06:00
};
2024-01-14 12:56:52 -06:00
2023-05-24 20:45:50 -05:00
#define DUK_FUNC(NAME, ARGS) JS_SetPropertyStr(js, globalThis, #NAME, JS_NewCFunction(js, duk_##NAME, #NAME, ARGS));
2023-01-10 14:02:24 -06:00
2023-05-12 13:22:05 -05:00
void ffi_load() {
2023-05-24 20:45:50 -05:00
globalThis = JS_GetGlobalObject(js);
2023-09-20 13:33:11 -05:00
DUK_FUNC(make_gameobject, 0)
2023-05-12 13:22:05 -05:00
DUK_FUNC(set_body, 3)
DUK_FUNC(q_body, 2)
DUK_FUNC(spline_cmd, 6)
2023-09-27 09:37:20 -05:00
DUK_FUNC(make_circle2d, 1)
2023-05-12 13:22:05 -05:00
DUK_FUNC(cmd_circle2d, 6)
2023-10-10 17:37:58 -05:00
DUK_FUNC(make_poly2d, 1)
2023-05-12 13:22:05 -05:00
DUK_FUNC(cmd_poly2d, 6)
DUK_FUNC(make_edge2d, 3)
DUK_FUNC(cmd_edge2d, 6)
2023-11-03 08:31:06 -05:00
DUK_FUNC(make_model,2);
2023-05-12 13:22:05 -05:00
DUK_FUNC(cmd, 6)
DUK_FUNC(register_collide, 6)
DUK_FUNC(ui_text, 8)
2023-06-06 15:49:55 -05:00
DUK_FUNC(gui_img, 10)
2023-05-12 13:22:05 -05:00
DUK_FUNC(inflate_cpv, 3)
2024-03-01 11:45:06 -06:00
DUK_FUNC(performance, 2)
2024-01-14 10:24:31 -06:00
QJSCLASSPREP(ptr);
QJSCLASSPREP_FUNCS(gameobject);
2024-01-14 10:24:31 -06:00
QJSCLASSPREP_FUNCS(dsp_node);
sound_proto = JS_NewObject(js);
JS_SetPropertyFunctionList(js, sound_proto, js_sound_funcs, countof(js_sound_funcs));
JS_SetPrototype(js, sound_proto, dsp_node_proto);
QJSCLASSPREP_FUNCS(emitter);
QJSCLASSPREP_FUNCS(warp_gravity);
QJSCLASSPREP_FUNCS(warp_damp);
QJSCLASSPREP_FUNCS(sprite);
QJSCLASSPREP_FUNCS(texture);
2024-01-14 10:24:31 -06:00
QJSCLASSPREP_FUNCS(constraint);
2024-03-13 16:30:55 -05:00
QJSCLASSPREP_FUNCS(window);
2024-02-25 17:31:48 -06:00
QJSGLOBALCLASS(os);
2024-03-18 08:16:25 -05:00
QJSGLOBALCLASS(nota);
QJSGLOBALCLASS(input);
QJSGLOBALCLASS(io);
QJSGLOBALCLASS(prosperon);
QJSGLOBALCLASS(time);
QJSGLOBALCLASS(console);
QJSGLOBALCLASS(profile);
2024-03-18 08:16:25 -05:00
QJSGLOBALCLASS(game);
QJSGLOBALCLASS(gui);
QJSGLOBALCLASS(debug);
QJSGLOBALCLASS(render);
QJSGLOBALCLASS(physics);
QJSGLOBALCLASS(vector);
JS_SetPropertyStr(js, prosperon, "version", str2js(VER));
JS_SetPropertyStr(js, prosperon, "revision", str2js(COM));
JS_SetPropertyStr(js, globalThis, "window", window2js(&mainwin));
JS_SetPropertyStr(js, globalThis, "texture", JS_DupValue(js,texture_proto));
2024-03-13 16:30:55 -05:00
JS_FreeValue(js,globalThis);
2023-01-10 15:41:43 -06:00
}
2024-02-01 10:11:09 -06:00
void ffi_stop()
{
JS_FreeValue(js, sound_proto);
}