Fix Linux and Windows build issues; texture looks in CDBs first

This commit is contained in:
John Alanbrook 2023-09-18 15:45:51 +00:00
parent c9c05d931d
commit 60d078321c
13 changed files with 54 additions and 63 deletions

View file

@ -127,7 +127,6 @@ LDLIBS := $(addprefix -l, $(LDLIBS))
DEPENDS = $(OBJS:.o=.d) DEPENDS = $(OBJS:.o=.d)
-include $(DEPENDS) -include $(DEPENDS)
MYTAG = $(VER)_$(PTYPE)_$(INFO) MYTAG = $(VER)_$(PTYPE)_$(INFO)
DIST = yugine-$(PLAT)-$(COM)$(ZIP) DIST = yugine-$(PLAT)-$(COM)$(ZIP)
@ -156,10 +155,12 @@ $(DISTDIR)/$(DIST): $(BIN)/$(NAME)
@mkdir -p $(DISTDIR) @mkdir -p $(DISTDIR)
@$(PKGCMD) @$(PKGCMD)
$(BIN)/libengine.a: $(OBJS) $(BIN)/libengine.a: $(SHADERS) source/engine/core.cdb.h .WAIT $(OBJS)
@$(AR) rcs $@ $(OBJS) @$(AR) rcs $@ $(OBJS)
$(BIN)/libcdb.a: $(BIN)/libcdb.a:
mkdir -p $(BIN)
rm -f $(CDB)/libcdb.a
make -C $(CDB) libcdb.a make -C $(CDB) libcdb.a
cp $(CDB)/libcdb.a $(BIN) cp $(CDB)/libcdb.a $(BIN)
@ -169,7 +170,7 @@ $(BIN)/libquickjs.a:
@mkdir -p $(BIN) @mkdir -p $(BIN)
cp -rf quickjs/libquickjs.* $(BIN) cp -rf quickjs/libquickjs.* $(BIN)
$(OBJDIR)/%.o: %.c $(SHADERS) $(OBJDIR)/%.o: %.c
@mkdir -p $(@D) @mkdir -p $(@D)
@echo Making C object $@ @echo Making C object $@
@$(CC) $(CFLAGS) -c $< -o $@ @$(CC) $(CFLAGS) -c $< -o $@
@ -182,7 +183,7 @@ shaders: $(SHADERS)
@./sokol-shdc --ifdef -i $^ --slang=glsl330:hlsl5:metal_macos -o $@ @./sokol-shdc --ifdef -i $^ --slang=glsl330:hlsl5:metal_macos -o $@
cdb: tools/cdb.c $(BIN)/libcdb.a cdb: tools/cdb.c $(BIN)/libcdb.a
$(CC) $^ -I$(CDB) -o cdb $(CC) $< -lcdb -L$(BIN) -I$(CDB) -o cdb
source/engine/core.cdb.h: core.cdb source/engine/core.cdb.h: core.cdb
xxd -i $< > $@ xxd -i $< > $@
@ -200,8 +201,8 @@ core.cdb: packer $(CORE)
packer: tools/packer.c $(BIN)/libcdb.a packer: tools/packer.c $(BIN)/libcdb.a
cc $^ -Isource/engine/thirdparty/tinycdb -o packer cc $^ -Isource/engine/thirdparty/tinycdb -o packer
jso: tools/jso.c quickjs/libquickjs.a jso: tools/jso.c $(BIN)/libquickjs.a
cc tools/jso.c -lquickjs -Lquickjs -Iquickjs -o jso $(CC) $< -lquickjs -lm -L$(BIN) -Iquickjs -o $@
%.jso: %.js jso %.jso: %.js jso
@echo Making $@ from $< @echo Making $@ from $<
@ -209,8 +210,10 @@ jso: tools/jso.c quickjs/libquickjs.a
clean: clean:
@echo Cleaning project @echo Cleaning project
@rm -rf bin/* dist/* @rm -rf bin dist
@rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer TAGS @rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer TAGS
@make -C $(CDB) clean
@make -C quickjs clean
TAGINC != find . -name "*.[chj]" TAGINC != find . -name "*.[chj]"
tags: $(TAGINC) tags: $(TAGINC)

View file

@ -208,7 +208,7 @@ Object.defineProperty(String.prototype, 'ext', {
Object.defineProperty(String.prototype, 'set_ext', { Object.defineProperty(String.prototype, 'set_ext', {
value: function(val) { value: function(val) {
return this.name() + val; return this.dir() + "/" + this.name() + val;
} }
}); });

View file

@ -1,14 +1,10 @@
var files = {}; var files = {};
function load(file) { function load(file) {
var modtime = cmd(0, file); var modtime = cmd(0, file);
if (modtime === 0) {
Log.stack();
return false;
}
files[file] = modtime; files[file] = modtime;
} }
load("scripts/base.js");
load("scripts/std.js"); load("scripts/std.js");
function initialize() function initialize()
@ -31,9 +27,6 @@ function run(file)
return cmd(117, file); return cmd(117, file);
} }
load("scripts/base.js");
load("scripts/diff.js"); load("scripts/diff.js");
Log.level = 1; Log.level = 1;
@ -629,5 +622,3 @@ Game.view_camera(Primum.spawn(ur.camera2d));
Window.name = "Primum Machinam (V0.1)"; Window.name = "Primum Machinam (V0.1)";
Window.width = 1280; Window.width = 1280;
Window.height = 720; Window.height = 720;
Log.warn("AMDE IT");

View file

@ -177,3 +177,4 @@ Cmdline.register_cmd("t", function() {
Log.warn("Testing not implemented yet."); Log.warn("Testing not implemented yet.");
Game.quit(); Game.quit();
}, "Test suite."); }, "Test suite.");

View file

@ -553,16 +553,6 @@ int point2segindex(cpVect p, cpVect *segs, double slop) {
return best; return best;
} }
int file_exists(char *path) {
FILE *o = fopen(path, "r");
if (o) {
fclose(o);
return 1;
}
return 0;
}
JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
int cmd = js2int(argv[0]); int cmd = js2int(argv[0]);
const char *str = NULL; const char *str = NULL;
@ -830,7 +820,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 65: case 65:
str = JS_ToCString(js, argv[1]); str = JS_ToCString(js, argv[1]);
ret = JS_NewBool(js, file_exists(str)); ret = JS_NewBool(js, fexists(str));
break; break;
case 66: case 66:
@ -1107,6 +1097,12 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 137: case 137:
ret = v22js(screen2world(js2hmmv2(argv[1]))); ret = v22js(screen2world(js2hmmv2(argv[1])));
break; break;
case 138:
str = JS_ToCString(js, argv[1]);
ret = JS_NewInt64(js, jso_file(str));
break;
} }
if (str) if (str)

View file

@ -94,7 +94,7 @@ char *seprint(char *fmt, ...)
char test[128]; char test[128];
int len = vsnprintf(test, 128, fmt, args); int len = vsnprintf(test, 128, fmt, args);
if (len > 128) { if (len > 128) {
char test = malloc(len+1); char *test = malloc(len+1);
vsnprintf(test, len+1, fmt, args); vsnprintf(test, len+1, fmt, args);
return strdup(test); return strdup(test);
} }
@ -190,6 +190,18 @@ void *cdb_slurp(struct cdb *cdb, const char *file, size_t *size)
return data; return data;
} }
int fexists(char *path)
{
return !access(path,R_OK);
int len = strlen(path);
if (cdb_find(&game_cdb, path,len)) return 1;
else if (cdb_find(&core_cdb, path, len)) return 1;
else if (!access(path, R_OK)) return 1;
return 0;
}
unsigned char *slurp_file(const char *filename, size_t *size) unsigned char *slurp_file(const char *filename, size_t *size)
{ {
if (cdb_find(&game_cdb, filename, strlen(filename))) if (cdb_find(&game_cdb, filename, strlen(filename)))

View file

@ -14,6 +14,7 @@ FILE *res_open(char *path, const char *tag);
FILE *path_open(const char *tag, const char *fmt, ...); FILE *path_open(const char *tag, const char *fmt, ...);
char *make_path(const char *file); char *make_path(const char *file);
char **ls(char *path); char **ls(char *path);
int fexists(char *path);
unsigned char *slurp_file(const char *filename, size_t *size); unsigned char *slurp_file(const char *filename, size_t *size);
char *slurp_text(const char *filename, size_t *size); char *slurp_text(const char *filename, size_t *size);

View file

@ -45,8 +45,8 @@ void script_startup() {
for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
num_cache[i] = int2js(i); num_cache[i] = int2js(i);
script_dofile("scripts/engine.js"); script_dofile("scripts/engine.js");
// jso_file("scripts/engine.js"); // jso_file("scripts/engine.js");
} }
@ -141,7 +141,7 @@ JSValue script_runjso(const uint8_t *buf, size_t len)
time_t jso_file(const char *file) time_t jso_file(const char *file)
{ {
size_t len; size_t len;
uint8_t *byte = compile_script(file, &len); uint8_t *byte = slurp_file(file, &len);
JSValue obj = JS_ReadObject(js, byte, len, JS_READ_OBJ_BYTECODE); JSValue obj = JS_ReadObject(js, byte, len, JS_READ_OBJ_BYTECODE);
JSValue ret = JS_EvalFunction(js, obj); JSValue ret = JS_EvalFunction(js, obj);
js_print_exception(ret); js_print_exception(ret);

View file

@ -25,7 +25,7 @@ static struct {
struct Texture *tex_default; struct Texture *tex_default;
struct Texture *texture_notex() { struct Texture *texture_notex() {
return texture_pullfromfile("./icons/no_tex.png"); return texture_pullfromfile("icons/no_tex.png");
} }
unsigned int next_pow2(unsigned int v) unsigned int next_pow2(unsigned int v)
@ -86,19 +86,22 @@ struct Texture *texture_pullfromfile(const char *path) {
int n; int n;
long rawlen;
unsigned char *raw = slurp_file(path, &rawlen);
unsigned char *data; unsigned char *data;
char *ext = strrchr(path, '.'); char *ext = strrchr(path, '.');
if (ext && !strcmp(ext, ".qoi")) { if (ext && !strcmp(ext, ".qoi")) {
qoi_desc qoi; qoi_desc qoi;
data = qoi_read(path, &qoi, 4); data = qoi_decode(raw, rawlen, &qoi, 4);
tex->width = qoi.width; tex->width = qoi.width;
tex->height = qoi.height; tex->height = qoi.height;
n = qoi.channels; n = qoi.channels;
} else { } else {
data = stbi_load(path, &tex->width, &tex->height, &n, 4); data = stbi_load_from_memory(raw, rawlen, &tex->width, &tex->height, &n, 4);
} }
free(raw);
if (data == NULL) { if (data == NULL) {
YughError("STBI failed to load file %s with message: %s\nOpening default instead.", path, stbi_failure_reason()); YughError("STBI failed to load file %s with message: %s\nOpening default instead.", path, stbi_failure_reason());

View file

@ -3897,6 +3897,7 @@ inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_
#define GL_TEXTURE_COMPARE_MODE 0x884C #define GL_TEXTURE_COMPARE_MODE 0x884C
#define GL_TEXTURE_COMPARE_FUNC 0x884D #define GL_TEXTURE_COMPARE_FUNC 0x884D
#define GL_COMPARE_REF_TO_TEXTURE 0x884E #define GL_COMPARE_REF_TO_TEXTURE 0x884E
#define GL_VIEWPORT 0x0BA2
#endif #endif
#ifndef GL_UNSIGNED_INT_2_10_10_10_REV #ifndef GL_UNSIGNED_INT_2_10_10_10_REV

View file

@ -147,7 +147,14 @@ cdb_make_finish_internal(struct cdb_make *cdbmp)
_cdb_make_fullwrite(cdbmp->cdb_fd, p, 2048) != 0) _cdb_make_fullwrite(cdbmp->cdb_fd, p, 2048) != 0)
return -1; return -1;
/* TODO: Windows doesn't support fsync; simple workaround is to not do it!
It does not seem that the lower level open(), read(), write() require it anyway.
*/
#ifdef _WIN32
return 0;
#else
return fsync(cdbmp->cdb_fd); return fsync(cdbmp->cdb_fd);
#endif
} }
static void static void

View file

@ -34,10 +34,6 @@
#include "string.h" #include "string.h"
#include "nuklear.h"
#include "sokol/sokol_gfx.h"
#include "sokol/sokol_nuklear.h"
#define SOKOL_TRACE_HOOKS #define SOKOL_TRACE_HOOKS
#define SOKOL_IMPL #define SOKOL_IMPL

View file

@ -4,21 +4,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
char *seprint(char *fmt, ...)
{
va_list args;
va_start (args, fmt);
char test[128];
int len = vsnprintf(test, 128, fmt, args);
if (len > 128) {
char test = malloc(len+1);
vsnprintf(test, len+1, fmt, args);
return test;
}
return strdup(test);
}
static JSContext *js = NULL; static JSContext *js = NULL;
static JSRuntime *rt = NULL; static JSRuntime *rt = NULL;
@ -46,18 +31,13 @@ int main (int argc, char **argv)
void *script = malloc(fsize); void *script = malloc(fsize);
fread(script,fsize,1,f); fread(script,fsize,1,f);
fclose(f); fclose(f);
JSValue obj = JS_Eval(js, script, fsize, argv[i], JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_FLAG_STRICT); JSValue obj = JS_Eval(js, script, fsize, argv[i], JS_EVAL_FLAG_COMPILE_ONLY);
size_t out_len; size_t out_len;
uint8_t *out; uint8_t *out;
out = JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE); out = JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE);
printf(out); printf(out);
return 0; return 0;
char *out_name = seprint("%s.o", argv[i]);
FILE *fo = fopen(out_name, "wb");
fputs(out, fo);
fclose(fo);
} }
return 0; return 0;