From 03c9f0c93db5ec2aeb2bbc4c381d2f2687b3d2bc Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 18 Sep 2023 17:35:40 +0000 Subject: [PATCH] Fix web build; make all file searching fns look through slurp/the CDBs first --- Makefile | 8 ++++---- scripts/engine.js | 2 +- source/engine/datastream.c | 6 +++++- source/engine/font.c | 4 ---- source/engine/nuke.c | 15 +-------------- source/engine/nuke.h | 16 ++++++++++++++++ source/engine/resources.c | 4 +++- source/engine/resources.h | 2 +- source/engine/sound.c | 33 +++++++++++++++++---------------- source/engine/sound/music.c | 16 +++++++++++++--- source/engine/texture.c | 2 ++ source/engine/yugine.c | 8 ++++++++ 12 files changed, 71 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index 0d33af6..9daa5d9 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ MAKEFLAGS = --jobs=4 UNAME != uname -MAKEDIR != pwd +nMAKEDIR != pwd # Options # DBG --- build with debugging symbols and logging @@ -78,7 +78,7 @@ ifeq ($(OS), Windows_NT) ZIP = .zip UNZIP = unzip -o -q $(DISTDIR)/$(DIST) -d $(DESTDIR) else ifeq ($(CC), emcc) - LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -pthread -sALLOW_MEMORY_GROWTH -sTOTAL_MEMORY=450MB --embed-file $(BIN)@ + LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -pthread -sTOTAL_MEMORY=450MB CFLAGS += -pthread LDLIBS += pthread quickjs GL openal c m dl CC = emcc @@ -166,7 +166,7 @@ $(BIN)/libcdb.a: $(BIN)/libquickjs.a: make -C quickjs clean - make -C quickjs OPT=$(OPT) HOST_CC=$(CC) libquickjs.a libquickjs.lto.a CC=$(CC) + make -C quickjs OPT=$(OPT) HOST_CC=$(CC) AR=$(AR) libquickjs.a libquickjs.lto.a CC=$(CC) @mkdir -p $(BIN) cp -rf quickjs/libquickjs.* $(BIN) @@ -180,7 +180,7 @@ shaders: $(SHADERS) %.sglsl.h:%.sglsl @echo Creating shader $^ - @./sokol-shdc --ifdef -i $^ --slang=glsl330:hlsl5:metal_macos -o $@ + @./sokol-shdc --ifdef -i $^ --slang=glsl330:hlsl5:metal_macos:glsl300es -o $@ cdb: tools/cdb.c $(BIN)/libcdb.a $(CC) $< -lcdb -L$(BIN) -I$(CDB) -o cdb diff --git a/scripts/engine.js b/scripts/engine.js index 4c8bf4c..3407344 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -2,7 +2,7 @@ var files = {}; function load(file) { var modtime = cmd(0, file); files[file] = modtime; -} +} load("scripts/base.js"); load("scripts/std.js"); diff --git a/source/engine/datastream.c b/source/engine/datastream.c index 17caf3e..0a27369 100644 --- a/source/engine/datastream.c +++ b/source/engine/datastream.c @@ -51,7 +51,11 @@ static void render_audio(plm_t *mpeg, plm_samples_t *samples, void *user) { } void ds_openvideo(struct datastream *ds, const char *video, const char *adriver) { - ds->plm = plm_create_with_filename(video); + long rawlen; + void *raw; + raw = slurp_file(video, &rawlen); + ds->plm = plm_create_with_memory(raw, rawlen, 0); + free(raw); if (!ds->plm) { YughError("Couldn't open %s", video); diff --git a/source/engine/font.c b/source/engine/font.c index 9407ac8..b068959 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -111,8 +111,6 @@ struct sFont *MakeSDFFont(const char *fontfile, int height) } struct sFont *MakeFont(const char *fontfile, int height) { - YughInfo("Making font %s.", fontfile); - int packsize = 1024; struct sFont *newfont = calloc(1, sizeof(struct sFont)); @@ -134,8 +132,6 @@ struct sFont *MakeFont(const char *fontfile, int height) { stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs); stbtt_PackEnd(&pc); - stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char) * packsize); - stbtt_fontinfo fontinfo; if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0))) { YughError("Failed to make font %s", fontfile); diff --git a/source/engine/nuke.c b/source/engine/nuke.c index bf896ad..4c0701b 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -2,31 +2,18 @@ #ifndef NO_EDITOR -#define NK_INCLUDE_STANDARD_IO -#define NK_IMPLEMENTATION -#define NK_KEYSTATE_BASED_INPUT -#define NK_INCLUDE_FIXED_TYPES -#define NK_INCLUDE_STANDARD_VARARGS -#define NK_INCLUDE_FONT_BAKING -#define NK_INCLUDE_DEFAULT_FONT -#define NK_INCLUDE_STANDARD_BOOL -#define NK_INCLUDE_DEFAULT_ALLOCATOR -#define NK_INCLUDE_STANDARD_IO -#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT - #define STBTT_STATIC #include "config.h" #include "sokol/sokol_gfx.h" +#define NK_IMPLEMENTATION #define SOKOL_NUKLEAR_IMPL -#define SOKOL_METAL #include "nuklear.h" #include "sokol/sokol_app.h" #include "sokol/sokol_nuklear.h" - #include #include "log.h" diff --git a/source/engine/nuke.h b/source/engine/nuke.h index b6db83d..467150e 100644 --- a/source/engine/nuke.h +++ b/source/engine/nuke.h @@ -2,6 +2,22 @@ #define NUKE_H #include "render.h" + +#define NK_INCLUDE_STANDARD_IO +#define NK_KEYSTATE_BASED_INPUT +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_STANDARD_VARARGS +#define NK_INCLUDE_FONT_BAKING +#define NK_INCLUDE_DEFAULT_FONT +#define NK_INCLUDE_STANDARD_BOOL +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_STANDARD_IO +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#include "nuklear.h" +#include "sokol/sokol_app.h" +#include "sokol/sokol_gfx.h" +#include "sokol/sokol_nuklear.h" + struct window; void nuke_init(struct window *win); diff --git a/source/engine/resources.c b/source/engine/resources.c index 0668814..62f03ff 100644 --- a/source/engine/resources.c +++ b/source/engine/resources.c @@ -202,7 +202,7 @@ int fexists(char *path) return 0; } -unsigned char *slurp_file(const char *filename, size_t *size) +void *slurp_file(const char *filename, size_t *size) { if (cdb_find(&game_cdb, filename, strlen(filename))) return cdb_slurp(&game_cdb, filename, size); @@ -296,4 +296,6 @@ void pack_engine(const char *fname) void pack_engine(const char *fname){ YughError("Cannot pack engine on a web build."); } + +char **ls(char *path) { return NULL; } #endif diff --git a/source/engine/resources.h b/source/engine/resources.h index 5fc4300..1898710 100644 --- a/source/engine/resources.h +++ b/source/engine/resources.h @@ -16,7 +16,7 @@ char *make_path(const char *file); char **ls(char *path); int fexists(char *path); -unsigned char *slurp_file(const char *filename, size_t *size); +void *slurp_file(const char *filename, size_t *size); char *slurp_text(const char *filename, size_t *size); int slurp_write(const char *txt, const char *filename); diff --git a/source/engine/sound.c b/source/engine/sound.c index 7c5d18b..eeacdfe 100644 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -18,26 +18,32 @@ #include "sokol/sokol_audio.h" +#define TSF_NO_STDIO #define TSF_IMPLEMENTATION #include "tsf.h" +#define TML_NO_STDIO #define TML_IMPLEMENTATION #include "tml.h" +#define DR_WAV_NO_STDIO #define DR_WAV_IMPLEMENTATION #include "dr_wav.h" - #ifndef NFLAC + #define DR_FLAC_IMPLEMENTATION +#define DR_FLAC_NO_STDIO #include "dr_flac.h" #endif #ifndef NMP3 +#define DR_MP3_NO_STDIO #define DR_MP3_IMPLEMENTATION #include "dr_mp3.h" #endif +#define QOA_NO_STDIO #define QOA_IMPLEMENTATION #include "qoa.h" @@ -143,43 +149,38 @@ struct wav *make_sound(const char *wav) { } struct wav mwav; + long rawlen; + void *raw = slurp_file(wav, &rawlen); - if (!strcmp(ext, "wav")) { - mwav.data = drwav_open_file_and_read_pcm_frames_f32(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); - } + if (!strcmp(ext, "wav")) + mwav.data = drwav_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); #ifndef NFLAC - else if (!strcmp(ext, "flac")) { - mwav.data = drflac_open_file_and_read_pcm_frames_f32(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); - } + else if (!strcmp(ext, "flac")) + mwav.data = drflac_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); #endif #ifndef NMP3 else if (!strcmp(ext, "mp3")) { drmp3_config cnf; - mwav.data = drmp3_open_file_and_read_pcm_frames_f32(wav, &cnf, &mwav.frames, NULL); + mwav.data = drmp3_open_memory_and_read_pcm_frames_f32(raw, rawlen, &cnf, &mwav.frames, NULL); mwav.ch = cnf.channels; mwav.samplerate = cnf.sampleRate; } #endif else if (!strcmp(ext, "qoa")) { - unsigned char header[QOA_MIN_FILESIZE]; - FILE *f = fopen(wav, "rb"); - fread(header, QOA_MIN_FILESIZE, 1, f); qoa_desc qoa; - unsigned int ff_pos = qoa_decode_header(header, QOA_MIN_FILESIZE, &qoa); + short *qoa_data = qoa_decode(raw, rawlen, &qoa); mwav.ch = qoa.channels; mwav.samplerate = qoa.samplerate; mwav.frames = qoa.samples; - - short *qoa_data = qoa_read(wav, &qoa); mwav.data = malloc(sizeof(soundbyte) * mwav.frames * mwav.ch); src_short_to_float_array(qoa_data, mwav.data, mwav.frames*mwav.ch); - - fclose(f); free(qoa_data); } else { YughWarn("Cannot process file type '%s'.", ext); + free (raw); return NULL; } + free(raw); if (mwav.samplerate != SAMPLERATE) mwav = change_samplerate(mwav, SAMPLERATE); diff --git a/source/engine/sound/music.c b/source/engine/sound/music.c index 299e57f..2c7d5cb 100644 --- a/source/engine/sound/music.c +++ b/source/engine/sound/music.c @@ -6,6 +6,8 @@ #include "mix.h" #include "sound.h" #include "log.h" +#include "resources.h" +#include #define TSF_BLOCK 32 @@ -58,18 +60,26 @@ struct bus *musicbus; void play_song(const char *midi, const char *sf) { - gsong.midi = tml_load_filename(midi); + long rawlen; + void *raw = slurp_file(midi, &rawlen); + + gsong.midi = tml_load_memory(raw, rawlen); if (gsong.midi == NULL) { YughWarn("Midi %s not found.", midi); + free(raw); return; } - - gsong.sf = tsf_load_filename(sf); + free(raw); + + raw = slurp_file(sf, &rawlen); + gsong.sf = tsf_load_memory(raw, rawlen); if (gsong.sf == NULL) { YughWarn("SF2 %s not found.", sf); + free(raw); return; } + free(raw); gsong.time = 0.f; diff --git a/source/engine/texture.c b/source/engine/texture.c index a7fb2be..40cbbc3 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -7,6 +7,8 @@ #include #include +#include "resources.h" + #define STB_IMAGE_RESIZE_IMPLEMENTATION #include "stb_image_resize.h" diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 7e21d17..0756195 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -34,6 +34,10 @@ #include "string.h" +#ifndef NO_EDITOR +#include "nuke.h" +#endif + #define SOKOL_TRACE_HOOKS #define SOKOL_IMPL @@ -46,11 +50,15 @@ #include #define STB_TRUETYPE_IMPLEMENTATION +#define STB_TRUETYPE_NO_STDIO #include #define STB_IMAGE_IMPLEMENTATION #define STBI_FAILURE_USERMSG +#define STBI_NO_STDIO +#ifdef __TINYC__ #define STBI_NO_SIMD +#endif #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION