Add update and physics timings; add instance reversion on save urtype

This commit is contained in:
John Alanbrook 2023-10-30 22:41:32 +00:00
parent 6eefa95546
commit 92ccb72c04
10 changed files with 80 additions and 49 deletions

View file

@ -629,7 +629,7 @@ var editor = {
}, },
/* Checking to save an entity as a subtype. */ /* Checking to save an entity as a subtype. */
/* sub is the name of the type; obj is the object to save it as */ /* sub is the name of the (sub)type; obj is the object to save it as */
saveas_check(sub, obj) { saveas_check(sub, obj) {
if (!sub) return; if (!sub) return;
obj ??= editor.selectlist[0]; obj ??= editor.selectlist[0];
@ -644,18 +644,20 @@ var editor = {
var saveobj = obj.json_obj(); var saveobj = obj.json_obj();
IO.slurpwrite(JSON.stringify(saveobj,null,1), path); IO.slurpwrite(JSON.stringify(saveobj,null,1), path);
if (obj === editor.edit_level) { if (obj === editor.edit_level) {
obj.clear(); if (obj === editor.desktop) {
var nobj = editor.edit_level.spawn(sub); obj.clear();
editor.selectlist = [nobj]; var nobj = editor.edit_level.spawn(sub);
return; editor.selectlist = [nobj];
return;
}
editor.edit_level = editor.edit_level.level;
} }
var t = obj.transform(); var t = obj.transform();
obj.kill();
editor.unselect(); editor.unselect();
obj = editor.load(sub); obj.kill();
obj = editor.edit_level.spawn(sub);
obj.pos = t.pos; obj.pos = t.pos;
obj.angle = t.angle; obj.angle = t.angle;
} }
@ -874,12 +876,18 @@ editor.inputs['C-s'] = function() {
Log.warn(`Wrote to file ${path}`); Log.warn(`Wrote to file ${path}`);
Object.values(saveobj.objects).forEach(function(x) { x._ed.check_dirty(); }); Object.values(saveobj.objects).forEach(function(x) { x._ed.check_dirty(); });
Game.objects.forEach(function(x) {
if (x._ed.dirty) return;
x.revert();
x._ed.check_dirty();
});
}; };
editor.inputs['C-s'].doc = "Save selected."; editor.inputs['C-s'].doc = "Save selected.";
editor.inputs['C-S'] = function() { editor.inputs['C-S'] = function() {
if (editor.selectlist.length !== 1) return; if (editor.selectlist.length !== 1) return;
saveaspanel.stem = this.selectlist[0].toString(); saveaspanel.stem = this.selectlist[0].ur;
editor.openpanel(saveaspanel); editor.openpanel(saveaspanel);
}; };
editor.inputs['C-S'].doc = "Save selected as."; editor.inputs['C-S'].doc = "Save selected as.";
@ -1077,7 +1085,7 @@ editor.inputs.mm = function() {
var o = editor.try_pick(); var o = editor.try_pick();
if (!o) return; if (!o) return;
editor.selectlist = [o]; // editor.selectlist = [o];
editor.grabselect = [o]; editor.grabselect = [o];
}; };
editor.inputs['C-mm'] = editor.inputs.mm; editor.inputs['C-mm'] = editor.inputs.mm;

View file

@ -790,7 +790,6 @@ preprimum.gscale = function() { return 1; };
preprimum.pos = [0,0]; preprimum.pos = [0,0];
preprimum.angle = 0; preprimum.angle = 0;
preprimum.remove_obj = function() {}; preprimum.remove_obj = function() {};
preprimum.instances = [];
preprimum.toString = function() { return "preprimum"; }; preprimum.toString = function() { return "preprimum"; };
globalThis.World = preprimum.make(preprimum); globalThis.World = preprimum.make(preprimum);
globalThis.Primum = World; globalThis.Primum = World;
@ -800,6 +799,8 @@ Primum.toString = function() { return "Primum"; };
Primum._ed.selectable = false; Primum._ed.selectable = false;
Primum._ed.check_dirty = function() { }; Primum._ed.check_dirty = function() { };
Primum._ed.dirty = false; Primum._ed.dirty = false;
Primum.revert = function(){};
Primum.ur = undefined;
globalThis.World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); } globalThis.World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); }
Game.view_camera(Primum.spawn(ur.camera2d)); Game.view_camera(Primum.spawn(ur.camera2d));
} }

View file

@ -238,7 +238,11 @@ var gameobject = {
/* Make a unique object the same as its prototype */ /* Make a unique object the same as its prototype */
revert() { revert() {
Object.merge(this,this.__proto__); var jobj = this.json_obj();
delete jobj.objects;
Object.keys(jobj).forEach(function(x) {
this[x] = this.__proto__[x];
}, this);
this.sync(); this.sync();
}, },
@ -416,7 +420,10 @@ var gameobject = {
Player.do_uncontrol(this); Player.do_uncontrol(this);
Register.unregister_obj(this); Register.unregister_obj(this);
// ur[this.ur].instances.remove(this);
if (this.__proto__.instances)
this.__proto__.instances.remove(this);
this.body = -1; this.body = -1;
for (var key in this.components) { for (var key in this.components) {
@ -446,7 +453,8 @@ var gameobject = {
obj.make = undefined; obj.make = undefined;
obj.level = level; obj.level = level;
// obj.toJSON = obj.transform_obj; // obj.toJSON = obj.transform_obj;
// this.instances.push(obj); if (this.instances)
this.instances.push(obj);
// Log.warn(`Made an object from ${this.toString()}`); // Log.warn(`Made an object from ${this.toString()}`);
// Log.warn(this.instances.length); // Log.warn(this.instances.length);
obj.body = make_gameobject(); obj.body = make_gameobject();
@ -509,12 +517,10 @@ var gameobject = {
obj.sync(); obj.sync();
gameobject.check_registers(obj); gameobject.check_registers(obj);
if (Game.playing() && typeof obj.start === 'function') obj.start(); if (data)
/* obj.objects.forEach(function(obj) { Object.dainty_assign(obj,data);
if (Game.playing() && typeof obj.start === 'function') obj.start();
});
*/
if (Game.playing() && typeof obj.start === 'function') obj.start();
return obj; return obj;
}, },

View file

@ -1,3 +1,7 @@
var Audio = {
};
var Music = { var Music = {
play(path) { play(path) {
Log.info("Playing " + path); Log.info("Playing " + path);

View file

@ -102,7 +102,7 @@ void debug_flush(HMM_Mat4 *view)
sg_apply_bindings(&line_bind); sg_apply_bindings(&line_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view));
lfs_params_t lt; lfs_params_t lt;
lt.time = appTime; lt.time = apptime();
sg_apply_uniforms(SG_SHADERSTAGE_FS,0,SG_RANGE_REF(lt)); sg_apply_uniforms(SG_SHADERSTAGE_FS,0,SG_RANGE_REF(lt));
sg_draw(line_sc,line_c,1); sg_draw(line_sc,line_c,1);
} }

View file

@ -29,14 +29,12 @@ FILE *logfile = NULL;
#define CONSOLE_BUF 1024*1024 /* 5MB */ #define CONSOLE_BUF 1024*1024 /* 5MB */
char *consolelog; char *consolelog;
FILE *consolefp;
static FILE *sout; static FILE *sout;
void log_init() void log_init()
{ {
consolelog = malloc(CONSOLE_BUF+1); consolelog = malloc(CONSOLE_BUF+1);
consolefp = fmemopen(consolelog, CONSOLE_BUF+1,"w");
sout = fdopen(dup(stdout),"w"); sout = fdopen(dup(stdout),"w");
sout = stdout; sout = stdout;
} }

View file

@ -514,7 +514,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break; break;
case 6: case 6:
// updateMS = js2number(argv[1]); updateMS = js2number(argv[1]);
break; break;
case 7: case 7:
@ -1047,7 +1047,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
gif_rec_end(str); gif_rec_end(str);
break; break;
case 133: case 133:
ret = JS_NewFloat64(js, appTime); ret = JS_NewFloat64(js, apptime());
break; break;
case 134: case 134:

View file

@ -56,7 +56,7 @@ void gif_rec_start(int w, int h, int cpf, int bitdepth)
gif.cpf = cpf; gif.cpf = cpf;
gif.spf = cpf/100.0; gif.spf = cpf/100.0;
gif.rec = 1; gif.rec = 1;
gif.timer = appTime; gif.timer = apptime();
if (gif.buffer) free(gif.buffer); if (gif.buffer) free(gif.buffer);
gif.buffer = malloc(gif.w*gif.h*4); gif.buffer = malloc(gif.w*gif.h*4);
@ -548,14 +548,14 @@ void openglRender(struct window *window) {
full_2d_pass(window); full_2d_pass(window);
sg_end_pass(); sg_end_pass();
if (gif.rec && (appTime - gif.timer) > gif.spf) { if (gif.rec && (apptime() - gif.timer) > gif.spf) {
sg_begin_pass(sg_gif.pass, &pass_action); sg_begin_pass(sg_gif.pass, &pass_action);
sg_apply_pipeline(sg_gif.pipe); sg_apply_pipeline(sg_gif.pipe);
sg_apply_bindings(&sg_gif.bind); sg_apply_bindings(&sg_gif.bind);
sg_draw(0,6,1); sg_draw(0,6,1);
sg_end_pass(); sg_end_pass();
gif.timer = appTime; gif.timer = apptime();
sg_query_image_pixels(sg_gif.img, crt_post.bind.fs.samplers[0], gif.buffer, gif.w*gif.h*4); sg_query_image_pixels(sg_gif.img, crt_post.bind.fs.samplers[0], gif.buffer, gif.w*gif.h*4);
msf_gif_frame(&gif_state, gif.buffer, gif.cpf, gif.depth, gif.w * -4); msf_gif_frame(&gif_state, gif.buffer, gif.cpf, gif.depth, gif.w * -4);
} }

View file

@ -54,12 +54,16 @@ static struct d_prof prof_input;
static struct d_prof prof_physics; static struct d_prof prof_physics;
double physlag = 0; double physlag = 0;
double physMS = 1 / 60.f; double physMS = 1 / 60.f;
uint64_t physlast = 0;
double updateMS = 1/60.f;
uint64_t updatelast = 0;
static int phys_step = 0; static int phys_step = 0;
double appTime = 0; uint64_t start_t;
uint64_t frame_t;
static float timescale = 1.f; static float timescale = 1.f;
@ -139,6 +143,8 @@ void c_init() {
window_set_icon("icons/moon.gif"); window_set_icon("icons/moon.gif");
window_resize(sapp_width(), sapp_height()); window_resize(sapp_width(), sapp_height());
script_evalf("Game.init();"); script_evalf("Game.init();");
} }
int frame_fps() { int frame_fps() {
@ -147,34 +153,34 @@ int frame_fps() {
static void process_frame() static void process_frame()
{ {
double elapsed = sapp_frame_duration(); double elapsed = stm_sec(stm_laptime(&frame_t));
appTime += elapsed; physlag += elapsed;
input_poll(0); input_poll(0);
/* Timers all update every frame - once per monitor refresh */
timer_update(elapsed, timescale); timer_update(elapsed, timescale);
if (sim_play == SIM_PLAY || sim_play == SIM_STEP) { if (sim_play == SIM_PLAY || sim_play == SIM_STEP && stm_sec(stm_diff(frame_t, updatelast)) > updateMS) {
double dt = stm_sec(stm_diff(frame_t, updatelast));
updatelast = frame_t;
prof_start(&prof_update); prof_start(&prof_update);
call_updates(dt * timescale);
call_updates(elapsed * timescale);
prof(&prof_update); prof(&prof_update);
physlag += elapsed;
while (physlag >= physMS) {
prof_start(&prof_physics);
phys_step = 1;
physlag -= physMS;
phys2d_update(physMS * timescale);
call_physics(physMS * timescale);
if (sim_play == SIM_STEP) sim_pause();
phys_step = 0;
prof(&prof_physics);
}
if (sim_play == SIM_STEP) if (sim_play == SIM_STEP)
sim_pause(); sim_pause();
} }
while ((sim_play == SIM_PLAY || sim_play == SIM_STEP) && physlag > physMS) {
physlag -= physMS;
prof_start(&prof_physics);
phys_step = 1;
phys2d_update(physMS * timescale);
call_physics(physMS * timescale);
phys_step = 0;
prof(&prof_physics);
}
prof_start(&prof_draw); prof_start(&prof_draw);
window_render(&mainwin); window_render(&mainwin);
prof(&prof_draw); prof(&prof_draw);
@ -338,6 +344,9 @@ int main(int argc, char **argv) {
#endif #endif
stm_setup(); /* time */ stm_setup(); /* time */
start_t = frame_t = stm_now();
physlast = updatelast = start_t;
resources_init(); resources_init();
phys2d_init(); phys2d_init();
script_startup(); script_startup();
@ -366,3 +375,8 @@ int main(int argc, char **argv) {
return 0; return 0;
} }
double apptime()
{
return stm_sec(stm_diff(start_t, stm_now()));
}

View file

@ -18,7 +18,7 @@ void app_name(char *name);
int frame_fps(); int frame_fps();
double get_timescale(); double get_timescale();
extern double appTime; double apptime();
extern double renderMS; extern double renderMS;
extern double physMS; extern double physMS;
extern double updateMS; extern double updateMS;