diff --git a/docs/api/util.md b/docs/api/util.md index 34ce9437..428cf0da 100644 --- a/docs/api/util.md +++ b/docs/api/util.md @@ -19,9 +19,6 @@ In-place insertion sort of an array using a comparison function cmp(a,b)->Number ## get(obj, path, defValue) -## isObject(o) - - ## isEmpty(o) diff --git a/scripts/core/base.js b/scripts/core/base.js index 5c3e6935..c282ff0e 100644 --- a/scripts/core/base.js +++ b/scripts/core/base.js @@ -1,23 +1,3 @@ -Object.defineProperty(Array.prototype, "dofilter", { - value: function array_dofilter(fn) { - for (let i = 0; i < this.length; i++) { - if (!fn.call(this, this[i], i, this)) { - this.splice(i, 1); - i--; - } - } - return this; - }, -}); - -Object.defineProperty(Array.prototype, "delete", { - value: function(item) { - var idx = this.indexOf(item); - if (idx > -1) this.splice(idx,1) - return undefined - } -}); - Object.mixin = function (target, source) { if (typeof source !== "object") return target; Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); @@ -170,6 +150,26 @@ Object.defineProperty(String.prototype, "updir", { }); /* ARRAY DEFS */ +Object.defineProperty(Array.prototype, "filter!", { + value: function array_dofilter(fn) { + for (let i = 0; i < this.length; i++) { + if (!fn.call(this, this[i], i, this)) { + this.splice(i, 1); + i--; + } + } + return this; + }, +}); + +Object.defineProperty(Array.prototype, "delete", { + value: function(item) { + var idx = this.indexOf(item); + if (idx > -1) this.splice(idx,1) + return undefined + } +}); + Object.defineProperty(Array.prototype, "copy", { value: function () { var c = []; @@ -182,138 +182,6 @@ Object.defineProperty(Array.prototype, "copy", { }, }); -function make_swizz() { - function setelem(n) { - return { - get: function get() { - return this[n]; - }, - set: function set(x) { - this[n] = x; - }, - }; - } - - function arrsetelem(str, n) { - Object.defineProperty(Array.prototype, str, setelem(n)); -// Object.defineProperty(Float32Array.prototype, setelem(n)); - } - - var arr_elems = ["x", "y", "z", "w"]; - var quat_elems = ["i", "j", "k"]; - var color_elems = ["r", "g", "b", "a"]; - - arr_elems.forEach(function (x, i) { - arrsetelem(x, i); - }); - quat_elems.forEach(function (x, i) { - arrsetelem(x, i); - }); - color_elems.forEach(function (x, i) { - arrsetelem(x, i); - }); - - var nums = [0, 1, 2, 3]; - - var swizz = []; - - for (var i of nums) for (var j of nums) swizz.push([i, j]); - - swizz.forEach(function (x) { - var str = ""; - for (var i of x) str += arr_elems[i]; - - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - }, - }); - - str = ""; - for (var i of x) str += color_elems[i]; - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - }, - }); - }); - - swizz = []; - for (var i of nums) for (var j of nums) for (var k of nums) swizz.push([i, j, k]); - - swizz.forEach(function (x) { - var str = ""; - for (var i of x) str += arr_elems[i]; - - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]], this[x[2]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - this[x[2]] = j[2]; - }, - }); - - str = ""; - for (var i of x) str += color_elems[i]; - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]], this[x[2]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - this[x[2]] = j[2]; - }, - }); - }); - - swizz = []; - for (var i of nums) for (var j of nums) for (var k of nums) for (var w of nums) swizz.push([i, j, k, w]); - - swizz.forEach(function (x) { - var str = ""; - for (var i of x) str += arr_elems[i]; - - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]], this[x[2]], this[x[3]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - this[x[2]] = j[2]; - this[x[3]] = j[3]; - }, - }); - - str = ""; - for (var i of x) str += color_elems[i]; - Object.defineProperty(Array.prototype, str, { - get() { - return [this[x[0]], this[x[1]], this[x[2]], this[x[3]]]; - }, - set(j) { - this[x[0]] = j[0]; - this[x[1]] = j[1]; - this[x[2]] = j[2]; - this[x[3]] = j[3]; - }, - }); - }); -} -make_swizz(); - Object.defineProperty(Array.prototype, "equal", { value: function equal(b) { if (this.length !== b.length) return false; @@ -329,26 +197,12 @@ Object.defineProperty(Array.prototype, "equal", { }, }); -Object.defineProperty(Array.prototype, "search", { - value: function (val) { - for (var i = 0; i < this.length; i++) if (this[i] === val) return i; - - return undefined; - }, -}); - Object.defineProperty(Array.prototype, "last", { value: function () { return this[this.length - 1]; }, }); -Object.defineProperty(Array.prototype, "at", { - value: function (x) { - return x < 0 ? this[this.length + x] : this[x]; - }, -}); - Object.defineProperty(Array.prototype, "wrapped", { value: function (x) { var c = this.slice(0, this.length); diff --git a/scripts/modules/loop.js b/scripts/modules/loop.js index ef554ce4..49eac7e7 100644 --- a/scripts/modules/loop.js +++ b/scripts/modules/loop.js @@ -5,7 +5,7 @@ var input = use('input') var emitter = use('emitter') var os = use('os') var event = use('event') -var imgui = use('imgui') +//var imgui = use('imgui') var waittime = 1/240 var last_frame_time = 0 @@ -47,7 +47,7 @@ function step() { render.setup_hud() // Show your UI or debug - imgui_fn() +// imgui_fn() // Now do the GPU present (calls gpupresent in render.js) render._main.present() diff --git a/scripts/modules/render.js b/scripts/modules/render.js index 4ddf6ce0..92cc6573 100644 --- a/scripts/modules/render.js +++ b/scripts/modules/render.js @@ -614,13 +614,13 @@ function gpupresent() }); // imgui - cmds.push_debug_group("imgui") +/* cmds.push_debug_group("imgui") imgui.prepend(cmds); var pass = cmds.render_pass({ color_targets:[{texture:swapchain_tex}]}); imgui.endframe(cmds,pass); pass.end(); - cmds.pop_debug_group() + cmds.pop_debug_group()*/ } cmds.submit() } @@ -709,7 +709,7 @@ render.scissor = function(rect) ////////////////////////////////////////////////// var screencolor; -globalThis.imtoggle = function (name, obj, field) { +function imtoggle(name, obj, field) { var changed = false; var old = obj[field]; obj[field] = imgui.checkbox(name, obj[field]); @@ -732,6 +732,7 @@ var observed_tex = undefined; var debug = {} debug.console = false var imgui_fn = function imgui_fn() { + return; imgui.newframe(); if (debug.console) debug.console = imgui.window("console", _ => { @@ -803,8 +804,8 @@ var imgui_fn = function imgui_fn() { } }); */ - prosperon.imgui(); - imgui.endframe(render._main); +// prosperon.imgui(); +// imgui.endframe(render._main); }; // Some initialization @@ -820,7 +821,7 @@ std_sampler = render._main.make_sampler({ }); render._main.present = gpupresent; -imgui.init(render._main, prosperon.window); +//imgui.init(render._main, prosperon.window); tracy.gpu_init() render.queue = function(cmd) diff --git a/source/jsffi.c b/source/jsffi.c index c856ff32..ec15235d 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -2444,12 +2444,106 @@ JSC_CCALL(array_lerp, return arr; ) +JSValue js_array_get_x(JSContext *js, JSValue self) { return JS_GetPropertyUint32(js,self,0); } +JSValue js_array_set_x(JSContext *js, JSValue self, JSValue val) { JS_SetPropertyUint32(js,self,0,val); return JS_UNDEFINED; } + +JSValue js_array_get_y(JSContext *js, JSValue self) { return JS_GetPropertyUint32(js,self,1); } +JSValue js_array_set_y(JSContext *js, JSValue self, JSValue val) { JS_SetPropertyUint32(js,self,1,val); return JS_UNDEFINED; } + +JSValue js_array_get_xy(JSContext *js, JSValue self) +{ + JSValue arr = JS_NewArray(js); + JS_SetPropertyUint32(js,arr,0,JS_GetPropertyUint32(js,self,0)); + JS_SetPropertyUint32(js,arr,1,JS_GetPropertyUint32(js,self,1)); + return arr; +} + +JSValue js_array_set_xy(JSContext *js, JSValue self, JSValue v) +{ + JS_SetPropertyUint32(js,self,0,JS_GetPropertyUint32(js,v,0)); + JS_SetPropertyUint32(js,self,1,JS_GetPropertyUint32(js,v,1)); + return JS_UNDEFINED; +} + +// Single-value accessors + +JSValue js_array_get_r(JSContext *js, JSValue self) +{ return JS_GetPropertyUint32(js, self, 0); } + +JSValue js_array_set_r(JSContext *js, JSValue self, JSValue val) +{ JS_SetPropertyUint32(js, self, 0, val); return JS_UNDEFINED; } + +JSValue js_array_get_g(JSContext *js, JSValue self) +{ return JS_GetPropertyUint32(js, self, 1); } + +JSValue js_array_set_g(JSContext *js, JSValue self, JSValue val) +{ JS_SetPropertyUint32(js, self, 1, val); return JS_UNDEFINED; } + +JSValue js_array_get_b(JSContext *js, JSValue self) +{ return JS_GetPropertyUint32(js, self, 2); } + +JSValue js_array_set_b(JSContext *js, JSValue self, JSValue val) +{ JS_SetPropertyUint32(js, self, 2, val); return JS_UNDEFINED; } + +JSValue js_array_get_a(JSContext *js, JSValue self) +{ return JS_GetPropertyUint32(js, self, 3); } + +JSValue js_array_set_a(JSContext *js, JSValue self, JSValue val) +{ JS_SetPropertyUint32(js, self, 3, val); return JS_UNDEFINED; } + +// Multi-value accessors + +JSValue js_array_get_rgb(JSContext *js, JSValue self) +{ + JSValue arr = JS_NewArray(js); + JS_SetPropertyUint32(js, arr, 0, JS_GetPropertyUint32(js, self, 0)); + JS_SetPropertyUint32(js, arr, 1, JS_GetPropertyUint32(js, self, 1)); + JS_SetPropertyUint32(js, arr, 2, JS_GetPropertyUint32(js, self, 2)); + return arr; +} + +JSValue js_array_set_rgb(JSContext *js, JSValue self, JSValue val) +{ + JS_SetPropertyUint32(js, self, 0, JS_GetPropertyUint32(js, val, 0)); + JS_SetPropertyUint32(js, self, 1, JS_GetPropertyUint32(js, val, 1)); + JS_SetPropertyUint32(js, self, 2, JS_GetPropertyUint32(js, val, 2)); + return JS_UNDEFINED; +} + +JSValue js_array_get_rgba(JSContext *js, JSValue self) +{ + JSValue arr = JS_NewArray(js); + JS_SetPropertyUint32(js, arr, 0, JS_GetPropertyUint32(js, self, 0)); + JS_SetPropertyUint32(js, arr, 1, JS_GetPropertyUint32(js, self, 1)); + JS_SetPropertyUint32(js, arr, 2, JS_GetPropertyUint32(js, self, 2)); + JS_SetPropertyUint32(js, arr, 3, JS_GetPropertyUint32(js, self, 3)); + return arr; +} + +JSValue js_array_set_rgba(JSContext *js, JSValue self, JSValue val) +{ + JS_SetPropertyUint32(js, self, 0, JS_GetPropertyUint32(js, val, 0)); + JS_SetPropertyUint32(js, self, 1, JS_GetPropertyUint32(js, val, 1)); + JS_SetPropertyUint32(js, self, 2, JS_GetPropertyUint32(js, val, 2)); + JS_SetPropertyUint32(js, self, 3, JS_GetPropertyUint32(js, val, 3)); + return JS_UNDEFINED; +} + static const JSCFunctionListEntry js_array_funcs[] = { PROTO_FUNC_DEF(array, add, 1), PROTO_FUNC_DEF(array, sub, 1), PROTO_FUNC_DEF(array, div,1), PROTO_FUNC_DEF(array, scale, 1), - PROTO_FUNC_DEF(array, lerp, 2) + PROTO_FUNC_DEF(array, lerp, 2), + JS_CGETSET_DEF("x", js_array_get_x,js_array_set_x), + JS_CGETSET_DEF("y", js_array_get_y, js_array_set_y), + JS_CGETSET_DEF("xy", js_array_get_xy, js_array_set_xy), + JS_CGETSET_DEF("r", js_array_get_r, js_array_set_r), + JS_CGETSET_DEF("g", js_array_get_g, js_array_set_g), + JS_CGETSET_DEF("b", js_array_get_b, js_array_set_b), + JS_CGETSET_DEF("a", js_array_get_a, js_array_set_a), + JS_CGETSET_DEF("rgb", js_array_get_rgb, js_array_set_rgb), + JS_CGETSET_DEF("rgba", js_array_get_rgba, js_array_set_rgba), }; JSC_CCALL(number_lerp, @@ -5661,12 +5755,34 @@ JSC_CCALL(input_keymod, return js_keymod(js); ) +JSC_CCALL(input_mousestate, + float x,y; + SDL_MouseButtonFlags flags = SDL_GetMouseState(&x,&y); + JSValue m = JS_NewObject(js); + JS_SetProperty(js,m,x_atom, number2js(js,x)); + JS_SetProperty(js,m,y_atom,number2js(js,y)); + + if (flags & SDL_BUTTON_LMASK) + JS_SetPropertyStr(js, m, "left", JS_NewBool(js, 1)); + if (flags & SDL_BUTTON_MMASK) + JS_SetPropertyStr(js, m, "middle", JS_NewBool(js, 1)); + if (flags & SDL_BUTTON_RMASK) + JS_SetPropertyStr(js, m, "right", JS_NewBool(js, 1)); + if (flags & SDL_BUTTON_X1MASK) + JS_SetPropertyStr(js, m, "x1", JS_NewBool(js, 1)); + if (flags & SDL_BUTTON_X2MASK) + JS_SetPropertyStr(js, m, "x2", JS_NewBool(js, 1)); + + return m; +) + static const JSCFunctionListEntry js_input_funcs[] = { MIST_FUNC_DEF(input, mouse_show, 1), MIST_FUNC_DEF(input, mouse_lock, 1), MIST_FUNC_DEF(input, cursor_set, 1), MIST_FUNC_DEF(input, keyname, 1), MIST_FUNC_DEF(input, keymod, 0), + MIST_FUNC_DEF(input, mousestate, 0), }; JSC_CCALL(os_guid, @@ -7786,4 +7902,4 @@ void ffi_load(JSContext *js, int argc, char **argv) { JS_FreeCString(caller_ctx,fn_src); #endif -*/ \ No newline at end of file +*/ diff --git a/source/script.c b/source/script.c index 717b4ff4..37afdb29 100644 --- a/source/script.c +++ b/source/script.c @@ -10,7 +10,15 @@ #include #include #include "quickjs.h" -#include + +#if defined(__APPLE__) + #include +#elif defined(_WIN32) + #include +#elif defined(__linux__) || defined(__GLIBC__) + #define _GNU_SOURCE + #include +#endif #include "physfs.h"