From 681fb27ae4ef4a8c7b2479293436888b2909fca6 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 13 Sep 2023 12:31:00 +0000 Subject: [PATCH] Removed global scope static variables; ad-hoc way so timers can function in app or timescaled mode --- source/engine/2dphysics.c | 15 +++++++-------- source/engine/debug/debugdraw.c | 1 - source/engine/debug/log.c | 12 +++++++++--- source/engine/debug/log.h | 6 ++++-- source/engine/ffi.c | 2 +- source/engine/sound/dsp.c | 2 +- source/engine/sound/mix.c | 12 ++++++++---- source/engine/sound/mix.h | 2 +- source/engine/sound/music.c | 2 +- source/engine/texture.c | 2 +- source/engine/timer.c | 11 ++++++++--- source/engine/timer.h | 5 +++-- source/engine/yugine.c | 4 +++- source/scripts/debug.js | 5 +++-- source/scripts/engine.js | 24 ++++++------------------ source/shaders/box.glsl | 2 +- 16 files changed, 57 insertions(+), 50 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index e8b3a8d..41e7bba 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -593,14 +593,13 @@ struct postphys_cb { JSValue send; }; -static struct postphys_cb begins[512]; -static uint32_t bptr; +static struct postphys_cb *begins = NULL; void flush_collide_cbs() { - for (int i = 0; i < bptr; i++) + for (int i = 0; i < arrlen(begins); i++) script_callee(begins[i].c, 1, &begins[i].send); - bptr = 0; + arrsetlen(begins,0); } void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { @@ -618,10 +617,10 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { JS_SetPropertyStr(js, obj, "id", JS_NewInt32(js,hit)); JS_SetPropertyStr(js,obj,"obj", JS_DupValue(js,id2go(hit)->ref)); - begins[bptr].c = c; - begins[bptr].send = obj; - bptr++; - return; + struct postphys_cb cb; + cb.c = c; + cb.send = obj; + arrput(begins, cb); } #define CTYPE_BEGIN 0 diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index d2f707f..f02f01c 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -46,7 +46,6 @@ static int line_sc = 0; static int line_sv = 0; static struct line_vert line_b[v_amt]; static uint16_t line_bi[v_amt]; - static sg_pipeline grid_pipe; static sg_bindings grid_bind; static sg_shader grid_shader; diff --git a/source/engine/debug/log.c b/source/engine/debug/log.c index 5a87885..125f666 100644 --- a/source/engine/debug/log.c +++ b/source/engine/debug/log.c @@ -24,10 +24,16 @@ char *catstr[] = {"engine", "script", "render"}; FILE *logfile = NULL; -#define CONSOLE_BUF 1024*1024*5/* 5MB */ +#define CONSOLE_BUF 1024*1024*5 /* 5MB */ -char lastlog[ERROR_BUFFER+1] = {'\0'}; -char consolelog[CONSOLE_BUF+1] = {'\0'}; +char *lastlog; +char *consolelog; + +void log_init() +{ + lastlog = malloc(ERROR_BUFFER+1); + consolelog = malloc(CONSOLE_BUF+1); +} void mYughLog(int category, int priority, int line, const char *file, const char *message, ...) { diff --git a/source/engine/debug/log.h b/source/engine/debug/log.h index 8f58a84..73a62a1 100644 --- a/source/engine/debug/log.h +++ b/source/engine/debug/log.h @@ -15,10 +15,12 @@ #define LOG_SCRIPT 1 #define LOG_RENDER 2 -extern char lastlog[]; -extern char consolelog[]; +extern char *lastlog; +extern char *consolelog; extern int logLevel; +void log_init(); + #ifndef NDEBUG #define YughLog(cat, pri, msg, ...) mYughLog(cat, pri, __LINE__, __FILE__, msg, ##__VA_ARGS__) #define YughInfo(msg, ...) mYughLog(0, 0, __LINE__, __FILE__, msg, ##__VA_ARGS__); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 5293e31..83f6628 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -1619,7 +1619,7 @@ JSValue duk_make_timer(JSContext *js, JSValueConst this, int argc, JSValueConst struct callee *c = malloc(sizeof(*c)); c->fn = JS_DupValue(js, argv[0]); c->obj = JS_DupValue(js, globalThis); - int id = timer_make(secs, call_callee, c, 1); + int id = timer_make(secs, call_callee, c, 1, js2bool(argv[2])); return JS_NewInt64(js, id); } diff --git a/source/engine/sound/dsp.c b/source/engine/sound/dsp.c index ff3cdd9..cef46f8 100644 --- a/source/engine/sound/dsp.c +++ b/source/engine/sound/dsp.c @@ -436,7 +436,7 @@ struct dsp_delay dsp_delay_make(unsigned int ms_delay) void dsp_delay_filbuf(struct dsp_delay *delay, soundbyte *buf, int n) { - static soundbyte cache[BUF_FRAMES*2]; + soundbyte cache[BUF_FRAMES*2]; dsp_run(delay->in, cache, n); for (int i = 0; i < n*CHANNELS; i++) { diff --git a/source/engine/sound/mix.c b/source/engine/sound/mix.c index 9c0eb3e..69abd2b 100644 --- a/source/engine/sound/mix.c +++ b/source/engine/sound/mix.c @@ -5,14 +5,16 @@ #include "dsp.h" #include #include "log.h" +#include "stdlib.h" #include -static struct bus bus[256]; +#define BUS_N 256 +static struct bus *bus; static int first = 0; /* First bus available */ static int first_on = -1; /* First bus to fill buffer with */ -soundbyte mastermix[BUF_FRAMES*CHANNELS]; +soundbyte *mastermix = NULL; static float master_volume = 1.f; @@ -23,13 +25,15 @@ void mix_master_vol(float v) { } void mixer_init() { - for (int i = 0; i < 256; i++) { + bus = malloc(sizeof(struct bus)*BUS_N); + mastermix = malloc(BUF_FRAMES*CHANNELS); + for (int i = 0; i < BUS_N; i++) { bus[i].next = i+1; bus[i].on = 0; bus[i].id = i; } - bus[255].next = -1; + bus[BUS_N-1].next = -1; } struct bus *first_free_bus(struct dsp_filter in) { diff --git a/source/engine/sound/mix.h b/source/engine/sound/mix.h index 93bf025..5d21e08 100644 --- a/source/engine/sound/mix.h +++ b/source/engine/sound/mix.h @@ -14,7 +14,7 @@ struct bus { int id; }; -extern soundbyte mastermix[BUF_FRAMES*CHANNELS]; +extern soundbyte *mastermix; void mixer_init(); diff --git a/source/engine/sound/music.c b/source/engine/sound/music.c index bbf70fa..299e57f 100644 --- a/source/engine/sound/music.c +++ b/source/engine/sound/music.c @@ -9,7 +9,6 @@ #define TSF_BLOCK 32 -struct dsp_filter cursong; struct dsp_midi_song gsong; float music_pan = 0.f; @@ -79,6 +78,7 @@ void play_song(const char *midi, const char *sf) // Preset on 10th MIDI channel to use percussion sound bank if possible tsf_channel_set_bank_preset(gsong.sf, 9, 128, 0); + struct dsp_filter cursong; cursong.data = &gsong; cursong.filter = dsp_midi_fillbuf; musicbus = first_free_bus(cursong); diff --git a/source/engine/texture.c b/source/engine/texture.c index 1154b1f..65f0142 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -305,7 +305,7 @@ void anim_play(struct anim2d *anim) { anim->playing = 1; if (anim->timer == NULL) - anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0)); + anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0)); else timerr_settime(anim->timer, 1.f / anim->anim->ms); diff --git a/source/engine/timer.c b/source/engine/timer.c index 5dac751..ba745dc 100644 --- a/source/engine/timer.c +++ b/source/engine/timer.c @@ -25,12 +25,16 @@ void check_timer(struct timer *t, double dt) { } } -void timer_update(double dt) { +void timer_update(double dt, double scale) { + double sdt = dt*scale; for (int i = 0; i < arrlen(timers); i++) - check_timer(&timers[i], dt); + if (timers[i].app) + check_timer(&timers[i], dt); + else + check_timer(&timers[i], sdt); } -int timer_make(double interval, void (*callback)(void *param), void *param, int own) { +int timer_make(double interval, void (*callback)(void *param), void *param, int own, int app) { struct timer new; new.remain_time = interval; new.interval = interval; @@ -39,6 +43,7 @@ int timer_make(double interval, void (*callback)(void *param), void *param, int new.repeat = 1; new.owndata = own; new.next = -1; + new.app = app; if (first < 0) { timer_start(&new); diff --git a/source/engine/timer.h b/source/engine/timer.h index fd2edea..2639593 100644 --- a/source/engine/timer.h +++ b/source/engine/timer.h @@ -11,15 +11,16 @@ struct timer { void *data; int owndata; int next; + int app; }; -int timer_make(double interval, void (*callback)(void *param), void *param, int own); +int timer_make(double interval, void (*callback)(void *param), void *param, int own, int app); struct timer *id2timer(int id); void timer_remove(int id); void timer_start(struct timer *t); void timer_pause(struct timer *t); void timer_stop(struct timer *t); -void timer_update(double dt); +void timer_update(double dt, double scale); void timerr_settime(struct timer *t, double interval); #endif diff --git a/source/engine/yugine.c b/source/engine/yugine.c index d3a1b79..a0c6bad 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -158,10 +158,11 @@ void c_frame() low_fps_c += elapsed; input_poll(0); + timer_update(elapsed, timescale); if (sim_play == SIM_PLAY || sim_play == SIM_STEP) { prof_start(&prof_update); - timer_update(elapsed * timescale); + call_updates(elapsed * timescale); prof(&prof_update); @@ -317,6 +318,7 @@ void app_name(char *name) sapp_desc sokol_main(int argc, char **argv) { #ifndef NDEBUG + log_init(); #ifdef __linux__ int logout = 0; if (logout) { diff --git a/source/scripts/debug.js b/source/scripts/debug.js index e5e88be..650cdae 100644 --- a/source/scripts/debug.js +++ b/source/scripts/debug.js @@ -251,7 +251,7 @@ Debug.Options.gif = { w: 640, /* Max width */ h: 480, /* Max height */ stretch: false, /* True if you want to stretch */ - cpf: 2, + cpf: 4, depth: 8, file: "out.gif", rec: false, @@ -275,10 +275,11 @@ Debug.Options.gif = { this.fps = (1/this.cpf)*100; this.start_time = Time.time; - timer.oneshot(this.stop.bind(this), this.secs, this); + timer.oneshot(this.stop.bind(this), this.secs, this, true); }, stop() { + Log.warn("STOPPED"); if (!this.rec) return; cmd(132, this.file); this.rec = false; diff --git a/source/scripts/engine.js b/source/scripts/engine.js index dc47f1e..5851521 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -55,17 +55,8 @@ function bb2wh(bb) { load("scripts/gui.js"); var timer = { - guardfn(fn) { - if (typeof fn === 'function') - fn(); - else { - Log.warn("TIMER TRYING TO EXECUTE WIHTOUT!!!"); - Log.warn(this); - this.kill(); - } - }, - - make(fn, secs,obj,loop) { + make(fn, secs,obj,loop,app) { + app ??= false; if (secs === 0) { fn.call(obj); return; @@ -79,17 +70,14 @@ var timer = { else Log.warn("Timer trying to execute without a function."); }; - t.id = make_timer(guardfn, secs, obj); + t.id = make_timer(guardfn, secs, app); return t; }, - oneshot(fn, secs,obj) { - var t = this.make(() => { - fn.call(); - t.kill(); - },secs); - t.loop = 0; + oneshot(fn, secs,obj, app) { + app ??= false; + var t = this.make(fn, secs, obj, 0, app); t.start(); }, diff --git a/source/shaders/box.glsl b/source/shaders/box.glsl index 2969c2c..8944eee 100644 --- a/source/shaders/box.glsl +++ b/source/shaders/box.glsl @@ -26,5 +26,5 @@ void main() } } -// frag_color = vec4(acc,1); + frag_color = vec4(acc,1); }