Removed global scope static variables; ad-hoc way so timers can function in app or timescaled mode

This commit is contained in:
John Alanbrook 2023-09-13 12:31:00 +00:00
parent 94c2a3c143
commit 681fb27ae4
16 changed files with 57 additions and 50 deletions

View file

@ -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

View file

@ -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;

View file

@ -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, ...)
{

View file

@ -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__);

View file

@ -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);
}

View file

@ -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++) {

View file

@ -5,14 +5,16 @@
#include "dsp.h"
#include <string.h>
#include "log.h"
#include "stdlib.h"
#include <assert.h>
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) {

View file

@ -14,7 +14,7 @@ struct bus {
int id;
};
extern soundbyte mastermix[BUF_FRAMES*CHANNELS];
extern soundbyte *mastermix;
void mixer_init();

View file

@ -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);

View file

@ -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);

View file

@ -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++)
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);

View file

@ -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

View file

@ -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) {

View file

@ -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;

View file

@ -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();
},

View file

@ -26,5 +26,5 @@ void main()
}
}
// frag_color = vec4(acc,1);
frag_color = vec4(acc,1);
}