actor delays; fix sound playing

This commit is contained in:
John Alanbrook 2024-03-19 17:00:49 -05:00
parent 62a34c1e7c
commit 18c07e9f82
9 changed files with 144 additions and 106 deletions

View file

@ -43,10 +43,46 @@ actor.kill = function(){
actor.kill.doc = `Remove this actor and all its padawans from existence.`; actor.kill.doc = `Remove this actor and all its padawans from existence.`;
actor.interval = function(fn, seconds) {
var cur;
var stop = function() {
cur();
}
var f = function() {
fn.call(this);
cur = this.delay(f,seconds);
}
cur = this.delay(f,seconds);
return stop;
}
actor.delay = function(fn, seconds) { actor.delay = function(fn, seconds) {
var t = timer.delay(fn.bind(this), seconds); var that = this;
this.timers.push(t); var stop = function() {
return t; that.timers.remove(stop);
rm();
}
function execute() {
fn.call(that);
stop();
}
stop.remain = seconds;
stop.seconds = seconds;
function update(dt) {
stop.remain -= dt;
if (stop.remain <= 0)
execute();
}
var rm = Register.appupdate.register(update);
this.timers.push(stop);
return stop;
}; };
actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`; actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`;

View file

@ -308,10 +308,10 @@ var collider2d = Object.copy(component, {
kill() {}, /* No killing is necessary - it is done through the gameobject's kill */ kill() {}, /* No killing is necessary - it is done through the gameobject's kill */
impl: { impl: {
set sensor(x) { physics.shape_set_sensor(this.shape,x); }, set sensor(x) { pshape.set_sensor(this.shape,x); },
get sensor() { return physics.shape_get_sensor(this.shape); }, get sensor() { return pshape.get_sensor(this.shape); },
set enabled(x) { physics.shape_set_enabled(this.shape,x); }, set enabled(x) { pshape.set_enabled(this.shape,x); },
get enabled() { return physics.shape_get_enabled(this.shape); } get enabled() { return pshape.get_enabled(this.shape); }
}, },
}); });

View file

@ -196,6 +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); });
render.flush(); render.flush();
render.pass(); render.pass();
prosperon.gui(); prosperon.gui();
@ -207,10 +208,15 @@ function process()
game.timescale = 1; game.timescale = 1;
var eachobj = function(obj,fn)
{
fn(obj);
for (var o in obj.objects)
eachobj(obj.objects[o],fn);
}
Object.assign(game, { Object.assign(game, {
all_objects(fn) { all_objects(fn) { eachobj(world,fn); },
/* Wind down from Primum */
},
/* Returns a list of objects by name */ /* Returns a list of objects by name */
find(name) { find(name) {

View file

@ -247,7 +247,7 @@ var gameobject = {
cry(file) { cry(file) {
return; return;
this.crying = audio.sound.play(file, audio.sound.bus.sfx); this.crying = audio.play(file, audio.bus.sfx);
var killfn = () => { this.crying = undefined; var killfn = () => { this.crying = undefined;
console.warn("killed"); } console.warn("killed"); }
this.crying.hook = killfn; this.crying.hook = killfn;

View file

@ -1,32 +1,21 @@
var audio = {}; var audio = {};
var sound_pref = ['wav', 'flac', 'mp3', 'qoa']; audio.samplerate = dspsound.samplerate();
audio.play = function(file,bus) {
audio.sound = { bus ??= audio.bus.master;
bus: {},
samplerate: dspsound.samplerate,
sounds: [], /* array of loaded sound files */
play(file, bus) {
file = Resources.find_sound(file); file = Resources.find_sound(file);
if (!file) { if (!file) {
console.error(`Cannot play sound ${file}: does not exist.`); console.error(`Cannot play sound ${file}: does not exist.`);
return; return;
} }
var src = audio.dsp.source(file); var src = audio.dsp.source(file);
bus ??= audio.sound.bus.master;
src.plugin(bus); src.plugin(bus);
return src; return src;
}, }
audio.bus = {};
doc: { audio.bus.master = dspsound.master();
play: "Play the given file once.",
volume: "Set the volume. 0 is no sound and 100 is loudest."
},
};
audio.dsp = {}; audio.dsp = {};
Object.assign(audio.dsp, dspsound); audio.dsp = dspsound;
audio.dsp.mix = function(to) { audio.dsp.mix = function(to) {
var n = audio.dsp.mix(); var n = audio.dsp.mix();
@ -61,17 +50,15 @@ audio.dsp.doc = {
red: "Red noise" red: "Red noise"
}; };
Object.mixin(dspsound.master().__proto__, { Object.mixin(audio.bus.master.__proto__, {
get db() { return 20*Math.log10(Math.abs(this.volume)); }, get db() { return 20*Math.log10(Math.abs(this.volume)); },
set db(x) { x = Math.clamp(x,-100,0); this.volume = Math.pow(10, x/20); }, set db(x) { x = Math.clamp(x,-100,0); this.volume = Math.pow(10, x/20); },
get volume() { return this.gain; }, get volume() { return this.gain; },
set volume(x) { this.gain = x; }, set volume(x) { this.gain = x; },
}); });
audio.sound.bus.master = dspsound.master();
/*Object.mixin(audio.dsp.source().__proto__, { /*Object.mixin(audio.dsp.source().__proto__, {
length() { return this.frames()/sound.samplerate(); }, length() { return this.frames()/audio.samplerate(); },
time() { return this.frame/sound.samplerate(); }, time() { return this.frame/sound.samplerate(); },
pct() { return this.time()/this.length(); }, pct() { return this.time()/this.length(); },
}); });

View file

@ -297,7 +297,7 @@ Cmdline.register_order("play", function(argv) {
game.engine_start(function() { game.engine_start(function() {
console.info(`eng start`); console.info(`eng start`);
global.mixin("scripts/sound.js"); global.mixin("scripts/sound.js");
global.game = actor.spawn("game.js"); global.app = actor.spawn("game.js");
if (project.icon) window.set_icon(project.icon); if (project.icon) window.set_icon(project.icon);
}); });
}, "Play the game present in this folder."); }, "Play the game present in this folder.");

View file

@ -2,7 +2,6 @@
#include "script.h" #include "script.h"
#include "anim.h"
#include "debugdraw.h" #include "debugdraw.h"
#include "font.h" #include "font.h"
#include "gameobject.h" #include "gameobject.h"
@ -568,7 +567,7 @@ 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, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_register_collide(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]); int cmd = js2number(argv[0]);
gameobject *go = js2gameobject(argv[2]); gameobject *go = js2gameobject(argv[2]);
JSValue fn = argv[1]; JSValue fn = argv[1];
@ -594,7 +593,7 @@ JSValue duk_register_collide(JSContext *js, JSValueConst this, int argc, JSValue
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue duk_make_circle2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_make_circle2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
gameobject *go = js2gameobject(argv[0]); gameobject *go = js2gameobject(argv[0]);
struct phys2d_circle *circle = Make2DCircle(go); struct phys2d_circle *circle = Make2DCircle(go);
@ -605,7 +604,7 @@ JSValue duk_make_circle2d(JSContext *js, JSValueConst this, int argc, JSValueCon
return circleval; return circleval;
} }
JSValue duk_make_model(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue duk_make_model(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
gameobject *go = js2gameobject(argv[0]); gameobject *go = js2gameobject(argv[0]);
struct drawmodel *dm = make_drawmodel(go); struct drawmodel *dm = make_drawmodel(go);
@ -614,7 +613,7 @@ JSValue duk_make_model(JSContext *js, JSValueConst this, int argc, JSValueConst
return ret; return ret;
} }
JSValue duk_cmd_circle2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_cmd_circle2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]); int cmd = js2number(argv[0]);
struct phys2d_circle *circle = js2ptr(argv[1]); struct phys2d_circle *circle = js2ptr(argv[1]);
@ -640,7 +639,7 @@ JSValue duk_cmd_circle2d(JSContext *js, JSValueConst this, int argc, JSValueCons
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue duk_make_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_make_poly2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
gameobject *go = js2gameobject(argv[0]); gameobject *go = js2gameobject(argv[0]);
struct phys2d_poly *poly = Make2DPoly(go); struct phys2d_poly *poly = Make2DPoly(go);
phys2d_poly_setverts(poly, NULL); phys2d_poly_setverts(poly, NULL);
@ -650,7 +649,7 @@ JSValue duk_make_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst
return polyval; return polyval;
} }
JSValue duk_cmd_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_cmd_poly2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]); int cmd = js2number(argv[0]);
struct phys2d_poly *poly = js2ptr(argv[1]); struct phys2d_poly *poly = js2ptr(argv[1]);
HMM_Vec2 *v1 = NULL; HMM_Vec2 *v1 = NULL;
@ -668,7 +667,7 @@ JSValue duk_cmd_poly2d(JSContext *js, JSValueConst this, int argc, JSValueConst
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue duk_make_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_make_edge2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
gameobject *go = js2gameobject(argv[0]); gameobject *go = js2gameobject(argv[0]);
struct phys2d_edge *edge = Make2DEdge(go); struct phys2d_edge *edge = Make2DEdge(go);
HMM_Vec2 *points = js2cpvec2arr(argv[1]); HMM_Vec2 *points = js2cpvec2arr(argv[1]);
@ -716,7 +715,7 @@ JSValue ID##_get_##ENTRY (JSContext *js, JSValue this) { \
} \ } \
#define DEF_FN_STR(NAME) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \ #define DEF_FN_STR(NAME) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
char *str = js2str(argv[0]); \ char *str = js2str(argv[0]); \
NAME (str); \ NAME (str); \
JS_FreeCString(js,str); \ JS_FreeCString(js,str); \
@ -741,12 +740,12 @@ GETSET_PAIR(warp_gravity, planar_force, vec3)
#define GGETSET_ADD(ENTRY) #define GGETSET_ADD(ENTRY)
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \ #define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
{FN;} \ {FN;} \
return JS_UNDEFINED; \ return JS_UNDEFINED; \
} \ } \
#define JSC_RET(NAME, FN) JSValue js_##NAME (JSContext *js, JSValueConst this, int argc, JSValue *argv) { \ #define JSC_RET(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
FN; } \ FN; } \
static const JSCFunctionListEntry js_warp_gravity_funcs [] = { static const JSCFunctionListEntry js_warp_gravity_funcs [] = {
@ -809,14 +808,14 @@ JSValue js_emitter_stop(JSContext *js, JSValue this, int argc, JSValue *argv)
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue js_emitter_emit(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_emitter_emit(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
emitter *n = js2emitter(this); emitter *n = js2emitter(this);
emitter_emit(n, js2number(argv[0])); emitter_emit(n, js2number(argv[0]));
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue js_os_cwd(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_os_cwd(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
@ -828,7 +827,7 @@ JSValue js_os_cwd(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return str2js(cwd); return str2js(cwd);
} }
JSValue js_os_env(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_os_env(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *str = js2str(argv[0]); char *str = js2str(argv[0]);
JSValue ret = JS_UNDEFINED; JSValue ret = JS_UNDEFINED;
@ -837,7 +836,7 @@ JSValue js_os_env(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_os_sys(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_os_sys(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
#ifdef __linux__ #ifdef __linux__
return str2js("linux"); return str2js("linux");
@ -863,7 +862,7 @@ JSC_CCALL(os_eval_env,
#define RETUN return JS_UNDEFINED #define RETUN return JS_UNDEFINED
JSValue js_os_capture(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_os_capture(JSContext *js, JSValue this, int argc, JSValue *argv) {
char *str = js2str(argv[0]); char *str = js2str(argv[0]);
capture_screen(js2number(argv[1]), js2number(argv[2]), js2number(argv[4]), js2number(argv[5]), str); capture_screen(js2number(argv[1]), js2number(argv[2]), js2number(argv[4]), js2number(argv[5]), str);
JS_FreeCString(js,str); JS_FreeCString(js,str);
@ -1028,7 +1027,7 @@ static const JSCFunctionListEntry js_spline_funcs[] = {
MIST_FUNC_DEF(spline, bezier, 2) MIST_FUNC_DEF(spline, bezier, 2)
}; };
JSValue js_vector_dot(JSContext *js, JSValueConst this, int argc, JSValue *argv) { return number2js(HMM_DotV2(js2vec2(argv[0]), js2vec2(argv[1]))) ; }; JSValue js_vector_dot(JSContext *js, JSValue this, int argc, JSValue *argv) { return number2js(HMM_DotV2(js2vec2(argv[0]), js2vec2(argv[1]))) ; };
JSC_RET(vector_project, return vec22js(HMM_ProjV2(js2vec2(argv[0]), js2vec2(argv[1])));) JSC_RET(vector_project, return vec22js(HMM_ProjV2(js2vec2(argv[0]), js2vec2(argv[1])));)
@ -1037,11 +1036,11 @@ static const JSCFunctionListEntry js_vector_funcs[] = {
MIST_FUNC_DEF(vector,project,2), MIST_FUNC_DEF(vector,project,2),
}; };
JSValue js_game_engine_start(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_game_engine_start(JSContext *js, JSValue this, int argc, JSValue *argv) {
engine_start(argv[0],argv[1]); engine_start(argv[0],argv[1]);
RETUN; RETUN;
} }
JSValue js_game_object_count(JSContext *js, JSValueConst this) { return number2js(go_count()); } JSValue js_game_object_count(JSContext *js, JSValue this) { return number2js(go_count()); }
static const JSCFunctionListEntry js_game_funcs[] = { static const JSCFunctionListEntry js_game_funcs[] = {
MIST_FUNC_DEF(game, engine_start, 2), MIST_FUNC_DEF(game, engine_start, 2),
@ -1049,7 +1048,7 @@ static const JSCFunctionListEntry js_game_funcs[] = {
}; };
JSC_CCALL(input_show_keyboard, sapp_show_keyboard(js2bool(argv[0]))) JSC_CCALL(input_show_keyboard, sapp_show_keyboard(js2bool(argv[0])))
JSValue js_input_keyboard_shown(JSContext *js, JSValueConst this) { return bool2js(sapp_keyboard_shown()); } JSValue js_input_keyboard_shown(JSContext *js, JSValue this) { return bool2js(sapp_keyboard_shown()); }
JSC_CCALL(input_mouse_mode, set_mouse_mode(js2number(argv[0]))) JSC_CCALL(input_mouse_mode, set_mouse_mode(js2number(argv[0])))
JSC_CCALL(input_mouse_cursor, sapp_set_mouse_cursor(js2number(argv[0]))) JSC_CCALL(input_mouse_cursor, sapp_set_mouse_cursor(js2number(argv[0])))
@ -1144,7 +1143,7 @@ static const JSCFunctionListEntry js_profile_funcs[] = {
MIST_FUNC_DEF(profile,now,0), MIST_FUNC_DEF(profile,now,0),
}; };
JSValue js_io_exists(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_exists(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *file = JS_ToCString(js, argv[0]); char *file = JS_ToCString(js, argv[0]);
JSValue ret = JS_NewBool(js, fexists(file)); JSValue ret = JS_NewBool(js, fexists(file));
@ -1152,12 +1151,12 @@ JSValue js_io_exists(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_ls(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_ls(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
return strarr2js(ls(".")); return strarr2js(ls("."));
} }
JSValue js_io_cp(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_cp(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f1, *f2; char *f1, *f2;
f1 = JS_ToCString(js, argv[0]); f1 = JS_ToCString(js, argv[0]);
@ -1168,7 +1167,7 @@ JSValue js_io_cp(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_mv(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_mv(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f1, *f2; char *f1, *f2;
f1 = JS_ToCString(js, argv[0]); f1 = JS_ToCString(js, argv[0]);
@ -1179,7 +1178,7 @@ JSValue js_io_mv(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_chdir(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_chdir(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *path = JS_ToCString(js, argv[0]); char *path = JS_ToCString(js, argv[0]);
JSValue ret = number2js(chdir(path)); JSValue ret = number2js(chdir(path));
@ -1187,7 +1186,7 @@ JSValue js_io_chdir(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_rm(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_rm(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *file = JS_ToCString(js, argv[0]); char *file = JS_ToCString(js, argv[0]);
JSValue ret = number2js(remove(file)); JSValue ret = number2js(remove(file));
@ -1195,7 +1194,7 @@ JSValue js_io_rm(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue js_io_mkdir(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_mkdir(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f = js2str(argv[0]); char *f = js2str(argv[0]);
JSValue ret = number2js(mkdir(f,0777)); JSValue ret = number2js(mkdir(f,0777));
@ -1203,7 +1202,7 @@ JSValue js_io_mkdir(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_slurpbytes(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_slurpbytes(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f = js2str(argv[0]); char *f = js2str(argv[0]);
size_t len; size_t len;
@ -1214,7 +1213,7 @@ JSValue js_io_slurpbytes(JSContext *js, JSValueConst this, int argc, JSValue *ar
return ret; return ret;
} }
JSValue js_io_slurp(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_slurp(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f = js2str(argv[0]); char *f = js2str(argv[0]);
size_t len; size_t len;
@ -1229,7 +1228,7 @@ JSValue js_io_slurp(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_slurpwrite(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_slurpwrite(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f = js2str(argv[0]); char *f = js2str(argv[0]);
size_t len; size_t len;
@ -1247,7 +1246,7 @@ JSValue js_io_slurpwrite(JSContext *js, JSValueConst this, int argc, JSValue *ar
return ret; return ret;
} }
JSValue js_io_chmod(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_io_chmod(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
char *f = js2str(argv[0]); char *f = js2str(argv[0]);
int mod = js2number(argv[1]); int mod = js2number(argv[1]);
@ -1257,7 +1256,7 @@ JSValue js_io_chmod(JSContext *js, JSValueConst this, int argc, JSValue *argv)
DEF_FN_STR(save_qoa) DEF_FN_STR(save_qoa)
JSValue js_io_compile(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_io_compile(JSContext *js, JSValue this, int argc, JSValue *argv) {
size_t len; size_t len;
char *str = js2str(argv[0]); char *str = js2str(argv[0]);
void *d = script_compile(str, &len); void *d = script_compile(str, &len);
@ -1267,7 +1266,7 @@ JSValue js_io_compile(JSContext *js, JSValueConst this, int argc, JSValue *argv)
return ret; return ret;
} }
JSValue js_io_run_bytecode(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_io_run_bytecode(JSContext *js, JSValue this, int argc, JSValue *argv) {
size_t len; size_t len;
char *str = js2str(argv[0]); char *str = js2str(argv[0]);
void *d = slurp_file(str, &len); void *d = slurp_file(str, &len);
@ -1295,20 +1294,20 @@ static const JSCFunctionListEntry js_io_funcs[] = {
MIST_FN_STR(pack_engine) MIST_FN_STR(pack_engine)
}; };
JSValue js_debug_draw_gameobject(JSContext *js, JSValueConst this, int argc, JSValue *argv) { gameobject_draw_debug(js2gameobject(argv[0])); RETUN; } JSValue js_debug_draw_gameobject(JSContext *js, JSValue this, int argc, JSValue *argv) { gameobject_draw_debug(js2gameobject(argv[0])); RETUN; }
static const JSCFunctionListEntry js_debug_funcs[] = { static const JSCFunctionListEntry js_debug_funcs[] = {
MIST_FUNC_DEF(debug, draw_gameobject, 1) MIST_FUNC_DEF(debug, draw_gameobject, 1)
}; };
JSValue js_physics_sgscale(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_physics_sgscale(JSContext *js, JSValue this, int argc, JSValue *argv) {
js2gameobject(argv[0])->scale.xy = js2vec2(argv[1]); js2gameobject(argv[0])->scale.xy = js2vec2(argv[1]);
gameobject_apply(js2gameobject(argv[0])); gameobject_apply(js2gameobject(argv[0]));
cpSpaceReindexShapesForBody(space, js2gameobject(argv[0])->body); cpSpaceReindexShapesForBody(space, js2gameobject(argv[0])->body);
RETUN; RETUN;
} }
JSValue js_physics_set_cat_mask(JSContext *js, JSValueConst this, int argc, JSValue *argv) { set_cat_mask(js2number(argv[0]), js2bitmask(argv[1])); RETUN; } JSValue js_physics_set_cat_mask(JSContext *js, JSValue this, int argc, JSValue *argv) { set_cat_mask(js2number(argv[0]), js2bitmask(argv[1])); RETUN; }
JSC_CCALL(physics_box_query, JSC_CCALL(physics_box_query,
int *ids = phys2d_query_box(js2vec2(argv[0]), js2vec2(argv[1])); int *ids = phys2d_query_box(js2vec2(argv[0]), js2vec2(argv[1]));
@ -1359,7 +1358,7 @@ static const JSCFunctionListEntry js_physics_funcs[] = {
MIST_FUNC_DEF(physics, query_shape, 1), MIST_FUNC_DEF(physics, query_shape, 1),
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),
}; };
static const JSCFunctionListEntry js_emitter_funcs[] = { static const JSCFunctionListEntry js_emitter_funcs[] = {
@ -1392,14 +1391,15 @@ GETSET_PAIR(dsp_node, off, bool)
GETSET_PAIR(dsp_node, gain, number) GETSET_PAIR(dsp_node, gain, number)
GETSET_PAIR(dsp_node, pan, number) GETSET_PAIR(dsp_node, pan, number)
JSValue js_dsp_node_plugin(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_dsp_node_plugin(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
plugin_node(js2dsp_node(this), js2dsp_node(argv[0])); plugin_node(js2dsp_node(this), js2dsp_node(argv[0]));
return JS_UNDEFINED; return JS_DupValue(js,this);
} }
JSValue js_dsp_node_unplug(JSContext *js, JSValueConst this, int argc, JSValue *argv) JSValue js_dsp_node_unplug(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
JS_FreeValue(js,this);
unplug_node(js2dsp_node(this)); unplug_node(js2dsp_node(this));
return JS_UNDEFINED; return JS_UNDEFINED;
} }
@ -1427,8 +1427,8 @@ static const JSCFunctionListEntry js_sound_funcs[] = {
MIST_FUNC_DEF(sound, frames, 0), MIST_FUNC_DEF(sound, frames, 0),
}; };
static JSValue window_get_size(JSContext *js, JSValueConst this) { return vec22js(js2window(this)->size); } static JSValue window_get_size(JSContext *js, JSValue this) { return vec22js(js2window(this)->size); }
static JSValue window_set_size(JSContext *js, JSValueConst this, JSValue v) { static JSValue window_set_size(JSContext *js, JSValue this, JSValue v) {
window *w = js2window(this); window *w = js2window(this);
if (!w->start) if (!w->start)
w->size = js2vec2(v); w->size = js2vec2(v);
@ -1439,18 +1439,18 @@ static JSValue window_set_size(JSContext *js, JSValueConst this, JSValue v) {
GETSET_PAIR_APPLY(window, rendersize, vec2) GETSET_PAIR_APPLY(window, rendersize, vec2)
GETSET_PAIR(window, mode, number) GETSET_PAIR(window, mode, number)
static JSValue window_get_fullscreen(JSContext *js, JSValueConst this) static JSValue window_get_fullscreen(JSContext *js, JSValue this)
{ {
return number2js(js2window(this)->fullscreen); return number2js(js2window(this)->fullscreen);
} }
static JSValue window_set_fullscreen(JSContext *js, JSValueConst this, JSValue v) static JSValue window_set_fullscreen(JSContext *js, JSValue this, JSValue v)
{ {
window_setfullscreen(js2window(this), js2bool(v)); window_setfullscreen(js2window(this), js2bool(v));
return JS_UNDEFINED; return JS_UNDEFINED;
} }
static JSValue window_set_title(JSContext *js, JSValueConst this, JSValue v) static JSValue window_set_title(JSContext *js, JSValue this, JSValue v)
{ {
window *w = js2window(this); window *w = js2window(this);
if (w->title) JS_FreeCString(js, w->title); if (w->title) JS_FreeCString(js, w->title);
@ -1460,7 +1460,7 @@ static JSValue window_set_title(JSContext *js, JSValueConst this, JSValue v)
return JS_UNDEFINED; return JS_UNDEFINED;
} }
static JSValue window_get_title(JSContext *js, JSValueConst this, JSValue v) static JSValue window_get_title(JSContext *js, JSValue this, JSValue v)
{ {
if (!js2window(this)->title) return JS_UNDEFINED; if (!js2window(this)->title) return JS_UNDEFINED;
return str2js(js2window(this)->title); return str2js(js2window(this)->title);
@ -1468,7 +1468,7 @@ static JSValue window_get_title(JSContext *js, JSValueConst this, JSValue v)
DEF_FN_STR(set_icon) DEF_FN_STR(set_icon)
JSValue js_window_icon(JSContext *js, JSValueConst this, int argc, JSValue *argv) { JSValue js_window_icon(JSContext *js, JSValue this, int argc, JSValue *argv) {
char *str = js2str(argv[0]); char *str = js2str(argv[0]);
set_icon(str); set_icon(str);
JS_FreeCString(js,str); JS_FreeCString(js,str);
@ -1654,16 +1654,16 @@ static const JSCFunctionListEntry js_dspsound_funcs[] = {
MIST_FUNC_DEF(dspsound, mod, 1) MIST_FUNC_DEF(dspsound, mod, 1)
}; };
JSC_CCALL(shape_set_sensor, shape_set_sensor(js2ptr(argv[0]), js2bool(argv[1]))) JSC_CCALL(pshape_set_sensor, shape_set_sensor(js2ptr(argv[0]), js2bool(argv[1])))
JSC_CCALL(shape_get_sensor, shape_get_sensor(js2ptr(argv[0]))) JSC_CCALL(pshape_get_sensor, shape_get_sensor(js2ptr(argv[0])))
JSC_CCALL(shape_set_enabled, shape_enabled(js2ptr(argv[0]), js2bool(argv[1]))) JSC_CCALL(pshape_set_enabled, shape_enabled(js2ptr(argv[0]), js2bool(argv[1])))
JSC_CCALL(shape_get_enabled, shape_is_enabled(js2ptr(argv[0]))) JSC_CCALL(pshape_get_enabled, shape_is_enabled(js2ptr(argv[0])))
static const JSCFunctionListEntry js_shape_funcs[] = { static const JSCFunctionListEntry js_pshape_funcs[] = {
MIST_FUNC_DEF(shape, set_sensor, 2), MIST_FUNC_DEF(pshape, set_sensor, 2),
MIST_FUNC_DEF(shape, get_sensor, 1), MIST_FUNC_DEF(pshape, get_sensor, 1),
MIST_FUNC_DEF(shape, set_enabled, 2), MIST_FUNC_DEF(pshape, set_enabled, 2),
MIST_FUNC_DEF(shape, get_enabled, 1) MIST_FUNC_DEF(pshape, get_enabled, 1)
}; };
GETSET_PAIR(sprite, color, color) GETSET_PAIR(sprite, color, color)
@ -1737,7 +1737,7 @@ static const JSCFunctionListEntry js_constraint_funcs[] = {
CGETSET_ADD(constraint, collide), CGETSET_ADD(constraint, collide),
}; };
JSValue duk_cmd_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_cmd_edge2d(JSContext *js, JSValue this, int argc, JSValue *argv) {
int cmd = js2number(argv[0]); int cmd = js2number(argv[0]);
struct phys2d_edge *edge = js2ptr(argv[1]); struct phys2d_edge *edge = js2ptr(argv[1]);
@ -1763,7 +1763,7 @@ JSValue duk_cmd_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue duk_inflate_cpv(JSContext *js, JSValueConst this, int argc, JSValueConst *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]);
int n = js2number(argv[1]); int n = js2number(argv[1]);
double d = js2number(argv[2]); double d = js2number(argv[2]);
@ -1777,7 +1777,7 @@ JSValue duk_inflate_cpv(JSContext *js, JSValueConst this, int argc, JSValueConst
const char *STRTEST = "TEST STRING"; const char *STRTEST = "TEST STRING";
JSValue duk_performance(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue duk_performance(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
int cmd = js2number(argv[0]); int cmd = js2number(argv[0]);
switch(cmd) { switch(cmd) {
@ -1808,7 +1808,7 @@ JSValue duk_performance(JSContext *js, JSValueConst this, int argc, JSValueConst
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue js_nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue js_nota_encode(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
if (argc < 1) return JS_UNDEFINED; if (argc < 1) return JS_UNDEFINED;
@ -1819,7 +1819,7 @@ JSValue js_nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst
return JS_NewArrayBufferCopy(js, nota, e-nota); return JS_NewArrayBufferCopy(js, nota, e-nota);
} }
JSValue js_nota_decode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue js_nota_decode(JSContext *js, JSValue this, int argc, JSValue *argv)
{ {
if (argc < 1) return JS_UNDEFINED; if (argc < 1) return JS_UNDEFINED;
@ -1887,7 +1887,7 @@ void ffi_load() {
QJSGLOBALCLASS(spline); QJSGLOBALCLASS(spline);
QJSGLOBALCLASS(joint); QJSGLOBALCLASS(joint);
QJSGLOBALCLASS(dspsound); QJSGLOBALCLASS(dspsound);
QJSGLOBALCLASS(shape); QJSGLOBALCLASS(pshape);
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

@ -136,6 +136,8 @@ void dsp_node_run(dsp_node *node)
} }
} }
static int node_count = 0;
dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int samples), void (*fr)(void *data)) dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int samples), void (*fr)(void *data))
{ {
dsp_node *self = malloc(sizeof(dsp_node)); dsp_node *self = malloc(sizeof(dsp_node));
@ -144,12 +146,17 @@ dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int sam
self->proc = proc; self->proc = proc;
self->pass = 0; self->pass = 0;
self->gain = 1; self->gain = 1;
self->id = node_count++;
YughSpam("Made node %d.", self->id);
return self; return self;
} }
void node_free(dsp_node *node) void node_free(dsp_node *node)
{ {
if (node == masterbus) return; /* Simple check to not delete the masterbus */ if (node == masterbus) {
YughWarn("Attempted to delete the master bus.");
return; /* Simple check to not delete the masterbus */
}
pthread_mutex_lock(&soundrun); pthread_mutex_lock(&soundrun);
unplug_node(node); unplug_node(node);
if (node->data) { if (node->data) {
@ -159,6 +166,7 @@ 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);
} }

View file

@ -22,6 +22,7 @@ typedef struct dsp_node {
int off; /* True if the filter shouldn't output */ int off; /* True if the filter shouldn't output */
float gain; /* Between 0 and 1, to attenuate this output */ float gain; /* Between 0 and 1, to attenuate this output */
float pan; /* Between -100 and +100, panning left to right in the speakers */ float pan; /* Between -100 and +100, panning left to right in the speakers */
int id;
} dsp_node; } dsp_node;
void dsp_init(); void dsp_init();