Add Sqlite packing; merge render and openglrender; remove ftw from web builds

This commit is contained in:
John Alanbrook 2023-09-04 14:48:44 +00:00
parent f41f547f63
commit 008458c7f1
22 changed files with 402 additions and 268 deletions

View file

@ -11,23 +11,26 @@ MAKEDIR != pwd
CC := $(notdir $(CC)) CC := $(notdir $(CC))
DBG ?= 1 DBG ?= 1
OPT ?= 0
QFLAGS :=
INFO := INFO :=
ifeq ($(DBG),1) ifeq ($(DBG),1)
QFLAGS += -g QFLAGS += -g
INFO = dbg INFO = dbg
ifeq ($(CC),tcc)
QFLAGS +=
endif
else else
QFLAGS += -DNDEBUG -s QFLAGS += -DNDEBUG -s
INFO = rel INFO = rel
endif endif
ifeq ($(OPT),1)
QFLAGS += -O2 -flto
AR = llvm-ar
INFO = opt
else
QFLAGS += -O0
endif
QFLAGS += -DHAVE_CEIL -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF QFLAGS += -DHAVE_CEIL -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF
# Uncomment for smallest binary # Uncomment for smallest binary
@ -47,7 +50,6 @@ ARCH = x64
COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -I. -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@ COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -I. -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@
ifeq ($(OS), Windows_NT) ifeq ($(OS), Windows_NT)
LINKER_FLAGS += -mwin32 -static LINKER_FLAGS += -mwin32 -static
COMPILER_FLAGS += -mwin32 COMPILER_FLAGS += -mwin32
@ -58,7 +60,7 @@ ifeq ($(OS), Windows_NT)
ZIP = .zip ZIP = .zip
UNZIP = unzip -o -q $(DISTDIR)/$(DIST) -d $(DESTDIR) UNZIP = unzip -o -q $(DISTDIR)/$(DIST) -d $(DESTDIR)
else ifeq ($(CC), emcc) else ifeq ($(CC), emcc)
LINKER_FLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -pthread LINKER_FLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -pthread --preload-file test.db -s ALLOW_MEMORY_GROWTH=1
COMPILER_FLAGS += -pthread COMPILER_FLAGS += -pthread
ELIBS += pthread quickjs GL openal c m dl ELIBS += pthread quickjs GL openal c m dl
CC = emcc CC = emcc
@ -172,11 +174,11 @@ $(BIN)libquickjs.a:
$(ENGINE): $(eobjects) $(ENGINE): $(eobjects)
@echo Making library engine.a @echo Making library engine.a
@ar r $(ENGINE) $(eobjects) @$(AR) r $(ENGINE) $(eobjects)
$(objprefix)/%.o:%.c $(objprefix)/%.o:%.c
@mkdir -p $(@D) @mkdir -p $(@D)
@echo Making C object $@ OS $(OS) @echo Making C object $@
@$(CC) $(COMPILER_FLAGS) @$(CC) $(COMPILER_FLAGS)
clean: clean:

View file

@ -169,7 +169,6 @@ void phys2d_init()
} }
void phys2d_set_gravity(cpVect v) { void phys2d_set_gravity(cpVect v) {
YughInfo("Set gravity to %g %g", v.x, v.y);
cpSpaceSetGravity(space, v); cpSpaceSetGravity(space, v);
} }
@ -632,8 +631,8 @@ static cpBool handle_collision(cpArbiter *arb, int type) {
cpBody *body1; cpBody *body1;
cpBody *body2; cpBody *body2;
cpArbiterGetBodies(arb, &body1, &body2); cpArbiterGetBodies(arb, &body1, &body2);
int g1 = *(int*)cpBodyGetUserData(body1); int g1 = (int)cpBodyGetUserData(body1);
int g2 = *(int*)cpBodyGetUserData(body2); int g2 = (int)cpBodyGetUserData(body2);
struct gameobject *go = id2go(g1); struct gameobject *go = id2go(g1);
struct gameobject *go2 = id2go(g2); struct gameobject *go2 = id2go(g2);

View file

@ -7,7 +7,7 @@
#include "font.h" #include "font.h"
#include "window.h" #include "window.h"
#include "openglrender.h" #include "render.h"
// #define HANDMADE_MATH_USE_TURNS // #define HANDMADE_MATH_USE_TURNS
#include "HandmadeMath.h" #include "HandmadeMath.h"

View file

@ -5,7 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "openglrender.h" #include "render.h"
static const float skyboxVertices[216] = { static const float skyboxVertices[216] = {
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,

View file

@ -6,7 +6,6 @@
#include "limits.h" #include "limits.h"
#include "log.h" #include "log.h"
#include "mix.h" #include "mix.h"
#include "render.h"
#include "resources.h" #include "resources.h"
#include "shader.h" #include "shader.h"
#include "sound.h" #include "sound.h"
@ -14,7 +13,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "font.h" #include "font.h"
#include "openglrender.h" #include "render.h"
#define CBUF_IMPLEMENT #define CBUF_IMPLEMENT
#include "cbuf.h" #include "cbuf.h"

View file

@ -1,10 +1,7 @@
#include "debugdraw.h" #include "debugdraw.h"
#include "openglrender.h"
#include "render.h" #include "render.h"
#include "yugine.h" #include "yugine.h"
#include "shader.h" #include "shader.h"
#include "log.h" #include "log.h"
#include <assert.h> #include <assert.h>
@ -461,7 +458,7 @@ void inflatepoints(cpVect *r, cpVect *p, float d, int n)
void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg) void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg)
{ {
static_assert(sizeof(cpVect) == 2*sizeof(float), "Size of cpVect is not 2 floats."); static_assert(sizeof(cpVect) == 2*sizeof(float));
if (thickness == 0) { if (thickness == 0) {
thickness = 1; thickness = 1;
} }

View file

@ -13,7 +13,6 @@
#include "mix.h" #include "mix.h"
#include "music.h" #include "music.h"
#include "nuke.h" #include "nuke.h"
#include "openglrender.h"
#include "sound.h" #include "sound.h"
#include "sprite.h" #include "sprite.h"
#include "stb_ds.h" #include "stb_ds.h"
@ -22,7 +21,7 @@
#include "window.h" #include "window.h"
#include "yugine.h" #include "yugine.h"
#include <assert.h> #include <assert.h>
#include <ftw.h> #include "resources.h"
#include "render.h" #include "render.h"
@ -412,7 +411,7 @@ JSValue duk_win_make(JSContext *js, JSValueConst this, int argc, JSValueConst *a
} }
JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
static_assert(sizeof(tsReal) * 2 == sizeof(cpVect), "Size of tsRealx2 is not cpVect"); static_assert(sizeof(tsReal) * 2 == sizeof(cpVect));
tsBSpline spline; tsBSpline spline;
@ -552,10 +551,7 @@ int file_exists(char *path) {
return 0; return 0;
} }
static char *dukext; /*
static JSValue dukarr;
static int dukidx;
static int duk2path(const char *path, const struct stat *sb, int typeflag) { static int duk2path(const char *path, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) { if (typeflag == FTW_F) {
char *ext = strrchr(path, '.'); char *ext = strrchr(path, '.');
@ -565,13 +561,13 @@ static int duk2path(const char *path, const struct stat *sb, int typeflag) {
return 0; return 0;
} }
*/
JSValue dukext2paths(char *ext) { JSValue dukext2paths(char *ext) {
dukext = ext; char *paths = NULL;
dukarr = JS_NewArray(js);
dukidx = 0; fill_extensions(paths, ".", ext);
ftw(".", duk2path, 10); // return dukarr;
return dukarr; return JS_NULL;
} }
JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
@ -1061,6 +1057,18 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
str = JS_ToCString(js, argv[1]); str = JS_ToCString(js, argv[1]);
file_eval_env(str, argv[2]); file_eval_env(str, argv[2]);
break; break;
case 124:
pack_engine();
break;
case 125:
mainwin.width = js2int(argv[1]);
break;
case 126:
mainwin.height = js2int(argv[2]);
break;
} }
if (str) if (str)

View file

@ -11,8 +11,7 @@
#include <window.h> #include <window.h>
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
#include "2dphysics.h" #include "2dphysics.h"
#include "resources.h"
#include "openglrender.h"
#include "stb_image_write.h" #include "stb_image_write.h"
#include "stb_rect_pack.h" #include "stb_rect_pack.h"
@ -24,51 +23,6 @@ struct sFont *font;
#define max_chars 40000 #define max_chars 40000
unsigned char *slurp_file(const char *filename) {
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *slurp = malloc(fsize + 1);
fread(slurp, fsize, 1, f);
fclose(f);
return slurp;
}
char *slurp_text(const char *filename) {
FILE *f = fopen(filename, "r");
if (!f) {
YughWarn("File %s doesn't exist.", filename);
return NULL;
}
char *buf;
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;
}
int slurp_write(const char *txt, const char *filename) {
FILE *f = fopen(filename, "w");
if (!f) return 1;
fputs(txt, f);
fclose(f);
return 0;
}
static sg_shader fontshader; static sg_shader fontshader;
static sg_bindings bind_text; static sg_bindings bind_text;
@ -83,7 +37,7 @@ struct text_vert {
static struct text_vert text_buffer[max_chars]; static struct text_vert text_buffer[max_chars];
void font_init(struct shader *textshader) { void font_init() {
fontshader = sg_compile_shader("shaders/textvert.glsl", "shaders/textfrag.glsl", &(sg_shader_desc){ fontshader = sg_compile_shader("shaders/textvert.glsl", "shaders/textfrag.glsl", &(sg_shader_desc){
.vs.uniform_blocks[0] = { .vs.uniform_blocks[0] = {
.size = sizeof(float) * 16, .size = sizeof(float) * 16,
@ -129,7 +83,7 @@ void font_init(struct shader *textshader) {
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "text buffer"}); .label = "text buffer"});
font = MakeFont("LessPerfectDOSVGA.ttf", 16); font = MakeFont("fonts/LessPerfectDOSVGA.ttf", 16);
bind_text.fs_images[0] = font->texID; bind_text.fs_images[0] = font->texID;
} }
@ -144,7 +98,7 @@ struct sFont *MakeSDFFont(const char *fontfile, int height)
char fontpath[256]; char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile); snprintf(fontpath, 256, "fonts/%s", fontfile);
unsigned char *ttf_buffer = slurp_file(fontpath); unsigned char *ttf_buffer = slurp_file(fontpath, NULL);
unsigned char *bitmap = malloc(packsize * packsize); unsigned char *bitmap = malloc(packsize * packsize);
stbtt_fontinfo fontinfo; stbtt_fontinfo fontinfo;
@ -170,8 +124,9 @@ struct sFont *MakeFont(const char *fontfile, int height) {
char fontpath[256]; char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile); snprintf(fontpath, 256, "fonts/%s", fontfile);
unsigned char *ttf_buffer = slurp_file(fontpath); unsigned char *ttf_buffer = slurp_file(fontfile, NULL);
YughWarn("TTF BUFFER P IS %p", ttf_buffer);
unsigned char *bitmap = malloc(packsize * packsize); unsigned char *bitmap = malloc(packsize * packsize);
stbtt_packedchar glyphs[95]; stbtt_packedchar glyphs[95];

View file

@ -29,7 +29,7 @@ struct sFont {
sg_image texID; sg_image texID;
}; };
void font_init(struct shader *s); void font_init();
struct sFont *MakeFont(const char *fontfile, int height); struct sFont *MakeFont(const char *fontfile, int height);
void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color); void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color);
void text_settype(struct sFont *font); void text_settype(struct sFont *font);
@ -39,9 +39,4 @@ int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, f
// void text_frame(); // void text_frame();
void text_flush(HMM_Mat4 *proj); void text_flush(HMM_Mat4 *proj);
unsigned char *slurp_file(const char *filename);
char *slurp_text(const char *filename);
int slurp_write(const char *txt, const char *filename);
#endif #endif

View file

@ -9,6 +9,7 @@
#include "time.h" #include "time.h"
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include "resources.h"
#include "stb_ds.h" #include "stb_ds.h"
@ -201,9 +202,6 @@ static void pawn_call_keydown(int key) {
void set_mouse_mode(int mousemode) { sapp_lock_mouse(mousemode); } void set_mouse_mode(int mousemode) { sapp_lock_mouse(mousemode); }
void input_init() { void input_init() {
char *paddb = slurp_text("data/gamecontrollerdb.txt");
free(paddb);
jsaxesstr[0] = str2js("ljoy"); jsaxesstr[0] = str2js("ljoy");
jsaxesstr[1] = str2js("rjoy"); jsaxesstr[1] = str2js("rjoy");
jsaxesstr[2] = str2js("ltrigger"); jsaxesstr[2] = str2js("ltrigger");

View file

@ -1,67 +0,0 @@
#ifndef OPENGL_RENDER_H
#define OPENGL_RENDER_H
#include "sokol/sokol_gfx.h"
#include "HandmadeMath.h"
struct mCamera;
struct window;
extern struct shader *spriteShader;
extern struct shader *animSpriteShader;
extern sg_image ddimg;
extern struct sprite *tsprite;
extern int renderMode;
extern HMM_Vec3 dirl_pos;
extern HMM_Mat4 projection;
extern HMM_Mat4 hudproj;
extern float gridScale;
extern float smallGridUnit;
extern float bigGridUnit;
extern float gridSmallThickness;
extern float gridBigThickness;
extern struct rgba gridBigColor;
extern struct rgba gridSmallColor;
extern float gridOpacity;
extern float editorFOV;
extern float shadowLookahead;
extern char objectName[];
extern int debugColorPickBO;
extern struct gameobject *selectedobject;
#include <chipmunk/chipmunk.h>
enum RenderMode {
LIT,
UNLIT,
WIREFRAME,
DIRSHADOWMAP,
OBJECTPICKER
};
void openglInit();
void openglRender(struct window *window);
void opengl_rendermode(enum RenderMode r);
void openglInit3d(struct window *window);
void openglRender3d(struct window *window, struct mCamera *camera);
void render_winsize();
void debug_draw_phys(int draw);
void set_cam_body(cpBody *body);
cpVect cam_pos();
float cam_zoom();
void add_zoom(float val);
sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d);
#endif

View file

@ -1,4 +1,4 @@
#include "openglrender.h" #include "render.h"
#include "camera.h" #include "camera.h"
#include "config.h" #include "config.h"
@ -13,6 +13,7 @@
#include "window.h" #include "window.h"
#include "model.h" #include "model.h"
#include "stb_ds.h" #include "stb_ds.h"
#include "resources.h"
#include "sokol/sokol_app.h" #include "sokol/sokol_app.h"
@ -93,30 +94,29 @@ static struct {
sg_image depth_img; sg_image depth_img;
} crt_post; } crt_post;
void make_shader(sg_shader_desc *d, sg_shader result, void *data) void trace_make_shader(sg_shader_desc *d, sg_shader result, void *data)
{ {
if (sg_query_shader_state(result) == SG_RESOURCESTATE_FAILED) { if (sg_query_shader_state(result) == SG_RESOURCESTATE_FAILED)
YughWarn("FAILED MAKING A SHADER: %s\n%s\n%s", d->label, d->vs.source, d->fs.source); YughError("FAILED MAKING A SHADER: %s\n%s\n%s", d->label);
}
} }
void fail_shader(sg_shader id, void *data) void trace_fail_shader(sg_shader id, void *data)
{ {
YughWarn("SHADER DID NOT COMPILE"); YughWarn("SHADER DID NOT COMPILE");
} }
void destroy_shader(sg_shader shd, void *data) void trace_destroy_shader(sg_shader shd, void *data)
{ {
YughWarn("DESTROYED SHADER"); YughWarn("DESTROYED SHADER");
} }
static sg_trace_hooks hooks = { static sg_trace_hooks hooks = {
.fail_shader = fail_shader, .fail_shader = trace_fail_shader,
.make_shader = make_shader, .make_shader = trace_make_shader,
.destroy_shader = destroy_shader, .destroy_shader = trace_destroy_shader,
}; };
void openglInit() { void render_init() {
mainwin.width = sapp_width(); mainwin.width = sapp_width();
mainwin.height = sapp_height(); mainwin.height = sapp_height();
@ -132,7 +132,7 @@ void openglInit() {
sg_trace_hooks hh = sg_install_trace_hooks(&hooks); sg_trace_hooks hh = sg_install_trace_hooks(&hooks);
font_init(NULL); font_init();
debugdraw_init(); debugdraw_init();
sprite_initialize(); sprite_initialize();
nuke_init(&mainwin); nuke_init(&mainwin);
@ -383,8 +383,10 @@ sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d)
d->vs.source = vs; d->vs.source = vs;
d->fs.source = fs; d->fs.source = fs;
d->label = v;
sg_shader ret = sg_make_shader(d); sg_shader ret = sg_make_shader(d);
free(vs); free(vs);
free(fs); free(fs);
return ret; return ret;

View file

@ -1,10 +1,69 @@
#ifndef RENDER_H #ifndef OPENGL_RENDER_H
#define RENDER_H #define OPENGL_RENDER_H
#include "sokol/sokol_gfx.h" #include "sokol/sokol_gfx.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
struct mCamera;
struct window;
extern struct shader *spriteShader;
extern struct shader *animSpriteShader;
extern sg_image ddimg;
extern struct sprite *tsprite;
extern int renderMode;
extern HMM_Vec3 dirl_pos;
extern HMM_Mat4 projection;
extern HMM_Mat4 hudproj;
extern float gridScale;
extern float smallGridUnit;
extern float bigGridUnit;
extern float gridSmallThickness;
extern float gridBigThickness;
extern struct rgba gridBigColor;
extern struct rgba gridSmallColor;
extern float gridOpacity;
extern float editorFOV;
extern float shadowLookahead;
extern char objectName[];
extern int debugColorPickBO;
extern struct gameobject *selectedobject;
#include <chipmunk/chipmunk.h>
enum RenderMode {
LIT,
UNLIT,
WIREFRAME,
DIRSHADOWMAP,
OBJECTPICKER
};
void render_init();
void openglRender(struct window *window);
void opengl_rendermode(enum RenderMode r);
void openglInit3d(struct window *window);
void openglRender3d(struct window *window, struct mCamera *camera);
void render_winsize();
void debug_draw_phys(int draw);
void set_cam_body(cpBody *body);
cpVect cam_pos();
float cam_zoom();
void add_zoom(float val);
sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d);
struct uv_n { struct uv_n {
unsigned short u; unsigned short u;
unsigned short v; unsigned short v;

View file

@ -11,8 +11,13 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "font.h"
#include <sqlite3.h>
#ifndef __EMSCRIPTEN__
#include <ftw.h> #include <ftw.h>
#endif
#include "stb_ds.h" #include "stb_ds.h"
@ -21,9 +26,6 @@ char *PREF_PATH = NULL;
char **prefabs; char **prefabs;
const char *EXT_PREFAB = ".prefab";
const char *EXT_LEVEL = ".level";
const char *EXT_ASSET = ".asset";
int stemlen = 0; int stemlen = 0;
static const char *cur_ext = NULL; static const char *cur_ext = NULL;
@ -31,6 +33,12 @@ struct dirent *c_dirent = NULL;
char pathbuf[MAXPATH + 1]; char pathbuf[MAXPATH + 1];
const char *DB_NAME = "test.db";
static sqlite3 *game_db = NULL;
static sqlite3_stmt *fopen_stmt;
#define sqlite_perr(db) YughError("Database error code %d, %s: %s", sqlite3_errcode(db), sqlite3_errstr(sqlite3_errcode(db)), sqlite3_errmsg(db));
void resources_init() { void resources_init() {
DATA_PATH = malloc(MAXPATH); DATA_PATH = malloc(MAXPATH);
getcwd(DATA_PATH, MAXPATH); getcwd(DATA_PATH, MAXPATH);
@ -38,6 +46,18 @@ void resources_init() {
if (!PREF_PATH) if (!PREF_PATH)
PREF_PATH = strdup("./tmp/"); PREF_PATH = strdup("./tmp/");
if (sqlite3_open_v2("test.db", &game_db, SQLITE_OPEN_READONLY, NULL)) {
sqlite_perr(game_db);
sqlite3_close(game_db);
game_db = NULL;
return;
}
if (sqlite3_prepare_v2(game_db, "select data from files where path=?1", -1, &fopen_stmt, NULL)) {
sqlite_perr(game_db);
}
} }
char *get_filename_from_path(char *path, int extension) { char *get_filename_from_path(char *path, int extension) {
@ -73,6 +93,10 @@ FILE *res_open(char *path, const char *tag) {
return f; return f;
} }
static char *ext_paths = NULL;
#ifndef __EMSCRIPTEN__
static int ext_check(const char *path, const struct stat *sb, int typeflag) { static int ext_check(const char *path, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) { if (typeflag == FTW_F) {
const char *ext = strrchr(path, '.'); const char *ext = strrchr(path, '.');
@ -89,12 +113,13 @@ static int ext_check(const char *path, const struct stat *sb, int typeflag) {
void fill_extensions(char *paths, const char *path, const char *ext) { void fill_extensions(char *paths, const char *path, const char *ext) {
cur_ext = ext; cur_ext = ext;
arrfree(paths); arrfree(paths);
ext_paths = paths;
ftw(".", ext_check, 10); ftw(".", ext_check, 10);
} }
#else
void findPrefabs() { void fill_extensions(char *paths, const char *path, const char *ext)
fill_extensions(prefabs, DATA_PATH, EXT_PREFAB); {};
} #endif
char *str_replace_ext(const char *s, const char *newext) { char *str_replace_ext(const char *s, const char *newext) {
static char ret[256]; static char ret[256];
@ -127,3 +152,173 @@ char *strdup(const char *s) {
strcpy(new, s); strcpy(new, s);
return new; return new;
} }
unsigned char *slurp_file(const char *filename, long *size)
{
if (game_db) {
sqlite3_reset(fopen_stmt);
if (sqlite3_bind_text(fopen_stmt, 1, filename, -1, NULL)) {
sqlite_perr(game_db);
goto jump;
}
if (sqlite3_step(fopen_stmt) == SQLITE_ERROR) {
sqlite_perr(game_db);
goto jump;
}
char *data = sqlite3_column_blob(fopen_stmt,0);
if (!data)
goto jump;
return strdup(data);
}
FILE *f;
jump:
f = fopen(filename, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *slurp = malloc(fsize + 1);
fread(slurp, fsize, 1, f);
fclose(f);
if (size) *size = fsize;
return slurp;
}
char *slurp_text(const char *filename) {
if (game_db) {
if (sqlite3_reset(fopen_stmt)) {
sqlite_perr(game_db);
goto jump;
}
if (sqlite3_bind_text(fopen_stmt, 1, filename, -1, NULL)) {
sqlite_perr(game_db);
goto jump;
}
if (sqlite3_step(fopen_stmt) == SQLITE_ERROR) {
sqlite_perr(game_db);
goto jump;
}
char *data = sqlite3_column_text(fopen_stmt,0);
if (!data)
goto jump;
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;
}
int slurp_write(const char *txt, const char *filename) {
FILE *f = fopen(filename, "w");
if (!f) return 1;
fputs(txt, f);
fclose(f);
return 0;
}
#ifndef __EMSCRIPTEN__
static sqlite3 *pack_db = NULL;
static sqlite3_stmt *pack_stmt;
static const char *pack_ext[] = {".qoi", ".qoa", ".js", ".wav", ".mp3", ".png", ".sf2", ".midi", ".lvl", ".glsl"};
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++) {
if (!strcmp(ext, pack_ext[i])) {
pack = 1;
break;
}
}
if (!pack) return 0;
long len;
void *file = slurp_file(path, &len);
if (sqlite3_bind_text(pack_stmt, 1, &path[2], -1, NULL))
sqlite_perr(pack_db);
if (sqlite3_bind_blob(pack_stmt, 2, file, len, NULL))
sqlite_perr(pack_db);
if (sqlite3_step(pack_stmt) != SQLITE_DONE)
sqlite_perr(pack_db);
free(file);
if (sqlite3_reset(pack_stmt))
sqlite_perr(pack_db);
return 0;
}
void pack_engine()
{
sqlite3 *db;
char *zErr = 0;
if (sqlite3_open("test.db", &db)) {
sqlite_perr(db);
sqlite3_close(db);
return;
}
if(sqlite3_exec(db, "create table files ( path text, data blob);", NULL, NULL, NULL))
sqlite_perr(db);
pack_db = db;
if(sqlite3_prepare_v2(db, "insert into files (path, data) values (?1, ?2)", -1, &pack_stmt, NULL)) {
sqlite_perr(db);
sqlite3_close(db);
return;
}
ftw(".", ftw_pack, 20);
sqlite3_close(db);
}
#else
void packengine(){
YughError("Cannot pack engine on a web build.");
}
#endif

View file

@ -3,18 +3,9 @@
#include <stdio.h> #include <stdio.h>
extern char *DATA_PATH;
extern char *PREF_PATH;
extern const char *EXT_PREFAB;
extern const char *EXT_LEVEL;
extern const char *EXT_ASSET;
extern int stemlen; extern int stemlen;
void resources_init(); void resources_init();
extern char **prefabs;
void findPrefabs();
void fill_extensions(char *paths, const char *path, const char *ext); void fill_extensions(char *paths, const char *path, const char *ext);
char *get_filename_from_path(char *path, int extension); char *get_filename_from_path(char *path, int extension);
char *get_directory_from_path(char *path); char *get_directory_from_path(char *path);
@ -25,4 +16,10 @@ char *make_path(const char *file);
char *strdup(const char *s); char *strdup(const char *s);
unsigned char *slurp_file(const char *filename, long *size);
char *slurp_text(const char *filename);
int slurp_write(const char *txt, const char *filename);
void pack_engine();
#endif #endif

View file

@ -15,6 +15,7 @@
#include "sys/stat.h" #include "sys/stat.h"
#include "sys/types.h" #include "sys/types.h"
#include "time.h" #include "time.h"
#include "resources.h"
#include <stdarg.h> #include <stdarg.h>
@ -42,6 +43,11 @@ void script_startup() {
JS_SetMaxStackSize(rt, 0); JS_SetMaxStackSize(rt, 0);
js = JS_NewContext(rt); js = JS_NewContext(rt);
ffi_load(); ffi_load();
for (int i = 0; i < 100; i++)
num_cache[i] = int2js(i);
script_dofile("scripts/engine.js");
} }
JSValue num_cache[100] = {0}; JSValue num_cache[100] = {0};
@ -73,17 +79,6 @@ int js_print_exception(JSValue v) {
return 0; return 0;
} }
void script_init() {
/* Load all prefabs into memory */
// if (DBG)
// script_dofile("scripts/debug.js");
// else
script_dofile("scripts/engine.js");
for (int i = 0; i < 100; i++)
num_cache[i] = int2js(i);
}
void script_run(const char *script, const char *file) { void script_run(const char *script, const char *file) {
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS); JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS);
js_print_exception(obj); js_print_exception(obj);

View file

@ -17,7 +17,6 @@ extern JSValue num_cache[100];
void js_stacktrace(); void js_stacktrace();
void script_startup(); void script_startup();
void script_init();
void script_run(const char *script, const char *file); void script_run(const char *script, const char *file);
JSValue script_compile(const char *file); JSValue script_compile(const char *file);
void script_evalf(const char *format, ...); void script_evalf(const char *format, ...);

View file

@ -4,7 +4,6 @@
#include "font.h" #include "font.h"
#include "gameobject.h" #include "gameobject.h"
#include "log.h" #include "log.h"
#include "openglrender.h"
#include "render.h" #include "render.h"
#include "shader.h" #include "shader.h"
#include "stb_ds.h" #include "stb_ds.h"

View file

@ -76,7 +76,6 @@ struct Texture *texture_pullfromfile(const char *path) {
YughInfo("Loading texture %s.", path); YughInfo("Loading texture %s.", path);
struct Texture *tex = calloc(1, sizeof(*tex)); struct Texture *tex = calloc(1, sizeof(*tex));
tex->opts.sprite = 1; tex->opts.sprite = 1;
tex->opts.mips = 0; tex->opts.mips = 0;

View file

@ -9,7 +9,7 @@
#include <string.h> #include <string.h>
#include "ffi.h" #include "ffi.h"
#include "openglrender.h" #include "render.h"
#include "stb_ds.h" #include "stb_ds.h"

View file

@ -4,7 +4,7 @@
#include "font.h" #include "font.h"
#include "gameobject.h" #include "gameobject.h"
#include "input.h" #include "input.h"
#include "openglrender.h" #include "render.h"
#include "window.h" #include "window.h"
#include "sound.h" #include "sound.h"
#include "resources.h" #include "resources.h"
@ -154,55 +154,7 @@ static int argc;
static char **args; static char **args;
void c_init() { void c_init() {
int logout = 0; render_init();
#if DBG
#if __linux
if (logout) {
time_t now = time(NULL);
char fname[100];
snprintf(fname, 100, "yugine-%d.log", now);
log_setfile(fname);
}
YughInfo("Starting yugine version %s.", VER);
FILE *sysinfo = NULL;
/* sysinfo = popen("uname -a", "r");
if (!sysinfo) {
YughWarn("Failed to get sys info.");
} else {
log_cat(sysinfo);
pclose(sysinfo);
}*/
signal(SIGSEGV, seghandle);
signal(SIGABRT, seghandle);
signal(SIGFPE, seghandle);
signal(SIGBUS, seghandle);
#endif
#endif
resources_init();
phys2d_init();
sound_init();
script_init();
input_init();
openglInit();
int argsize = 0;
for (int i = 1; i < argc; i++) {
argsize += strlen(args[i]);
if (argc > i+1) argsize++;
}
char cmdstr[argsize];
cmdstr[0] = '\0';
for (int i = 0; i < argc; i++) {
strcat(cmdstr, args[i]);
if (argc > i+1) strcat(cmdstr, " ");
}
script_evalf("cmd_args('%s');", cmdstr);
} }
int frame_fps() { int frame_fps() {
@ -345,15 +297,62 @@ double get_timescale()
} }
sapp_desc sokol_main(int sargc, char **sargs) { sapp_desc sokol_main(int sargc, char **sargs) {
argc = sargc; #ifndef NDEBUG
args = sargs; #ifdef __linux__
int logout = 0;
if (logout) {
time_t now = time(NULL);
char fname[100];
snprintf(fname, 100, "yugine-%d.log", now);
log_setfile(fname);
}
YughInfo("Starting yugine version %s.", VER);
FILE *sysinfo = NULL;
/* sysinfo = popen("uname -a", "r");
if (!sysinfo) {
YughWarn("Failed to get sys info.");
} else {
log_cat(sysinfo);
pclose(sysinfo);
}*/
signal(SIGSEGV, seghandle);
signal(SIGABRT, seghandle);
signal(SIGFPE, seghandle);
signal(SIGBUS, seghandle);
#endif
#endif
stm_setup(); /* time */
resources_init();
phys2d_init();
stm_setup();
script_startup(); script_startup();
int argsize = 0;
for (int i = 1; i < argc; i++) {
argsize += strlen(args[i]);
if (argc > i+1) argsize++;
}
mainwin.width = 1200; char cmdstr[argsize];
mainwin.height = 700; cmdstr[0] = '\0';
for (int i = 0; i < argc; i++) {
strcat(cmdstr, args[i]);
if (argc > i+1) strcat(cmdstr, " ");
}
script_evalf("cmd_args('%s');", cmdstr);
YughWarn("Width, height %d %d", mainwin.width, mainwin.height);
sound_init();
input_init();
return (sapp_desc){ return (sapp_desc){
.width = mainwin.width, .width = mainwin.width,

View file

@ -58,6 +58,7 @@ Cmdline.register_cmd("h", function() {
Game.quit(); Game.quit();
}, },
"Help."); "Help.");
Cmdline.register_cmd("b", function() { cmd(124); Game.quit(); }, "Pack the game into the given name.");
Cmdline.register_cmd("e", function(pawn) { Cmdline.register_cmd("e", function(pawn) {
run("scripts/editor.js"); run("scripts/editor.js");
@ -1098,8 +1099,10 @@ var Signal = {
}; };
var Window = { var Window = {
width: 0, set width(w) { cmd(125, w); },
height: 0, set height(h) { cmd(126, h); },
get width() { return cmd(48); },
get height() { return cmd(49); },
dimensions:[0,0], dimensions:[0,0],
}; };
@ -2229,6 +2232,7 @@ var locks = ['height', 'width', 'visible', 'body', 'controlled', 'selectable', '
locks.forEach(x => gameobject.obscure(x)); locks.forEach(x => gameobject.obscure(x));
/* Load configs */ /* Load configs */
function load_configs(file) { function load_configs(file) {
Log.info(`Loading config file ${file}.`);
var configs = JSON.parse(IO.slurp(file)); var configs = JSON.parse(IO.slurp(file));
for (var key in configs) { for (var key in configs) {
if (typeof globalThis[key] !== "object") continue; if (typeof globalThis[key] !== "object") continue;
@ -2359,8 +2363,8 @@ gameobject.clone("sprite", {
}); });
if (IO.exists("config.js")) //if (IO.exists("config.js"))
load("config.js"); // load("config.js");
var prototypes = {}; var prototypes = {};
if (IO.exists("proto.json")) if (IO.exists("proto.json"))