clean up jsffi
This commit is contained in:
223
source/jsffi.c
223
source/jsffi.c
@@ -377,6 +377,8 @@ static SDL_GPUDevice *global_gpu;
|
||||
|
||||
SDL_GPUGraphicsPipelineTargetInfo js2SDL_GPUGraphicsPipelineTargetInfo(JSContext *js, JSValue v)
|
||||
{
|
||||
SDL_GPUGraphicsPipelineTargetInfo info = {0};
|
||||
return info;
|
||||
}
|
||||
|
||||
SDL_GPUSampleCount js2SDL_GPUSampleCount(JSContext *js, JSValue v)
|
||||
@@ -1177,41 +1179,6 @@ int js_arrlen(JSContext *js,JSValue v) {
|
||||
len = js_getnum_str(js,v,"length");
|
||||
return len;
|
||||
}
|
||||
static inline int js_transform_dirty_chain(JSContext *js, JSValue v)
|
||||
{
|
||||
transform *t = js2transform(js, v);
|
||||
if (!t) return 0; // no transform => assume not dirty
|
||||
|
||||
if (t->dirty) return 1;
|
||||
|
||||
JSValue parentVal = JS_GetProperty(js, v, parent_atom);
|
||||
if (JS_IsObject(parentVal)) {
|
||||
int r = js_transform_dirty_chain(js, parentVal);
|
||||
JS_FreeValue(js, parentVal);
|
||||
return r;
|
||||
}
|
||||
|
||||
JS_FreeValue(js, parentVal);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline HMM_Mat3 js2transform_mat3(JSContext *js, JSValue v)
|
||||
{
|
||||
transform *T = js2transform(js,v);
|
||||
if (!js_transform_dirty_chain(js,v)) return T->gcache3;
|
||||
if (!T) return HMM_M3D(1);
|
||||
transform *P;
|
||||
JS_GETATOM(js,P,v,parent_atom,transform);
|
||||
if (P) {
|
||||
HMM_Mat3 pm = transform2mat3(P);
|
||||
HMM_Mat3 tm = transform2mat3(T);
|
||||
T->gcache3 = HMM_MulM3(pm,tm);
|
||||
|
||||
} else
|
||||
T->gcache3 = transform2mat3(T);
|
||||
|
||||
return T->gcache3;
|
||||
}
|
||||
|
||||
// Unpacks a typed array javascript object. If it has a gpu property, returns it, too. Otherwise, if requested, makes one.
|
||||
void *gpu_buffer_unpack(JSContext *js, SDL_GPUDevice *device, JSValue buffer, size_t *size, void **send_data, SDL_GPUBuffer **send_gpu)
|
||||
@@ -1240,8 +1207,6 @@ void *gpu_buffer_unpack(JSContext *js, SDL_GPUDevice *device, JSValue buffer, si
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void *get_typed_buffer(JSContext *js, JSValue argv, size_t *len)
|
||||
{
|
||||
size_t o,bytes,size;
|
||||
@@ -2099,8 +2064,8 @@ JSC_CCALL(vector_sum,
|
||||
|
||||
JSC_CCALL(vector_fastsum,
|
||||
float sum = 0.0;
|
||||
size_t len;
|
||||
float *a = get_typed_buffer(js, argv[0], &len);
|
||||
// size_t len;
|
||||
// float *a = get_typed_buffer(js, argv[0], &len);
|
||||
// sum = cblas_sasum(len, a,1);
|
||||
ret = number2js(js,sum);
|
||||
)
|
||||
@@ -2184,7 +2149,7 @@ JSC_CCALL(vector_from_to,
|
||||
JSC_CCALL(vector_float32add,
|
||||
size_t len;
|
||||
float *vec_a = get_typed_buffer(js,self, &len);
|
||||
float *vec_b = get_typed_buffer(js,argv[0], &len);
|
||||
// float *vec_b = get_typed_buffer(js,argv[0], &len);
|
||||
// cblas_saxpy(len,1.0f,vec_b,1,vec_a,1);
|
||||
JSValue tstack[3];
|
||||
tstack[0] = JS_NewArrayBufferCopy(js,vec_a,sizeof(float)*4);
|
||||
@@ -2824,7 +2789,6 @@ SDL_GPUShaderFormat js2SDL_GPUShaderFormat(JSContext *js, JSValue v)
|
||||
}
|
||||
|
||||
JSC_SCALL(SDL_Window_make_gpu,
|
||||
SDL_Window *win = js2SDL_Window(js,self);
|
||||
const char *name = JS_ToCString(js,argv[1]);
|
||||
SDL_PropertiesID props = SDL_CreateProperties();
|
||||
SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name);
|
||||
@@ -2841,7 +2805,7 @@ JSC_SCALL(SDL_Window_make_gpu,
|
||||
SDL_GPUDevice *gpu = SDL_CreateGPUDeviceWithProperties(props);
|
||||
SDL_DestroyProperties(props);
|
||||
|
||||
if (!gpu) return JS_ThrowReferenceError("Could not create GPU device! %s", SDL_GetError());
|
||||
if (!gpu) return JS_ThrowReferenceError(js, "Could not create GPU device! %s", SDL_GetError());
|
||||
|
||||
global_gpu = gpu;
|
||||
return SDL_GPUDevice2js(js,gpu);
|
||||
@@ -2881,7 +2845,6 @@ JSValue js_window_get_title(JSContext *js, JSValue self)
|
||||
SDL_Window *w = js2SDL_Window(js,self);
|
||||
const char *title = SDL_GetWindowTitle(w);
|
||||
return JS_NewString(js,title);
|
||||
|
||||
}
|
||||
|
||||
JSValue js_window_set_title(JSContext *js, JSValue self, JSValue val)
|
||||
@@ -2906,6 +2869,7 @@ JSValue js_window_set_size(JSContext *js, JSValue self, JSValue val)
|
||||
SDL_Window *w = js2SDL_Window(js,self);
|
||||
HMM_Vec2 size = js2vec2(js,val);
|
||||
SDL_SetWindowSize(w,size.x,size.y);
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
JSValue js_window_set_icon(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
@@ -2914,13 +2878,14 @@ JSValue js_window_set_icon(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
SDL_Surface *s = js2SDL_Surface(js,argv[0]);
|
||||
if (!SDL_SetWindowIcon(w,s))
|
||||
return JS_ThrowReferenceError(js, "could not set window icon: %s", SDL_GetError());
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
JSValue js_window_mouse_grab(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
{
|
||||
SDL_Window *w = js2SDL_Window(js,self);
|
||||
SDL_SetWindowMouseGrab(w, JS_ToBool(js,argv[0]));
|
||||
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_SDL_Window_funcs[] = {
|
||||
@@ -2942,15 +2907,6 @@ JSC_CCALL(SDL_Renderer_clear,
|
||||
SDL_RenderClear(renderer);
|
||||
)
|
||||
|
||||
const char *tn = "present thread";
|
||||
static int present_thread(SDL_Renderer *r)
|
||||
{
|
||||
TracyCZone(present,1)
|
||||
SDL_RenderPresent(r);
|
||||
TracyCZoneEnd(present)
|
||||
return 0;
|
||||
}
|
||||
|
||||
JSC_CCALL(SDL_Renderer_present,
|
||||
// SDL_Thread *thread;
|
||||
SDL_Renderer *ren = js2SDL_Renderer(js,self);
|
||||
@@ -3032,12 +2988,12 @@ JSC_CCALL(SDL_Renderer_fillrect,
|
||||
JS_FreeValue(js,val);
|
||||
}
|
||||
if (!SDL_RenderFillRects(r,rects,len))
|
||||
return JS_ThrowReferenceError("Could not render rectangle: %s", SDL_GetError());
|
||||
return JS_ThrowReferenceError(js, "Could not render rectangle: %s", SDL_GetError());
|
||||
}
|
||||
rect rect = transform_rect(r,js2rect(js,argv[0]),&cam_mat);
|
||||
|
||||
if (!SDL_RenderFillRect(r, &rect))
|
||||
return JS_ThrowReferenceError("Could not render rectangle: %s", SDL_GetError());
|
||||
return JS_ThrowReferenceError(js, "Could not render rectangle: %s", SDL_GetError());
|
||||
)
|
||||
|
||||
JSC_CCALL(renderer_texture,
|
||||
@@ -3317,62 +3273,6 @@ JSC_CCALL(renderer_target,
|
||||
}
|
||||
)
|
||||
|
||||
static void generate_normals(float* positions, size_t vertex_count, uint16_t* indices, size_t index_count, int16_t* normal_data) {
|
||||
// Initialize normals to zero
|
||||
float* temp_normals = malloc(sizeof(float)*3*vertex_count);
|
||||
for (size_t i = 0; i < vertex_count*3; i++) {
|
||||
temp_normals[i] = 0.0f;
|
||||
}
|
||||
|
||||
// Compute face normals and accumulate
|
||||
for (size_t i = 0; i < index_count; i += 3) {
|
||||
uint16_t i0 = indices[i+0];
|
||||
uint16_t i1 = indices[i+1];
|
||||
uint16_t i2 = indices[i+2];
|
||||
|
||||
float* p0 = &positions[i0*3];
|
||||
float* p1 = &positions[i1*3];
|
||||
float* p2 = &positions[i2*3];
|
||||
|
||||
float ux = p1[0] - p0[0];
|
||||
float uy = p1[1] - p0[1];
|
||||
float uz = p1[2] - p0[2];
|
||||
|
||||
float vx = p2[0] - p0[0];
|
||||
float vy = p2[1] - p0[1];
|
||||
float vz = p2[2] - p0[2];
|
||||
|
||||
// Cross product (u x v)
|
||||
float nx = uy*vz - uz*vy;
|
||||
float ny = uz*vx - ux*vz;
|
||||
float nz = ux*vy - uy*vx;
|
||||
|
||||
// Accumulate to each vertex normal
|
||||
temp_normals[i0*3+0] += nx; temp_normals[i0*3+1] += ny; temp_normals[i0*3+2] += nz;
|
||||
temp_normals[i1*3+0] += nx; temp_normals[i1*3+1] += ny; temp_normals[i1*3+2] += nz;
|
||||
temp_normals[i2*3+0] += nx; temp_normals[i2*3+1] += ny; temp_normals[i2*3+2] += nz;
|
||||
}
|
||||
|
||||
// Normalize and pack
|
||||
for (size_t i = 0; i < vertex_count; i++) {
|
||||
float x = temp_normals[i*3+0];
|
||||
float y = temp_normals[i*3+1];
|
||||
float z = temp_normals[i*3+2];
|
||||
float length = sqrtf(x*x + y*y + z*z);
|
||||
if (length > 1e-6f) {
|
||||
x /= length; y /= length; z /= length;
|
||||
} else {
|
||||
x = 0; y = 0; z = 1; // arbitrary default
|
||||
}
|
||||
|
||||
normal_data[i*3+0] = (int16_t)(x * 32767.0f);
|
||||
normal_data[i*3+1] = (int16_t)(y * 32767.0f);
|
||||
normal_data[i*3+2] = (int16_t)(z * 32767.0f);
|
||||
}
|
||||
|
||||
free(temp_normals);
|
||||
}
|
||||
|
||||
// Given an array of sprites, make the necessary geometry
|
||||
// A sprite is expected to have:
|
||||
// transform: a transform encoding position and rotation. its scale is in pixels - so a scale of 1 means the image will draw only on a single pixel.
|
||||
@@ -3380,7 +3280,6 @@ static void generate_normals(float* positions, size_t vertex_count, uint16_t* in
|
||||
// color: the color this sprite should be hued by
|
||||
JSC_CCALL(renderer_make_sprite_mesh,
|
||||
JSValue sprites = argv[0];
|
||||
JSValue old = argv[1];
|
||||
size_t quads = js_arrlen(js,argv[0]);
|
||||
size_t verts = quads*4;
|
||||
size_t count = quads*6;
|
||||
@@ -3489,7 +3388,7 @@ JSC_CCALL(gpu_claim_window,
|
||||
|
||||
JSC_CCALL(gpu_set_swapchain,
|
||||
if (!SDL_SetGPUSwapchainParameters(global_gpu,global_window, js2SDL_GPUSwapchainComposition(js,argv[0]), js2SDL_GPUPresentMode(js,argv[1])))
|
||||
return JS_ThrowReferenceError("Could not set: %s\n", SDL_GetError());
|
||||
return JS_ThrowReferenceError(js, "Could not set: %s\n", SDL_GetError());
|
||||
)
|
||||
|
||||
static JSValue *js_swapchains;
|
||||
@@ -3841,7 +3740,7 @@ static JSValue js_gpu_make_pipeline(JSContext *js, JSValueConst self, int argc,
|
||||
|
||||
// format
|
||||
const char *fmt_str = JS_ToCString(js, fmt_val);
|
||||
SDL_GPUVertexElementFormat format;
|
||||
SDL_GPUVertexElementFormat format = -1; // TODO: Check for error (if this is still -1)
|
||||
if (fmt_str) {
|
||||
// Map from string to SDL_GPUVertexElementFormat
|
||||
if (!strcmp(fmt_str, "float2")) format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
|
||||
@@ -3919,7 +3818,6 @@ static JSValue js_gpu_make_pipeline(JSContext *js, JSValueConst self, int argc,
|
||||
|
||||
// Write Mask
|
||||
JSValue write_mask_val = JS_GetPropertyStr(js, stencil_val, "write_mask");
|
||||
tmp;
|
||||
JS_ToUint32(js, &tmp, write_mask_val);
|
||||
info.depth_stencil_state.write_mask = tmp;
|
||||
}
|
||||
@@ -4008,7 +3906,7 @@ JSC_CCALL(gpu_texture,
|
||||
info.usage |= SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE;
|
||||
|
||||
SDL_GPUTexture *tex = SDL_CreateGPUTexture(gpu,&info);
|
||||
if (!tex) return JS_ThrowReferenceError("Unable to create texture: %s", SDL_GetError());
|
||||
if (!tex) return JS_ThrowReferenceError(js, "Unable to create texture: %s", SDL_GetError());
|
||||
JSValue jstex = SDL_GPUTexture2js(js,tex);
|
||||
JS_SetPropertyStr(js,jstex,"width", number2js(js,info.width));
|
||||
JS_SetPropertyStr(js,jstex,"height", number2js(js,info.height));
|
||||
@@ -4393,10 +4291,6 @@ static JSValue js_gpu_make_shader(JSContext *js, JSValueConst self, int argc, JS
|
||||
|
||||
JSValue obj = argv[0];
|
||||
|
||||
JSValue jsfile = JS_GetPropertyStr(js,obj,"file");
|
||||
const char *file = JS_ToCString(js,jsfile);
|
||||
JS_FreeValue(js,jsfile);
|
||||
|
||||
SDL_GPUShaderCreateInfo info = {0};
|
||||
|
||||
// code
|
||||
@@ -4410,12 +4304,11 @@ static JSValue js_gpu_make_shader(JSContext *js, JSValueConst self, int argc, JS
|
||||
// stage
|
||||
JSValue stage_val = JS_GetPropertyStr(js, obj, "stage");
|
||||
const char *stage_str = JS_ToCString(js, stage_val);
|
||||
SDL_GPUShaderStage stage = SDL_GPU_SHADERSTAGE_VERTEX;
|
||||
if (stage_str) {
|
||||
if (!strcmp(stage_str, "vertex")) stage = SDL_GPU_SHADERSTAGE_VERTEX;
|
||||
else if (!strcmp(stage_str, "fragment")) stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
|
||||
if (!strcmp(stage_str, "vertex")) info.stage = SDL_GPU_SHADERSTAGE_VERTEX;
|
||||
else if (!strcmp(stage_str, "fragment")) info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
|
||||
JS_FreeCString(js, stage_str);
|
||||
}
|
||||
} // TODO: Make this its own function
|
||||
JS_FreeValue(js, stage_val);
|
||||
|
||||
// num_samplers
|
||||
@@ -4496,14 +4389,15 @@ JSC_CCALL(gpu_upload,
|
||||
total_size_needed += items[i].size;
|
||||
JS_FreeValue(js, js_buf);
|
||||
}
|
||||
|
||||
|
||||
SDL_GPUTransferBuffer *transfer = js2SDL_GPUTransferBuffer(js,js_transfer);
|
||||
if (transfer) {
|
||||
// ensure it's large enough
|
||||
size_t transfer_size = js_getnum_str(js,js_transfer, "size");
|
||||
if (transfer_size < total_size_needed) {
|
||||
printf("New transfer buffer needed of size %d, old was %d\n", total_size_needed, transfer_size);
|
||||
transfer = SDL_CreateGPUTransferBuffer( gpu, &(SDL_GPUTransferBufferCreateInfo){
|
||||
printf("New transfer buffer needed of size %zu, old was %zu\n", total_size_needed, transfer_size);
|
||||
total_size_needed *= 1.5;
|
||||
transfer = SDL_CreateGPUTransferBuffer( gpu, &(SDL_GPUTransferBufferCreateInfo){
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = total_size_needed
|
||||
}
|
||||
@@ -4930,6 +4824,14 @@ JSC_CCALL(gpu_tile,
|
||||
arrfree(verts);
|
||||
)
|
||||
|
||||
static const JSCFunctionListEntry js_SDL_GPUCopyPass_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUFence_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUTransferBuffer_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUShader_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUSampler_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUGraphicsPipeline_funcs[] = {};
|
||||
static const JSCFunctionListEntry js_SDL_GPUComputePipeline_funcs[] = {};
|
||||
|
||||
static const JSCFunctionListEntry js_SDL_GPUDevice_funcs[] = {
|
||||
MIST_FUNC_DEF(gpu, claim_window, 1),
|
||||
MIST_FUNC_DEF(gpu, make_pipeline, 1), // loads pipeline state into an object
|
||||
@@ -5075,11 +4977,6 @@ static const JSCFunctionListEntry js_SDL_GPURenderPass_funcs[] = {
|
||||
MIST_FUNC_DEF(renderpass, bind_storage_textures, 2),
|
||||
};
|
||||
|
||||
JSC_CCALL(copypass_end,
|
||||
SDL_GPUCopyPass *pass = js2SDL_GPUCopyPass(js,self);
|
||||
SDL_EndGPUCopyPass(pass);
|
||||
)
|
||||
|
||||
JSC_CCALL(cmd_render_pass,
|
||||
SDL_GPUCommandBuffer *cmds = js2SDL_GPUCommandBuffer(js, self);
|
||||
|
||||
@@ -5185,7 +5082,6 @@ JSC_CCALL(cmd_push_compute_uniform_data,
|
||||
)
|
||||
|
||||
JSC_CCALL(cmd_submit,
|
||||
Uint64 start = SDL_GetTicksNS();
|
||||
SDL_GPUCommandBuffer *cmds = js2SDL_GPUCommandBuffer(js,self);
|
||||
SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(cmds);
|
||||
return SDL_GPUFence2js(js,fence);
|
||||
@@ -6454,11 +6350,6 @@ JSC_CCALL(os_make_aseprite,
|
||||
if (!ase)
|
||||
return JS_ThrowReferenceError(js, "could not load aseprite from supplied array buffer: %s", aseprite_GetError());
|
||||
|
||||
int w = ase->w;
|
||||
int h = ase->h;
|
||||
|
||||
int pixels = w*h;
|
||||
|
||||
if (ase->tag_count == 0) {
|
||||
// we're dealing with a single frame image, or single animation
|
||||
if (ase->frame_count == 1) {
|
||||
@@ -6512,8 +6403,7 @@ JSC_CCALL(os_make_cursor,
|
||||
SDL_Surface *s = js2SDL_Surface(js,argv[0]);
|
||||
HMM_Vec2 hot = js2vec2(js,argv[1]);
|
||||
SDL_Cursor *c = SDL_CreateColorCursor(s, hot.x, hot.y);
|
||||
if (!c) return JS_ThrowReferenceError("couldn't make cursor: %s", SDL_GetError());
|
||||
printf("made cursor %p\n", c);
|
||||
if (!c) return JS_ThrowReferenceError(js,"couldn't make cursor: %s", SDL_GetError());
|
||||
return SDL_Cursor2js(js,c);
|
||||
)
|
||||
|
||||
@@ -6765,16 +6655,16 @@ JSC_SCALL(os_model_buffer,
|
||||
)
|
||||
|
||||
JSC_SCALL(os_gltf_buffer,
|
||||
int buffer_idx = js2number(js,argv[1]);
|
||||
int type = js2number(js,argv[2]);
|
||||
cgltf_options options = {0};
|
||||
cgltf_data *data = NULL;
|
||||
cgltf_result result = cgltf_parse_file(&options, str, &data);
|
||||
result = cgltf_load_buffers(&options, data, str);
|
||||
// int buffer_idx = js2number(js,argv[1]);
|
||||
// int type = js2number(js,argv[2]);
|
||||
// cgltf_options options = {0};
|
||||
// cgltf_data *data = NULL;
|
||||
// cgltf_result result = cgltf_parse_file(&options, str, &data);
|
||||
// result = cgltf_load_buffers(&options, data, str);
|
||||
|
||||
SDL_GPUBuffer *b = SDL_CreateGPUBuffer(NULL,NULL);
|
||||
// SDL_GPUBuffer *b = SDL_CreateGPUBuffer(NULL,NULL);
|
||||
// *b = accessor2buffer(&data->accessors[buffer_idx], type);
|
||||
cgltf_free(data);
|
||||
// cgltf_free(data);
|
||||
|
||||
// ret = sg_buffer2js(js,b);
|
||||
)
|
||||
@@ -7031,7 +6921,7 @@ JSC_CCALL(os_sleep,
|
||||
|
||||
JSC_CCALL(os_battery_pct,
|
||||
int pct;
|
||||
SDL_PowerState state = SDL_GetPowerInfo(NULL, &pct);
|
||||
SDL_GetPowerInfo(NULL, &pct);
|
||||
return number2js(js,pct);
|
||||
)
|
||||
|
||||
@@ -7041,7 +6931,7 @@ JSC_CCALL(os_battery_voltage,
|
||||
|
||||
JSC_CCALL(os_battery_seconds,
|
||||
int seconds;
|
||||
SDL_PowerState state = SDL_GetPowerInfo(&seconds, NULL);
|
||||
SDL_GetPowerInfo(&seconds, NULL);
|
||||
return number2js(js,seconds);
|
||||
)
|
||||
|
||||
@@ -7152,6 +7042,7 @@ JSC_CCALL(os_make_quadtree,
|
||||
|
||||
JSC_CCALL(os_make_rtree,
|
||||
struct rtree *tree = rtree_new();
|
||||
if (!tree) return JS_ThrowOutOfMemory(js);
|
||||
return rtree2js(js,tree);
|
||||
)
|
||||
|
||||
@@ -7261,15 +7152,16 @@ JSC_CCALL(rtree_insert,
|
||||
JSValue v = argv[0];
|
||||
rect r;
|
||||
JS_GETATOM(js,r,v,rect_atom,rect)
|
||||
double min[2];
|
||||
double max[2];
|
||||
NUMTYPE min[2];
|
||||
NUMTYPE max[2];
|
||||
min[0] = r.x;
|
||||
min[1] = r.y;
|
||||
max[0] = r.x+r.w;
|
||||
max[1] = r.y+r.h;
|
||||
JSValue *ins = malloc(sizeof(*ins));
|
||||
*ins = JS_DupValue(js,v);
|
||||
rtree_insert(tree, min, max, ins);
|
||||
if (!rtree_insert(tree, min, max, ins))
|
||||
return JS_ThrowOutOfMemory(js);
|
||||
)
|
||||
|
||||
int rtree_cmp(const JSValue *a, const JSValue *b, JSContext *js)
|
||||
@@ -7286,14 +7178,15 @@ JSC_CCALL(rtree_remove,
|
||||
JSValue v = argv[0];
|
||||
rect r;
|
||||
JS_GETATOM(js,r,v,rect_atom,rect)
|
||||
double min[2];
|
||||
double max[2];
|
||||
NUMTYPE min[2];
|
||||
NUMTYPE max[2];
|
||||
min[0] = r.x;
|
||||
min[1] = r.y;
|
||||
max[0] = r.x+r.w;
|
||||
max[1] = r.y+r.h;
|
||||
|
||||
int deleted = rtree_delete_with_comparator(tree, min, max, &v, rtree_cmp, js);
|
||||
if (!rtree_delete_with_comparator(tree, min, max, &v, rtree_cmp, js))
|
||||
return JS_ThrowOutOfMemory(js);
|
||||
)
|
||||
|
||||
struct rtree_iter_data {
|
||||
@@ -7302,17 +7195,18 @@ struct rtree_iter_data {
|
||||
int n;
|
||||
};
|
||||
|
||||
bool rtree_iter(const double *min, const double *max, const JSValue *data, struct rtree_iter_data *ctx)
|
||||
bool rtree_iter(const NUMTYPE *min, const NUMTYPE *max, const JSValue *data, struct rtree_iter_data *ctx)
|
||||
{
|
||||
JS_SetPropertyUint32(ctx->js,ctx->arr,ctx->n, JS_DupValue(ctx->js,*data));
|
||||
ctx->n++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSC_CCALL(rtree_query,
|
||||
rtree *tree = js2rtree(js,self);
|
||||
rect r = js2rect(js,argv[0]);
|
||||
double min[2];
|
||||
double max[2];
|
||||
NUMTYPE min[2];
|
||||
NUMTYPE max[2];
|
||||
min[0] = r.x;
|
||||
min[1] = r.y;
|
||||
max[0] = r.x+r.w;
|
||||
@@ -7397,6 +7291,7 @@ void ffi_load(JSContext *js) {
|
||||
|
||||
JSValue globalThis = JS_GetGlobalObject(js);
|
||||
|
||||
|
||||
QJSCLASSPREP_FUNCS(qtree)
|
||||
QJSCLASSPREP_FUNCS(rtree)
|
||||
QJSCLASSPREP_FUNCS(SDL_Window)
|
||||
@@ -7410,6 +7305,14 @@ void ffi_load(JSContext *js) {
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUTexture)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUCommandBuffer)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPURenderPass)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUComputePass)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUCopyPass)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUFence)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUTransferBuffer)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUShader)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUSampler)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUGraphicsPipeline)
|
||||
QJSCLASSPREP_FUNCS(SDL_GPUComputePipeline)
|
||||
QJSCLASSPREP_FUNCS(jssprite)
|
||||
// QJSCLASSPREP_FUNCS(SDL_GPUGraphicsPipeline)
|
||||
// QJSCLASSPREP_FUNCS(SDL_GPUSampler)
|
||||
@@ -7556,7 +7459,5 @@ void ffi_load(JSContext *js) {
|
||||
|
||||
global_js = js;
|
||||
|
||||
JSValue *vals = NULL;
|
||||
|
||||
JS_FreeValue(js,globalThis);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
#define DATATYPE void *
|
||||
#define DIMS 2
|
||||
#define NUMTYPE double
|
||||
#define MAXITEMS 64
|
||||
|
||||
////////////////////////////////
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#ifndef RTREE_H
|
||||
#define RTREE_H
|
||||
|
||||
#define NUMTYPE double
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -56,22 +58,22 @@ void rtree_set_udata(struct rtree *tr, void *udata);
|
||||
// When inserting points, the max coordinates is optional (set to NULL).
|
||||
//
|
||||
// Returns false if the system is out of memory.
|
||||
bool rtree_insert(struct rtree *tr, const double *min, const double *max, const void *data);
|
||||
bool rtree_insert(struct rtree *tr, const NUMTYPE *min, const NUMTYPE *max, const void *data);
|
||||
|
||||
|
||||
// rtree_search searches the rtree and iterates over each item that intersect
|
||||
// the provided rectangle.
|
||||
//
|
||||
// Returning false from the iter will stop the search.
|
||||
void rtree_search(const struct rtree *tr, const double *min, const double *max,
|
||||
bool (*iter)(const double *min, const double *max, const void *data, void *udata),
|
||||
void rtree_search(const struct rtree *tr, const NUMTYPE *min, const NUMTYPE *max,
|
||||
bool (*iter)(const NUMTYPE *min, const NUMTYPE *max, const void *data, void *udata),
|
||||
void *udata);
|
||||
|
||||
// rtree_scan iterates over every item in the rtree.
|
||||
//
|
||||
// Returning false from the iter will stop the scan.
|
||||
void rtree_scan(const struct rtree *tr,
|
||||
bool (*iter)(const double *min, const double *max, const void *data, void *udata),
|
||||
bool (*iter)(const NUMTYPE *min, const NUMTYPE *max, const void *data, void *udata),
|
||||
void *udata);
|
||||
|
||||
// rtree_count returns the number of items in the rtree.
|
||||
@@ -84,7 +86,7 @@ size_t rtree_count(const struct rtree *tr);
|
||||
// data. The first item that is found is deleted.
|
||||
//
|
||||
// Returns false if the system is out of memory.
|
||||
bool rtree_delete(struct rtree *tr, const double *min, const double *max, const void *data);
|
||||
bool rtree_delete(struct rtree *tr, const NUMTYPE *min, const NUMTYPE *max, const void *data);
|
||||
|
||||
// rtree_delete_with_comparator deletes an item from the rtree.
|
||||
// This searches the tree for an item that is contained within the provided
|
||||
@@ -92,8 +94,8 @@ bool rtree_delete(struct rtree *tr, const double *min, const double *max, const
|
||||
// a compare function. The first item that is found is deleted.
|
||||
//
|
||||
// Returns false if the system is out of memory.
|
||||
bool rtree_delete_with_comparator(struct rtree *tr, const double *min,
|
||||
const double *max, const void *data,
|
||||
bool rtree_delete_with_comparator(struct rtree *tr, const NUMTYPE *min,
|
||||
const NUMTYPE *max, const void *data,
|
||||
int (*compare)(const void *a, const void *b, void *udata),
|
||||
void *udata);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user