Compare commits

..

No commits in common. "7e6fc1ffbeb6a490031c94df5f685acbac1b6f95" and "18c07e9f820cd0fb416e14ec218f8d574d56462a" have entirely different histories.

12 changed files with 449 additions and 347 deletions

View file

@ -73,7 +73,6 @@ actor.delay = function(fn, seconds) {
stop.remain = seconds;
stop.seconds = seconds;
function update(dt) {
stop.remain -= dt;
if (stop.remain <= 0)

View file

@ -338,7 +338,7 @@ component.polygon2d = Object.copy(collider2d, {
},
hides: ['id', 'shape', 'gameobject'],
_enghook: os.make_poly2d,
_enghook: make_poly2d,
points:[],
setpoints(points) {
this.points = points;
@ -369,7 +369,7 @@ component.polygon2d = Object.copy(collider2d, {
gizmo() {
this.spoints().forEach(x => render.point(this.gameobject.this2screen(x), 3, Color.green));
this.points.forEach((x,i)=>render.coordinate(this.gameobject.this2screen(x), i));
this.points.forEach((x,i)=>Debug.numbered_point(this.gameobject.this2screen(x), i));
},
pick(pos) {
@ -391,7 +391,7 @@ function pointscaler(x) {
}
component.polygon2d.impl = Object.mix(collider2d.impl, {
sync() { poly2d.setverts(this.id,this.spoints()); },
sync() { cmd_poly2d(0, this.id, this.spoints());},
query() { return physics.shape_query(this.shape); },
grow: pointscaler,
});
@ -523,20 +523,20 @@ component.edge2d = Object.copy(collider2d, {
boundingbox() { return bbox.frompoints(this.points.map(x => x.scale(this.gameobject.scale))); },
hides: ['gameobject', 'id', 'shape'],
_enghook: os.make_edge2d,
_enghook: make_edge2d,
/* EDITOR */
gizmo() {
if (this.type === Spline.type.catmull || this.type === -1) {
this.spoints().forEach(x => render.point(this.gameobject.this2screen(x), 3, Color.teal));
this.points.forEach((x,i) => render.coordinate(this.gameobject.this2screen(x), i));
this.points.forEach((x,i) => Debug.numbered_point(this.gameobject.this2screen(x), i));
} else {
for (var i = 0; i < this.points.length; i += 3)
render.coordinate(this.gameobject.this2screen(this.points[i]), i, Color.teal);
Debug.numbered_point(this.gameobject.this2screen(this.points[i]), i, Color.teal);
for (var i = 1; i < this.points.length; i+=3) {
render.coordinate(this.gameobject.this2screen(this.points[i]), i, Color.green);
render.coordinate(this.gameobject.this2screen(this.points[i+1]), i+1, Color.green);
Debug.numbered_point(this.gameobject.this2screen(this.points[i]), i, Color.green);
Debug.numbered_point(this.gameobject.this2screen(this.points[i+1]), i+1, Color.green);
render.line([this.gameobject.this2screen(this.points[i-1]), this.gameobject.this2screen(this.points[i])], Color.yellow);
render.line([this.gameobject.this2screen(this.points[i+1]), this.gameobject.this2screen(this.points[i+2])], Color.yellow);
}
@ -635,14 +635,16 @@ component.edge2d = Object.copy(collider2d, {
});
component.edge2d.impl = Object.mix(collider2d.impl, {
set thickness(x) { edge2d.set_thickness(this.id,x); },
get thickness() { return edge2d.get_thickness(this.id); },
set thickness(x) {
cmd_edge2d(1,this.id,x);
},
get thickness() { return physics.edge_thickness(this.id); },
grow: pointscaler,
sync() {
var sensor = this.sensor;
var points = this.sample();
if (!points) return;
edge2d.setverts(this.id,points);
cmd_edge2d(0,this.id,points);
this.sensor = sensor;
},
});
@ -786,7 +788,7 @@ component.circle2d = Object.copy(collider2d, {
},
hides: ['gameobject', 'id', 'shape', 'scale'],
_enghook: os.make_circle2d,
_enghook: make_circle2d,
});
component.circle2d.impl = Object.mix({
@ -795,17 +797,17 @@ component.circle2d.impl = Object.mix({
"radius",
]),
set radius(x) { circle2d.set_radius(this.id,x); circle2d.sync(this.id); },
get radius() { return circle2d.get_radius(this.id); },
set radius(x) { cmd_circle2d(0,this.id,x); },
get radius() { return cmd_circle2d(2,this.id); },
set scale(x) { this.radius = x; },
get scale() { return this.radius; },
set offset(x) { circle2d.set_offset(this.id,x); circle2d.sync(this.id); },
get offset() { circle2d.get_offset(this.id); },
set offset(x) { cmd_circle2d(1,this.id,x); },
get offset() { return cmd_circle2d(3,this.id); },
get pos() { return this.offset; },
set pos(x) { this.offset = x; },
get pos() { return cmd_circle2d(3,this.id); },
set pos(x) { cmd_circle2d(1,this.id,x); },
grow(x) {
if (typeof x === 'number') this.scale *= x;

View file

@ -1,4 +1,5 @@
debug.fn_break = function(fn,obj) {
var Debug = {
fn_break(fn, obj) {
if (typeof fn !== 'function') return;
obj ??= globalThis;
@ -7,19 +8,38 @@ debug.fn_break = function(fn,obj) {
fn();
};
obj[fn.name] = newfn;
}
},
debug.draw_phys = false;
debug.draw_bb = false;
debug.draw_gizmos = false;
debug.draw_names = false;
debug.draw = function() {
if (this.draw_phys) game.all_objects(function(x) { debug.draw_gameobject(x.body); });
draw_grid(width, span, color) {
color = color ? color : Color.green;
render.grid(width,span,color);
},
coordinate(pos, size, color) { GUI.text(JSON.stringify(pos.map(p=>Math.round(p))), pos, size, color); },
boundingbox(bb, color) {
color ??= Color.white;
cmd_points(0, bbox.topoints(bb), color);
},
numbered_point(pos, n, color) {
color ??= Color.white;
render.point(pos, 3, color);
GUI.text(n, pos.add([0,4]), 1, color);
},
draw_phys: false,
draw_obj_phys(obj) { debug.draw_gameobject(obj.body); },
draw_bb: false,
draw_gizmos: false,
draw_names: false,
draw() {
if (this.draw_bb)
game.all_objects(function(x) { debug.boundingbox(x.boundingbox(), Color.debug.boundingbox.alpha(0.05)); });
game.all_objects(function(x) { Debug.boundingbox(x.boundingbox(), Color.Debug.boundingbox.alpha(0.05)); });
if (sim.paused()) GUI.text("PAUSED", [0,0],1);
if (this.draw_gizmos)
game.all_objects(function(x) {
@ -29,25 +49,22 @@ debug.draw = function() {
if (this.draw_names)
game.all_objects(function(x) {
GUI.text(x, window.world2screen(x.pos).add([0,32]), 1, Color.debug.names);
GUI.text(x, window.world2screen(x.pos).add([0,32]), 1, Color.Debug.names);
});
if (debug.gif.rec) {
if (Debug.Options.gif.rec) {
GUI.text("REC", [0,40], 1);
GUI.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0,30], 1);
GUI.text(time.timecode(time.timenow() - Debug.Options.gif.start_time, Debug.Options.gif.fps), [0,30], 1);
}
return;
if (sim.paused()) GUI.text("PAUSED", [0,0],1);
GUI.text(sim.playing() ? "PLAYING"
: sim.stepping() ?
"STEP" :
sim.paused() ?
"PAUSED; EDITING" :
"EDIT", [0, 0], 1);
}
},
};
function assert(op, str)
{
@ -66,20 +83,20 @@ var Gizmos = {
},
};
profile.best_t = function(t) {
Object.assign(profile, {
best_t(t) {
var qq = 'ns';
if (t > 1000) {
t /= 1000;
qq = 'us';
if (t > 1000) {
t /= 1000;
qq = 'ms';
qq = 'ms';
}
}
return `${t.toPrecision(4)} ${qq}`;
}
profile.cpu = function(fn, times, q) {
},
cpu(fn, times, q) {
times ??= 1;
q ??= "unnamed";
var start = profile.now();
@ -91,24 +108,56 @@ profile.cpu = function(fn, times, q) {
var totalt = profile.best_t(elapsed);
say(`profile [${q}]: ${profile.best_t(avgt)} average [${profile.best_t(totalt)} for ${times} loops]`);
}
},
profile.ms = function(t) { return t/1000000; }
profile.secs = function(t) { return t/1000000000; }
time(fn) {
var start = profile.now();
fn();
return profile.lap(start);
},
lap(t) {
return profile.best_t(profile.now()-t);
},
measure(fn, str) {
str ??= 'unnamed';
var start = profile.now();
fn();
say(`profile [${str}]: ${profile.lap(start)}`);
},
secs() { return this.now()/1000000000; },
});
performance.test = {
barecall() { performance(0); },
unpack_num(n) { performance(1,n); },
unpack_array(n) { performance(2,n); },
pack_num() { performance(3); },
pack_string() { performance(6); },
unpack_string(s) { performance(4,s); },
unpack_32farr(a) { performance(5,a); },
call_fn_n(fn1, n) { performance(7,fn1,n,fn2); },
};
performance.test.call_fn_n.doc = "Calls fn1 n times, and then fn2.";
/* These controls are available during editing, and during play of debug builds */
debug.inputs = {};
debug.inputs.f1 = function () { debug.draw_phys = !debug.draw_phys; };
debug.inputs.f1.doc = "Draw physics debugging aids.";
debug.inputs.f3 = function() { debug.draw_bb = !debug.draw_bb; };
debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
debug.inputs.f4 = function() {
debug.draw_names = !debug.draw_names;
debug.draw_gizmos = !debug.draw_gizmos;
Debug.inputs = {};
Debug.inputs.f1 = function () { Debug.draw_phys = !Debug.draw_phys; };
Debug.inputs.f1.doc = "Draw physics debugging aids.";
Debug.inputs.f3 = function() { Debug.draw_bb = !Debug.draw_bb; };
Debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
Debug.inputs.f4 = function() {
Debug.draw_names = !Debug.draw_names;
Debug.draw_gizmos = !Debug.draw_gizmos;
};
debug.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
Debug.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
debug.gif = {
Debug.Options = {};
Debug.Options.gif = {
w: 640, /* Max width */
h: 480, /* Max height */
stretch: false, /* True if you want to stretch */
@ -146,29 +195,29 @@ debug.gif = {
},
};
debug.inputs.f8 = function() {
Debug.inputs.f8 = function() {
var now = new Date();
debug.gif.file = now.toISOString() + ".gif";
debug.gif.start();
Debug.Options.gif.file = now.toISOString() + ".gif";
Debug.Options.gif.start();
};
debug.inputs.f9 = function() {
debug.gif.stop();
Debug.inputs.f9 = function() {
Debug.Options.gif.stop();
}
debug.inputs.f10 = function() { time.timescale = 0.1; };
debug.inputs.f10.doc = "Toggle timescale to 1/10.";
debug.inputs.f10.released = function () { time.timescale = 1.0; };
debug.inputs.f12 = function() { GUI.defaults.debug = !GUI.defaults.debug; console.warn("GUI toggle debug");};
debug.inputs.f12.doc = "Toggle drawing GUI debugging aids.";
Debug.inputs.f10 = function() { time.timescale = 0.1; };
Debug.inputs.f10.doc = "Toggle timescale to 1/10.";
Debug.inputs.f10.released = function () { time.timescale = 1.0; };
Debug.inputs.f12 = function() { GUI.defaults.debug = !GUI.defaults.debug; console.warn("GUI toggle debug");};
Debug.inputs.f12.doc = "Toggle drawing GUI debugging aids.";
debug.inputs['M-1'] = render.normal;
debug.inputs['M-2'] = render.wireframe;
Debug.inputs['M-1'] = render.normal;
Debug.inputs['M-2'] = render.wireframe;
debug.inputs['C-M-f'] = function() {};
debug.inputs['C-M-f'].doc = "Enter camera fly mode.";
Debug.inputs['C-M-f'] = function() {};
Debug.inputs['C-M-f'].doc = "Enter camera fly mode.";
debug.api = {};
debug.api.doc_entry = function(obj, key)
Debug.api = {};
Debug.api.doc_entry = function(obj, key)
{
if (typeof key !== 'string') {
console.warn("Cannot print a key that isn't a string.");
@ -204,7 +253,7 @@ ${doc}
`;
}
debug.api.print_doc = function(name)
Debug.api.print_doc = function(name)
{
var obj = name;
if (typeof name === 'string') {
@ -237,24 +286,14 @@ debug.api.print_doc = function(name)
if (key === 'doc') continue;
if (key === 'toString') continue;
mdoc += debug.api.doc_entry(obj, key) + "\n";
mdoc += Debug.api.doc_entry(obj, key) + "\n";
}
return mdoc;
}
debug.log = {};
debug.log.time = function(fn, name, avg=0)
{
debug.log.time[name] ??= [];
var start = profile.now();
fn();
debug.log.time[name].push(profile.now()-start);
}
return {
debug,
Debug,
Gizmos,
assert
}

View file

@ -6,8 +6,8 @@
window.mode = window.modetypes.full;
game.loadurs();
player[0].control(debug);
Register.gui.register(debug.draw, debug);
player[0].control(Debug);
Register.gui.register(Debug.draw, Debug);
var editor = {
toString() { return "editor"; },
@ -391,7 +391,7 @@ var editor = {
wh[0] = Math.abs(endpos[0] - this.sel_start[0]);
wh[1] = Math.abs(endpos[1] - this.sel_start[1]);
var bb = bbox.fromcwh(c,wh);
render.boundingbox(bb, Color.Editor.select.alpha(0.1));
Debug.boundingbox(bb, Color.Editor.select.alpha(0.1));
render.line(bbox.topoints(bb).wrapped(1), Color.white);
}
},
@ -489,7 +489,7 @@ var editor = {
GUI.text("lock", obj,screenpos());
});
render.grid(1, editor.grid_size, Color.Editor.grid.alpha(0.3));
Debug.draw_grid(1, editor.grid_size, Color.Editor.grid.alpha(0.3));
var startgrid = window.screen2world([-20,0]).map(function(x) { return Math.snap(x, editor.grid_size); });
var endgrid = window.screen2world([window.width, window.height]);
@ -518,8 +518,8 @@ var editor = {
},
ed_debug() {
if (!debug.phys_drawing)
this.selectlist.forEach(function(x) { debug.draw_obj_phys(x); });
if (!Debug.phys_drawing)
this.selectlist.forEach(function(x) { Debug.draw_obj_phys(x); });
},
killring: [],
@ -2020,7 +2020,7 @@ if (io.exists("editor.config"))
/* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */
sim.pause();
window.editor = true;
debug.draw_phys = true;
Debug.draw_phys = true;
return {
editor

View file

@ -88,7 +88,7 @@ function eval_env(script, env, file)
{
env ??= {};
file ??= "SCRIPT";
console.spam(`eval ${file}`);
console.info(`eval ${file}`);
script = `(function() { ${script}; }).call(this);\n`;
return os.eval_env(file,env,script);
}
@ -102,10 +102,10 @@ global.check_registers = function(obj)
obj.timers.push(Register.physupdate.register(obj.physupdate.bind(obj)));
if (typeof obj.collide === 'function')
physics.collide_begin(obj.collide.bind(obj), obj.body);
register_collide(0, obj.collide.bind(obj), obj.body);
if (typeof obj.separate === 'function')
physics.collide_separate(obj.separate.bind(obj), obj.body);
register_collide(3,obj.separate.bind(obj), obj.body);
if (typeof obj.draw === 'function')
obj.timers.push(Register.draw.register(obj.draw.bind(obj), obj));
@ -146,7 +146,7 @@ global.obscure('global');
global.mixin("scripts/render.js");
global.mixin("scripts/debug.js");
var frame_t = profile.secs(profile.now());
var frame_t = profile.secs();
var updateMS = 1/60;
var physMS = 1/60;
@ -170,8 +170,8 @@ game.engine_start = function(s) {
function process()
{
var dt = profile.secs(profile.now()) - frame_t;
frame_t = profile.secs(profile.now());
var dt = profile.secs() - frame_t;
frame_t = profile.secs();
prosperon.appupdate(dt);
prosperon.emitters_step(dt);
@ -196,7 +196,7 @@ function process()
render.models();
render.emitters();
prosperon.draw();
debug.draw();
if (Debug.draw_phys) game.all_objects(function(o) { debug.draw_gameobject(o); });
render.flush();
render.pass();
prosperon.gui();
@ -215,7 +215,24 @@ var eachobj = function(obj,fn)
eachobj(obj.objects[o],fn);
}
game.all_objects = function(fn) { eachobj(world,fn); };
Object.assign(game, {
all_objects(fn) { eachobj(world,fn); },
/* Returns a list of objects by name */
find(name) {
},
/* Return a list of objects derived from a specific prototype */
find_proto(proto) {
},
/* List of all objects spawned that have a specific tag */
find_tag(tag){
},
});
game.doc = {};
game.doc.object = "Returns the entity belonging to a given id.";
@ -286,10 +303,7 @@ prosperon.touchpress = function(touches){};
prosperon.touchrelease = function(touches){};
prosperon.touchmove = function(touches){};
prosperon.clipboardpaste = function(str){};
prosperon.quit = function(){
for (var i in debug.log.time)
console.warn(debug.log.time[i].map(x=>profile.ms(x)));
};
prosperon.quit = function(){};
global.mixin("scripts/input.js");
global.mixin("scripts/std.js");
@ -345,7 +359,7 @@ var Register = {
fn = fn.bind(obj);
fns.push(fn);
return function() {
console.spam(`removed from ${name}.`);
console.info(`removed from ${name}.`);
fns.remove(fn);
};
}
@ -429,7 +443,7 @@ global.mixin("scripts/actor.js");
global.mixin("scripts/entity.js");
function world_start() {
gameobject.body = os.make_gameobject();
gameobject.body = make_gameobject();
gameobject.body.setref(gameobject);
console.info("START WORLD");

View file

@ -316,10 +316,8 @@ var gameobject = {
}
}
console.info(`Creating entity of type ${ent.ur}`);
Object.mixin(ent, gameobject_impl);
ent.body = os.make_gameobject();
ent.body = make_gameobject();
ent.warp_layer = [true];
ent.phys = 2;
ent.components = {};
@ -361,7 +359,7 @@ var gameobject = {
check_registers(ent);
ent.components.forEach(function(x) {
if (typeof x.collide === 'function')
physics.collide_shape(x.collide.bind(x), ent.body, x.shape);
register_collide(1, x.collide.bind(x), ent.body, x.shape);
});
@ -597,13 +595,12 @@ var gameobject = {
kill() {
if (this.__kill) return;
this.__kill = true;
console.info(`Killing entity of type ${this.ur}`);
this.timers.forEach(t => t());
this.timers = [];
Event.rm_obj(this);
Player.do_uncontrol(this);
physics.collide_rm(this.body);
register_collide(2, undefined, this.body);
if (this.master) {
this.master.remove_obj(this);
@ -692,7 +689,7 @@ gameobject.doc = {
scale: "Scale of the object, relative to its master.",
flipx: "Check if the object is flipped on its x axis.",
flipy: "Check if the object is flipped on its y axis.",
elasticity: `When two objects collide, their elasticities are multiplied together. Their velocities are then multiplied by this value to find their resultant velocities.`,
elasticity: `When two objects collide, their elasticities are multiplied together. Their velocities are then multiplied by this value to find their resultant velocities.`,
friction: `When one object touches another, friction slows them down.`,
mass: `The higher the mass of the object, the less forces will affect it.`,
phys: `Set to 0, 1, or 2, representing dynamic, kinematic, and static.`,
@ -758,6 +755,7 @@ ur {
/* Apply an ur u to an entity e */
/* u is given as */
function apply_ur(u, e) {
console.info(`Applying ur named ${u}.`);
if (typeof u !== 'string') {
console.warn("Must give u as a string.");
return;

View file

@ -85,18 +85,7 @@ render.arrow = function(start, end, color, wingspan, wingangle) {
render.line([start,end],color);
render.line(wing1,color);
render.line(wing2,color);
};
render.coordinate = function(pos, size, color) {
color ??= Color.white;
GUI.text(JSON.stringify(pos.map(p=>Math.round(p))), pos, size, color);
render.point(pos, 2, color);
}
render.boundingbox = function(bb, color) {
color ??= Color.white;
cmd_points(0, bbox.topoints(bb), color);
}
};
render.rectangle = function(lowerleft, upperright, color) {
var points = [lowerleft, lowerleft.add([upperright.x-lowerleft.x,0]), upperright, lowerleft.add([0,upperright.y-lowerleft.y])];

View file

@ -422,7 +422,7 @@ Cmdline.register_order("api", function(obj) {
}
load("scripts/editor.js");
var api = debug.api.print_doc(obj[0]);
var api = Debug.api.print_doc(obj[0]);
if (!api)
return;

View file

@ -105,10 +105,10 @@ void go_shape_moi(cpBody *body, cpShape *shape, gameobject *go) {
}
void gameobject_apply(gameobject *go) {
cpBodySetType(go->body, go->phys);
cpBodySetType(go->body, go->bodytype);
cpBodyEachShape(go->body, go_shape_apply, go);
if (go->phys == CP_BODY_TYPE_DYNAMIC) {
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) {
cpBodySetMass(go->body, go->mass);
cpBodySetMoment(go->body, 0.f);
cpBodyEachShape(go->body, go_shape_moi, go);
@ -149,7 +149,7 @@ gameobject *MakeGameobject() {
gameobject *ngo = malloc(sizeof(*ngo));
gameobject go = {
.scale = (HMM_Vec3){1.f,1.f,1.f},
.phys = CP_BODY_TYPE_STATIC,
.bodytype = CP_BODY_TYPE_STATIC,
.maxvelocity = INFINITY,
.maxangularvelocity = INFINITY,
.mass = 1.f,
@ -230,5 +230,6 @@ void body_draw_shapes_dbg(cpBody *body, cpShape *shape, void *data) {
void gameobject_draw_debug(gameobject *go) {
if (!go || !go->body) return;
cpVect pos = cpBodyGetPosition(go->body);
cpBodyEachShape(go->body, body_draw_shapes_dbg, NULL);
}

View file

@ -27,7 +27,7 @@
}while(0)
struct gameobject {
cpBodyType phys;
cpBodyType bodytype;
cpBody *body; /* NULL if this object is dead; has 2d position and rotation, relative to global 0 */
HMM_Vec3 scale; /* local */
int next;
@ -41,6 +41,7 @@ struct gameobject {
unsigned int layer;
cpShapeFilter filter;
unsigned int warp_filter;
// warpmask warpmask;
struct phys_cbs cbs;
struct shape_cb *shape_cbs;
JSValue ref;

View file

@ -23,22 +23,28 @@
#include "resources.h"
#include <sokol/sokol_time.h>
#include <sokol/sokol_app.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include "nota.h"
#include "render.h"
#include "model.h"
#include "HandmadeMath.h"
#if (defined(_WIN32) || defined(__WIN32__))
#include <direct.h>
#define mkdir(x,y) _mkdir(x)
#endif
#include "nota.h"
#include "render.h"
#include "model.h"
#include "HandmadeMath.h"
#define countof(x) (sizeof(x)/sizeof((x)[0]))
static JSValue globalThis;
@ -46,36 +52,10 @@ static JSValue globalThis;
static JSClassID js_ptr_id;
static JSClassDef js_ptr_class = { "POINTER" };
JSValue str2js(const char *c) { return JS_NewString(js, c); }
const char *js2str(JSValue v) {
return JS_ToCString(js, v);
}
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define MIST_FUNC_DEF(TYPE, FN, LEN) MIST_CFUNC_DEF(#FN, LEN, js_##TYPE##_##FN)
#define MIST_FN_STR(NAME) MIST_CFUNC_DEF(#NAME, 1, js_##NAME)
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, ID##_get_##ENTRY, ID##_set_##ENTRY)
#define GGETSET_ADD(ENTRY)
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
{FN;} \
return JS_UNDEFINED; \
} \
#define JSC_RET(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
FN; } \
#define QJSCLASS(TYPE)\
static JSClassID js_ ## TYPE ## _id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
YughSpam("Freeing " #TYPE " at %p", n); \
TYPE##_free(n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
@ -84,7 +64,6 @@ static JSClassDef js_##TYPE##_class = {\
static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
static JSValue TYPE##2js(TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
YughSpam("Created " #TYPE " at %p", n); \
JS_SetOpaque(j,n);\
return j; }\
@ -152,15 +131,6 @@ JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
int js2bool(JSValue v) { return JS_ToBool(js, v); }
JSValue bool2js(int b) { return JS_NewBool(js,b); }
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;
}
JSValue gos2ref(gameobject **go)
{
JSValue array = JS_NewArray(js);
@ -177,8 +147,20 @@ JSValue js_getpropstr(JSValue v, const char *str)
return p;
}
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;
}
static inline cpBody *js2body(JSValue v) { return js2gameobject(v)->body; }
JSValue str2js(const char *c) { return JS_NewString(js, c); }
const char *js2str(JSValue v) {
return JS_ToCString(js, v);
}
JSValue strarr2js(char **c)
{
@ -585,34 +567,118 @@ void gameobject_add_shape_collider(gameobject *go, JSValue fn, struct phys2d_sha
arrpush(go->shape_cbs, shapecb);
}
struct phys2d_circle *js2circle2d(JSValue v) { return js2ptr(v); }
JSValue duk_register_collide(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]);
gameobject *go = js2gameobject(argv[2]);
JSValue fn = argv[1];
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))
switch (cmd) {
case 0:
go->cbs.begin = JS_DupValue(js,fn);
break;
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),
};
case 1:
gameobject_add_shape_collider(go, JS_DupValue(js,fn), js2ptr(argv[3]));
break;
struct phys2d_poly *js2poly2d(JSValue v) { return js2ptr(v); }
case 2:
phys2d_rm_go_handlers(go);
break;
JSC_CCALL(poly2d_setverts,
struct phys2d_poly *p = js2poly2d(argv[0]);
HMM_Vec2 *v = js2cpvec2arr(argv[1]);
phys2d_poly_setverts(p,v);
arrfree(v);
)
case 3:
go->cbs.separate = JS_DupValue(js,fn);
break;
}
static const JSCFunctionListEntry js_poly2d_funcs[] = {
MIST_FUNC_DEF(poly2d, setverts, 2),
};
return JS_UNDEFINED;
}
JSValue duk_make_circle2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
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));
return circleval;
}
JSValue duk_make_model(JSContext *js, JSValue this, int argc, JSValue *argv)
{
gameobject *go = js2gameobject(argv[0]);
struct drawmodel *dm = make_drawmodel(go);
JSValue ret = JS_NewObject(js);
js_setprop_str(ret, "id", ptr2js(dm));
return ret;
}
JSValue duk_cmd_circle2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]);
struct phys2d_circle *circle = js2ptr(argv[1]);
if (!circle) return JS_UNDEFINED;
switch (cmd) {
case 0:
circle->radius = js2number(argv[2]);
break;
case 1:
circle->offset = js2vec2(argv[2]);
break;
case 2:
return number2js(circle->radius);
case 3:
return vec22js(circle->offset);
}
phys2d_shape_apply(&circle->shape);
return JS_UNDEFINED;
}
JSValue duk_make_poly2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
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));
return polyval;
}
JSValue duk_cmd_poly2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]);
struct phys2d_poly *poly = js2ptr(argv[1]);
HMM_Vec2 *v1 = NULL;
if (!poly) return JS_UNDEFINED;
switch (cmd) {
case 0:
v1 = js2cpvec2arr(argv[2]);
phys2d_poly_setverts(poly, v1);
break;
}
if (v1) arrfree(v1);
return JS_UNDEFINED;
}
JSValue duk_make_edge2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
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));
return edgeval;
}
#define GETSET_PAIR(ID, ENTRY, TYPE) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
@ -662,6 +728,26 @@ GETSET_PAIR(warp_gravity, spherical, bool)
GETSET_PAIR(warp_gravity, mask, bitmask)
GETSET_PAIR(warp_gravity, planar_force, vec3)
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define MIST_FUNC_DEF(TYPE, FN, LEN) MIST_CFUNC_DEF(#FN, LEN, js_##TYPE##_##FN)
#define MIST_FN_STR(NAME) MIST_CFUNC_DEF(#NAME, 1, js_##NAME)
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, ID##_get_##ENTRY, ID##_set_##ENTRY)
#define GGETSET_ADD(ENTRY)
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
{FN;} \
return JS_UNDEFINED; \
} \
#define JSC_RET(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
FN; } \
static const JSCFunctionListEntry js_warp_gravity_funcs [] = {
CGETSET_ADD(warp_gravity, strength),
CGETSET_ADD(warp_gravity, decay),
@ -788,47 +874,6 @@ JSC_CCALL(os_sprite,
return sprite2js(sprite_make());
)
JSC_CCALL(os_make_gameobject, return gameobject2js(MakeGameobject()))
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));
return circleval;
)
JSC_CCALL(os_make_model,
gameobject *go = js2gameobject(argv[0]);
struct drawmodel *dm = make_drawmodel(go);
JSValue ret = JS_NewObject(js);
js_setprop_str(ret, "id", ptr2js(dm));
return ret;
)
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));
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));
return edgeval;
)
static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os,sprite,1),
MIST_FUNC_DEF(os, cwd, 0),
@ -839,11 +884,6 @@ static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, gc, 0),
MIST_FUNC_DEF(os, capture, 5),
MIST_FUNC_DEF(os, eval_env, 3),
MIST_FUNC_DEF(os, make_gameobject, 0),
MIST_FUNC_DEF(os, make_circle2d, 1),
MIST_FUNC_DEF(os, make_poly2d, 1),
MIST_FUNC_DEF(os, make_edge2d, 1),
MIST_FUNC_DEF(os, make_model, 2),
};
JSC_CCALL(render_normal, opengl_rendermode(LIT))
@ -1306,13 +1346,10 @@ JSC_CCALL(physics_closest_point,
JSC_CCALL(physics_make_gravity, return warp_gravity2js(warp_gravity_make()))
JSC_CCALL(physics_make_damp, return warp_damp2js(warp_damp_make()))
JSC_CCALL(physics_collide_begin, js2gameobject(argv[1])->cbs.begin = JS_DupValue(js,argv[0]))
JSC_CCALL(physics_collide_rm, phys2d_rm_go_handlers(js2gameobject(argv[0])))
JSC_CCALL(physics_collide_separate, js2gameobject(argv[1])->cbs.separate = JS_DupValue(js,argv[0]))
JSC_CCALL(physics_collide_shape, gameobject_add_shape_collider(js2gameobject(argv[1]), JS_DupValue(js,argv[0]), js2ptr(argv[2])))
JSC_CCALL(physics_edge_thickness, return number2js(((struct phys2d_edge*)js2ptr(argv[0]))->thickness))
static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics,edge_thickness,1),
MIST_FUNC_DEF(physics, sgscale, 2),
MIST_FUNC_DEF(physics, set_cat_mask, 2),
MIST_FUNC_DEF(physics, box_query, 2),
@ -1322,10 +1359,6 @@ static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics, closest_point, 3),
MIST_FUNC_DEF(physics, make_damp, 0),
MIST_FUNC_DEF(physics, make_gravity, 0),
MIST_FUNC_DEF(physics, collide_begin, 2),
MIST_FUNC_DEF(physics, collide_rm, 1),
MIST_FUNC_DEF(physics, collide_separate, 2),
MIST_FUNC_DEF(physics, collide_shape, 3)
};
static const JSCFunctionListEntry js_emitter_funcs[] = {
@ -1480,8 +1513,7 @@ GETSET_PAIR(gameobject, scale, vec3)
BODY_GETSET(velocity, Velocity, cvec2)
BODY_GETSET(angularvelocity, AngularVelocity, number)
BODY_GETSET(moi, Moment, number)
//BODY_GETSET(phys, Type, number)
GETSET_PAIR(gameobject, phys, number)
BODY_GETSET(phys, Type, number)
BODY_GETSET(torque, Torque, number)
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)))
@ -1503,6 +1535,8 @@ GETSET_PAIR(gameobject, warp_filter, bitmask)
GETSET_PAIR(gameobject, drawlayer, number)
JSC_CCALL(gameobject_setref, js2gameobject(this)->ref = argv[0]);
JSValue duk_make_gameobject(JSContext *js, JSValue this) { return gameobject2js(MakeGameobject()); }
static const JSCFunctionListEntry js_gameobject_funcs[] = {
CGETSET_ADD(gameobject, f),
CGETSET_ADD(gameobject, e),
@ -1703,21 +1737,31 @@ static const JSCFunctionListEntry js_constraint_funcs[] = {
CGETSET_ADD(constraint, collide),
};
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))
JSValue duk_cmd_edge2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]);
struct phys2d_edge *edge = js2ptr(argv[1]);
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),
};
if (!edge) {
YughError("Attempted to do a cmd on edge %p. Not found.", edge);
return JS_UNDEFINED;
}
HMM_Vec2 *v1 = NULL;
switch (cmd) {
case 0:
v1 = js2cpvec2arr(argv[2]);
phys2d_edge_update_verts(edge,v1);
break;
case 1:
edge->thickness = js2number(argv[2]);
break;
}
if (v1) arrfree(v1);
return JS_UNDEFINED;
}
JSValue duk_inflate_cpv(JSContext *js, JSValue this, int argc, JSValue *argv) {
HMM_Vec2 *points = js2cpvec2arr(argv[0]);
@ -1733,29 +1777,36 @@ JSValue duk_inflate_cpv(JSContext *js, JSValue this, int argc, JSValue *argv) {
const char *STRTEST = "TEST STRING";
JSC_CCALL(performance_barecall,)
JSC_CCALL(performance_unpack_num, int i = js2number(argv[0]))
JSC_CCALL(performance_unpack_array, js2cpvec2arr(argv[0]))
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)
};
JSValue duk_performance(JSContext *js, JSValue this, int argc, JSValue *argv)
{
int cmd = js2number(argv[0]);
switch(cmd) {
case 0:
break;
case 1:
js2number(argv[1]);
break;
case 2:
js2cpvec2arr(argv[1]);
break;
case 3:
return number2js(1.0);
case 4:
js2str(argv[1]);
break;
case 5:
jsfloat2vec(argv[1]);
break;
case 6:
return JS_NewStringLen(js, STRTEST, sizeof(*STRTEST));
case 7:
for (int i = 0; i < js2number(argv[2]); i++)
script_call_sym(argv[1],0,NULL);
script_call_sym(argv[3],0,NULL);
break;
}
return JS_UNDEFINED;
}
JSValue js_nota_encode(JSContext *js, JSValue this, int argc, JSValue *argv)
{
@ -1789,7 +1840,18 @@ static const JSCFunctionListEntry js_nota_funcs[] = {
void ffi_load() {
globalThis = JS_GetGlobalObject(js);
DUK_FUNC(make_gameobject, 0)
DUK_FUNC(make_circle2d, 1)
DUK_FUNC(cmd_circle2d, 6)
DUK_FUNC(make_poly2d, 1)
DUK_FUNC(cmd_poly2d, 6)
DUK_FUNC(make_edge2d, 3)
DUK_FUNC(cmd_edge2d, 6)
DUK_FUNC(make_model,2);
DUK_FUNC(register_collide, 6)
DUK_FUNC(inflate_cpv, 3)
DUK_FUNC(performance, 2)
QJSCLASSPREP(ptr);
QJSCLASSPREP_FUNCS(gameobject);
@ -1826,11 +1888,6 @@ void ffi_load() {
QJSGLOBALCLASS(joint);
QJSGLOBALCLASS(dspsound);
QJSGLOBALCLASS(pshape);
QJSGLOBALCLASS(performance);
QJSGLOBALCLASS(circle2d);
QJSGLOBALCLASS(poly2d);
QJSGLOBALCLASS(edge2d);
JS_SetPropertyStr(js, prosperon, "version", str2js(VER));
JS_SetPropertyStr(js, prosperon, "revision", str2js(COM));

View file

@ -147,6 +147,7 @@ dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int sam
self->pass = 0;
self->gain = 1;
self->id = node_count++;
YughSpam("Made node %d.", self->id);
return self;
}
@ -165,6 +166,7 @@ void node_free(dsp_node *node)
free(node->data);
}
YughSpam("Freed node %d.", node->id);
free(node);
pthread_mutex_unlock(&soundrun);
}