proper CPU timing; ur type extensions
This commit is contained in:
@@ -51,8 +51,8 @@ void mYughLog(int category, int priority, int line, const char *file, const char
|
||||
snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer);
|
||||
|
||||
log_print(buffer);
|
||||
if (category == LOG_SCRIPT && priority >= 2)
|
||||
js_stacktrace();
|
||||
// if (category == LOG_SCRIPT && priority >= 2)
|
||||
// js_stacktrace();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
#include "yugine.h"
|
||||
#include <assert.h>
|
||||
#include "resources.h"
|
||||
#include <sokol/sokol_time.h>
|
||||
|
||||
#include <glob.h>
|
||||
|
||||
#include "render.h"
|
||||
|
||||
@@ -56,6 +59,14 @@ JSValue js_getpropidx(JSValue v, uint32_t i)
|
||||
return p;
|
||||
}
|
||||
|
||||
uint64_t js2uint64(JSValue v)
|
||||
{
|
||||
int64_t i;
|
||||
JS_ToInt64(js, &i, v);
|
||||
uint64_t n = i;
|
||||
return n;
|
||||
}
|
||||
|
||||
int js2int(JSValue v) {
|
||||
int32_t i;
|
||||
JS_ToInt32(js, &i, v);
|
||||
@@ -70,6 +81,24 @@ JSValue str2js(const char *c) {
|
||||
return JS_NewString(js, c);
|
||||
}
|
||||
|
||||
JSValue strarr2js(const char **c, int len)
|
||||
{
|
||||
JSValue arr = JS_NewArray(js);
|
||||
for (int i = 0; i < len; i++)
|
||||
JS_SetPropertyUint32(js, arr, i, JS_NewString(js, c[i]));
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
JSValue glob2js(char *pat)
|
||||
{
|
||||
glob_t mglob;
|
||||
glob(pat, GLOB_NOSORT, NULL, &mglob);
|
||||
JSValue arr = strarr2js(mglob.gl_pathv, mglob.gl_pathc);
|
||||
globfree(&mglob);
|
||||
return arr;
|
||||
}
|
||||
|
||||
double js2number(JSValue v) {
|
||||
double g;
|
||||
JS_ToFloat64(js, &g, v);
|
||||
@@ -1050,6 +1079,9 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
|
||||
return num2js(get_timescale());
|
||||
break;
|
||||
case 122:
|
||||
str = JS_ToCString(js, argv[1]);
|
||||
ret = glob2js(str);
|
||||
|
||||
break;
|
||||
|
||||
case 123:
|
||||
@@ -1070,6 +1102,23 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
|
||||
case 126:
|
||||
mainwin.height = js2int(argv[2]);
|
||||
break;
|
||||
|
||||
case 127:
|
||||
ret = JS_NewInt64(js, stm_now());
|
||||
break;
|
||||
|
||||
case 128:
|
||||
YughWarn("%g",stm_ms(9737310));
|
||||
ret = JS_NewFloat64(js, stm_ns(js2uint64(argv[1])));
|
||||
break;
|
||||
|
||||
case 129:
|
||||
ret = JS_NewFloat64(js, stm_us(js2uint64(argv[1])));
|
||||
break;
|
||||
|
||||
case 130:
|
||||
ret = JS_NewFloat64(js, stm_ms(js2uint64(argv[1])));
|
||||
break;
|
||||
}
|
||||
|
||||
if (str)
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include <unistd.h>
|
||||
#include "font.h"
|
||||
|
||||
#include <glob.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include "cdb.h"
|
||||
|
||||
@@ -155,7 +157,7 @@ unsigned char *slurp_file(const char *filename, size_t *size)
|
||||
char *data = malloc(vlen);
|
||||
cdb_read(&game_cdb, data, vlen, vpos);
|
||||
if (size) *size = vlen;
|
||||
return strdup(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
FILE *f;
|
||||
@@ -183,7 +185,7 @@ char *slurp_text(const char *filename, size_t *size)
|
||||
char *str = slurp_file(filename, &len);
|
||||
char *retstr = malloc(len+1);
|
||||
memcpy(retstr, str, len);
|
||||
retstr[len] = 0;
|
||||
retstr[len] = '\0';
|
||||
free(str);
|
||||
if (size) *size = len;
|
||||
return retstr;
|
||||
@@ -198,23 +200,26 @@ int slurp_write(const char *txt, const char *filename) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int glob_err(const char *epath, int errno)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
static struct cdb_make cdbm;
|
||||
|
||||
static const char *pack_ext[] = {".qoi", ".qoa", ".js", ".wav", ".mp3", ".png", ".sf2", ".midi", ".lvl", ".glsl"};
|
||||
static const char *pack_ext[] = {".qoi", ".qoa", ".js", ".wav", ".mp3", ".png", ".sf2", ".midi", ".lvl", ".glsl", ".ttf"};
|
||||
|
||||
static int ftw_pack(const char *path, const struct stat *sb, int flag)
|
||||
{
|
||||
if (flag != FTW_F) return 0;
|
||||
|
||||
int pack = 0;
|
||||
|
||||
char *ext = strrchr(path, '.');
|
||||
|
||||
if (!ext)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
for (int i = 0; i < 11; i++) {
|
||||
if (!strcmp(ext, pack_ext[i])) {
|
||||
pack = 1;
|
||||
break;
|
||||
|
||||
@@ -169,17 +169,11 @@ struct wav *make_sound(const char *wav) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
YughWarn("%s opened with %d ch, %d samplerate, %d frames", ext, mwav.ch, mwav.samplerate, mwav.frames);
|
||||
|
||||
if (mwav.samplerate != SAMPLERATE) {
|
||||
YughWarn("Changing samplerate of %s from %d to %d.", wav, mwav.samplerate, SAMPLERATE);
|
||||
if (mwav.samplerate != SAMPLERATE)
|
||||
mwav = change_samplerate(mwav, SAMPLERATE);
|
||||
}
|
||||
|
||||
if (mwav.ch != CHANNELS) {
|
||||
YughWarn("Changing channels of %s from %d to %d.", wav, mwav.ch, CHANNELS);
|
||||
if (mwav.ch != CHANNELS)
|
||||
mwav = change_channels(mwav, CHANNELS);
|
||||
}
|
||||
|
||||
mwav.gain = 1.f;
|
||||
struct wav *newwav = malloc(sizeof(*newwav));
|
||||
@@ -187,8 +181,6 @@ struct wav *make_sound(const char *wav) {
|
||||
if (shlen(wavhash) == 0) sh_new_arena(wavhash);
|
||||
shput(wavhash, wav, newwav);
|
||||
|
||||
YughWarn("Channels %d, sr %d", newwav->ch,newwav->samplerate);
|
||||
|
||||
return newwav;
|
||||
}
|
||||
|
||||
|
||||
@@ -200,7 +200,15 @@ Object.defineProperty(String.prototype, 'shift', {
|
||||
|
||||
Object.defineProperty(String.prototype, 'ext', {
|
||||
value: function() {
|
||||
return this.slice(this.lastIndexOf('.'));
|
||||
var idx = this.lastIndexOf('.');
|
||||
if (idx === -1) return undefined;
|
||||
return this.slice(idx);
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(String.prototype, 'set_ext', {
|
||||
value: function(val) {
|
||||
return this.name() + val;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -208,6 +216,7 @@ Object.defineProperty(String.prototype, 'name', {
|
||||
value: function() {
|
||||
var s = this.lastIndexOf('/');
|
||||
var e = this.lastIndexOf('.');
|
||||
if (e === -1) e = this.length;
|
||||
return this.slice(s+1,e);
|
||||
}
|
||||
});
|
||||
@@ -505,6 +514,8 @@ Object.defineProperty(Array.prototype, 'lerp', {
|
||||
}
|
||||
});
|
||||
|
||||
Math.lerp = function(s,f,t) { return (f-s)*t + s; };
|
||||
|
||||
Object.defineProperty(Object.prototype, 'lerp',{
|
||||
value: function(to, t) {
|
||||
var self = this;
|
||||
|
||||
@@ -35,7 +35,8 @@ var sprite = clone(component, {
|
||||
rect: {s0:0, s1: 1, t0: 0, t1: 1},
|
||||
|
||||
get dimensions() { return cmd(64,this.path); },
|
||||
set dimensions(x) {},
|
||||
get width() { return cmd(64,this.path).x; },
|
||||
get height() { return cmd(64,this.path).y; },
|
||||
|
||||
make(go) {
|
||||
var sprite = Object.create(this);
|
||||
|
||||
@@ -124,13 +124,18 @@ var Gizmos = {
|
||||
};
|
||||
|
||||
var Profile = {
|
||||
cpu(fn, times) {
|
||||
tick_now() { return cmd(127); },
|
||||
ns(ticks) { return cmd(128, ticks); },
|
||||
us(ticks) { return cmd(129, ticks); },
|
||||
ms(ticks) { return cmd(130, ticks); },
|
||||
cpu(fn, times, q) {
|
||||
times ??= 1;
|
||||
var start = Date.now();
|
||||
q ??= "ns";
|
||||
var start = Profile.tick_now();
|
||||
for (var i = 0; i < times; i++)
|
||||
fn();
|
||||
|
||||
Log.warn(`Profiled in ${(Date.now()-start)/1000} seconds.`);
|
||||
var elapsed = Profile.tick_now() - start;
|
||||
Log.say(`Profiled in ${Profile[q](elapsed)/times} avg ${q}.`);
|
||||
},
|
||||
|
||||
get fps() { return sys_cmd(8); },
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
selectable
|
||||
*/
|
||||
|
||||
var required_files = ["proto.json"];
|
||||
|
||||
required_files.forEach(x => {
|
||||
if (!IO.exists(x)) IO.slurpwrite("", x);
|
||||
});
|
||||
prototypes.generate_ur('.');
|
||||
|
||||
/* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */
|
||||
var editor_level = gameobject.make(Primum);
|
||||
|
||||
@@ -13,9 +13,6 @@ load("scripts/std.js");
|
||||
|
||||
function initialize()
|
||||
{
|
||||
if (IO.exists("config.js"))
|
||||
load("config.js");
|
||||
|
||||
if (Cmdline.play)
|
||||
run("scripts/play.js");
|
||||
else
|
||||
@@ -629,24 +626,6 @@ World.unparent = function() { }
|
||||
|
||||
World.name = "World";
|
||||
World.fullpath = function() { return World.name; };
|
||||
World.load = function(lvl) {
|
||||
if (World.loaded)
|
||||
World.loaded.kill();
|
||||
|
||||
World.loaded = World.spawn(lvl);
|
||||
return World.loaded;
|
||||
};
|
||||
|
||||
World.run = function(file)
|
||||
{
|
||||
var newobject = {};
|
||||
newobject.kill = function() {
|
||||
Register.unregister_obj(newobject);
|
||||
}
|
||||
var script = IO.slurp(file);
|
||||
compile_env(`var self = this;${script}`, newobject, file);
|
||||
}
|
||||
|
||||
|
||||
/* Load configs */
|
||||
function load_configs(file) {
|
||||
@@ -734,7 +713,6 @@ Game.view_camera(camera2d.make(World));
|
||||
win_make(Game.title, Game.resolution[0], Game.resolution[1]);
|
||||
|
||||
/* Default objects */
|
||||
|
||||
var prototypes = {};
|
||||
prototypes.ur = {};
|
||||
prototypes.load_all = function()
|
||||
@@ -771,37 +749,52 @@ prototypes.from_file = function(file)
|
||||
|
||||
var newobj = gameobject.clone(file, {});
|
||||
var script = IO.slurp(file);
|
||||
compile_env(`var self = this;${script}`, newobj, file);
|
||||
prototypes.ur[file.name()] = newobj;
|
||||
return newobj;
|
||||
|
||||
newobj.$ = {};
|
||||
var json = {};
|
||||
if (IO.exists(file.name() + ".json")) {
|
||||
json = JSON.parse(IO.slurp(file.name() + ".json"));
|
||||
Object.assign(newobj.$, json.$);
|
||||
delete json.$;
|
||||
}
|
||||
|
||||
compile_env(`var self = this; var $ = self.$; ${script}`, newobj, file);
|
||||
dainty_assign(newobj, json);
|
||||
|
||||
var path = file.replaceAll('/', '.');
|
||||
path = path.name().split('.');
|
||||
var nested_access = function(base, names) {
|
||||
for (var i = 0; i < names.length; i++)
|
||||
base = base[names[i]] = base[names[i]] || {};
|
||||
|
||||
return base;
|
||||
};
|
||||
var a = nested_access(ur, path);
|
||||
|
||||
a.tag = path.at(-1);
|
||||
a.type = newobj;
|
||||
a.instances = [];
|
||||
|
||||
return a;
|
||||
}
|
||||
prototypes.from_file.doc = "Create a new ur-type from a given script file.";
|
||||
|
||||
prototypes.from_obj = function(name, obj)
|
||||
{
|
||||
var newobj = gameobject.clone(name, obj);
|
||||
prototypes.ur[name] = newobj;
|
||||
return newobj;
|
||||
prototypes.ur[name] = {
|
||||
tag: name,
|
||||
type: newobj
|
||||
};
|
||||
return prototypes.ur[name];
|
||||
}
|
||||
|
||||
prototypes.load_config = function(name)
|
||||
{
|
||||
if (!prototypes.config) {
|
||||
prototypes.config = {};
|
||||
|
||||
if (IO.exists("proto.json"))
|
||||
prototypes.config = JSON.parse(IO.slurp("proto.json"));
|
||||
}
|
||||
|
||||
Log.warn(`Loading a config for ${name}`);
|
||||
|
||||
if (!prototypes.ur[name])
|
||||
prototypes.ur[name] = gameobject.clone(name);
|
||||
|
||||
if (prototypes.config[name]) {
|
||||
Log.warn(`Assigning ${name} from config.`);
|
||||
dainty_assign(prototypes.config[name], prototypes.ur[name]);
|
||||
}
|
||||
Log.warn(`Made new ur of name ${name}`);
|
||||
|
||||
return prototypes.ur[name];
|
||||
}
|
||||
@@ -831,3 +824,19 @@ prototypes.from_obj("edge2d", {
|
||||
prototypes.from_obj("sprite", {
|
||||
sprite: sprite.clone(),
|
||||
});
|
||||
|
||||
prototypes.generate_ur = function(path)
|
||||
{
|
||||
var ob = IO.glob("*.js");
|
||||
ob = ob.concat(IO.glob("**/*.js"));
|
||||
ob = ob.filter(function(str) { return !str.startsWith("scripts"); });
|
||||
|
||||
ob.forEach(function(name) {
|
||||
if (name === "game.js") return;
|
||||
if (name === "play.js") return;
|
||||
|
||||
prototypes.from_file(name);
|
||||
});
|
||||
}
|
||||
|
||||
var ur = prototypes.ur;
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var gameobjects = {};
|
||||
var Prefabs = gameobjects;
|
||||
|
||||
function grab_from_points(pos, points, slop) {
|
||||
var shortest = slop;
|
||||
var idx = -1;
|
||||
@@ -15,14 +12,13 @@ function grab_from_points(pos, points, slop) {
|
||||
|
||||
var gameobject = {
|
||||
scale: 1.0,
|
||||
|
||||
save: true,
|
||||
|
||||
selectable: true,
|
||||
|
||||
spawn(ur) {
|
||||
Log.warn("DEPRECIATED");
|
||||
return ur.make(this);
|
||||
if (typeof ur === 'string')
|
||||
ur = prototypes.get_ur(ur);
|
||||
return ur.type.make(this);
|
||||
},
|
||||
|
||||
layer: 0, /* Collision layer; should probably have been called "mask" */
|
||||
@@ -54,12 +50,6 @@ var gameobject = {
|
||||
clone(name, ext) {
|
||||
var obj = Object.create(this);
|
||||
complete_assign(obj, ext);
|
||||
gameobjects[name] = obj;
|
||||
obj.defc('name', name);
|
||||
obj.from = this.name;
|
||||
obj.defn('instances', []);
|
||||
obj.obscure('from');
|
||||
|
||||
return obj;
|
||||
},
|
||||
|
||||
@@ -430,7 +420,7 @@ var gameobject = {
|
||||
|
||||
obj.check_registers(obj);
|
||||
|
||||
if ('begin' in obj) obj.begin();
|
||||
if (typeof obj.start === 'function') obj.start();
|
||||
|
||||
return obj;
|
||||
},
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
function compile_env(str, env, file)
|
||||
{
|
||||
file ??= "unknown";
|
||||
@@ -80,6 +79,7 @@ var IO = {
|
||||
},
|
||||
slurpwrite(str, file) { return cmd(39, str, file); },
|
||||
extensions(ext) { return cmd(66, "." + ext); },
|
||||
glob(pat) { return cmd(122, pat); },
|
||||
};
|
||||
|
||||
var Cmdline = {};
|
||||
|
||||
Reference in New Issue
Block a user