From e78e67357630c58c748b8619242acc76206bc677 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 6 Sep 2023 17:17:16 +0000 Subject: [PATCH] Quicjs optimizes similar to engine; objects lerp from their prototype --- Makefile | 5 ++-- quickjs/Makefile | 10 +++++++- source/engine/font.c | 1 - source/engine/resources.c | 49 +++++++++++---------------------------- source/engine/resources.h | 2 +- source/engine/script.c | 2 -- source/engine/yugine.c | 47 +++++++++++++++++++++++-------------- source/scripts/base.js | 11 +++------ source/scripts/engine.js | 13 +++++------ 9 files changed, 64 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index a33c668..9677afb 100755 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ endif ifeq ($(DBG),1) CFLAGS += -g INFO += dbg + LDFLAGS += -g else CFLAGS += -DNDEBUG LDFLAGS += -s @@ -149,11 +150,11 @@ $(DISTDIR)/$(DIST): $(BIN)/$(NAME) source/shaders/* $(SCRIPTS) assets/* @$(PKGCMD) $(BIN)/libengine.a: $(OBJS) - @$(AR) r $@ $^ + @$(AR) rcs $@ $^ $(BIN)/libquickjs.a: make -C quickjs clean - make -C quickjs libquickjs.a libquickjs.lto.a CC=$(CC) + make -C quickjs OPT=$(OPT) libquickjs.a libquickjs.lto.a CC=$(CC) cp quickjs/libquickjs.* $(BIN) $(OBJDIR)/%.o:%.c diff --git a/quickjs/Makefile b/quickjs/Makefile index 7f80ba9..20eef80 100644 --- a/quickjs/Makefile +++ b/quickjs/Makefile @@ -110,7 +110,7 @@ endif CFLAGS+=$(DEFINES) CFLAGS_DEBUG=$(CFLAGS) -O0 CFLAGS_SMALL=$(CFLAGS) -Os -CFLAGS_OPT=$(CFLAGS) -Oz +CFLAGS_OPT=$(CFLAGS) -O2 CFLAGS_NOLTO:=$(CFLAGS_OPT) LDFLAGS=-g ifdef CONFIG_LTO @@ -132,6 +132,14 @@ else LDEXPORT=-rdynamic endif +ifeq ($(OPT), small) + USEFLAGS = $(CFLAGS_SMALL) +else ifeq ($(OPT), 1) + USEFLAGS = $(CFLAGS_OPT) +else + USEFLAGS = $(CFLAGS_DEBUG) +endif + PROGS=qjs$(EXE) qjsc$(EXE) run-test262 ifneq ($(CROSS_PREFIX),) QJSC_CC=gcc diff --git a/source/engine/font.c b/source/engine/font.c index 80cd3af..6e301d2 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -126,7 +126,6 @@ struct sFont *MakeFont(const char *fontfile, int height) { snprintf(fontpath, 256, "fonts/%s", fontfile); unsigned char *ttf_buffer = slurp_file(fontfile, NULL); - YughWarn("TTF BUFFER P IS %p", ttf_buffer); unsigned char *bitmap = malloc(packsize * packsize); stbtt_packedchar glyphs[95]; diff --git a/source/engine/resources.c b/source/engine/resources.c index e0e675a..5bb57b8 100644 --- a/source/engine/resources.c +++ b/source/engine/resources.c @@ -146,7 +146,7 @@ char *strdup(const char *s) { return new; } -unsigned char *slurp_file(const char *filename, long *size) +unsigned char *slurp_file(const char *filename, size_t *size) { if (cdb_find(&game_cdb, filename, strlen(filename))) { unsigned vlen, vpos; @@ -154,6 +154,7 @@ unsigned char *slurp_file(const char *filename, long *size) vlen = cdb_datalen(&game_cdb); char *data = malloc(vlen); cdb_read(&game_cdb, data, vlen, vpos); + if (size) *size = vlen; return strdup(data); } @@ -165,9 +166,9 @@ unsigned char *slurp_file(const char *filename, long *size) if (!f) return NULL; fseek(f, 0, SEEK_END); - long fsize = ftell(f); - fseek(f, 0, SEEK_SET); - unsigned char *slurp = malloc(fsize + 1); + size_t fsize = ftell(f); + rewind(f); + void *slurp = malloc(fsize); fread(slurp, fsize, 1, f); fclose(f); @@ -178,37 +179,13 @@ unsigned char *slurp_file(const char *filename, long *size) char *slurp_text(const char *filename) { - if (cdb_find(&game_cdb, filename, strlen(filename))) { - unsigned vlen, vpos; - vpos = cdb_datapos(&game_cdb); - vlen = cdb_datalen(&game_cdb); - char *data = malloc(vlen); - cdb_read(&game_cdb, data, vlen, vpos); - return strdup(data); - } - - FILE *f; - char *buf; - - jump: - f = fopen(filename, "r"); - - if (!f) { - YughWarn("File %s doesn't exist.", filename); - return NULL; - } - - long int fsize; - fseek(f, 0, SEEK_END); - fsize = ftell(f); - buf = malloc(fsize + 1); - rewind(f); - size_t r = fread(buf, sizeof(char), fsize, f); - buf[r] = '\0'; - - fclose(f); - - return buf; + size_t len; + char *str = slurp_file(filename, &len); + char *retstr = malloc(len+1); + memcpy(retstr, str, len); + retstr[len] = 0; + free(str); + return retstr; } int slurp_write(const char *txt, const char *filename) { @@ -245,7 +222,7 @@ static int ftw_pack(const char *path, const struct stat *sb, int flag) if (!pack) return 0; - long len; + size_t len; void *file = slurp_file(path, &len); cdb_make_add(&cdbm, &path[2], strlen(&path[2]), file, len); diff --git a/source/engine/resources.h b/source/engine/resources.h index 119f603..857a85c 100644 --- a/source/engine/resources.h +++ b/source/engine/resources.h @@ -16,7 +16,7 @@ char *make_path(const char *file); char *strdup(const char *s); -unsigned char *slurp_file(const char *filename, long *size); +unsigned char *slurp_file(const char *filename, size_t *size); char *slurp_text(const char *filename); int slurp_write(const char *txt, const char *filename); diff --git a/source/engine/script.c b/source/engine/script.c index 7d2a185..a30a0da 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -45,8 +45,6 @@ void script_startup() { for (int i = 0; i < 100; i++) num_cache[i] = int2js(i); - - script_dofile("scripts/engine.js"); } JSValue num_cache[100] = {0}; diff --git a/source/engine/yugine.c b/source/engine/yugine.c index a28b209..ce5d621 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -87,6 +87,7 @@ static float timescale = 1.f; static int sim_play = SIM_PLAY; + #ifdef __TINYC__ int backtrace(void **buffer, int size) { extern uint64_t *__libc_stack_end; @@ -148,6 +149,8 @@ void c_init() { render_init(); script_evalf("initialize();"); + + } int frame_fps() { @@ -289,6 +292,25 @@ double get_timescale() return timescale; } +static sapp_desc start_desc = { + .width = 720, + .height = 1080, + .high_dpi = 0, + .sample_count = 1, + .fullscreen = 1, + .window_title = "Yugine", + .enable_clipboard = false, + .clipboard_size = 0, + .enable_dragndrop = true, + .max_dropped_files = 1, + .max_dropped_file_path_length = 2048, + .init_cb = c_init, + .frame_cb = c_frame, + .cleanup_cb = c_clean, + .event_cb = c_event, + .logger.func = sg_logging, +}; + sapp_desc sokol_main(int argc, char **argv) { #ifndef NDEBUG #ifdef __linux__ @@ -323,6 +345,8 @@ sapp_desc sokol_main(int argc, char **argv) { phys2d_init(); script_startup(); + + script_dofile("scripts/engine.js"); int argsize = 0; for (int i = 1; i < argc; i++) { @@ -343,22 +367,9 @@ sapp_desc sokol_main(int argc, char **argv) { sound_init(); input_init(); - return (sapp_desc){ - .width = mainwin.width, - .height = mainwin.height, - .high_dpi = 0, - .sample_count = 8, - .fullscreen = 0, - .window_title = "Yugine", - .enable_clipboard = false, - .clipboard_size = 0, - .enable_dragndrop = true, - .max_dropped_files = 1, - .max_dropped_file_path_length = 2048, - .init_cb = c_init, - .frame_cb = c_frame, - .cleanup_cb = c_clean, - .event_cb = c_event, - .logger.func = sg_logging, - }; + start_desc.width = mainwin.width; + start_desc.height = mainwin.height; + start_desc.fullscreen = 0; + + return start_desc; } diff --git a/source/scripts/base.js b/source/scripts/base.js index 19173bb..a5fc6ab 100644 --- a/source/scripts/base.js +++ b/source/scripts/base.js @@ -477,7 +477,8 @@ Object.defineProperty(Array.prototype, 'lerp', { } }); -Object.lerp = function(to, t) { +Object.defineProperty(Object.prototype, 'lerp',{ + value: function(to, t) { var self = this; var obj = {}; @@ -486,7 +487,7 @@ Object.lerp = function(to, t) { }); return obj; -}; +}}); /* MATH EXTENSIONS */ Object.defineProperty(Number.prototype, 'lerp', { @@ -496,12 +497,6 @@ Object.defineProperty(Number.prototype, 'lerp', { } }); -Math.lerp = function(from, to, t) { - var v = (to - from) * t + from; - v = Math.clamp(v, from, to); - return v; -} - Math.clamp = function (x, l, h) { return x > h ? h : x < l ? l : x; } Math.random_range = function(min,max) { return Math.random() * (max-min) + min; }; diff --git a/source/scripts/engine.js b/source/scripts/engine.js index 88ce9c4..918fd92 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -104,6 +104,11 @@ Cmdline.register_cmd("e", function(pawn) { Game.quit(); }, "Print input documentation for a given object in a markdown table." ); +Cmdline.register_cmd("t", function() { + Log.warn("Testing not implemented yet."); + Game.quit(); +}, "Test suite."); + function run(file) { var modtime = cmd(119, file); @@ -1142,18 +1147,12 @@ var Window = { set height(h) { cmd(126, h); }, get width() { return cmd(48); }, get height() { return cmd(49); }, - dimensions:[0,0], + get dimensions() { return [this.width, this.height]; }, }; Window.icon = function(path) { cmd(90, path); }; Window.icon.doc = "Set the icon of the window using the PNG image at path."; -Signal.register("window_resize", function(w, h) { - Window.width = w; - Window.height = h; - Window.dimensions = [w, h]; -}); - var IO = { exists(file) { return cmd(65, file);}, slurp(file) {