move pain process loop into javascript

This commit is contained in:
John Alanbrook 2024-03-15 11:21:36 -05:00
parent 32b0cc7377
commit e03dc703ed
6 changed files with 24 additions and 137 deletions

View file

@ -146,7 +146,22 @@ global.mixin("scripts/render.js");
function process()
{
say ('holy cow');
var elapsed = 0.16;
prosperon.appupdate(elapsed);
prosperon.emitters_step(elapsed).
if (sim.play || sim.step) {
var dt = 0.16;
prosperon.update(dt*game.timescale);
if (sim.mode = "step")
sim.pause();
}
physlag += elapsed;
while (physlag > physMS) {
physlag -= physMS;
prosperon.physstep(physMS*timescale);
prosperon.physupdate(physMS*timescale);
}
prosperon.render();
}
global.Game = {

View file

@ -3,7 +3,6 @@
#include "script.h"
#include "anim.h"
#include "timer.h"
#include "debug.h"
#include "debugdraw.h"
#include "font.h"
@ -206,8 +205,6 @@ JSValue ptr2js(void *ptr) {
return obj;
}
struct timer *js2timer(JSValue v) { return id2timer(js2int(v)); }
double js_get_prop_number(JSValue v, const char *p) {
double num;
JS_ToFloat64(js, &num, js_getpropstr(v,p));
@ -1485,6 +1482,14 @@ GETSET_GLOBAL(physMS, number)
GETSET_GLOBAL(timescale, number)
GETSET_GLOBAL(updateMS, number)
JSValue js_emitters_step(JSContext *js, JSValue this, int argc, JSValue *argv)
{
emitters_step(js2number(argv[0]));
return JS_UNDEFINED;
}
JSValue js_
static const JSCFunctionListEntry js_prosperon_funcs[] = {
CGETSET_ADD(global, updateMS),
CGETSET_ADD(global, physMS),

View file

@ -5,7 +5,6 @@
#include "render.h"
#include "stb_ds.h"
#include "texture.h"
#include "timer.h"
#include "HandmadeMath.h"
#include "sprite.sglsl.h"

View file

@ -1,100 +0,0 @@
#include "timer.h"
#include "log.h"
#include <stdlib.h>
#include <stb_ds.h>
#include "script.h"
struct timer *timers;
static int first = -1;
void check_timer(struct timer *t, double dt) {
if (!t->on)
return;
t->remain_time -= dt;
if (t->remain_time <= 0) {
t->cb(t->data);
if (t->repeat) {
t->remain_time = t->interval;
return;
}
timer_pause(t);
return;
}
}
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 app) {
struct timer new;
new.remain_time = interval;
new.interval = interval;
new.cb = callback;
new.data = param;
new.repeat = 1;
new.owndata = own;
new.next = -1;
new.app = app;
if (first < 0) {
timer_start(&new);
arrput(timers, new);
return arrlen(timers) - 1;
} else {
int retid = first;
first = id2timer(first)->next;
*id2timer(retid) = new;
timer_start(id2timer(retid));
return retid;
}
}
void timer_pause(struct timer *t) {
if (!t->on) return;
t->on = 0;
}
void timer_stop(struct timer *t) {
if (!t->on) return;
t->on = 0;
t->remain_time = t->interval;
}
void timer_start(struct timer *t) {
if (t->on) return;
t->on = 1;
}
void timer_remove(int id) {
struct timer *t = id2timer(id);
// free_callee(t->data);
t->next = first;
t->on = 0;
first = id;
}
void timerr_settime(struct timer *t, double interval) {
t->remain_time += (interval - t->interval);
t->interval = interval;
}
struct timer *id2timer(int id) {
return &timers[id];
}
void timers_free()
{
// for (int i = 0; i < arrlen(timers); i++)
// if (timers[i].on)
// free_callee(timers[i].data);
}

View file

@ -1,29 +0,0 @@
#ifndef TIMER
#define TIMER
struct timer {
int timerid;
int on;
double interval; // Time of timer
int repeat;
double remain_time; // How much time until the timer executes
void (*cb)(void *data);
void *data;
int owndata;
int next;
int app; /* True if this timer is an "app" timer, and should always update; otherwise, only update with game time */
};
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, double scale);
void timerr_settime(struct timer *t, double interval);
void timers_free();
#endif

View file

@ -14,7 +14,6 @@
#include "datastream.h"
#include "timer.h"
#include "quickjs/quickjs.h"
#include "jsffi.h"
@ -104,8 +103,6 @@ static void process_frame()
script_call_sym(c_process_fn,0,NULL);
double elapsed = stm_sec(stm_laptime(&frame_t));
script_evalf("prosperon.appupdate(%g);", elapsed);
/* Timers all update every frame - once per monitor refresh */
timer_update(elapsed, timescale);
emitters_step(elapsed);