add layers to callbacks

This commit is contained in:
John Alanbrook 2024-09-13 10:27:01 -05:00
parent ad6b82ded1
commit 01905a26a2
2 changed files with 30 additions and 11 deletions

View file

@ -4,6 +4,7 @@ global.check_registers = function (obj) {
for (var reg in Register.registries) {
if (typeof obj[reg] === 'function') {
var fn = obj[reg].bind(obj);
fn.layer = obj[reg].layer;
var name = obj.ur ? obj.ur.name : obj.toString();
obj.timers.push(Register.registries[reg].register(fn, name));
}
@ -315,9 +316,9 @@ which returns a function that, when invoked, cancels the registry.
var Register = {
registries: [],
add_cb(name, e_event = false) {
add_cb(name, e_event = false, flush = undefined) {
var n = {};
var fns = {};
var fns = [];
n.register = function (fn, oname) {
if (!(fn instanceof Function)) return;
@ -331,15 +332,33 @@ var Register = {
profile.endcache();
}
fns[guid] = dofn;
fns.push(dofn);
dofn.layer = fn.layer;
dofn.layer ??= 0;
fns.sort((a,b) => a.layer > b.layer);
return function () {
delete fns[guid];
fns.remove(dofn);
};
};
if (!flush) {
prosperon[name] = function(...args) {
fns.forEach(x => x(...args));
};
fns.forEach(fn => fn(...args));
}
}
else
prosperon[name] = function(...args) {
var layer = undefined;
for (var fn of fns) {
if (layer !== fn.layer) {
flush();
layer = fn.layer;
}
fn();
}
}
prosperon[name].fns = fns;
n.clear = function () {
@ -357,13 +376,12 @@ Register.add_cb("appupdate", true);
Register.add_cb("update", true).doc = "Called once per frame.";
Register.add_cb("physupdate", true);
Register.add_cb("gui", true);
Register.add_cb("hud", true);
Register.add_cb("hud", true, render.flush);
Register.add_cb("draw_dbg", true);
Register.add_cb("gui_dbg", true);
Register.add_cb("hud_dbg", true);
Register.add_cb("draw", true);
Register.add_cb("imgui", true);
Register.add_cb("post", true);
var Event = {
events: {},

View file

@ -642,6 +642,8 @@ function check_flush(flush_fn)
}
}
render.flush = check_flush;
var poly_cache = [];
var poly_idx = 0;
var poly_ssbo;
@ -1121,7 +1123,6 @@ prosperon.render = function()
render.flush_text();
mum.style = mum.base;
prosperon.post();
check_flush();
profile.endframe();