actor cleanup

This commit is contained in:
2025-01-18 18:15:15 -06:00
parent aa38fd2c19
commit a142b6d1f4
11 changed files with 179 additions and 269 deletions

View File

@@ -53,6 +53,10 @@ typedef struct rtree rtree;
static JSAtom width_atom;
static JSAtom height_atom;
static JSAtom l_atom;
static JSAtom r_atom;
static JSAtom t_atom;
static JSAtom b_atom;
static JSAtom x_atom;
static JSAtom y_atom;
static JSAtom anchor_x_atom;
@@ -239,25 +243,15 @@ double js_getnum(JSContext *js, JSValue v, JSAtom prop)
return ret;
}
#define JS_PULLPROPSTR(JS,VALUE,STR, TYPE) { \
JSValue __v = JS_GetPropertyStr(JS,VALUE,#STR); \
STR = js2##TYPE(JS, __v); \
JS_FreeValue(JS, __v); }
#define JS_GETPROPSTR(JS, VALUE, TARGET, STR, TYPE) {\
JSValue STR##__v = JS_GetPropertyStr(JS,VALUE,#STR); \
TARGET.STR = js2##TYPE(JS, STR##__v); \
JS_FreeValue(JS,STR##__v); }\
#define JS_GETPROP(JS, TARGET, VALUE, PROP, TYPE) {\
JSValue VALUE##__##PROP##__v = JS_GetPropertyStr(JS,VALUE,#PROP); \
TARGET = js2##TYPE(JS, VALUE##__##PROP##__v); \
JS_FreeValue(JS,VALUE##__##PROP##__v); }\
JSValue __##PROP##__v = JS_GetPropertyStr(JS,VALUE,#PROP); \
TARGET = js2##TYPE(JS, __##PROP##__v); \
JS_FreeValue(JS,__##PROP##__v); }\
#define JS_GETATOM(JS, TARGET, VALUE, ATOM, TYPE) {\
JSValue VALUE##__##PROP##__v = JS_GetProperty(JS,VALUE,ATOM); \
TARGET = js2##TYPE(JS, VALUE##__##PROP##__v); \
JS_FreeValue(JS,VALUE##__##PROP##__v); }\
JSValue __##PROP##__v = JS_GetProperty(JS,VALUE,ATOM); \
TARGET = js2##TYPE(JS, __##PROP##__v); \
JS_FreeValue(JS,__##PROP##__v); }\
int JS_GETBOOL(JSContext *js, JSValue v, const char *prop)
{
@@ -1031,10 +1025,10 @@ typedef struct lrtb lrtb;
lrtb js2lrtb(JSContext *js, JSValue v)
{
lrtb ret = {0};
JS_GETPROPSTR(js,v,ret,l,number)
JS_GETPROPSTR(js,v,ret,b,number)
JS_GETPROPSTR(js,v,ret,t,number)
JS_GETPROPSTR(js,v,ret,r,number)
JS_GETATOM(js,ret.l,v,l_atom,number)
JS_GETATOM(js,ret.r,v,r_atom,number)
JS_GETATOM(js,ret.b,v,b_atom,number)
JS_GETATOM(js,ret.t,v,t_atom,number)
return ret;
}
@@ -1122,6 +1116,7 @@ static void js_transform_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark
transform *t = JS_GetOpaque(val, js_transform_id);
if (!t) return;
// Mark the JSValue references stored in your struct
JS_MarkValue(rt, t->self, mark_func);
JS_MarkValue(rt, t->change_hook, mark_func);
JS_MarkValue(rt, t->jsparent, mark_func);
// Mark the array elements
@@ -1441,14 +1436,10 @@ JSValue vecarr2js(JSContext *js,HMM_Vec2 *points, int n) {
rect js2rect(JSContext *js,JSValue v) {
if (JS_IsUndefined(v)) return (rect){0,0,1,1};
rect rect;
rect.w = js_getnum(js,v,width_atom);
rect.h = js_getnum(js,v,height_atom);
JSValue xv = JS_GetProperty(js,v,x_atom);
rect.x = js2number(js,xv);
JS_FreeValue(js,xv);
JSValue yv = JS_GetProperty(js,v,y_atom);
rect.y = js2number(js,yv);
JS_FreeValue(js,yv);
JS_GETATOM(js,rect.x,v,x_atom,number)
JS_GETATOM(js,rect.y,v,y_atom,number)
JS_GETATOM(js,rect.w,v,width_atom,number)
JS_GETATOM(js,rect.h,v,height_atom,number)
float anchor_x = js_getnum(js,v, anchor_x_atom);
float anchor_y = js_getnum(js,v, anchor_y_atom);
@@ -4131,6 +4122,8 @@ JSC_CCALL(gpu_make_sprite_queue,
JS_GETATOM(js,sp.layer,sub,layer_atom,number)
JS_GETATOM(js,sp.uv,sub,src_atom,rect)
sp.image = JS_GetProperty(js,sub,image_atom);
JS_GETATOM(js,sp.tex,sp.image,texture_atom,SDL_GPUTexture)
arrput(sprites,sp);
}
JS_FreeValue(js, sub);
@@ -4899,8 +4892,8 @@ JSC_CCALL(gpu_slice9,
JSC_CCALL(gpu_tile,
HMM_Vec2 size;
JSValue jstex = argv[0];
JS_GETPROP(js,size.x,jstex,width,number)
JS_GETPROP(js, size.y, jstex, height, number)
JS_GETATOM(js,size.x,jstex,width_atom,number)
JS_GETATOM(js, size.y, jstex, height_atom, number)
rect src_pixels = js2rect(js, argv[1]); // 'src' as pixel dimensions
rect dst = js2rect(js, argv[2]); // 'dst' as screen coords
@@ -5095,7 +5088,7 @@ JSC_CCALL(cmd_render_pass,
JSValue depthval = JS_GetPropertyStr(js, passObj, "depth_stencil");
if (!JS_IsUndefined(depthval)) {
has_depth = 1;
JS_GETPROPSTR(js, depthval, depthtar, texture, SDL_GPUTexture)
JS_GETPROP(js, depthtar.texture, depthval, texture, SDL_GPUTexture)
JS_GETPROP(js, depthtar.load_op, depthval, load, SDL_GPULoadOp)
JS_GETPROP(js, depthtar.store_op, depthval, store, SDL_GPUStoreOp)
JS_GETPROP(js, depthtar.stencil_load_op, depthval, stencil_load, SDL_GPULoadOp)
@@ -6582,7 +6575,11 @@ JSC_CCALL(os_make_font,
JS_SetPropertyStr(js, ret, "surface", SDL_Surface2js(js,f->surface));
)
JSC_CCALL(os_make_transform, return transform2js(js,make_transform()))
JSC_CCALL(os_make_transform,
transform *t = make_transform();
ret = transform2js(js,t);
t->self = JS_DupValue(js,ret);
)
JSC_CCALL(os_make_sprite, return sprite2js(js,make_sprite()))
JSC_SCALL(os_system, return number2js(js,system(str)); )
@@ -7248,9 +7245,14 @@ JSC_CCALL(os_on,
on_exception = JS_DupValue(js,argv[1]);
)
JSC_CCALL(os_clean_transforms,
clean_all();
)
static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, turbulence, 4),
MIST_FUNC_DEF(os, model_buffer, 1),
MIST_FUNC_DEF(os, clean_transforms, 0),
MIST_FUNC_DEF(os, fbm, 4),
MIST_FUNC_DEF(os, ridge, 5),
MIST_FUNC_DEF(os, perlin, 3),
@@ -7365,8 +7367,10 @@ JSC_CCALL(rtree_insert,
max[1] = r.y+r.h;
JSValue *ins = malloc(sizeof(*ins));
*ins = JS_DupValue(js,v);
if (!rtree_insert(tree, min, max, ins))
if (!rtree_insert(tree, min, max, ins)) {
JS_FreeValue(js,*ins);
return JS_ThrowOutOfMemory(js);
}
)
int rtree_cmp(const JSValue *a, const JSValue *b, JSContext *js)
@@ -7708,6 +7712,11 @@ void ffi_load(JSContext *js) {
parent_atom = JS_NewAtom(js,"parent");
rect_atom = JS_NewAtom(js,"rect");
l_atom = JS_NewAtom(js,"l");
r_atom = JS_NewAtom(js,"r");
b_atom = JS_NewAtom(js,"b");
t_atom = JS_NewAtom(js,"t");
fill_event_atoms(js);
global_js = js;

View File

@@ -85,6 +85,7 @@ JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue self) { \
static JSClassID js_##TYPE##_id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
TracyCFreeN(n, #TYPE); \
TYPE##_free(rt,n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
@@ -98,12 +99,14 @@ JSValue TYPE##2js(JSContext *js, TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
JS_SetOpaque(j,n);\
__VA_ARGS__ \
TracyCAllocN(n, 1, #TYPE); \
return j; }\
\
#define QJSCLASSMARK(TYPE, ...)\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
TracyCFreeN(n, #TYPE); \
TYPE##_free(rt,n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
@@ -118,6 +121,7 @@ JSValue TYPE##2js(JSContext *js, TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
JS_SetOpaque(j,n);\
__VA_ARGS__ \
TracyCAllocN(n, 1, #TYPE); \
return j; }\
\

View File

@@ -2,9 +2,10 @@
#include <string.h>
#include <stdio.h>
#include "script.h"
#include "stb_ds.h"
static transform **dirties;
static transform model = {
.pos = {0,0,0},
.scale = {1,1,1},
@@ -25,6 +26,7 @@ transform *make_transform()
}
void transform_free(JSRuntime *rt, transform *t) {
JS_FreeValueRT(rt,t->self);
JS_FreeValueRT(rt,t->change_hook);
JS_FreeValueRT(rt,t->jsparent);
for (int i = 0; i < arrlen(t->jschildren); i++)
@@ -33,6 +35,10 @@ void transform_free(JSRuntime *rt, transform *t) {
arrfree(t->jschildren);
arrfree(t->children);
free(t);
for (int i = arrlen(dirties)-1; i >= 0; i--) {
if (dirties[i] == t) arrdelswap(dirties, i);
}
}
void transform_clean(transform *t)
@@ -51,11 +57,17 @@ void transform_clean(transform *t)
}
if (!JS_IsUndefined(t->change_hook)) {
JSValue ret = JS_Call(global_js, t->change_hook, JS_UNDEFINED, 0, NULL);
JSValue ret = JS_Call(global_js, t->change_hook, JS_DupValue(global_js,t->self), 0, NULL);
JS_FreeValue(global_js,ret);
}
}
void clean_all()
{
for (int i = 0; i < arrlen(dirties); i++)
transform_clean(dirties[i]);
}
void transform_move(transform *t, HMM_Vec3 v)
{
t->pos = HMM_AddV3(t->pos, v);
@@ -124,7 +136,7 @@ rect transform2rect(transform *t)
void transform_apply(transform *t)
{
t->dirty = 1;
transform_clean(t);
arrput(dirties,t);
}
HMM_Quat angle2rotation(float angle)

View File

@@ -12,6 +12,7 @@ typedef struct transform {
HMM_Mat4 cache;
HMM_Mat4 gcache;
int dirty;
JSValue self;
struct transform *parent;
JSValue jsparent;
struct transform **children;
@@ -19,6 +20,8 @@ typedef struct transform {
JSValue change_hook;
} transform;
void clean_all();
transform *make_transform();
void transform_free(JSRuntime *rt,transform *t);