Compare commits

...

2 commits

12 changed files with 347 additions and 449 deletions

View file

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

View file

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

View file

@ -1,5 +1,4 @@
var Debug = { debug.fn_break = function(fn,obj) {
fn_break(fn, obj) {
if (typeof fn !== 'function') return; if (typeof fn !== 'function') return;
obj ??= globalThis; obj ??= globalThis;
@ -8,38 +7,19 @@ var Debug = {
fn(); fn();
}; };
obj[fn.name] = newfn; obj[fn.name] = newfn;
}, }
draw_grid(width, span, color) { debug.draw_phys = false;
color = color ? color : Color.green; debug.draw_bb = false;
render.grid(width,span,color); 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); });
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) 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) if (this.draw_gizmos)
game.all_objects(function(x) { game.all_objects(function(x) {
@ -49,22 +29,25 @@ var Debug = {
if (this.draw_names) if (this.draw_names)
game.all_objects(function(x) { 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.Options.gif.rec) { if (debug.gif.rec) {
GUI.text("REC", [0,40], 1); GUI.text("REC", [0,40], 1);
GUI.text(time.timecode(time.timenow() - Debug.Options.gif.start_time, Debug.Options.gif.fps), [0,30], 1); GUI.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0,30], 1);
} }
return;
if (sim.paused()) GUI.text("PAUSED", [0,0],1);
GUI.text(sim.playing() ? "PLAYING" GUI.text(sim.playing() ? "PLAYING"
: sim.stepping() ? : sim.stepping() ?
"STEP" : "STEP" :
sim.paused() ? sim.paused() ?
"PAUSED; EDITING" : "PAUSED; EDITING" :
"EDIT", [0, 0], 1); "EDIT", [0, 0], 1);
}, }
};
function assert(op, str) function assert(op, str)
{ {
@ -83,8 +66,7 @@ var Gizmos = {
}, },
}; };
Object.assign(profile, { profile.best_t = function(t) {
best_t(t) {
var qq = 'ns'; var qq = 'ns';
if (t > 1000) { if (t > 1000) {
t /= 1000; t /= 1000;
@ -95,8 +77,9 @@ Object.assign(profile, {
} }
} }
return `${t.toPrecision(4)} ${qq}`; return `${t.toPrecision(4)} ${qq}`;
}, }
cpu(fn, times, q) {
profile.cpu = function(fn, times, q) {
times ??= 1; times ??= 1;
q ??= "unnamed"; q ??= "unnamed";
var start = profile.now(); var start = profile.now();
@ -108,56 +91,24 @@ Object.assign(profile, {
var totalt = profile.best_t(elapsed); var totalt = profile.best_t(elapsed);
say(`profile [${q}]: ${profile.best_t(avgt)} average [${profile.best_t(totalt)} for ${times} loops]`); say(`profile [${q}]: ${profile.best_t(avgt)} average [${profile.best_t(totalt)} for ${times} loops]`);
}, }
time(fn) { profile.ms = function(t) { return t/1000000; }
var start = profile.now(); profile.secs = function(t) { return t/1000000000; }
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 */ /* These controls are available during editing, and during play of debug builds */
Debug.inputs = {}; debug.inputs = {};
Debug.inputs.f1 = function () { Debug.draw_phys = !Debug.draw_phys; }; debug.inputs.f1 = function () { debug.draw_phys = !debug.draw_phys; };
Debug.inputs.f1.doc = "Draw physics debugging aids."; debug.inputs.f1.doc = "Draw physics debugging aids.";
Debug.inputs.f3 = function() { Debug.draw_bb = !Debug.draw_bb; }; debug.inputs.f3 = function() { debug.draw_bb = !debug.draw_bb; };
Debug.inputs.f3.doc = "Toggle drawing bounding boxes."; debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
Debug.inputs.f4 = function() { debug.inputs.f4 = function() {
Debug.draw_names = !Debug.draw_names; debug.draw_names = !debug.draw_names;
Debug.draw_gizmos = !Debug.draw_gizmos; 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.Options = {}; debug.gif = {
Debug.Options.gif = {
w: 640, /* Max width */ w: 640, /* Max width */
h: 480, /* Max height */ h: 480, /* Max height */
stretch: false, /* True if you want to stretch */ stretch: false, /* True if you want to stretch */
@ -195,29 +146,29 @@ Debug.Options.gif = {
}, },
}; };
Debug.inputs.f8 = function() { debug.inputs.f8 = function() {
var now = new Date(); var now = new Date();
Debug.Options.gif.file = now.toISOString() + ".gif"; debug.gif.file = now.toISOString() + ".gif";
Debug.Options.gif.start(); debug.gif.start();
}; };
Debug.inputs.f9 = function() { debug.inputs.f9 = function() {
Debug.Options.gif.stop(); debug.gif.stop();
} }
Debug.inputs.f10 = function() { time.timescale = 0.1; }; debug.inputs.f10 = function() { time.timescale = 0.1; };
Debug.inputs.f10.doc = "Toggle timescale to 1/10."; debug.inputs.f10.doc = "Toggle timescale to 1/10.";
Debug.inputs.f10.released = function () { time.timescale = 1.0; }; 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 = function() { GUI.defaults.debug = !GUI.defaults.debug; console.warn("GUI toggle debug");};
Debug.inputs.f12.doc = "Toggle drawing GUI debugging aids."; debug.inputs.f12.doc = "Toggle drawing GUI debugging aids.";
Debug.inputs['M-1'] = render.normal; debug.inputs['M-1'] = render.normal;
Debug.inputs['M-2'] = render.wireframe; debug.inputs['M-2'] = render.wireframe;
Debug.inputs['C-M-f'] = function() {}; debug.inputs['C-M-f'] = function() {};
Debug.inputs['C-M-f'].doc = "Enter camera fly mode."; debug.inputs['C-M-f'].doc = "Enter camera fly mode.";
Debug.api = {}; debug.api = {};
Debug.api.doc_entry = function(obj, key) debug.api.doc_entry = function(obj, key)
{ {
if (typeof key !== 'string') { if (typeof key !== 'string') {
console.warn("Cannot print a key that isn't a string."); console.warn("Cannot print a key that isn't a string.");
@ -253,7 +204,7 @@ ${doc}
`; `;
} }
Debug.api.print_doc = function(name) debug.api.print_doc = function(name)
{ {
var obj = name; var obj = name;
if (typeof name === 'string') { if (typeof name === 'string') {
@ -286,14 +237,24 @@ Debug.api.print_doc = function(name)
if (key === 'doc') continue; if (key === 'doc') continue;
if (key === 'toString') continue; if (key === 'toString') continue;
mdoc += Debug.api.doc_entry(obj, key) + "\n"; mdoc += debug.api.doc_entry(obj, key) + "\n";
} }
return mdoc; 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 { return {
Debug, debug,
Gizmos, Gizmos,
assert assert
} }

View file

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

View file

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

View file

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

View file

@ -87,6 +87,17 @@ render.arrow = function(start, end, color, wingspan, wingangle) {
render.line(wing2,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) { render.rectangle = function(lowerleft, upperright, color) {
var points = [lowerleft, lowerleft.add([upperright.x-lowerleft.x,0]), upperright, lowerleft.add([0,upperright.y-lowerleft.y])]; var points = [lowerleft, lowerleft.add([upperright.x-lowerleft.x,0]), upperright, lowerleft.add([0,upperright.y-lowerleft.y])];
render.poly(points, color); render.poly(points, color);

View file

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

View file

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

View file

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

View file

@ -23,28 +23,22 @@
#include "resources.h" #include "resources.h"
#include <sokol/sokol_time.h> #include <sokol/sokol_time.h>
#include <sokol/sokol_app.h> #include <sokol/sokol_app.h>
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include "nota.h"
#include "render.h"
#include "model.h"
#include "HandmadeMath.h"
#if (defined(_WIN32) || defined(__WIN32__)) #if (defined(_WIN32) || defined(__WIN32__))
#include <direct.h> #include <direct.h>
#define mkdir(x,y) _mkdir(x) #define mkdir(x,y) _mkdir(x)
#endif #endif
#include "nota.h"
#include "render.h"
#include "model.h"
#include "HandmadeMath.h"
#define countof(x) (sizeof(x)/sizeof((x)[0])) #define countof(x) (sizeof(x)/sizeof((x)[0]))
static JSValue globalThis; static JSValue globalThis;
@ -52,10 +46,36 @@ static JSValue globalThis;
static JSClassID js_ptr_id; static JSClassID js_ptr_id;
static JSClassDef js_ptr_class = { "POINTER" }; 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)\ #define QJSCLASS(TYPE)\
static JSClassID js_ ## TYPE ## _id;\ static JSClassID js_ ## TYPE ## _id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\ static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\ TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
YughSpam("Freeing " #TYPE " at %p", n); \
TYPE##_free(n);}\ TYPE##_free(n);}\
static JSClassDef js_##TYPE##_class = {\ static JSClassDef js_##TYPE##_class = {\
#TYPE,\ #TYPE,\
@ -64,6 +84,7 @@ static JSClassDef js_##TYPE##_class = {\
static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\ static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
static JSValue TYPE##2js(TYPE *n) { \ static JSValue TYPE##2js(TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\ JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
YughSpam("Created " #TYPE " at %p", n); \
JS_SetOpaque(j,n);\ JS_SetOpaque(j,n);\
return j; }\ return j; }\
@ -131,6 +152,15 @@ JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
int js2bool(JSValue v) { return JS_ToBool(js, v); } int js2bool(JSValue v) { return JS_ToBool(js, v); }
JSValue bool2js(int b) { return JS_NewBool(js,b); } 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 gos2ref(gameobject **go)
{ {
JSValue array = JS_NewArray(js); JSValue array = JS_NewArray(js);
@ -147,20 +177,8 @@ JSValue js_getpropstr(JSValue v, const char *str)
return p; 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; } 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) JSValue strarr2js(char **c)
{ {
@ -567,118 +585,34 @@ void gameobject_add_shape_collider(gameobject *go, JSValue fn, struct phys2d_sha
arrpush(go->shape_cbs, shapecb); arrpush(go->shape_cbs, shapecb);
} }
JSValue duk_register_collide(JSContext *js, JSValue this, int argc, JSValue *argv) { struct phys2d_circle *js2circle2d(JSValue v) { return js2ptr(v); }
int cmd = js2number(argv[0]);
gameobject *go = js2gameobject(argv[2]);
JSValue fn = argv[1];
switch (cmd) { JSC_CCALL(circle2d_set_radius, js2circle2d(argv[0])->radius = js2number(argv[1]))
case 0: JSC_CCALL(circle2d_get_radius, return number2js(js2circle2d(argv[0])->radius))
go->cbs.begin = JS_DupValue(js,fn); JSC_CCALL(circle2d_set_offset, js2circle2d(argv[0])->offset = js2vec2(argv[1]))
break; JSC_CCALL(circle2d_get_offset, return vec22js(js2circle2d(argv[0])->offset))
JSC_CCALL(circle2d_sync, phys2d_shape_apply(&js2circle2d(argv[0])->shape))
case 1: static const JSCFunctionListEntry js_circle2d_funcs[] = {
gameobject_add_shape_collider(go, JS_DupValue(js,fn), js2ptr(argv[3])); MIST_FUNC_DEF(circle2d, set_radius, 2),
break; 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 2: struct phys2d_poly *js2poly2d(JSValue v) { return js2ptr(v); }
phys2d_rm_go_handlers(go);
break;
case 3: JSC_CCALL(poly2d_setverts,
go->cbs.separate = JS_DupValue(js,fn); struct phys2d_poly *p = js2poly2d(argv[0]);
break; HMM_Vec2 *v = js2cpvec2arr(argv[1]);
} phys2d_poly_setverts(p,v);
arrfree(v);
)
return JS_UNDEFINED; static const JSCFunctionListEntry js_poly2d_funcs[] = {
} MIST_FUNC_DEF(poly2d, setverts, 2),
};
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) \ #define GETSET_PAIR(ID, ENTRY, TYPE) \
JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \ JSValue ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
@ -728,26 +662,6 @@ GETSET_PAIR(warp_gravity, spherical, bool)
GETSET_PAIR(warp_gravity, mask, bitmask) GETSET_PAIR(warp_gravity, mask, bitmask)
GETSET_PAIR(warp_gravity, planar_force, vec3) 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 [] = { static const JSCFunctionListEntry js_warp_gravity_funcs [] = {
CGETSET_ADD(warp_gravity, strength), CGETSET_ADD(warp_gravity, strength),
CGETSET_ADD(warp_gravity, decay), CGETSET_ADD(warp_gravity, decay),
@ -874,6 +788,47 @@ JSC_CCALL(os_sprite,
return sprite2js(sprite_make()); 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[] = { static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os,sprite,1), MIST_FUNC_DEF(os,sprite,1),
MIST_FUNC_DEF(os, cwd, 0), MIST_FUNC_DEF(os, cwd, 0),
@ -884,6 +839,11 @@ static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, gc, 0), MIST_FUNC_DEF(os, gc, 0),
MIST_FUNC_DEF(os, capture, 5), MIST_FUNC_DEF(os, capture, 5),
MIST_FUNC_DEF(os, eval_env, 3), 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)) JSC_CCALL(render_normal, opengl_rendermode(LIT))
@ -1346,10 +1306,13 @@ JSC_CCALL(physics_closest_point,
JSC_CCALL(physics_make_gravity, return warp_gravity2js(warp_gravity_make())) 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_make_damp, return warp_damp2js(warp_damp_make()))
JSC_CCALL(physics_edge_thickness, return number2js(((struct phys2d_edge*)js2ptr(argv[0]))->thickness))
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])))
static const JSCFunctionListEntry js_physics_funcs[] = { static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics,edge_thickness,1),
MIST_FUNC_DEF(physics, sgscale, 2), MIST_FUNC_DEF(physics, sgscale, 2),
MIST_FUNC_DEF(physics, set_cat_mask, 2), MIST_FUNC_DEF(physics, set_cat_mask, 2),
MIST_FUNC_DEF(physics, box_query, 2), MIST_FUNC_DEF(physics, box_query, 2),
@ -1359,6 +1322,10 @@ static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics, closest_point, 3), MIST_FUNC_DEF(physics, closest_point, 3),
MIST_FUNC_DEF(physics, make_damp, 0), MIST_FUNC_DEF(physics, make_damp, 0),
MIST_FUNC_DEF(physics, make_gravity, 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[] = { static const JSCFunctionListEntry js_emitter_funcs[] = {
@ -1513,7 +1480,8 @@ GETSET_PAIR(gameobject, scale, vec3)
BODY_GETSET(velocity, Velocity, cvec2) BODY_GETSET(velocity, Velocity, cvec2)
BODY_GETSET(angularvelocity, AngularVelocity, number) BODY_GETSET(angularvelocity, AngularVelocity, number)
BODY_GETSET(moi, Moment, number) BODY_GETSET(moi, Moment, number)
BODY_GETSET(phys, Type, number) //BODY_GETSET(phys, Type, number)
GETSET_PAIR(gameobject, phys, number)
BODY_GETSET(torque, Torque, 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_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, cpBodyApplyForceAtWorldPoint(js2gameobject(this)->body, js2vec2(argv[0]).cp, cpBodyGetPosition(js2gameobject(this)->body)))
@ -1535,8 +1503,6 @@ GETSET_PAIR(gameobject, warp_filter, bitmask)
GETSET_PAIR(gameobject, drawlayer, number) GETSET_PAIR(gameobject, drawlayer, number)
JSC_CCALL(gameobject_setref, js2gameobject(this)->ref = argv[0]); 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[] = { static const JSCFunctionListEntry js_gameobject_funcs[] = {
CGETSET_ADD(gameobject, f), CGETSET_ADD(gameobject, f),
CGETSET_ADD(gameobject, e), CGETSET_ADD(gameobject, e),
@ -1737,31 +1703,21 @@ static const JSCFunctionListEntry js_constraint_funcs[] = {
CGETSET_ADD(constraint, collide), CGETSET_ADD(constraint, collide),
}; };
JSValue duk_cmd_edge2d(JSContext *js, JSValue this, int argc, JSValue *argv) { struct phys2d_edge *js2edge2d(JSValue v) { return js2ptr(v); }
int cmd = js2number(argv[0]); JSC_CCALL(edge2d_setverts,
struct phys2d_edge *edge = js2ptr(argv[1]); 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))
if (!edge) { static const JSCFunctionListEntry js_edge2d_funcs[] = {
YughError("Attempted to do a cmd on edge %p. Not found.", edge); MIST_FUNC_DEF(edge2d, setverts, 2),
return JS_UNDEFINED; MIST_FUNC_DEF(edge2d, set_thickness, 2),
} MIST_FUNC_DEF(edge2d, get_thickness, 1),
};
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) { JSValue duk_inflate_cpv(JSContext *js, JSValue this, int argc, JSValue *argv) {
HMM_Vec2 *points = js2cpvec2arr(argv[0]); HMM_Vec2 *points = js2cpvec2arr(argv[0]);
@ -1777,36 +1733,29 @@ JSValue duk_inflate_cpv(JSContext *js, JSValue this, int argc, JSValue *argv) {
const char *STRTEST = "TEST STRING"; const char *STRTEST = "TEST STRING";
JSValue duk_performance(JSContext *js, JSValue this, int argc, JSValue *argv) JSC_CCALL(performance_barecall,)
{ JSC_CCALL(performance_unpack_num, int i = js2number(argv[0]))
int cmd = js2number(argv[0]); JSC_CCALL(performance_unpack_array, js2cpvec2arr(argv[0]))
switch(cmd) { JSC_CCALL(performance_pack_num, return number2js(1.0))
case 0: JSC_CCALL(performance_pack_string, return JS_NewStringLen(js, STRTEST, sizeof(*STRTEST)))
break; JSC_CCALL(performance_unpack_string, js2str(argv[0]))
case 1: JSC_CCALL(performance_unpack_32farr, jsfloat2vec(argv[0]))
js2number(argv[1]); JSC_CCALL(performance_call_fn_n,
break; for (int i = 0; i < js2number(argv[1]); i++)
case 2: script_call_sym(argv[0],0,NULL);
js2cpvec2arr(argv[1]); script_call_sym(argv[2],0,NULL);
break; )
case 3:
return number2js(1.0); static const JSCFunctionListEntry js_performance_funcs[] = {
case 4: MIST_FUNC_DEF(performance, barecall,0),
js2str(argv[1]); MIST_FUNC_DEF(performance, unpack_num, 1),
break; MIST_FUNC_DEF(performance, unpack_array, 1),
case 5: MIST_FUNC_DEF(performance, pack_num, 0),
jsfloat2vec(argv[1]); MIST_FUNC_DEF(performance, pack_string, 0),
break; MIST_FUNC_DEF(performance, unpack_string, 1),
case 6: MIST_FUNC_DEF(performance, unpack_32farr, 1),
return JS_NewStringLen(js, STRTEST, sizeof(*STRTEST)); MIST_FUNC_DEF(performance, call_fn_n, 3)
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) JSValue js_nota_encode(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
@ -1840,18 +1789,7 @@ static const JSCFunctionListEntry js_nota_funcs[] = {
void ffi_load() { void ffi_load() {
globalThis = JS_GetGlobalObject(js); 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(inflate_cpv, 3)
DUK_FUNC(performance, 2)
QJSCLASSPREP(ptr); QJSCLASSPREP(ptr);
QJSCLASSPREP_FUNCS(gameobject); QJSCLASSPREP_FUNCS(gameobject);
@ -1888,6 +1826,11 @@ void ffi_load() {
QJSGLOBALCLASS(joint); QJSGLOBALCLASS(joint);
QJSGLOBALCLASS(dspsound); QJSGLOBALCLASS(dspsound);
QJSGLOBALCLASS(pshape); QJSGLOBALCLASS(pshape);
QJSGLOBALCLASS(performance);
QJSGLOBALCLASS(circle2d);
QJSGLOBALCLASS(poly2d);
QJSGLOBALCLASS(edge2d);
JS_SetPropertyStr(js, prosperon, "version", str2js(VER)); JS_SetPropertyStr(js, prosperon, "version", str2js(VER));
JS_SetPropertyStr(js, prosperon, "revision", str2js(COM)); JS_SetPropertyStr(js, prosperon, "revision", str2js(COM));

View file

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