Fix web build; make all file searching fns look through slurp/the CDBs first

This commit is contained in:
John Alanbrook 2023-09-18 17:35:40 +00:00
parent 60d078321c
commit 03c9f0c93d
12 changed files with 71 additions and 45 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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 <stdarg.h>
#include "log.h"

View file

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

View file

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

View file

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

View file

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

View file

@ -6,6 +6,8 @@
#include "mix.h"
#include "sound.h"
#include "log.h"
#include "resources.h"
#include <stdlib.h>
#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;

View file

@ -7,6 +7,8 @@
#include <stb_ds.h>
#include <stb_image.h>
#include "resources.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"

View file

@ -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 <stb_ds.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_NO_STDIO
#include <stb_truetype.h>
#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