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 "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"
|
2023-09-15 03:37:07 -05:00
|
|
|
#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>
|
2023-09-04 09:48:44 -05:00
|
|
|
#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>
|
2024-03-14 14:10:06 -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>
|
2024-03-20 09:04:28 -05:00
|
|
|
#include "nota.h"
|
|
|
|
#include "render.h"
|
|
|
|
#include "model.h"
|
|
|
|
#include "HandmadeMath.h"
|
2024-05-06 21:59:22 -05:00
|
|
|
#include "par/par_streamlines.h"
|
2024-02-25 17:31:48 -06:00
|
|
|
|
|
|
|
#if (defined(_WIN32) || defined(__WIN32__))
|
|
|
|
#include <direct.h>
|
|
|
|
#define mkdir(x,y) _mkdir(x)
|
|
|
|
#endif
|
2024-02-23 16:05:30 -06:00
|
|
|
|
2023-05-24 20:45:50 -05:00
|
|
|
static JSValue globalThis;
|
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
static JSClassID js_ptr_id;
|
|
|
|
static JSClassDef js_ptr_class = { "POINTER" };
|
|
|
|
|
2024-04-02 07:41:46 -05:00
|
|
|
JSValue str2js(const char *c, ...) {
|
2024-03-20 14:32:35 -05:00
|
|
|
if (!c) return JS_UNDEFINED;
|
2024-04-02 07:41:46 -05:00
|
|
|
char *result = NULL;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, c);
|
|
|
|
va_list args2;
|
|
|
|
va_copy(args2,args);
|
|
|
|
char buf[1+vsnprintf(NULL, 0, c, args)];
|
|
|
|
va_end(args);
|
|
|
|
vsnprintf(buf, sizeof buf, c, args2);
|
|
|
|
va_end(args2);
|
|
|
|
return JS_NewString(js, buf);
|
2024-03-20 14:32:35 -05:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
const char *js2str(JSValue v) {
|
|
|
|
return JS_ToCString(js, v);
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:59:22 -05:00
|
|
|
void sg_buffer_free(sg_buffer *b)
|
|
|
|
{
|
|
|
|
sg_dealloc_buffer(*b);
|
|
|
|
}
|
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
void jsfreestr(const char *s) { JS_FreeCString(js, s); }
|
2023-11-29 12:40:13 -06:00
|
|
|
QJSCLASS(gameobject)
|
2024-05-02 13:52:28 -05:00
|
|
|
QJSCLASS(transform3d)
|
|
|
|
QJSCLASS(transform2d)
|
2024-01-14 10:24:31 -06:00
|
|
|
QJSCLASS(emitter)
|
|
|
|
QJSCLASS(dsp_node)
|
2024-03-13 03:51:44 -05:00
|
|
|
QJSCLASS(texture)
|
2024-04-09 16:48:15 -05:00
|
|
|
QJSCLASS(font)
|
2024-03-13 03:51:44 -05:00
|
|
|
QJSCLASS(sprite)
|
2024-01-14 10:24:31 -06:00
|
|
|
QJSCLASS(warp_gravity)
|
|
|
|
QJSCLASS(warp_damp)
|
|
|
|
QJSCLASS(material)
|
2024-04-20 12:55:20 -05:00
|
|
|
QJSCLASS(model)
|
2024-03-13 16:30:55 -05:00
|
|
|
QJSCLASS(window)
|
2024-04-02 07:41:46 -05:00
|
|
|
QJSCLASS(constraint)
|
2024-05-06 21:59:22 -05:00
|
|
|
QJSCLASS(primitive)
|
|
|
|
QJSCLASS(sg_buffer)
|
2024-01-14 10:24:31 -06:00
|
|
|
|
|
|
|
static JSValue sound_proto;
|
|
|
|
sound *js2sound(JSValue v) { return js2dsp_node(v)->data; }
|
2023-11-29 12:40:13 -06:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
#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')
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
int js2boolean(JSValue v) { return JS_ToBool(js, v); }
|
|
|
|
JSValue boolean2js(int b) { return JS_NewBool(js,b); }
|
2023-12-09 22:09:15 -06:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
void js_setprop_num(JSValue obj, uint32_t i, JSValue v) { JS_SetPropertyUint32(js, obj, i, v); }
|
|
|
|
|
|
|
|
JSValue js_getpropidx(JSValue v, uint32_t i)
|
|
|
|
{
|
|
|
|
JSValue p = JS_GetPropertyUint32(js, v, i);
|
|
|
|
JS_FreeValue(js,p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
void js_setprop_str(JSValue obj, const char *prop, JSValue v) { JS_SetPropertyStr(js, obj, prop, v); }
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:59:22 -05:00
|
|
|
void js_setpropstr(JSValue v, const char *str, JSValue p)
|
|
|
|
{
|
|
|
|
JS_SetPropertyStr(js, v, str, p);
|
|
|
|
}
|
|
|
|
|
2024-01-01 07:44:43 -06:00
|
|
|
static inline cpBody *js2body(JSValue v) { return js2gameobject(v)->body; }
|
|
|
|
|
2023-12-21 17:21:01 -06:00
|
|
|
JSValue strarr2js(char **c)
|
2023-09-11 02:46:12 -05:00
|
|
|
{
|
|
|
|
JSValue arr = JS_NewArray(js);
|
2023-09-13 01:08:32 -05:00
|
|
|
for (int i = 0; i < arrlen(c); i++)
|
2023-10-03 17:16:38 -05:00
|
|
|
js_setprop_num(arr,i,JS_NewString(js, c[i]));
|
2023-09-11 02:46:12 -05:00
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2024-04-17 06:32:25 -05:00
|
|
|
JSValue js2strarr(JSValue v)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
JSValue number2js(double g) { return JS_NewFloat64(js,g); }
|
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); }
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
JSValue ptr2js(void *ptr) {
|
2023-10-16 09:40:43 -05:00
|
|
|
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) {
|
2024-05-02 13:52:28 -05:00
|
|
|
if (JS_IsUndefined(v)) return 0;
|
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
|
2024-03-20 14:32:35 -05:00
|
|
|
*tmp = boolean2js(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);
|
2023-09-23 12:35:02 -05:00
|
|
|
float a = JS_IsUndefined(c[3]) ? 1.0 : js2number(c[3]);
|
2023-05-16 01:31:13 -05:00
|
|
|
struct rgba color = {
|
2023-09-23 12:35:02 -05:00
|
|
|
.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
|
|
|
|
2023-10-03 17:16:38 -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
|
|
|
|
2023-10-31 12:38:23 -05: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));
|
2023-10-31 12:38:23 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
cpVect js2cvec2(JSValue v) { return js2vec2(v).cp; }
|
|
|
|
JSValue cvec22js(cpVect v) { return vec22js((HMM_Vec2)v); }
|
2023-10-04 08:18:09 -05:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
HMM_Vec4 js2vec4(JSValue v)
|
|
|
|
{
|
|
|
|
HMM_Vec4 v4;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
v4.e[i] = js2number(js_getpropidx(v,i));
|
|
|
|
return v4;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue vec42js(HMM_Vec4 v)
|
|
|
|
{
|
|
|
|
JSValue array = JS_NewArray(js);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
js_setprop_num(array,i,number2js(v.e[i]));
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
cpBitmask js2bitmask(JSValue v) {
|
2024-04-12 13:53:00 -05:00
|
|
|
cpBitmask a;
|
|
|
|
JS_ToUint32(js, &a, v);
|
|
|
|
return a;
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
2024-04-12 13:53:00 -05:00
|
|
|
JSValue bitmask2js(cpBitmask mask) { return JS_NewUint32(js, mask); }
|
2023-02-02 17:52:15 -06:00
|
|
|
|
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
|
|
|
|
2024-04-20 12:55:20 -05:00
|
|
|
HMM_Vec3 *js2cpvec3arr(JSValue v)
|
|
|
|
{
|
|
|
|
HMM_Vec3 *arr = NULL;
|
|
|
|
int n = js_arrlen(v);
|
|
|
|
arrsetlen(arr,n);
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
arr[i] = js2vec3(js_getpropidx(v,i));
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
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-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++)
|
2024-03-13 03:51:44 -05:00
|
|
|
js_setprop_num(array,i,vec22js(points[i]));
|
2023-10-03 17:16:38 -05:00
|
|
|
|
2023-04-18 14:49:17 -05:00
|
|
|
return array;
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
|
|
|
|
2023-10-10 17:37:58 -05:00
|
|
|
int js_print_exception(JSValue v)
|
|
|
|
{
|
2024-04-16 12:36:26 -05:00
|
|
|
if (!js) return 0;
|
2023-10-10 17:37:58 -05:00
|
|
|
#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-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 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;
|
|
|
|
|
2023-09-04 01:20:55 -05:00
|
|
|
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-09-04 01:20:55 -05:00
|
|
|
}
|
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-09-04 01:20:55 -05:00
|
|
|
}
|
2023-02-08 15:30:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
circle2d *js2circle2d(JSValue v) { return js2ptr(v); }
|
2023-01-11 16:57:34 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
JSC_CCALL(circle2d_set_radius, js2circle2d(argv[0])->radius = js2number(argv[1]))
|
|
|
|
JSC_CCALL(circle2d_get_radius, return number2js(js2circle2d(argv[0])->radius))
|
|
|
|
JSC_CCALL(circle2d_set_offset, js2circle2d(argv[0])->offset = js2vec2(argv[1]))
|
|
|
|
JSC_CCALL(circle2d_get_offset, return vec22js(js2circle2d(argv[0])->offset))
|
|
|
|
JSC_CCALL(circle2d_sync, phys2d_shape_apply(&js2circle2d(argv[0])->shape))
|
2023-01-10 07:13:00 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
static const JSCFunctionListEntry js_circle2d_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(circle2d, set_radius, 2),
|
|
|
|
MIST_FUNC_DEF(circle2d, get_radius, 1),
|
|
|
|
MIST_FUNC_DEF(circle2d, set_offset, 2),
|
|
|
|
MIST_FUNC_DEF(circle2d, get_offset, 1),
|
|
|
|
MIST_FUNC_DEF(circle2d, sync, 1),
|
|
|
|
};
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
struct phys2d_poly *js2poly2d(JSValue v) { return js2ptr(v); }
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
JSC_CCALL(poly2d_setverts,
|
|
|
|
struct phys2d_poly *p = js2poly2d(argv[0]);
|
|
|
|
HMM_Vec2 *v = js2cpvec2arr(argv[1]);
|
|
|
|
phys2d_poly_setverts(p,v);
|
|
|
|
arrfree(v);
|
|
|
|
)
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
static const JSCFunctionListEntry js_poly2d_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(poly2d, setverts, 2),
|
|
|
|
};
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(warp_gravity, strength, number)
|
|
|
|
JSC_GETSET(warp_gravity, decay, number)
|
|
|
|
JSC_GETSET(warp_gravity, spherical, boolean)
|
|
|
|
JSC_GETSET(warp_gravity, mask, bitmask)
|
|
|
|
JSC_GETSET(warp_gravity, planar_force, vec3)
|
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),
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(warp_damp, damp, vec3)
|
2024-01-14 10:24:31 -06:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_warp_damp_funcs [] = {
|
|
|
|
CGETSET_ADD(warp_damp, damp)
|
|
|
|
};
|
|
|
|
|
2024-05-06 21:59:22 -05:00
|
|
|
sg_bindings js2bind(JSValue mat, JSValue prim)
|
|
|
|
{
|
|
|
|
sg_bindings bind = {0};
|
|
|
|
for (int i = 0; i < js_arrlen(mat); i++) {
|
|
|
|
bind.fs.images[i] = js2texture(js_getpropidx(mat, i))->id;
|
|
|
|
bind.fs.samplers[i] = std_sampler;
|
|
|
|
}
|
|
|
|
|
|
|
|
bind.vertex_buffers[0] = *js2sg_buffer(js_getpropstr(prim, "pos"));
|
|
|
|
bind.index_buffer = *js2sg_buffer(js_getpropstr(prim, "index"));
|
|
|
|
bind.vertex_buffers[1] = *js2sg_buffer(js_getpropstr(prim, "uv"));
|
|
|
|
|
|
|
|
return bind;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(emitter, life, number)
|
|
|
|
JSC_GETSET(emitter, life_var, number)
|
|
|
|
JSC_GETSET(emitter, speed, number)
|
|
|
|
JSC_GETSET(emitter, variation, number)
|
|
|
|
JSC_GETSET(emitter, divergence, number)
|
|
|
|
JSC_GETSET(emitter, scale, number)
|
|
|
|
JSC_GETSET(emitter, scale_var, number)
|
|
|
|
JSC_GETSET(emitter, grow_for, number)
|
|
|
|
JSC_GETSET(emitter, shrink_for, number)
|
|
|
|
JSC_GETSET(emitter, max, number)
|
|
|
|
JSC_GETSET(emitter, explosiveness, number)
|
|
|
|
JSC_GETSET(emitter, bounce, number)
|
|
|
|
JSC_GETSET(emitter, collision_mask, bitmask)
|
|
|
|
JSC_GETSET(emitter, die_after_collision, boolean)
|
|
|
|
JSC_GETSET(emitter, persist, number)
|
|
|
|
JSC_GETSET(emitter, persist_var, number)
|
|
|
|
JSC_GETSET(emitter, warp_mask, bitmask)
|
2024-05-06 21:59:22 -05:00
|
|
|
JSC_CCALL(emitter_emit, emitter_emit(js2emitter(this), js2number(argv[0]), js2transform2d(argv[1])))
|
|
|
|
JSC_CCALL(emitter_step, emitter_step(js2emitter(this), js2number(argv[0]), js2transform2d(argv[1])))
|
|
|
|
JSC_CCALL(emitter_draw, emitter_draw(js2emitter(this), js2bind(argv[0], argv[1])))
|
2024-01-14 10:24:31 -06:00
|
|
|
|
2024-03-18 14:27:52 -05:00
|
|
|
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-04-20 12:55:20 -05:00
|
|
|
arrfree(v1);
|
2024-03-18 14:27:52 -05:00
|
|
|
)
|
|
|
|
|
2024-04-20 12:55:20 -05:00
|
|
|
JSC_CCALL(render_line3d,
|
|
|
|
void *v1 = js2cpvec3arr(argv[0]);
|
|
|
|
draw_line3d(v1, js_arrlen(argv[0]), js2color(argv[1]), 0, 0);
|
|
|
|
arrfree(v1);
|
|
|
|
);
|
|
|
|
|
2024-05-03 16:26:40 -05:00
|
|
|
JSC_CCALL(render_flush, debug_flush(&useproj); )
|
|
|
|
|
|
|
|
JSC_CCALL(render_flushtext, text_flush())
|
|
|
|
|
2024-03-27 15:00:59 -05:00
|
|
|
JSC_CCALL(render_end_pass,
|
|
|
|
sg_end_pass();
|
|
|
|
sg_commit();
|
|
|
|
debug_newframe();
|
|
|
|
)
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1)))
|
2024-03-27 15:00:59 -05:00
|
|
|
JSC_CCALL(render_set_camera, useproj = projection)
|
2024-04-18 17:34:56 -05:00
|
|
|
JSC_CCALL(render_hud_res,
|
|
|
|
HMM_Vec2 xy = js2vec2(argv[0]);
|
2024-04-20 12:55:20 -05:00
|
|
|
useproj = HMM_Orthographic_RH_ZO(0, xy.x, 0, xy.y, -1, 1);
|
2024-04-18 17:34:56 -05:00
|
|
|
)
|
2024-04-17 08:57:45 -05:00
|
|
|
JSC_CCALL(render_clear_color,
|
|
|
|
sg_color c;
|
|
|
|
rgba2floats(&c, js2color(argv[0]));
|
|
|
|
pass_action.colors[0].clear_value = c;
|
|
|
|
)
|
2024-03-18 08:16:25 -05:00
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
sg_shader js2shader(JSValue v)
|
|
|
|
{
|
2024-04-30 10:32:27 -05:00
|
|
|
sg_shader_desc desc = {0};
|
2024-05-02 13:52:28 -05:00
|
|
|
JSValue prog = v;
|
2024-04-30 10:32:27 -05:00
|
|
|
JSValue vs = js_getpropstr(prog, "vs");
|
|
|
|
JSValue fs = js_getpropstr(prog, "fs");
|
|
|
|
char *vsf = js2str(js_getpropstr(vs, "code"));
|
|
|
|
char *fsf = js2str(js_getpropstr(fs, "code"));
|
|
|
|
desc.vs.source = vsf;
|
|
|
|
desc.fs.source = fsf;
|
|
|
|
char *vsmain = js2str(js_getpropstr(vs, "entry_point"));
|
|
|
|
char *fsmain = js2str(js_getpropstr(fs, "entry_point"));
|
|
|
|
desc.vs.entry = vsmain;
|
|
|
|
desc.fs.entry = fsmain;
|
|
|
|
JSValue vsu = js_getpropstr(vs, "uniform_blocks");
|
|
|
|
int unin = js_arrlen(vsu);
|
|
|
|
for (int i = 0; i < unin; i++) {
|
|
|
|
JSValue u = js_getpropidx(vsu, i);
|
|
|
|
int slot = js2number(js_getpropstr(u, "slot"));
|
|
|
|
desc.vs.uniform_blocks[slot].size = js2number(js_getpropstr(u, "size"));
|
|
|
|
desc.vs.uniform_blocks[slot].layout = SG_UNIFORMLAYOUT_STD140;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue fsu = js_getpropstr(fs, "uniform_blocks");
|
|
|
|
unin = js_arrlen(fsu);
|
|
|
|
for (int i = 0; i < unin; i++) {
|
|
|
|
JSValue u = js_getpropidx(fsu, i);
|
|
|
|
int slot = js2number(js_getpropstr(u, "slot"));
|
|
|
|
desc.fs.uniform_blocks[slot].size = js2number(js_getpropstr(u, "size"));
|
|
|
|
desc.fs.uniform_blocks[slot].layout = SG_UNIFORMLAYOUT_STD140;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue imgs = js_getpropstr(fs, "images");
|
|
|
|
unin = js_arrlen(imgs);
|
|
|
|
for (int i = 0; i < unin; i++) {
|
|
|
|
JSValue u = js_getpropidx(imgs, i);
|
|
|
|
int slot = js2number(js_getpropstr(u, "slot"));
|
|
|
|
desc.fs.images[i].used = true;
|
|
|
|
desc.fs.images[i].multisampled = js2boolean(js_getpropstr(u, "multisampled"));
|
|
|
|
desc.fs.images[i].image_type = SG_IMAGETYPE_2D;
|
|
|
|
desc.fs.images[i].sample_type = SG_IMAGESAMPLETYPE_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue samps = js_getpropstr(fs, "samplers");
|
|
|
|
unin = js_arrlen(samps);
|
|
|
|
for (int i = 0; i < unin; i++) {
|
|
|
|
desc.fs.samplers[0].used = true;
|
|
|
|
desc.fs.samplers[0].sampler_type = SG_SAMPLERTYPE_FILTERING;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue pairs = js_getpropstr(fs, "image_sampler_pairs");
|
|
|
|
unin = js_arrlen(pairs);
|
|
|
|
for (int i = 0; i < unin; i++) {
|
|
|
|
desc.fs.image_sampler_pairs[0].used = true;
|
|
|
|
desc.fs.image_sampler_pairs[0].image_slot = 0;
|
|
|
|
desc.fs.image_sampler_pairs[0].sampler_slot = 0;
|
|
|
|
}
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
sg_shader sh = sg_make_shader(&desc);
|
|
|
|
|
|
|
|
jsfreestr(vsf);
|
|
|
|
jsfreestr(fsf);
|
|
|
|
jsfreestr(vsmain);
|
|
|
|
jsfreestr(fsmain);
|
|
|
|
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(render_pipeline3d,
|
|
|
|
sg_shader sgshader = js2shader(argv[0]);
|
|
|
|
sg_pipeline_desc p = {0};
|
|
|
|
p.shader = sgshader;
|
|
|
|
sg_vertex_layout_state st = {0};
|
|
|
|
st.attrs[MAT_POS].format = SG_VERTEXFORMAT_FLOAT3;
|
|
|
|
st.attrs[MAT_UV].format = SG_VERTEXFORMAT_USHORT2N;
|
|
|
|
st.attrs[MAT_UV].buffer_index = MAT_UV;
|
|
|
|
st.attrs[MAT_NORM].format = SG_VERTEXFORMAT_UINT10_N2;
|
|
|
|
st.attrs[MAT_NORM].buffer_index = MAT_NORM;
|
|
|
|
st.attrs[MAT_WEIGHT].format = SG_VERTEXFORMAT_UBYTE4N;
|
|
|
|
st.attrs[MAT_WEIGHT].buffer_index = MAT_WEIGHT;
|
|
|
|
st.attrs[MAT_BONE].format = SG_VERTEXFORMAT_UBYTE4;
|
|
|
|
st.attrs[MAT_BONE].buffer_index = MAT_BONE;
|
|
|
|
st.attrs[MAT_COLOR].format = SG_VERTEXFORMAT_UBYTE4N;
|
|
|
|
st.attrs[MAT_COLOR].buffer_index = MAT_COLOR;
|
|
|
|
p.layout = st;
|
|
|
|
p.index_type = SG_INDEXTYPE_UINT16;
|
|
|
|
p.depth.write_enabled = true;
|
|
|
|
p.depth.compare = SG_COMPAREFUNC_LESS_EQUAL;
|
|
|
|
p.cull_mode = SG_CULLMODE_FRONT;
|
|
|
|
|
|
|
|
sg_pipeline pipe = sg_make_pipeline(&p);
|
|
|
|
return number2js(pipe.id);
|
|
|
|
)
|
|
|
|
|
2024-05-06 21:59:22 -05:00
|
|
|
sg_vertex_layout_state js2layout(JSValue v)
|
|
|
|
{
|
|
|
|
sg_vertex_layout_state st = {0};
|
|
|
|
JSValue inputs = js_getpropstr(js_getpropstr(v, "vs"), "inputs");
|
|
|
|
for (int i = 0; i < js_arrlen(inputs); i++) {
|
|
|
|
JSValue attr = js_getpropidx(inputs, i);
|
|
|
|
int slot = js2number(js_getpropstr(attr, "slot"));
|
|
|
|
int mat = js2number(js_getpropstr(attr, "mat"));
|
|
|
|
st.attrs[slot].format = mat2type(mat);
|
|
|
|
st.attrs[slot].buffer_index = slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(render_pipelineparticle,
|
|
|
|
sg_pipeline_desc p = {0};
|
|
|
|
p.shader = js2shader(argv[0]);
|
|
|
|
p.layout = js2layout(argv[0]);
|
|
|
|
p.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP;
|
|
|
|
p.index_type = SG_INDEXTYPE_UINT16;
|
|
|
|
//p.cull_mode = SG_CULLMODE_BACK;
|
|
|
|
//p.colors[0].blend = blend_trans;
|
|
|
|
//p.depth.write_enabled = true;
|
|
|
|
//p.depth.compare = SG_COMPAREFUNC_LESS_EQUAL;
|
|
|
|
|
|
|
|
sg_pipeline pipe = sg_make_pipeline(&p);
|
|
|
|
return number2js(pipe.id);
|
|
|
|
)
|
|
|
|
|
2024-05-03 16:26:40 -05:00
|
|
|
JSC_CCALL(render_pipelinetext,
|
|
|
|
sg_pipeline_desc p = {0};
|
2024-05-06 21:59:22 -05:00
|
|
|
p.shader = js2shader(argv[0]);
|
|
|
|
p.layout = js2layout(argv[0]);
|
2024-05-03 16:26:40 -05:00
|
|
|
p.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP;
|
|
|
|
p.colors[0].blend = blend_trans;
|
|
|
|
|
|
|
|
sg_pipeline pipe = sg_make_pipeline(&p);
|
|
|
|
return number2js(pipe.id);
|
|
|
|
)
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(render_pipeline,
|
2024-05-06 21:59:22 -05:00
|
|
|
sg_pipeline_desc p = {0};
|
|
|
|
p.shader = js2shader(argv[0]);
|
|
|
|
p.layout = js2layout(argv[0]);
|
|
|
|
//p.cull_mode = SG_CULLMODE_FRONT;
|
|
|
|
p.index_type = SG_INDEXTYPE_UINT16;
|
2024-05-02 13:52:28 -05:00
|
|
|
if (js2boolean(js_getpropstr(argv[0], "blend")))
|
2024-05-06 21:59:22 -05:00
|
|
|
p.colors[0].blend = blend_trans;
|
2024-04-30 10:32:27 -05:00
|
|
|
|
2024-05-06 21:59:22 -05:00
|
|
|
sg_pipeline pipe = sg_make_pipeline(&p);
|
2024-05-02 13:52:28 -05:00
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
return number2js(pipe.id);
|
|
|
|
)
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(render_setuniv,
|
|
|
|
float f = js2number(argv[2]);
|
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(f));
|
2024-04-30 10:32:27 -05:00
|
|
|
)
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(render_setuniv2,
|
|
|
|
HMM_Vec2 v = js2vec2(argv[2]);
|
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(v.e));
|
2024-04-30 10:32:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(render_setuniv3,
|
2024-05-02 13:52:28 -05:00
|
|
|
HMM_Vec3 v = js2vec3(argv[2]);
|
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(v.e));
|
2024-04-30 10:32:27 -05:00
|
|
|
)
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(render_setuniv4,
|
|
|
|
HMM_Vec4 v = js2vec4(argv[2]);
|
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(v.e));
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(render_setuniproj,
|
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(useproj));
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(render_setunim4,
|
2024-05-02 17:13:09 -05:00
|
|
|
HMM_Mat4 m = MAT1;
|
|
|
|
if (JS_IsArray(js, argv[2])) {
|
|
|
|
JSValue arr = argv[2];
|
|
|
|
int n = js_arrlen(arr);
|
|
|
|
if (n == 1)
|
|
|
|
m = transform2d2mat4(js2transform2d(js_getpropidx(arr,0)));
|
|
|
|
else {
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
HMM_Mat4 p = transform2d2mat4(js2transform2d(js_getpropidx(arr, i)));
|
|
|
|
m = HMM_MulM4(p,m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
m = transform2d2mat4(js2transform2d(argv[2]));
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(m.e));
|
|
|
|
);
|
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
JSC_CCALL(render_spdraw,
|
2024-05-06 21:59:22 -05:00
|
|
|
sg_bindings bind = js2bind(argv[0], argv[1]);
|
2024-04-30 10:32:27 -05:00
|
|
|
sg_apply_bindings(&bind);
|
2024-05-06 21:59:22 -05:00
|
|
|
int p = js2number(js_getpropstr(argv[1], "count"));
|
|
|
|
sg_draw(0,p,1);
|
2024-04-30 10:32:27 -05:00
|
|
|
)
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(render_setpipeline,
|
|
|
|
sg_pipeline p = {0};
|
|
|
|
p.id = js2number(argv[0]);
|
|
|
|
sg_apply_pipeline(p);
|
|
|
|
)
|
|
|
|
|
2024-03-18 08:16:25 -05:00
|
|
|
static const JSCFunctionListEntry js_render_funcs[] = {
|
|
|
|
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-04-20 12:55:20 -05:00
|
|
|
MIST_FUNC_DEF(render, line3d, 2),
|
2024-05-03 16:26:40 -05:00
|
|
|
MIST_FUNC_DEF(render, flushtext, 0),
|
2024-03-18 14:27:52 -05:00
|
|
|
MIST_FUNC_DEF(render, flush, 0),
|
|
|
|
MIST_FUNC_DEF(render, end_pass, 0),
|
2024-03-27 15:00:59 -05:00
|
|
|
MIST_FUNC_DEF(render, text_size, 3),
|
|
|
|
MIST_FUNC_DEF(render, set_camera, 0),
|
2024-04-18 17:34:56 -05:00
|
|
|
MIST_FUNC_DEF(render, hud_res, 1),
|
2024-04-17 08:57:45 -05:00
|
|
|
MIST_FUNC_DEF(render, clear_color, 1),
|
2024-04-30 10:32:27 -05:00
|
|
|
MIST_FUNC_DEF(render, pipeline, 1),
|
2024-05-02 13:52:28 -05:00
|
|
|
MIST_FUNC_DEF(render, pipeline3d, 1),
|
2024-05-03 16:26:40 -05:00
|
|
|
MIST_FUNC_DEF(render, pipelinetext, 1),
|
2024-05-06 21:59:22 -05:00
|
|
|
MIST_FUNC_DEF(render, pipelineparticle, 1),
|
2024-04-30 10:32:27 -05:00
|
|
|
MIST_FUNC_DEF(render, setuniv3, 2),
|
|
|
|
MIST_FUNC_DEF(render, setuniv, 2),
|
2024-05-06 21:59:22 -05:00
|
|
|
MIST_FUNC_DEF(render, spdraw, 1),
|
2024-05-02 13:52:28 -05:00
|
|
|
MIST_FUNC_DEF(render, setuniproj, 2),
|
|
|
|
MIST_FUNC_DEF(render, setunim4, 3),
|
|
|
|
MIST_FUNC_DEF(render, setuniv2, 2),
|
|
|
|
MIST_FUNC_DEF(render, setuniv4, 2),
|
|
|
|
MIST_FUNC_DEF(render, setpipeline, 1)
|
2024-03-18 08:16:25 -05:00
|
|
|
};
|
|
|
|
|
2024-03-27 15:00:59 -05:00
|
|
|
JSC_CCALL(gui_flush, text_flush(&useproj));
|
2024-04-11 17:17:49 -05:00
|
|
|
JSC_CCALL(gui_scissor, sg_apply_scissor_rect(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), 0))
|
2024-03-19 14:39:19 -05:00
|
|
|
JSC_CCALL(gui_text,
|
|
|
|
const char *s = JS_ToCString(js, argv[0]);
|
|
|
|
HMM_Vec2 pos = js2vec2(argv[1]);
|
|
|
|
|
|
|
|
float size = js2number(argv[2]);
|
|
|
|
struct rgba c = js2color(argv[3]);
|
|
|
|
int wrap = js2number(argv[4]);
|
|
|
|
int cursor = js2number(argv[5]);
|
|
|
|
JSValue ret = JS_NewInt64(js, renderText(s, pos, size, c, wrap, cursor, 1.0));
|
|
|
|
JS_FreeCString(js, s);
|
|
|
|
return ret;
|
|
|
|
)
|
|
|
|
|
2024-04-09 16:48:15 -05:00
|
|
|
JSC_CCALL(gui_font_set, font_set(js2font(argv[0])))
|
2024-03-18 08:16:25 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_gui_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(gui, flush, 0),
|
|
|
|
MIST_FUNC_DEF(gui, scissor, 4),
|
2024-03-19 14:39:19 -05:00
|
|
|
MIST_FUNC_DEF(gui, text, 6),
|
2024-03-20 14:32:35 -05:00
|
|
|
MIST_FUNC_DEF(gui, font_set,1)
|
2024-03-18 08:16:25 -05:00
|
|
|
};
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
JSC_CCALL(spline_catmull,
|
|
|
|
HMM_Vec2 *points = js2cpvec2arr(argv[0]);
|
|
|
|
float param = js2number(argv[1]);
|
|
|
|
HMM_Vec2 *samples = catmull_rom_ma_v2(points,param);
|
|
|
|
arrfree(points);
|
|
|
|
if (!samples) return JS_UNDEFINED;
|
|
|
|
JSValue arr = vecarr2js(samples, arrlen(samples));
|
|
|
|
arrfree(samples);
|
|
|
|
return arr;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(spline_bezier,
|
|
|
|
HMM_Vec2 *points = js2cpvec2arr(argv[0]);
|
|
|
|
float param = js2number(argv[1]);
|
|
|
|
HMM_Vec2 *samples = catmull_rom_ma_v2(points,param);
|
|
|
|
arrfree(points);
|
|
|
|
if (!samples) return JS_UNDEFINED;
|
|
|
|
JSValue arr = vecarr2js(samples, arrlen(samples));
|
|
|
|
arrfree(samples);
|
|
|
|
return arr;
|
|
|
|
)
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_spline_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(spline, catmull, 2),
|
|
|
|
MIST_FUNC_DEF(spline, bezier, 2)
|
|
|
|
};
|
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_vector_dot(JSContext *js, JSValue this, int argc, JSValue *argv) { return number2js(HMM_DotV2(js2vec2(argv[0]), js2vec2(argv[1]))) ; };
|
2024-03-18 08:16:25 -05:00
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(vector_project, return vec22js(HMM_ProjV2(js2vec2(argv[0]), js2vec2(argv[1]))))
|
|
|
|
|
|
|
|
JSC_CCALL(vector_inflate,
|
|
|
|
HMM_Vec2 *p = js2cpvec2arr(argv[0]);
|
|
|
|
double d = js2number(argv[1]);
|
|
|
|
HMM_Vec2 *infl = inflatepoints(p,d, js_arrlen(argv[0]));
|
|
|
|
ret = vecarr2js(infl, arrlen(infl));
|
|
|
|
arrfree(infl);
|
|
|
|
arrfree(p);
|
|
|
|
)
|
2024-03-18 14:27:52 -05:00
|
|
|
|
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-20 14:32:35 -05:00
|
|
|
MIST_FUNC_DEF(vector, inflate, 2)
|
2024-03-18 08:16:25 -05:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(game_engine_start, engine_start(argv[0],argv[1]))
|
|
|
|
|
2024-03-18 08:16:25 -05:00
|
|
|
static const JSCFunctionListEntry js_game_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(game, engine_start, 2),
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(input_show_keyboard, sapp_show_keyboard(js2boolean(argv[0])))
|
|
|
|
JSValue js_input_keyboard_shown(JSContext *js, JSValue this) { return boolean2js(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-20 14:32:35 -05:00
|
|
|
JSC_SCALL(input_cursor_img, cursor_img(str))
|
2024-03-18 08:16:25 -05:00
|
|
|
|
|
|
|
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),
|
2024-03-20 14:32:35 -05:00
|
|
|
MIST_FUNC_DEF(input, cursor_img, 1)
|
2024-03-14 14:10:06 -05:00
|
|
|
};
|
|
|
|
|
2024-03-18 14:27:52 -05:00
|
|
|
JSC_CCALL(prosperon_phys2d_step, phys2d_update(js2number(argv[0])))
|
2024-05-02 17:13:09 -05:00
|
|
|
JSC_CCALL(prosperon_window_render, openglRender(&mainwin, js2transform2d(argv[0]), js2number(argv[1])))
|
2024-04-03 00:44:08 -05:00
|
|
|
JSC_CCALL(prosperon_guid,
|
|
|
|
uint8_t bytes[16];
|
|
|
|
for (int i = 0; i < 16; i++) bytes[i] = rand()%256;
|
|
|
|
char uuid[37];
|
|
|
|
snprintf(uuid, 37, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
|
|
|
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
|
|
|
|
return str2js(uuid);
|
|
|
|
)
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_prosperon_funcs[] = {
|
2024-03-15 11:21:36 -05:00
|
|
|
MIST_FUNC_DEF(prosperon, phys2d_step, 1),
|
2024-04-03 00:44:08 -05:00
|
|
|
MIST_FUNC_DEF(prosperon, window_render, 0),
|
|
|
|
MIST_FUNC_DEF(prosperon, guid, 0),
|
2024-03-14 14:10:06 -05:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(time_now,
|
2024-03-14 14:10:06 -05:00
|
|
|
struct timeval ct;
|
|
|
|
gettimeofday(&ct, NULL);
|
|
|
|
return number2js((double)ct.tv_sec+(double)(ct.tv_usec/1000000.0));
|
2024-03-20 14:32:35 -05:00
|
|
|
)
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
JSValue js_time_computer_dst(JSContext *js, JSValue this) {
|
|
|
|
time_t t = time(NULL);
|
2024-03-20 14:32:35 -05:00
|
|
|
return boolean2js(localtime(&t)->tm_isdst);
|
2024-03-14 14:10:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_SCALL(console_print, log_print(str))
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
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]);
|
2024-03-14 14:10:06 -05:00
|
|
|
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]);
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
mYughLog(LOG_SCRIPT, level, line, file, msg);
|
|
|
|
|
|
|
|
JS_FreeCString(js, msg);
|
|
|
|
JS_FreeCString(js, file);
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET_GLOBAL(stdout_lvl, number)
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_console_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(console,print,1),
|
|
|
|
MIST_FUNC_DEF(console,rec,4),
|
|
|
|
CGETSET_ADD(global, stdout_lvl)
|
|
|
|
};
|
|
|
|
|
2024-04-10 16:21:46 -05:00
|
|
|
JSC_GETSET_GLOBAL(CHANNELS, number)
|
|
|
|
JSC_GETSET_GLOBAL(BUF_FRAMES, number)
|
|
|
|
JSC_GETSET_GLOBAL(SAMPLERATE, number)
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_audio_funcs[] = {
|
|
|
|
CGETSET_ADD_NAME(global, CHANNELS, channels),
|
|
|
|
CGETSET_ADD_NAME(global, BUF_FRAMES, buffer_frames),
|
|
|
|
CGETSET_ADD_NAME(global, SAMPLERATE, samplerate),
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(profile_now, return number2js(stm_now()))
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_profile_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(profile,now,0),
|
2024-02-27 10:09:15 -06:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_SCALL(io_exists, ret = boolean2js(fexists(str)))
|
|
|
|
JSC_CCALL(io_ls, return strarr2js(ls(".")))
|
|
|
|
JSC_SSCALL(io_cp, ret = number2js(cp(str,str2)))
|
|
|
|
JSC_SSCALL(io_mv, ret = number2js(rename(str,str2)))
|
|
|
|
JSC_SCALL(io_chdir, ret = number2js(chdir(str)))
|
|
|
|
JSC_SCALL(io_rm, ret = number2js(remove(str)))
|
|
|
|
JSC_SCALL(io_mkdir, ret = number2js(mkdir(str,0777)))
|
2024-02-27 10:09:15 -06:00
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_io_slurpbytes(JSContext *js, JSValue this, int argc, JSValue *argv)
|
2024-02-27 10:09:15 -06:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_io_slurp(JSContext *js, JSValue this, int argc, JSValue *argv)
|
2024-02-27 10:09:15 -06:00
|
|
|
{
|
|
|
|
char *f = js2str(argv[0]);
|
|
|
|
size_t len;
|
2024-03-02 23:34:41 -06:00
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_io_slurpwrite(JSContext *js, JSValue this, int argc, JSValue *argv)
|
2024-02-27 10:09:15 -06:00
|
|
|
{
|
|
|
|
char *f = js2str(argv[0]);
|
|
|
|
size_t len;
|
2024-03-20 14:32:35 -05:00
|
|
|
const char *data;
|
2024-02-27 10:09:15 -06:00
|
|
|
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));
|
2024-02-27 10:09:15 -06:00
|
|
|
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));
|
2024-02-27 10:09:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_io_chmod(JSContext *js, JSValue this, int argc, JSValue *argv)
|
2024-03-02 00:00:35 -06:00
|
|
|
{
|
|
|
|
char *f = js2str(argv[0]);
|
2024-03-18 14:27:52 -05:00
|
|
|
int mod = js2number(argv[1]);
|
2024-03-02 00:00:35 -06:00
|
|
|
chmod(f, mod);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_SCALL(io_save_qoa, save_qoa(str))
|
2024-03-18 08:16:25 -05:00
|
|
|
|
2024-04-17 06:32:25 -05:00
|
|
|
JSC_SCALL(io_pack_start, pack_start(str))
|
|
|
|
JSC_SCALL(io_pack_add, pack_add(str))
|
|
|
|
JSC_CCALL(io_pack_end, pack_end())
|
2024-04-30 10:32:27 -05:00
|
|
|
JSC_SCALL(io_mod, ret = number2js(file_mod_secs(str));)
|
2024-04-17 06:32:25 -05:00
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
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),
|
2024-03-20 14:32:35 -05:00
|
|
|
MIST_FUNC_DEF(io, save_qoa,1),
|
2024-04-17 06:32:25 -05:00
|
|
|
MIST_FUNC_DEF(io, pack_start, 1),
|
|
|
|
MIST_FUNC_DEF(io, pack_add, 1),
|
2024-04-30 10:32:27 -05:00
|
|
|
MIST_FUNC_DEF(io, pack_end, 0),
|
|
|
|
MIST_FUNC_DEF(io, mod,1)
|
2024-03-18 08:16:25 -05:00
|
|
|
};
|
|
|
|
|
2024-04-17 08:57:45 -05:00
|
|
|
JSC_GETSET_GLOBAL(disabled_color, color)
|
|
|
|
JSC_GETSET_GLOBAL(sleep_color, color)
|
|
|
|
JSC_GETSET_GLOBAL(dynamic_color, color)
|
|
|
|
JSC_GETSET_GLOBAL(kinematic_color, color)
|
|
|
|
JSC_GETSET_GLOBAL(static_color, color)
|
|
|
|
|
2024-03-18 08:16:25 -05:00
|
|
|
static const JSCFunctionListEntry js_debug_funcs[] = {
|
2024-04-17 08:57:45 -05:00
|
|
|
CGETSET_ADD(global, disabled_color),
|
|
|
|
CGETSET_ADD(global, sleep_color),
|
|
|
|
CGETSET_ADD(global, dynamic_color),
|
|
|
|
CGETSET_ADD(global, kinematic_color),
|
|
|
|
CGETSET_ADD(global, static_color),
|
2024-03-18 08:16:25 -05:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(physics_sgscale,
|
2024-03-18 08:16:25 -05:00
|
|
|
js2gameobject(argv[0])->scale.xy = js2vec2(argv[1]);
|
|
|
|
gameobject_apply(js2gameobject(argv[0]));
|
|
|
|
cpSpaceReindexShapesForBody(space, js2gameobject(argv[0])->body);
|
2024-03-20 14:32:35 -05:00
|
|
|
)
|
2024-03-18 08:16:25 -05:00
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
JSC_CCALL(physics_closest_point,
|
|
|
|
void *v1 = js2cpvec2arr(argv[1]);
|
|
|
|
JSValue ret = number2js(point2segindex(js2vec2(argv[0]), v1, js2number(argv[2])));
|
|
|
|
arrfree(v1);
|
|
|
|
return ret;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(physics_make_gravity, return warp_gravity2js(warp_gravity_make()))
|
|
|
|
JSC_CCALL(physics_make_damp, return warp_damp2js(warp_damp_make()))
|
2024-03-20 09:04:28 -05:00
|
|
|
|
2024-04-01 17:58:29 -05:00
|
|
|
void bb_query_fn(cpShape *shape, JSValue *cb)
|
|
|
|
{
|
|
|
|
JSValue go = JS_DupValue(js,shape2go(shape)->ref);
|
|
|
|
script_call_sym(*cb, 1, &go);
|
|
|
|
JS_FreeValue(js, go);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(physics_box_query,
|
|
|
|
HMM_Vec2 pos = js2vec2(argv[0]);
|
|
|
|
HMM_Vec2 wh = js2vec2(argv[1]);
|
|
|
|
cpBB bbox;
|
|
|
|
bbox.l = pos.x-wh.x/2;
|
|
|
|
bbox.r = pos.x+wh.x/2;
|
|
|
|
bbox.t = pos.y+wh.y/2;
|
|
|
|
bbox.b = pos.y-wh.y/2;
|
2024-04-12 13:53:00 -05:00
|
|
|
cpSpaceBBQuery(space, bbox, CP_SHAPE_FILTER_ALL, bb_query_fn, &argv[3]);
|
2024-04-01 17:58:29 -05:00
|
|
|
return ret;
|
|
|
|
)
|
|
|
|
|
|
|
|
static void shape_query_fn(cpShape *shape, cpContactPointSet *points, JSValue *cb)
|
|
|
|
{
|
|
|
|
JSValue go = JS_DupValue(js,shape2go(shape)->ref);
|
|
|
|
script_call_sym(*cb, 1, &go);
|
|
|
|
JS_FreeValue(js, go);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(physics_shape_query,
|
|
|
|
cpSpaceShapeQuery(space, ((struct phys2d_shape*)js2ptr(argv[0]))->shape, shape_query_fn, &argv[1]);
|
|
|
|
)
|
|
|
|
|
2024-03-26 18:31:20 -05:00
|
|
|
static void ray_query_fn(cpShape *shape, float t, cpVect n, float a, JSValue *cb)
|
|
|
|
{
|
|
|
|
JSValue argv[3] = {
|
|
|
|
JS_DupValue(js, shape2go(shape)->ref),
|
|
|
|
vec22js((HMM_Vec2)n),
|
|
|
|
number2js(a)
|
|
|
|
};
|
|
|
|
script_call_sym(*cb, 3, argv);
|
2024-04-03 00:44:08 -05:00
|
|
|
for (int i = 0; i < 3; i++) JS_FreeValue(js, argv[i]);
|
2024-03-26 18:31:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(physics_ray_query,
|
2024-04-12 13:53:00 -05:00
|
|
|
cpSpaceSegmentQuery(space, js2vec2(argv[0]).cp, js2vec2(argv[1]).cp, js2number(argv[2]), CP_SHAPE_FILTER_ALL, ray_query_fn, &argv[3]);
|
2024-03-26 18:31:20 -05:00
|
|
|
);
|
|
|
|
|
2024-04-03 00:44:08 -05:00
|
|
|
static void point_query_fn(cpShape *shape, float dist, cpVect point, JSValue *cb)
|
|
|
|
{
|
|
|
|
JSValue argv[3] = {
|
|
|
|
JS_DupValue(js, shape2go(shape)->ref),
|
|
|
|
vec22js((HMM_Vec2)point),
|
|
|
|
number2js(dist)
|
|
|
|
};
|
|
|
|
script_call_sym(*cb, 3, argv);
|
|
|
|
for (int i = 0; i < 3; i++) JS_FreeValue(js, argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(physics_point_query,
|
2024-04-12 13:53:00 -05:00
|
|
|
cpSpacePointQuery(space, js2vec2(argv[0]).cp, js2number(argv[1]), CP_SHAPE_FILTER_ALL, point_query_fn, &argv[2]);
|
2024-04-03 00:44:08 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
JSValue pointinfo2js(cpPointQueryInfo info)
|
|
|
|
{
|
|
|
|
JSValue o = JS_NewObject(js);
|
|
|
|
JS_SetPropertyStr(js, o, "distance", number2js(info.distance));
|
|
|
|
JS_SetPropertyStr(js, o, "point", vec22js((HMM_Vec2)info.point));
|
|
|
|
JS_SetPropertyStr(js, o, "entity", JS_DupValue(js, shape2go(info.shape)->ref));
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(physics_point_query_nearest,
|
|
|
|
cpPointQueryInfo info;
|
2024-04-12 13:53:00 -05:00
|
|
|
cpShape *sh = cpSpacePointQueryNearest(space, js2vec2(argv[0]).cp, js2number(argv[1]), CP_SHAPE_FILTER_ALL, &info);
|
2024-04-03 00:44:08 -05:00
|
|
|
if (!sh) return JS_UNDEFINED;
|
|
|
|
return pointinfo2js(info);
|
|
|
|
)
|
|
|
|
|
2024-03-18 08:16:25 -05:00
|
|
|
static const JSCFunctionListEntry js_physics_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(physics, sgscale, 2),
|
2024-04-03 00:44:08 -05:00
|
|
|
MIST_FUNC_DEF(physics, point_query, 3),
|
2024-04-11 17:17:49 -05:00
|
|
|
MIST_FUNC_DEF(physics, point_query_nearest, 2),
|
|
|
|
MIST_FUNC_DEF(physics, ray_query, 4),
|
2024-04-01 17:58:29 -05:00
|
|
|
MIST_FUNC_DEF(physics, box_query, 2),
|
|
|
|
MIST_FUNC_DEF(physics, shape_query, 1),
|
2024-03-19 14:39:19 -05:00
|
|
|
MIST_FUNC_DEF(physics, closest_point, 3),
|
|
|
|
MIST_FUNC_DEF(physics, make_damp, 0),
|
2024-03-19 17:00:49 -05:00
|
|
|
MIST_FUNC_DEF(physics, make_gravity, 0),
|
2024-02-23 16:05:30 -06:00
|
|
|
};
|
|
|
|
|
2024-04-20 12:55:20 -05:00
|
|
|
JSC_CCALL(model_draw_go,
|
|
|
|
model_draw_go(js2model(this), js2gameobject(argv[0]), js2gameobject(argv[1]))
|
|
|
|
);
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_model_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(model, draw_go, 1)
|
|
|
|
};
|
|
|
|
|
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, 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, emit, 1),
|
2024-05-06 21:59:22 -05:00
|
|
|
MIST_FUNC_DEF(emitter, step, 1),
|
|
|
|
MIST_FUNC_DEF(emitter, draw, 1)
|
2024-01-14 10:24:31 -06:00
|
|
|
};
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_GETSET(transform2d, pos, vec2)
|
|
|
|
JSC_GETSET(transform2d, scale, vec2)
|
|
|
|
JSC_GETSET(transform2d, angle, number)
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_transform2d_funcs[] = {
|
|
|
|
CGETSET_ADD(transform2d, pos),
|
|
|
|
CGETSET_ADD(transform2d, scale),
|
|
|
|
CGETSET_ADD(transform2d, angle)
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(dsp_node, pass, boolean)
|
|
|
|
JSC_GETSET(dsp_node, off, boolean)
|
|
|
|
JSC_GETSET(dsp_node, gain, number)
|
|
|
|
JSC_GETSET(dsp_node, pan, number)
|
2024-01-14 10:24:31 -06:00
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(dsp_node_plugin, plugin_node(js2dsp_node(this), js2dsp_node(argv[0])))
|
|
|
|
JSC_CCALL(dsp_node_unplug, unplug_node(js2dsp_node(this)))
|
2024-01-14 10:24:31 -06:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(sound, loop, boolean)
|
|
|
|
JSC_GETSET(sound, timescale, number)
|
|
|
|
JSC_GETSET(sound, frame, number)
|
2024-03-19 14:39:19 -05:00
|
|
|
JSC_CCALL(sound_frames, return number2js(js2sound(this)->data->frames))
|
2024-01-14 10:24:31 -06:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_sound_funcs[] = {
|
|
|
|
CGETSET_ADD(sound, loop),
|
|
|
|
CGETSET_ADD(sound, timescale),
|
|
|
|
CGETSET_ADD(sound, frame),
|
2024-03-19 14:39:19 -05:00
|
|
|
MIST_FUNC_DEF(sound, frames, 0),
|
2024-01-14 10:24:31 -06:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
static JSValue js_window_get_size(JSContext *js, JSValue this) { return vec22js(js2window(this)->size); }
|
|
|
|
static JSValue js_window_set_size(JSContext *js, JSValue this, JSValue v) {
|
2024-03-13 16:30:55 -05:00
|
|
|
window *w = js2window(this);
|
|
|
|
if (!w->start)
|
|
|
|
w->size = js2vec2(v);
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
2024-04-03 17:17:32 -05:00
|
|
|
static JSValue js_window_get_rendersize(JSContext *js, JSValue this) {
|
|
|
|
window *w = js2window(this);
|
|
|
|
if (w->rendersize.x == 0 || w->rendersize.y == 0) return vec22js(w->size);
|
|
|
|
return vec22js(w->rendersize);
|
|
|
|
}
|
|
|
|
static JSValue js_window_set_rendersize(JSContext *js, JSValue this, JSValue v) {
|
|
|
|
js2window(this)->rendersize = js2vec2(v);
|
2024-04-16 22:27:29 -05:00
|
|
|
return JS_UNDEFINED;
|
2024-04-03 17:17:32 -05:00
|
|
|
}
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(window, mode, number)
|
|
|
|
static JSValue js_window_get_fullscreen(JSContext *js, JSValue this) { return boolean2js(js2window(this)->fullscreen); }
|
2024-04-16 22:27:29 -05:00
|
|
|
static JSValue js_window_set_fullscreen(JSContext *js, JSValue this, JSValue v) { window_setfullscreen(js2window(this), js2boolean(v)); return JS_UNDEFINED; }
|
2024-03-13 16:30:55 -05:00
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
static JSValue js_window_set_title(JSContext *js, JSValue this, JSValue v)
|
2024-03-13 16:30:55 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2024-04-16 22:27:29 -05:00
|
|
|
JSC_CCALL(window_get_title, return str2js(js2window(this)->title))
|
2024-03-21 21:07:24 -05:00
|
|
|
JSC_CCALL(window_set_icon, window_seticon(&mainwin, js2texture(argv[0])))
|
2024-04-10 16:21:46 -05:00
|
|
|
JSC_GETSET(window, vsync, boolean)
|
|
|
|
JSC_GETSET(window, enable_clipboard, boolean)
|
|
|
|
JSC_GETSET(window, enable_dragndrop, boolean)
|
|
|
|
JSC_GETSET(window, high_dpi, boolean)
|
|
|
|
JSC_GETSET(window, sample_count, number)
|
2024-03-18 08:16:25 -05:00
|
|
|
|
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-04-10 16:21:46 -05:00
|
|
|
CGETSET_ADD(window, vsync),
|
|
|
|
CGETSET_ADD(window, enable_clipboard),
|
|
|
|
CGETSET_ADD(window, enable_dragndrop),
|
|
|
|
CGETSET_ADD(window, high_dpi),
|
|
|
|
CGETSET_ADD(window, sample_count),
|
2024-03-20 14:32:35 -05:00
|
|
|
MIST_FUNC_DEF(window, set_icon, 1)
|
2024-03-13 16:30:55 -05:00
|
|
|
};
|
|
|
|
|
2024-04-09 08:01:54 -05:00
|
|
|
JSValue js_gameobject_set_rpos(JSContext *js, JSValue this, JSValue val) {
|
2024-04-03 17:17:32 -05:00
|
|
|
cpBody *b = js2gameobject(this)->body;
|
|
|
|
cpBodySetPosition(b, js2cvec2(val));
|
|
|
|
if (cpBodyGetType(b) == CP_BODY_TYPE_STATIC)
|
|
|
|
cpSpaceReindexShapesForBody(space, b);
|
|
|
|
return JS_UNDEFINED;
|
2024-04-03 00:44:08 -05:00
|
|
|
}
|
2024-04-09 08:01:54 -05:00
|
|
|
JSValue js_gameobject_get_rpos(JSContext *js, JSValue this) { return cvec22js(cpBodyGetPosition(js2gameobject(this)->body)); }
|
2024-04-16 22:27:29 -05:00
|
|
|
JSValue js_gameobject_set_rangle (JSContext *js, JSValue this, JSValue val) { cpBodySetAngle(js2gameobject(this)->body, HMM_TurnToRad*js2number(val)); return JS_UNDEFINED; }
|
2024-04-09 08:01:54 -05:00
|
|
|
JSValue js_gameobject_get_rangle (JSContext *js, JSValue this) { return number2js(HMM_RadToTurn*cpBodyGetAngle(js2gameobject(this)->body)); }
|
|
|
|
JSValue js_gameobject_get_rscale(JSContext *js, JSValue this) { return vec32js(js2gameobject(this)->scale); }
|
2024-04-16 22:27:29 -05:00
|
|
|
JSValue js_gameobject_set_rscale(JSContext *js, JSValue this, JSValue val) { js2gameobject(this)->scale = js2vec3(val); return JS_UNDEFINED; }
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET_BODY(velocity, Velocity, cvec2)
|
2024-04-16 22:27:29 -05:00
|
|
|
JSValue js_gameobject_set_angularvelocity (JSContext *js, JSValue this, JSValue val) { cpBodySetAngularVelocity(js2gameobject(this)->body, HMM_TurnToRad*js2number(val)); return JS_UNDEFINED;}
|
2024-04-01 17:58:29 -05:00
|
|
|
JSValue js_gameobject_get_angularvelocity (JSContext *js, JSValue this) { return number2js(HMM_RadToTurn*cpBodyGetAngularVelocity(js2gameobject(this)->body)); }
|
2024-04-09 16:48:15 -05:00
|
|
|
//JSC_GETSET_BODY(moi, Moment, number)
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET_BODY(torque, Torque, number)
|
2024-03-19 14:39:19 -05:00
|
|
|
JSC_CCALL(gameobject_impulse, cpBodyApplyImpulseAtWorldPoint(js2gameobject(this)->body, js2vec2(argv[0]).cp, cpBodyGetPosition(js2gameobject(this)->body)))
|
|
|
|
JSC_CCALL(gameobject_force, cpBodyApplyForceAtWorldPoint(js2gameobject(this)->body, js2vec2(argv[0]).cp, cpBodyGetPosition(js2gameobject(this)->body)))
|
|
|
|
JSC_CCALL(gameobject_force_local, cpBodyApplyForceAtLocalPoint(js2gameobject(this)->body, js2vec2(argv[0]).cp, js2vec2(argv[1]).cp))
|
2024-03-20 16:07:23 -05:00
|
|
|
JSC_GETSET_APPLY(gameobject, friction, number)
|
|
|
|
JSC_GETSET_APPLY(gameobject, elasticity, number)
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET_APPLY(gameobject, mass, number)
|
2024-03-20 16:07:23 -05:00
|
|
|
JSC_GETSET_APPLY(gameobject, phys, number)
|
|
|
|
JSC_GETSET_APPLY(gameobject, layer, number)
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(gameobject, damping, number)
|
|
|
|
JSC_GETSET(gameobject, timescale, number)
|
|
|
|
JSC_GETSET(gameobject, maxvelocity, number)
|
|
|
|
JSC_GETSET(gameobject, maxangularvelocity, number)
|
2024-04-12 13:53:00 -05:00
|
|
|
JSC_GETSET(gameobject, warp_mask, bitmask)
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(gameobject, drawlayer, number)
|
2024-04-12 13:53:00 -05:00
|
|
|
JSC_GETSET(gameobject, categories, bitmask)
|
|
|
|
JSC_GETSET(gameobject, mask, bitmask)
|
2024-04-04 17:28:11 -05:00
|
|
|
JSC_CCALL(gameobject_selfsync, gameobject_apply(js2gameobject(this)))
|
2024-03-20 16:07:23 -05:00
|
|
|
JSC_CCALL(gameobject_world2this, return vec22js(world2go(js2gameobject(this), js2vec2(argv[0]))))
|
|
|
|
JSC_CCALL(gameobject_this2world, return vec22js(go2world(js2gameobject(this), js2vec2(argv[0]))))
|
|
|
|
JSC_CCALL(gameobject_dir_world2this, return vec22js(mat_t_dir(t_world2go(js2gameobject(this)), js2vec2(argv[0]))))
|
|
|
|
JSC_CCALL(gameobject_dir_this2world, return vec22js(mat_t_dir(t_go2world(js2gameobject(this)), js2vec2(argv[0]))))
|
2024-03-19 14:39:19 -05:00
|
|
|
|
2024-04-20 12:55:20 -05:00
|
|
|
JSC_CCALL(gameobject_rotate3d,
|
|
|
|
HMM_Vec3 rot = js2vec3(argv[0]);
|
|
|
|
HMM_Quat qrot = HMM_QFromAxisAngle_RH((HMM_Vec3){1,0,0}, rot.x);
|
|
|
|
qrot = HMM_MulQ(qrot, HMM_QFromAxisAngle_RH((HMM_Vec3){0,1,0}, rot.y));
|
|
|
|
qrot = HMM_MulQ(qrot, HMM_QFromAxisAngle_RH((HMM_Vec3){0,0,1}, rot.z));
|
|
|
|
gameobject *go = js2gameobject(this);
|
|
|
|
go->quat = HMM_MulQ(go->quat, qrot);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(gameobject_lookat,
|
|
|
|
HMM_Vec3 point = js2vec3(argv[0]);
|
|
|
|
gameobject *go = js2gameobject(this);
|
|
|
|
HMM_Vec3 pos = go_pos3d(go);
|
|
|
|
HMM_Mat4 m = HMM_LookAt_RH(pos, point, (HMM_Vec3){0,1,0});
|
|
|
|
go->quat = HMM_M4ToQ_RH(m);
|
|
|
|
)
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
static const JSCFunctionListEntry js_gameobject_funcs[] = {
|
2024-03-20 16:07:23 -05:00
|
|
|
CGETSET_ADD(gameobject, friction),
|
|
|
|
CGETSET_ADD(gameobject, elasticity),
|
2024-03-13 03:51:44 -05:00
|
|
|
CGETSET_ADD(gameobject,mass),
|
|
|
|
CGETSET_ADD(gameobject,damping),
|
|
|
|
CGETSET_ADD(gameobject,timescale),
|
|
|
|
CGETSET_ADD(gameobject,maxvelocity),
|
|
|
|
CGETSET_ADD(gameobject,maxangularvelocity),
|
|
|
|
CGETSET_ADD(gameobject,layer),
|
2024-04-12 13:53:00 -05:00
|
|
|
CGETSET_ADD(gameobject,warp_mask),
|
2024-03-19 14:39:19 -05:00
|
|
|
CGETSET_ADD(gameobject,drawlayer),
|
2024-04-12 13:53:00 -05:00
|
|
|
CGETSET_ADD(gameobject, categories),
|
|
|
|
CGETSET_ADD(gameobject, mask),
|
2024-04-09 16:48:15 -05:00
|
|
|
CGETSET_ADD_HID(gameobject, rpos),
|
|
|
|
CGETSET_ADD_HID(gameobject, rangle),
|
|
|
|
CGETSET_ADD_HID(gameobject, rscale),
|
2024-03-19 14:39:19 -05:00
|
|
|
CGETSET_ADD(gameobject, velocity),
|
|
|
|
CGETSET_ADD(gameobject, angularvelocity),
|
2024-04-09 16:48:15 -05:00
|
|
|
// CGETSET_ADD(gameobject, moi),
|
2024-03-19 14:39:19 -05:00
|
|
|
CGETSET_ADD(gameobject, phys),
|
|
|
|
CGETSET_ADD(gameobject, torque),
|
|
|
|
MIST_FUNC_DEF(gameobject, impulse, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, force, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, force_local, 2),
|
|
|
|
MIST_FUNC_DEF(gameobject, world2this, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, this2world, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, dir_world2this, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, dir_this2world, 1),
|
2024-04-04 17:28:11 -05:00
|
|
|
MIST_FUNC_DEF(gameobject, selfsync, 0),
|
2024-04-20 12:55:20 -05:00
|
|
|
MIST_FUNC_DEF(gameobject, rotate3d, 1),
|
|
|
|
MIST_FUNC_DEF(gameobject, lookat, 1)
|
2024-03-19 14:39:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
JSC_CCALL(joint_pin, return constraint2js(constraint_make(cpPinJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, cpvzero,cpvzero))))
|
|
|
|
JSC_CCALL(joint_pivot, return constraint2js(constraint_make(cpPivotJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body,js2vec2(argv[2]).cp))))
|
|
|
|
JSC_CCALL(joint_gear, return constraint2js(constraint_make(cpGearJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2number(argv[2]), js2number(argv[3])))))
|
|
|
|
JSC_CCALL(joint_rotary, return constraint2js(constraint_make(cpRotaryLimitJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2number(argv[2]), js2number(argv[3])))))
|
|
|
|
JSC_CCALL(joint_damped_rotary, return constraint2js(constraint_make(cpDampedRotarySpringNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2number(argv[2]), js2number(argv[3]), js2number(argv[4])))))
|
|
|
|
JSC_CCALL(joint_damped_spring, return constraint2js(constraint_make(cpDampedSpringNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2vec2(argv[2]).cp, js2vec2(argv[3]).cp, js2number(argv[4]), js2number(argv[5]), js2number(argv[6])))))
|
|
|
|
JSC_CCALL(joint_groove, return constraint2js(constraint_make(cpGrooveJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2vec2(argv[2]).cp, js2vec2(argv[3]).cp, js2vec2(argv[4]).cp))))
|
|
|
|
JSC_CCALL(joint_slide, return constraint2js(constraint_make(cpSlideJointNew(js2gameobject(argv[0])->body, js2gameobject(argv[1])->body, js2vec2(argv[2]).cp, js2vec2(argv[3]).cp, js2number(argv[4]), js2number(argv[5])))))
|
|
|
|
JSC_CCALL(joint_ratchet, return constraint2js(constraint_make(cpRatchetJointNew(js2body(argv[0]), js2body(argv[1]), js2number(argv[2]), js2number(argv[3])))))
|
|
|
|
JSC_CCALL(joint_motor, return constraint2js(constraint_make(cpSimpleMotorNew(js2body(argv[0]), js2body(argv[1]), js2number(argv[2])))))
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_joint_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(joint, pin, 2),
|
|
|
|
MIST_FUNC_DEF(joint, pivot, 3),
|
|
|
|
MIST_FUNC_DEF(joint, gear, 4),
|
|
|
|
MIST_FUNC_DEF(joint, rotary, 4),
|
|
|
|
MIST_FUNC_DEF(joint, damped_rotary, 5),
|
|
|
|
MIST_FUNC_DEF(joint, damped_spring, 7),
|
|
|
|
MIST_FUNC_DEF(joint, groove, 5),
|
|
|
|
MIST_FUNC_DEF(joint, slide, 6),
|
|
|
|
MIST_FUNC_DEF(joint, ratchet, 4),
|
|
|
|
MIST_FUNC_DEF(joint, motor, 3)
|
|
|
|
};
|
|
|
|
|
|
|
|
JSC_CCALL(dspsound_noise, return dsp_node2js(dsp_whitenoise()))
|
|
|
|
JSC_CCALL(dspsound_pink, return dsp_node2js(dsp_pinknoise()))
|
|
|
|
JSC_CCALL(dspsound_red, return dsp_node2js(dsp_rednoise()))
|
|
|
|
JSC_CCALL(dspsound_pitchshift, return dsp_node2js(dsp_pitchshift(js2number(argv[0]))))
|
|
|
|
JSC_CCALL(dspsound_noise_gate, return dsp_node2js(dsp_noise_gate(js2number(argv[0]))))
|
|
|
|
JSC_CCALL(dspsound_limiter, return dsp_node2js(dsp_limiter(js2number(argv[0]))))
|
|
|
|
JSC_CCALL(dspsound_compressor, return dsp_node2js(dsp_compressor()))
|
|
|
|
JSC_CCALL(dspsound_crush, return dsp_node2js(dsp_bitcrush(js2number(argv[0]), js2number(argv[1]))))
|
|
|
|
JSC_CCALL(dspsound_lpf, return dsp_node2js(dsp_lpf(js2number(argv[0]))))
|
|
|
|
JSC_CCALL(dspsound_hpf, return dsp_node2js(dsp_hpf(js2number(argv[0]))))
|
|
|
|
JSC_CCALL(dspsound_delay, return dsp_node2js(dsp_delay(js2number(argv[0]), js2number(argv[1]))))
|
|
|
|
JSC_CCALL(dspsound_fwd_delay, return dsp_node2js(dsp_fwd_delay(js2number(argv[0]), js2number(argv[1]))))
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_SCALL(dspsound_source,
|
|
|
|
ret = dsp_node2js(dsp_source(str));
|
2024-03-19 14:39:19 -05:00
|
|
|
JS_SetPrototype(js, ret, sound_proto);
|
|
|
|
)
|
|
|
|
JSC_CCALL(dspsound_mix, return dsp_node2js(make_node(NULL,NULL,NULL)))
|
|
|
|
JSC_CCALL(dspsound_master, return dsp_node2js(masterbus))
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(dspsound_plugin_node, plugin_node(js2dsp_node(argv[0]), js2dsp_node(argv[1]));)
|
|
|
|
JSC_SCALL(dspsound_mod, ret = dsp_node2js(dsp_mod(str)))
|
|
|
|
JSC_SSCALL(dspsound_midi, ret = dsp_node2js(dsp_midi(str, make_soundfont(str2))))
|
2024-03-19 14:39:19 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_dspsound_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(dspsound, noise, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, pink, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, red, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, pitchshift, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, noise_gate, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, limiter, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, compressor, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, crush, 2),
|
|
|
|
MIST_FUNC_DEF(dspsound, lpf, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, hpf, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, delay, 2),
|
|
|
|
MIST_FUNC_DEF(dspsound, fwd_delay, 2),
|
|
|
|
MIST_FUNC_DEF(dspsound, source, 1),
|
|
|
|
MIST_FUNC_DEF(dspsound, mix, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, master, 0),
|
|
|
|
MIST_FUNC_DEF(dspsound, plugin_node, 2),
|
|
|
|
MIST_FUNC_DEF(dspsound, midi, 2),
|
|
|
|
MIST_FUNC_DEF(dspsound, mod, 1)
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(pshape_set_sensor, shape_set_sensor(js2ptr(argv[0]), js2boolean(argv[1])))
|
2024-04-06 19:41:14 -05:00
|
|
|
JSC_CCALL(pshape_get_sensor, return boolean2js(shape_get_sensor(js2ptr(argv[0]))))
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_CCALL(pshape_set_enabled, shape_enabled(js2ptr(argv[0]), js2boolean(argv[1])))
|
2024-04-06 19:41:14 -05:00
|
|
|
JSC_CCALL(pshape_get_enabled, return boolean2js(shape_is_enabled(js2ptr(argv[0]))))
|
2024-03-19 14:39:19 -05:00
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
static const JSCFunctionListEntry js_pshape_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(pshape, set_sensor, 2),
|
|
|
|
MIST_FUNC_DEF(pshape, get_sensor, 1),
|
|
|
|
MIST_FUNC_DEF(pshape, set_enabled, 2),
|
|
|
|
MIST_FUNC_DEF(pshape, get_enabled, 1)
|
2024-03-13 03:51:44 -05:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSC_GETSET(sprite, color, color)
|
|
|
|
JSC_GETSET(sprite, emissive, color)
|
2024-04-23 15:58:08 -05:00
|
|
|
JSC_GETSET(sprite, spriteoffset, vec2)
|
2024-03-13 03:51:44 -05:00
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_sprite_funcs[] = {
|
|
|
|
CGETSET_ADD(sprite,color),
|
|
|
|
CGETSET_ADD(sprite,emissive),
|
2024-04-23 15:58:08 -05:00
|
|
|
CGETSET_ADD(sprite, spriteoffset),
|
2024-03-13 03:51:44 -05:00
|
|
|
};
|
|
|
|
|
2024-03-21 21:07:24 -05:00
|
|
|
JSC_GET(texture, width, number)
|
|
|
|
JSC_GET(texture, height, number)
|
|
|
|
JSC_GET(texture, frames, number)
|
|
|
|
JSC_GET(texture, delays, ints)
|
2024-03-18 14:27:52 -05:00
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
static const JSCFunctionListEntry js_texture_funcs[] = {
|
2024-03-21 21:07:24 -05:00
|
|
|
MIST_GET(texture, width),
|
|
|
|
MIST_GET(texture, height),
|
|
|
|
MIST_GET(texture, frames),
|
|
|
|
MIST_GET(texture, delays),
|
2024-03-13 03:51:44 -05:00
|
|
|
};
|
|
|
|
|
2024-04-09 16:48:15 -05:00
|
|
|
JSC_GETSET(font, linegap, number)
|
|
|
|
JSC_GET(font, height, number)
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_font_funcs[] = {
|
|
|
|
CGETSET_ADD(font, linegap),
|
|
|
|
MIST_GET(font, height),
|
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSValue js_constraint_set_max_force (JSContext *js, JSValue this, JSValue val) {
|
2024-01-14 10:24:31 -06:00
|
|
|
cpConstraintSetMaxForce(js2constraint(this)->c, js2number(val));
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSValue js_constraint_get_max_force(JSContext *js, JSValue this) { return number2js(cpConstraintGetMaxForce(js2constraint(this)->c));
|
2024-01-14 10:24:31 -06:00
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSValue js_constraint_set_collide (JSContext *js, JSValue this, JSValue val) {
|
|
|
|
cpConstraintSetCollideBodies(js2constraint(this)->c, js2boolean(val));
|
2024-01-14 10:24:31 -06:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
JSValue js_constraint_get_collide(JSContext *js, JSValue this) { return boolean2js(cpConstraintGetCollideBodies(js2constraint(this)->c));
|
2024-01-14 10:24:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_constraint_funcs[] = {
|
|
|
|
CGETSET_ADD(constraint, max_force),
|
|
|
|
CGETSET_ADD(constraint, collide),
|
|
|
|
};
|
2023-02-24 14:13:35 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
struct phys2d_edge *js2edge2d(JSValue v) { return js2ptr(v); }
|
|
|
|
JSC_CCALL(edge2d_setverts,
|
|
|
|
struct phys2d_edge *edge = js2edge2d(argv[0]);
|
|
|
|
HMM_Vec2 *v = js2cpvec2arr(argv[1]);
|
|
|
|
phys2d_edge_update_verts(edge,v);
|
|
|
|
arrfree(v);
|
|
|
|
)
|
|
|
|
JSC_CCALL(edge2d_set_thickness, js2edge2d(argv[0])->thickness = js2number(argv[1]))
|
|
|
|
JSC_CCALL(edge2d_get_thickness, return number2js(js2edge2d(argv[0])->thickness))
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
static const JSCFunctionListEntry js_edge2d_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(edge2d, setverts, 2),
|
|
|
|
MIST_FUNC_DEF(edge2d, set_thickness, 2),
|
|
|
|
MIST_FUNC_DEF(edge2d, get_thickness, 1),
|
|
|
|
};
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2023-12-18 06:45:27 -06:00
|
|
|
const char *STRTEST = "TEST STRING";
|
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
JSC_CCALL(performance_barecall,)
|
|
|
|
JSC_CCALL(performance_unpack_num, int i = js2number(argv[0]))
|
2024-04-20 12:55:20 -05:00
|
|
|
JSC_CCALL(performance_unpack_array,
|
|
|
|
void *v = js2cpvec2arr(argv[0]);
|
|
|
|
arrfree(v);
|
|
|
|
)
|
2024-03-20 09:04:28 -05:00
|
|
|
JSC_CCALL(performance_pack_num, return number2js(1.0))
|
|
|
|
JSC_CCALL(performance_pack_string, return JS_NewStringLen(js, STRTEST, sizeof(*STRTEST)))
|
|
|
|
JSC_CCALL(performance_unpack_string, js2str(argv[0]))
|
|
|
|
JSC_CCALL(performance_unpack_32farr, jsfloat2vec(argv[0]))
|
|
|
|
JSC_CCALL(performance_call_fn_n,
|
|
|
|
for (int i = 0; i < js2number(argv[1]); i++)
|
|
|
|
script_call_sym(argv[0],0,NULL);
|
|
|
|
script_call_sym(argv[2],0,NULL);
|
|
|
|
)
|
|
|
|
|
|
|
|
static const JSCFunctionListEntry js_performance_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(performance, barecall,0),
|
|
|
|
MIST_FUNC_DEF(performance, unpack_num, 1),
|
|
|
|
MIST_FUNC_DEF(performance, unpack_array, 1),
|
|
|
|
MIST_FUNC_DEF(performance, pack_num, 0),
|
|
|
|
MIST_FUNC_DEF(performance, pack_string, 0),
|
|
|
|
MIST_FUNC_DEF(performance, unpack_string, 1),
|
|
|
|
MIST_FUNC_DEF(performance, unpack_32farr, 1),
|
|
|
|
MIST_FUNC_DEF(performance, call_fn_n, 3)
|
|
|
|
};
|
2023-12-18 06:45:27 -06:00
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
JSValue js_nota_encode(JSContext *js, JSValue this, int argc, JSValue *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-19 17:00:49 -05:00
|
|
|
JSValue js_nota_decode(JSContext *js, JSValue this, int argc, JSValue *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
|
|
|
|
2024-04-03 17:17:32 -05:00
|
|
|
|
|
|
|
JSValue js_os_cwd(JSContext *js, JSValue this, int argc, JSValue *argv)
|
|
|
|
{
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
getcwd(cwd, sizeof(cwd));
|
|
|
|
#else
|
|
|
|
cwd[0] = '.';
|
|
|
|
cwd[1] = 0;
|
|
|
|
#endif
|
|
|
|
return str2js(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_SCALL(os_env, ret = str2js(getenv(str)))
|
|
|
|
|
|
|
|
JSValue js_os_sys(JSContext *js, JSValue this, int argc, JSValue *argv)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
return str2js("linux");
|
|
|
|
#elif defined(_WIN32) || defined(_WIN64)
|
|
|
|
return str2js("windows");
|
2024-05-06 21:59:22 -05:00
|
|
|
#elif defined(IOS)
|
|
|
|
return str2js("ios");
|
2024-04-03 17:17:32 -05:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return str2js("macos");
|
2024-05-06 21:59:22 -05:00
|
|
|
#elif define(__EMSCRIPTEN__)
|
|
|
|
return str2js("web");
|
2024-04-03 17:17:32 -05:00
|
|
|
#endif
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSC_CCALL(os_quit, quit();)
|
|
|
|
JSC_CCALL(os_exit, exit(js2number(argv[0]));)
|
|
|
|
JSC_CCALL(os_reindex_static, cpSpaceReindexStatic(space));
|
|
|
|
JSC_CCALL(os_gc, script_gc());
|
|
|
|
JSC_SSCALL(os_eval, ret = script_eval(str, str2))
|
|
|
|
JSC_SCALL(os_capture, capture_screen(js2number(argv[1]), js2number(argv[2]), js2number(argv[4]), js2number(argv[5]), str))
|
|
|
|
|
|
|
|
JSC_CCALL(os_sprite,
|
|
|
|
return sprite2js(sprite_make());
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(os_make_gameobject,
|
|
|
|
ret = gameobject2js(MakeGameobject());
|
|
|
|
JS_SetPropertyFunctionList(js, ret, js_gameobject_funcs, countof(js_gameobject_funcs));
|
2024-04-04 17:28:11 -05:00
|
|
|
js2gameobject(ret)->ref = ret;
|
2024-04-03 17:17:32 -05:00
|
|
|
return ret;
|
|
|
|
)
|
|
|
|
JSC_CCALL(os_make_circle2d,
|
|
|
|
gameobject *go = js2gameobject(argv[0]);
|
|
|
|
struct phys2d_circle *circle = Make2DCircle(go);
|
|
|
|
JSValue circleval = JS_NewObject(js);
|
|
|
|
js_setprop_str(circleval, "id", ptr2js(circle));
|
|
|
|
js_setprop_str(circleval, "shape", ptr2js(&circle->shape));
|
2024-04-11 17:17:49 -05:00
|
|
|
circle->shape.ref = JS_DupValue(js,argv[1]);
|
2024-04-03 17:17:32 -05:00
|
|
|
return circleval;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(os_make_poly2d,
|
|
|
|
gameobject *go = js2gameobject(argv[0]);
|
|
|
|
struct phys2d_poly *poly = Make2DPoly(go);
|
|
|
|
phys2d_poly_setverts(poly, NULL);
|
|
|
|
JSValue polyval = JS_NewObject(js);
|
|
|
|
js_setprop_str(polyval, "id", ptr2js(poly));
|
|
|
|
js_setprop_str(polyval, "shape", ptr2js(&poly->shape));
|
2024-04-11 17:17:49 -05:00
|
|
|
poly->shape.ref = JS_DupValue(js,argv[1]);
|
2024-04-03 17:17:32 -05:00
|
|
|
return polyval;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(os_make_edge2d,
|
|
|
|
gameobject *go = js2gameobject(argv[0]);
|
|
|
|
struct phys2d_edge *edge = Make2DEdge(go);
|
|
|
|
HMM_Vec2 *points = js2cpvec2arr(argv[1]);
|
|
|
|
phys2d_edge_update_verts(edge, points);
|
|
|
|
arrfree(points);
|
|
|
|
|
|
|
|
JSValue edgeval = JS_NewObject(js);
|
|
|
|
js_setprop_str(edgeval, "id", ptr2js(edge));
|
|
|
|
js_setprop_str(edgeval, "shape", ptr2js(&edge->shape));
|
2024-04-11 17:17:49 -05:00
|
|
|
edge->shape.ref = JS_DupValue(js, argv[1]);
|
2024-04-03 17:17:32 -05:00
|
|
|
return edgeval;
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_SCALL(os_make_texture,
|
|
|
|
ret = texture2js(texture_from_file(str));
|
|
|
|
YughInfo("Made texture with %s", str);
|
|
|
|
JS_SetPropertyStr(js, ret, "path", JS_DupValue(js,argv[0]));
|
|
|
|
)
|
2024-04-06 19:41:14 -05:00
|
|
|
|
2024-04-09 16:48:15 -05:00
|
|
|
JSC_CCALL(os_make_font, return font2js(MakeFont(js2str(argv[0]), js2number(argv[1]))))
|
|
|
|
|
2024-05-02 13:52:28 -05:00
|
|
|
JSC_CCALL(os_make_transform2d,
|
2024-05-02 17:13:09 -05:00
|
|
|
if (JS_IsUndefined(argv[0]))
|
|
|
|
return transform2d2js(make_transform2d());
|
|
|
|
|
|
|
|
int n = js2number(argv[0]);
|
|
|
|
transform2d *t = calloc(sizeof(transform2d), n);
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
js_setprop_num(arr, i, transform2d2js(t+i));
|
|
|
|
|
|
|
|
return arr;
|
2024-05-02 13:52:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
JSC_SCALL(os_system, return number2js(system(str)); )
|
2024-04-03 17:17:32 -05:00
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
JSC_SCALL(os_make_model, ret = model2js(model_make(str)))
|
2024-05-06 21:59:22 -05:00
|
|
|
JSC_CCALL(os_make_emitter, ret = emitter2js(make_emitter()))
|
|
|
|
|
|
|
|
JSC_CCALL(os_make_buffer,
|
|
|
|
int type = js2number(argv[1]);
|
|
|
|
float *b = malloc(sizeof(float)*js_arrlen(argv[0]));
|
|
|
|
for (int i = 0; i < js_arrlen(argv[0]); i++)
|
|
|
|
b[i] = js2number(js_getpropidx(argv[0],i));
|
|
|
|
|
|
|
|
sg_buffer *p = malloc(sizeof(sg_buffer));
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case 0:
|
|
|
|
*p = sg_make_buffer(&(sg_buffer_desc){
|
|
|
|
.size = sizeof(float)*js_arrlen(argv[0]),
|
|
|
|
.data = b
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*p = index_buffer(b, js_arrlen(argv[0]));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*p = texcoord_floats(b, js_arrlen(argv[0]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(b);
|
|
|
|
return sg_buffer2js(p);
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(os_make_line_prim,
|
|
|
|
JSValue prim = JS_NewObject(js);
|
|
|
|
HMM_Vec2 *v = js2cpvec2arr(argv[0]);
|
|
|
|
parsl_position par_v[arrlen(v)];
|
|
|
|
for (int i = 0; i < arrlen(v); i++) {
|
|
|
|
par_v[i].x = v[i].x;
|
|
|
|
par_v[i].y = v[i].y;
|
|
|
|
}
|
|
|
|
|
|
|
|
parsl_context *par_ctx = parsl_create_context((parsl_config){
|
|
|
|
.thickness = js2number(argv[1]),
|
|
|
|
.flags= PARSL_FLAG_ANNOTATIONS,
|
|
|
|
.u_mode = PAR_U_MODE_NORMALIZED_DISTANCE
|
|
|
|
});
|
|
|
|
|
|
|
|
uint16_t spine_lens[] = {arrlen(v)};
|
|
|
|
|
|
|
|
parsl_mesh *m = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){
|
|
|
|
.num_vertices = arrlen(v),
|
|
|
|
.num_spines = 1,
|
|
|
|
.vertices = par_v,
|
|
|
|
.spine_lengths = spine_lens
|
|
|
|
});
|
|
|
|
|
|
|
|
sg_buffer *pos = malloc(sizeof(*pos));
|
|
|
|
*pos = sg_make_buffer(&(sg_buffer_desc){
|
|
|
|
.data = (sg_range){
|
|
|
|
.ptr = m->positions,
|
|
|
|
.size = sizeof(parsl_position)*m->num_vertices
|
|
|
|
}
|
|
|
|
});
|
|
|
|
js_setpropstr(prim, "pos", sg_buffer2js(pos));
|
|
|
|
js_setpropstr(prim, "count", number2js(m->num_triangles*3));
|
|
|
|
sg_buffer *idx = malloc(sizeof(*idx));
|
|
|
|
*idx = par_idx_buffer(m->triangle_indices, m->num_triangles*3);
|
|
|
|
js_setpropstr(prim, "index", sg_buffer2js(idx));
|
|
|
|
|
|
|
|
printf("there are %d verts\n", m->num_vertices);
|
|
|
|
float uv[m->num_vertices*2];
|
|
|
|
for (int i = 0; i < m->num_vertices; i++) {
|
|
|
|
uv[i*2] = m->annotations[i].u_along_curve;
|
|
|
|
uv[i*2+1] = m->annotations[i].v_across_curve;
|
|
|
|
printf("uv is %g,%g\n", uv[i*2], uv[i*2+1]);
|
|
|
|
}
|
|
|
|
sg_buffer *buv = malloc(sizeof(*buv));
|
|
|
|
*buv = texcoord_floats(uv, m->num_vertices*2);
|
|
|
|
//*buv = float_buffer(uv, m->num_vertices*2);
|
|
|
|
js_setpropstr(prim, "uv", sg_buffer2js(buv));
|
|
|
|
|
|
|
|
return prim;
|
|
|
|
)
|
2024-04-20 12:55:20 -05:00
|
|
|
|
2024-04-03 17:17:32 -05:00
|
|
|
static const JSCFunctionListEntry js_os_funcs[] = {
|
2024-05-06 21:59:22 -05:00
|
|
|
MIST_FUNC_DEF(os,sprite,0),
|
2024-04-03 17:17:32 -05:00
|
|
|
MIST_FUNC_DEF(os, cwd, 0),
|
|
|
|
MIST_FUNC_DEF(os, env, 1),
|
|
|
|
MIST_FUNC_DEF(os, sys, 0),
|
|
|
|
MIST_FUNC_DEF(os, system, 1),
|
|
|
|
MIST_FUNC_DEF(os, quit, 0),
|
|
|
|
MIST_FUNC_DEF(os, exit, 1),
|
|
|
|
MIST_FUNC_DEF(os, reindex_static, 0),
|
|
|
|
MIST_FUNC_DEF(os, gc, 0),
|
|
|
|
MIST_FUNC_DEF(os, capture, 5),
|
|
|
|
MIST_FUNC_DEF(os, eval, 2),
|
|
|
|
MIST_FUNC_DEF(os, make_gameobject, 0),
|
2024-04-06 19:41:14 -05:00
|
|
|
MIST_FUNC_DEF(os, make_circle2d, 2),
|
|
|
|
MIST_FUNC_DEF(os, make_poly2d, 2),
|
|
|
|
MIST_FUNC_DEF(os, make_edge2d, 2),
|
2024-04-03 17:17:32 -05:00
|
|
|
MIST_FUNC_DEF(os, make_texture, 1),
|
2024-04-09 16:48:15 -05:00
|
|
|
MIST_FUNC_DEF(os, make_font, 2),
|
2024-04-21 10:05:18 -05:00
|
|
|
MIST_FUNC_DEF(os, make_model, 1),
|
2024-05-02 17:13:09 -05:00
|
|
|
MIST_FUNC_DEF(os, make_transform2d, 1),
|
2024-05-06 21:59:22 -05:00
|
|
|
MIST_FUNC_DEF(os, make_emitter, 0),
|
|
|
|
MIST_FUNC_DEF(os, make_buffer, 1),
|
|
|
|
MIST_FUNC_DEF(os, make_line_prim, 2),
|
2024-04-03 17:17:32 -05:00
|
|
|
};
|
|
|
|
|
2024-03-26 13:05:25 -05:00
|
|
|
#include "steam.h"
|
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);
|
2024-03-20 09:04:28 -05:00
|
|
|
|
2024-01-14 10:24:31 -06:00
|
|
|
QJSCLASSPREP(ptr);
|
2024-05-02 13:52:28 -05:00
|
|
|
QJSCLASSPREP(transform3d);
|
|
|
|
|
2024-04-09 16:48:15 -05:00
|
|
|
|
2024-03-21 11:33:36 -05:00
|
|
|
QJSGLOBALCLASS(os);
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
QJSCLASSPREP_FUNCS(gameobject);
|
2024-05-02 13:52:28 -05:00
|
|
|
QJSCLASSPREP_FUNCS(transform2d);
|
2024-01-14 10:24:31 -06:00
|
|
|
QJSCLASSPREP_FUNCS(dsp_node);
|
|
|
|
QJSCLASSPREP_FUNCS(emitter);
|
|
|
|
QJSCLASSPREP_FUNCS(warp_gravity);
|
|
|
|
QJSCLASSPREP_FUNCS(warp_damp);
|
2024-03-13 03:51:44 -05:00
|
|
|
QJSCLASSPREP_FUNCS(sprite);
|
|
|
|
QJSCLASSPREP_FUNCS(texture);
|
2024-04-09 16:48:15 -05:00
|
|
|
QJSCLASSPREP_FUNCS(font);
|
2024-01-14 10:24:31 -06:00
|
|
|
QJSCLASSPREP_FUNCS(constraint);
|
2024-03-13 16:30:55 -05:00
|
|
|
QJSCLASSPREP_FUNCS(window);
|
2024-04-20 12:55:20 -05:00
|
|
|
QJSCLASSPREP_FUNCS(model);
|
2024-02-25 17:31:48 -06:00
|
|
|
|
2024-03-18 08:16:25 -05:00
|
|
|
QJSGLOBALCLASS(nota);
|
|
|
|
QJSGLOBALCLASS(input);
|
2024-02-27 10:09:15 -06:00
|
|
|
QJSGLOBALCLASS(io);
|
2024-03-14 14:10:06 -05:00
|
|
|
QJSGLOBALCLASS(prosperon);
|
|
|
|
QJSGLOBALCLASS(time);
|
|
|
|
QJSGLOBALCLASS(console);
|
2024-04-10 16:21:46 -05:00
|
|
|
QJSGLOBALCLASS(audio);
|
2024-03-14 14:10:06 -05:00
|
|
|
QJSGLOBALCLASS(profile);
|
2024-03-18 08:16:25 -05:00
|
|
|
QJSGLOBALCLASS(game);
|
|
|
|
QJSGLOBALCLASS(gui);
|
|
|
|
QJSGLOBALCLASS(debug);
|
|
|
|
QJSGLOBALCLASS(render);
|
|
|
|
QJSGLOBALCLASS(physics);
|
|
|
|
QJSGLOBALCLASS(vector);
|
2024-03-19 14:39:19 -05:00
|
|
|
QJSGLOBALCLASS(spline);
|
|
|
|
QJSGLOBALCLASS(joint);
|
|
|
|
QJSGLOBALCLASS(dspsound);
|
2024-03-19 17:00:49 -05:00
|
|
|
QJSGLOBALCLASS(pshape);
|
2024-03-20 09:04:28 -05:00
|
|
|
QJSGLOBALCLASS(performance);
|
|
|
|
QJSGLOBALCLASS(circle2d);
|
|
|
|
QJSGLOBALCLASS(poly2d);
|
|
|
|
QJSGLOBALCLASS(edge2d);
|
2024-03-18 08:16:25 -05:00
|
|
|
|
|
|
|
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-26 14:57:35 -05:00
|
|
|
|
2024-04-15 15:01:28 -05:00
|
|
|
//JS_SetPropertyStr(js, globalThis, "steam", js_init_steam(js));
|
2024-04-16 07:48:34 -05:00
|
|
|
|
2024-03-20 16:07:23 -05:00
|
|
|
sound_proto = JS_NewObject(js);
|
2024-03-22 09:02:10 -05:00
|
|
|
JS_SetPropertyStr(js,globalThis, "sound_proto", sound_proto);
|
2024-03-20 16:07:23 -05:00
|
|
|
JS_SetPropertyFunctionList(js, sound_proto, js_sound_funcs, countof(js_sound_funcs));
|
|
|
|
JS_SetPrototype(js, sound_proto, dsp_node_proto);
|
2024-03-22 09:02:10 -05:00
|
|
|
|
|
|
|
JS_FreeValue(js,globalThis);
|
2023-01-10 15:41:43 -06:00
|
|
|
}
|