From 2ae45a5bb88b613e7e5fa738643f6c944fc17fc9 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 21 Dec 2023 23:21:01 +0000 Subject: [PATCH] many bug fixes; const correctness; restrict; memory leaks fixed --- Makefile | 8 +++--- scripts/engine.js | 1 + source/engine/2dphysics.c | 30 ++++++++++----------- source/engine/2dphysics.h | 6 ++--- source/engine/3d/model.c | 6 +++-- source/engine/anim.c | 2 +- source/engine/debug/debug.c | 30 ++++++++++++++++----- source/engine/debug/debug.h | 9 +++---- source/engine/debug/debugdraw.c | 1 - source/engine/font.c | 18 ++++++------- source/engine/font.h | 4 +-- source/engine/freelist.h | 6 ++--- source/engine/gameobject.c | 4 +-- source/engine/jsffi.c | 19 +++++++------- source/engine/render.c | 8 +++--- source/engine/render.h | 4 +-- source/engine/resources.c | 20 +++++++------- source/engine/resources.h | 10 ++++--- source/engine/script.c | 2 +- source/engine/sound/dsp.c | 46 +++++++++++++++++++-------------- source/engine/sound/music.c | 8 +++--- source/engine/sound/sound.c | 7 ++--- source/engine/sound/sound.h | 2 +- source/engine/spline.c | 10 ++++--- source/engine/texture.c | 13 +++------- source/engine/texture.h | 4 +-- source/engine/transform.c | 3 ++- source/engine/yugine.c | 18 +++++++------ source/engine/yugine.h | 2 +- 29 files changed, 166 insertions(+), 135 deletions(-) diff --git a/Makefile b/Makefile index d45774b..4d95d4c 100755 --- a/Makefile +++ b/Makefile @@ -50,8 +50,10 @@ ifdef NQOA CPPFLAGS += -DNQOA endif +CPPFLAGS += -ffast-math + ifeq ($(DBG),1) - CPPFLAGS += -g #-fsanitize=address + CPPFLAGS += -g -fsanitize=address INFO += _dbg else CPPFLAGS += -DNDEBUG @@ -76,7 +78,7 @@ else endif endif -CPPFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" #-DENABLE_SINC_MEDIUM_CONVERTER -DENABLE_SINC_FAST_CONVERTER +CPPFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" #-DENABLE_SINC_MEDIUM_CONVERTER -DENABLE_SINC_FAST_CONVERTER -DCP_COLLISION_TYPE_TYPE=uintptr_t -DCP_BITMASK_TYPE=uintptr_t # ENABLE_SINC_[BEST|FAST|MEDIUM]_CONVERTER # default, fast and medium available in game at runtime; best available in editor @@ -166,7 +168,7 @@ ifdef STEAM # BIN += /steam endif -WARNING_FLAGS = -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unused-function -Wno-unused-const-variable -Wno-address-of-temporary +WARNING_FLAGS = -Wno-incompatible-function-pointer-types NAME = primum$(EXT) SEM = 0.0.1 diff --git a/scripts/engine.js b/scripts/engine.js index 356e316..f678c9b 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -523,6 +523,7 @@ function world_start() { }; Primum.toString = function() { return "Primum"; }; Primum.ur = undefined; + Primum.kill = function() { this.clear(); }; } load("scripts/physics.js"); diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 6582c84..695016d 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -62,7 +62,7 @@ int p_compare(void *a, void *b) { if (a > b) return 1; if (a < b) return -1; - if (a == b) return 0; + return 0; } gameobject **clean_ids(gameobject **ids) @@ -105,7 +105,7 @@ int *phys2d_query_box_points(HMM_Vec2 pos, HMM_Vec2 wh, HMM_Vec2 *points, int n) } /* Return all gameobjects within the given box */ -gameobject *phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh) { +gameobject **phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh) { cpShape *box = cpBoxShapeNew(NULL, wh.x, wh.y, 0.f); cpTransform T = {0}; T.a = 1; @@ -128,7 +128,7 @@ gameobject *phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh) { return clean_ids(qb.ids); } -gameobject *phys2d_query_shape(struct phys2d_shape *shape) +gameobject **phys2d_query_shape(struct phys2d_shape *shape) { gameobject **ids = NULL; cpSpaceShapeQuery(space, shape->shape, querylist, ids); @@ -184,7 +184,7 @@ void init_phys2dshape(struct phys2d_shape *shape, gameobject *go, void *data) { shape->data = data; shape->t.scale = (HMM_Vec2){1.0,1.0}; go_shape_apply(go->body, shape->shape, go); - cpShapeSetCollisionType(shape->shape, (int)go); + cpShapeSetCollisionType(shape->shape, (cpCollisionType)go); cpShapeSetUserData(shape->shape, shape); } @@ -221,7 +221,7 @@ void phys2d_circledel(struct phys2d_circle *c) { phys2d_shape_del(&c->shape); } -void phys2d_dbgdrawcpcirc(cpCircleShape *c) { +void phys2d_dbgdrawcpcirc(cpShape *c) { HMM_Vec2 pos = mat_t_pos(t_go2world(shape2go(c)), (HMM_Vec2)cpCircleShapeGetOffset(c)); float radius = cpCircleShapeGetRadius(c); struct rgba color = shape_color(c); @@ -232,7 +232,7 @@ void phys2d_dbgdrawcpcirc(cpCircleShape *c) { } void phys2d_dbgdrawcircle(struct phys2d_circle *circle) { - phys2d_dbgdrawcpcirc((cpCircleShape *)circle->shape.shape); + phys2d_dbgdrawcpcirc(circle->shape.shape); } void phys2d_applycircle(struct phys2d_circle *circle) { @@ -308,7 +308,7 @@ struct phys2d_poly *Make2DPoly(gameobject *go) { arrsetlen(new->points, 0); new->radius = 0.f; - new->shape.shape = cpSpaceAddShape(space, cpPolyShapeNewRaw(go->body, 0, new->points, new->radius)); + new->shape.shape = cpSpaceAddShape(space, cpPolyShapeNewRaw(go->body, 0, (cpVect*)new->points, new->radius)); new->shape.debugdraw = phys2d_dbgdrawpoly; new->shape.moi = phys2d_poly_moi; new->shape.apply = phys2d_applypoly; @@ -317,7 +317,7 @@ struct phys2d_poly *Make2DPoly(gameobject *go) { } float phys2d_poly_moi(struct phys2d_poly *poly, float m) { - float moi = cpMomentForPoly(m, arrlen(poly->points), poly->points, cpvzero, poly->radius); + float moi = cpMomentForPoly(m, arrlen(poly->points), (cpVect*)poly->points, cpvzero, poly->radius); if (isnan(moi)) { // YughError("Polygon MOI returned an error. Returning 0."); return 0; @@ -335,16 +335,14 @@ void phys2d_polyaddvert(struct phys2d_poly *poly) { arrput(poly->points, v2zero); } -void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts) { +void phys2d_poly_setverts(struct phys2d_poly *poly, HMM_Vec2 *verts) { if (!verts) return; if (poly->points) arrfree(poly->points); arrsetlen(poly->points, arrlen(verts)); - for (int i = 0; i < arrlen(verts); i++) { - poly->points[i].X = verts[i].x; - poly->points[i].Y = verts[i].y; - } + for (int i = 0; i < arrlen(verts); i++) + poly->points[i] = verts[i]; phys2d_applypoly(poly); } @@ -354,7 +352,7 @@ void phys2d_applypoly(struct phys2d_poly *poly) { assert(sizeof(poly->points[0]) == sizeof(cpVect)); struct gameobject *go = poly->shape.go; // cpTransform T = m3_to_cpt(transform2d2mat(poly->t)); - cpPolyShapeSetVerts(poly->shape.shape, arrlen(poly->points), poly->points, cpTransformIdentity); + cpPolyShapeSetVerts(poly->shape.shape, arrlen(poly->points), (cpVect*)poly->points, cpTransformIdentity); cpPolyShapeSetRadius(poly->shape.shape, poly->radius); cpSpaceReindexShapesForBody(space, cpShapeGetBody(poly->shape.shape)); } @@ -634,14 +632,14 @@ static cpBool script_phys_cb_separate(cpArbiter *arb, cpSpace *space, void *data } void phys2d_rm_go_handlers(gameobject *go) { - cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (int)go); + cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (cpCollisionType)go); handler->userData = NULL; handler->beginFunc = NULL; handler->separateFunc = NULL; } void phys2d_setup_handlers(gameobject *go) { - cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (int)go); + cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (cpCollisionType)go); handler->userData = go; handler->beginFunc = script_phys_cb_begin; handler->separateFunc = script_phys_cb_separate; diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 691ab6c..23e42e6 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -86,7 +86,7 @@ void phys2d_polydel(struct phys2d_poly *poly); void phys2d_applypoly(struct phys2d_poly *poly); void phys2d_dbgdrawpoly(struct phys2d_poly *poly); void phys2d_polyaddvert(struct phys2d_poly *poly); -void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts); +void phys2d_poly_setverts(struct phys2d_poly *poly, HMM_Vec2 *verts); float phys2d_poly_moi(struct phys2d_poly *poly, float m); struct phys2d_edge *Make2DEdge(gameobject *go); @@ -108,7 +108,7 @@ void phys2d_edge_set_enabled(struct phys2d_edge *edge, int enabled); void phys2d_init(); void phys2d_update(float deltaT); cpShape *phys2d_query_pos(cpVect pos); -gameobject *phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh); +gameobject **phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh); struct shape_cb { @@ -130,7 +130,7 @@ struct rgba shape_color_s(cpShape *shape); void shape_gui(struct phys2d_shape *shape); void phys2d_setup_handlers(gameobject *go); -gameobject *phys2d_query_shape(struct phys2d_shape *shape); +gameobject **phys2d_query_shape(struct phys2d_shape *shape); int *phys2d_query_box_points(HMM_Vec2 pos, HMM_Vec2 wh, HMM_Vec2 *points, int n); void flush_collide_cbs(); diff --git a/source/engine/3d/model.c b/source/engine/3d/model.c index cd0fe11..a565766 100644 --- a/source/engine/3d/model.c +++ b/source/engine/3d/model.c @@ -6,7 +6,6 @@ #include "font.h" #include "window.h" #include "gameobject.h" -#include "libgen.h" //#include "diffuse.sglsl.h" #include "unlit.sglsl.h" @@ -144,7 +143,7 @@ void mesh_add_material(mesh *mesh, cgltf_material *mat) cgltf_buffer_view *buf = img->buffer_view; mesh->bind.fs.images[0] = texture_fromdata(buf->buffer->data, buf->size)->id; } else { - const char *imp = seprint("%s/%s", dirname(mesh->model->path), img->uri); + char *imp = seprint("%s/%s", dirname(mesh->model->path), img->uri); mesh->bind.fs.images[0] = texture_pullfromfile(imp)->id; free(imp); } @@ -258,6 +257,9 @@ void mesh_add_primitive(mesh *mesh, cgltf_primitive *prim) case cgltf_attribute_type_texcoord: mesh->bind.vertex_buffers[1] = texcoord_floats(vs, verts, comp); break; + case cgltf_attribute_type_invalid: + YughWarn("Invalid type."); + break; } } /* diff --git a/source/engine/anim.c b/source/engine/anim.c index 66db0bc..0cc8fa0 100644 --- a/source/engine/anim.c +++ b/source/engine/anim.c @@ -39,7 +39,6 @@ HMM_Vec4 sample_sampler(sampler *sampler, float time) float td = sampler->times[next_time]-sampler->times[previous_time]; float t = (time - sampler->times[previous_time])/td; - switch(sampler->type) { case LINEAR: return sample_linear(sampler,t,previous_time,next_time); @@ -51,4 +50,5 @@ HMM_Vec4 sample_sampler(sampler *sampler, float time) return sample_cubicspline(sampler,t, previous_time, next_time); break; } + return sample_cubicspline(sampler,t, previous_time, next_time); } diff --git a/source/engine/debug/debug.c b/source/engine/debug/debug.c index c3846fb..25f50f8 100644 --- a/source/engine/debug/debug.c +++ b/source/engine/debug/debug.c @@ -8,17 +8,35 @@ unsigned long long triCount = 0; void prof_start(struct d_prof *prof) { -#ifndef NDEBUG - prof->lap = stm_now(); -#endif + arrsetlen(prof->ms, prof->laps); } -void prof(struct d_prof *prof) +void prof_reset(struct d_prof *prof) { -#ifndef NDEBUG + prof->lap = stm_now(); +} + +float prof_lap_avg(struct d_prof *prof) +{ + float avg; + for (int i = 0; i < arrlen(prof->ms); i++) + avg += prof->ms[i]; + + avg /= arrlen(prof->ms); + return avg; +} + +void prof_lap(struct d_prof *prof) +{ + if (prof->ms == NULL) + prof_start(prof); + + if (arrlen(prof->ms) >= prof->laps) { + YughWarn("Profiled time of %s is avg %g", prof->name, prof_lap_avg(prof)); + arrsetlen(prof->ms, 0); + } uint64_t t = stm_since(prof->lap); arrput(prof->ms, stm_sec(t)); -#endif } void resetTriangles() diff --git a/source/engine/debug/debug.h b/source/engine/debug/debug.h index d9b75fe..1c186bc 100644 --- a/source/engine/debug/debug.h +++ b/source/engine/debug/debug.h @@ -1,21 +1,20 @@ #ifndef DEBUG_GUI_H #define DEBUG_GUI_H -//#ifndef static_assert -//#define static_assert(pred) switch(0){case 0:case pred:;} -//#endif - #include struct d_prof { char *name; float *ms; uint64_t lap; + int laps; }; extern unsigned long long triCount; void resetTriangles(); void prof_start(struct d_prof *prof); -void prof(struct d_prof *prof); +void prof_reset(struct d_prof *prof); +void prof_lap(struct d_prof *prof); +float prof_lap_avg(struct d_prof *prof); #endif diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index d53df7e..c00be88 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -292,7 +292,6 @@ void debugdraw_init() .cull_mode = SG_CULLMODE_BACK, .label = "grid pipeline", .colors[0].blend = blend_trans, - .label = "dbg grid", }); grid_bind.vertex_buffers[0] = circle_bind.vertex_buffers[1]; diff --git a/source/engine/font.c b/source/engine/font.c index 07bd8c9..794742e 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -118,7 +118,7 @@ struct sFont *MakeFont(const char *fontfile, int height) { struct sFont *newfont = calloc(1, sizeof(struct sFont)); newfont->height = height; - unsigned char *ttf_buffer = slurp_text(fontfile, NULL); + unsigned char *ttf_buffer = slurp_file(fontfile, NULL); unsigned char *bitmap = malloc(packsize * packsize); stbtt_packedchar glyphs[95]; @@ -251,7 +251,7 @@ void text_settype(struct sFont *mfont) { font = mfont; } -unsigned char *esc_color(unsigned char *c, struct rgba *color, struct rgba defc) +const char *esc_color(const char *c, struct rgba *color, struct rgba defc) { struct rgba d; if (!color) color = &d; @@ -278,13 +278,13 @@ unsigned char *esc_color(unsigned char *c, struct rgba *color, struct rgba defc) return c; } -struct boundingbox text_bb(const unsigned char *text, float scale, float lw, float tracking) +struct boundingbox text_bb(const char *text, float scale, float lw, float tracking) { struct rgba dummy; HMM_Vec2 cursor = {0,0}; - const unsigned char *c = text; - const unsigned char *line, *wordstart, *drawstart; - line = drawstart = (unsigned char *)text; + const char *c = text; + const char *line, *wordstart, *drawstart; + line = drawstart = text; while (*line != '\0') { if (isblank(*line)) { @@ -332,13 +332,13 @@ void check_caret(int caret, int l, HMM_Vec2 pos, float scale, struct rgba color) /* pos given in screen coordinates */ -int renderText(const unsigned char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) { +int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) { int len = strlen(text); HMM_Vec2 cursor = pos; - const unsigned char *line, *wordstart, *drawstart; - line = drawstart = (unsigned char *)text; + const char *line, *wordstart, *drawstart; + line = drawstart = text; struct rgba usecolor = color; check_caret(caret, line-drawstart, cursor, scale, usecolor); diff --git a/source/engine/font.h b/source/engine/font.h index a56c544..6a7ded4 100644 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -33,8 +33,8 @@ void font_init(); struct sFont *MakeFont(const char *fontfile, int height); void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color); void text_settype(struct sFont *font); -struct boundingbox text_bb(const unsigned char *text, float scale, float lw, float tracking); -int renderText(const unsigned char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking); +struct boundingbox text_bb(const char *text, float scale, float lw, float tracking); +int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking); // void text_frame(); void text_flush(HMM_Mat4 *proj); diff --git a/source/engine/freelist.h b/source/engine/freelist.h index ac899a7..3baf337 100644 --- a/source/engine/freelist.h +++ b/source/engine/freelist.h @@ -13,9 +13,9 @@ struct freelistheader }; #define freelist_header(p) ((struct freelistheader*)p-1) -static inline void *freelist_make(struct freelistheader *list, size_t elemsize, unsigned int n) +static inline void *freelist_make(size_t elemsize, unsigned int n) { - list = malloc(elemsize*n*sizeof(struct freelistheader)); + struct freelistheader *list = malloc(elemsize*n*sizeof(struct freelistheader)); list->first = 0; list->len = n; list->count = 0; @@ -29,7 +29,7 @@ static inline unsigned int freelist_check(struct freelistheader *h, void *data, return id; } -#define freelist_size(p,l) do{p = freelist_make(p,sizeof(*p),l); for(int i = 0; i < l; i++) { p[i].next = i+1; }}while(0) +#define freelist_size(p,l) do{p = freelist_make(sizeof(*p),l); for(int i = 0; i < l; i++) { p[i].next = i+1; }}while(0) #define freelist_len(p) (freelist_header(p)->len) #define freelist_first(p) (freelist_header(p)->first) #define freelist_grab(i,p) do{i=freelist_header(p)->first; freelist_header(p)->first = p[i].next; p[i].next = -1;freelist_header(p)->count++;}while(0) diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 91d978a..83419e2 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -77,10 +77,10 @@ unsigned int editor_cat = 1<<31; void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go) { cpShapeSetFriction(shape, go->f); cpShapeSetElasticity(shape, go->e); - cpShapeSetCollisionType(shape, (int)go); + cpShapeSetCollisionType(shape, (cpCollisionType)go); cpShapeFilter filter; - filter.group = (int)go; + filter.group = (cpCollisionType)go; filter.categories = 1<layer | editor_cat; // filter.mask = CP_ALL_CATEGORIES; filter.mask = category_masks[go->layer] | editor_cat; diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 3b1659f..7304fb9 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -140,7 +140,7 @@ JSValue int2js(int i) { return JS_NewInt64(js, i); } JSValue str2js(const char *c) { return JS_NewString(js, c); } const char *js2str(JSValue v) { return JS_ToCString(js, v); } -JSValue strarr2js(const char **c) +JSValue strarr2js(char **c) { JSValue arr = JS_NewArray(js); for (int i = 0; i < arrlen(c); i++) @@ -306,7 +306,7 @@ JSValue vecarr2js(HMM_Vec2 *points, int n) { } JSValue duk_ui_text(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { - const unsigned char *s = JS_ToCString(js, argv[0]); + const char *s = JS_ToCString(js, argv[0]); HMM_Vec2 pos = js2vec2(argv[1]); float size = js2number(argv[2]); @@ -497,10 +497,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) int cmd = js2int(argv[0]); const char *str = NULL; const char *str2 = NULL; - const void *d1 = NULL; - const void *d2 = NULL; - const void *v1 = NULL; - gameobject *ids = NULL; + void *d1 = NULL; + void *d2 = NULL; + void *v1 = NULL; + gameobject **ids = NULL; + int *intids = NULL; gameobject *go = NULL; JSValue ret = JS_UNDEFINED; @@ -874,9 +875,9 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 86: v1 = js2cpvec2arr(argv[3]); - ids = phys2d_query_box_points(js2vec2(argv[1]), js2vec2(argv[2]), v1, js2int(argv[4])); - ret = gos2ref(ids); - arrfree(ids); + intids = phys2d_query_box_points(js2vec2(argv[1]), js2vec2(argv[2]), v1, js2int(argv[4])); + ret = ints2js(intids); + arrfree(intids); break; case 87: diff --git a/source/engine/render.c b/source/engine/render.c index 839c6b6..d0a66bb 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -42,7 +42,7 @@ static struct { double timer; double spf; int rec; - char *buffer; + uint8_t *buffer; } gif; static struct { @@ -95,7 +95,7 @@ void gif_rec_start(int w, int h, int cpf, int bitdepth) }); } -void gif_rec_end(char *path) +void gif_rec_end(const char *path) { if (!gif.rec) return; @@ -109,7 +109,7 @@ void gif_rec_end(char *path) gif.rec = 0; } -void capture_screen(int x, int y, int w, int h, char *path) +void capture_screen(int x, int y, int w, int h, const char *path) { int n = 4; void *data = malloc(w*h*n); @@ -297,7 +297,7 @@ void render_init() { model_init(); sg_color c; - rgba2floats(&c, editorClearColor); + rgba2floats((float*)&c, editorClearColor); pass_action = (sg_pass_action){ .colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = c} }; diff --git a/source/engine/render.h b/source/engine/render.h index 9b01ef4..b9ae5e6 100644 --- a/source/engine/render.h +++ b/source/engine/render.h @@ -70,7 +70,7 @@ void opengl_rendermode(enum RenderMode r); void openglInit3d(struct window *window); void openglRender3d(struct window *window, struct mCamera *camera); -void capture_screen(int x, int y, int w, int h, char *path); +void capture_screen(int x, int y, int w, int h, const char *path); void render_winsize(); @@ -86,7 +86,7 @@ HMM_Vec2 screen2world(HMM_Vec2 pos); sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d); void gif_rec_start(int w, int h, int cpf, int bitdepth); -void gif_rec_end(char *path); +void gif_rec_end(const char *path); struct uv_n { unsigned short u; diff --git a/source/engine/resources.c b/source/engine/resources.c index 397517e..325c99b 100644 --- a/source/engine/resources.c +++ b/source/engine/resources.c @@ -72,11 +72,13 @@ char *get_filename_from_path(char *path, int extension) { return filename; } -char *get_directory_from_path(char *path) { +char *dirname(const char *path) +{ const char *dirpos = strrchr(path, '/'); - char *directory = (char *)malloc(sizeof(char) * (dirpos - path + 1)); - strncpy(directory, path, dirpos - path); - return directory; + if (!dirpos) return "."; + char *dir = malloc(dirpos-path+1); + strncpy(dir,path,dirpos-path); + return dir; } FILE *res_open(char *path, const char *tag) { @@ -135,7 +137,7 @@ static int ls_ftw(const char *path, const struct stat *sb, int typeflag) return 0; } -char **ls(char *path) +char **ls(const char *path) { if (ls_paths) { for (int i = 0; i < arrlen(ls_paths); i++) @@ -189,13 +191,13 @@ void *cdb_slurp(struct cdb *cdb, const char *file, size_t *size) return data; } -int fexists(char *path) +int fexists(const 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 (cdb_find(&corecdb, path, len)) return 1; else if (!access(path, R_OK)) return 1; return 0; @@ -247,7 +249,7 @@ char *slurp_text(const char *filename, size_t *size) return retstr; } -int cp(char *p1, char *p2) +int cp(const char *p1, const char *p2) { size_t len; void *data = slurp_file(p1, &len); @@ -275,7 +277,7 @@ void rek_mkdir(char *path) { printf("error while trying to create '%s'\n%m\n", path); } -FILE *fopen_mkdir(char *path, char *mode) { +FILE *fopen_mkdir(const char *path, const char *mode) { char *sep = strrchr(path, '/'); if(sep) { char *path0 = strdup(path); diff --git a/source/engine/resources.h b/source/engine/resources.h index 4164b56..69355ca 100644 --- a/source/engine/resources.h +++ b/source/engine/resources.h @@ -15,10 +15,12 @@ char *str_replace_ext(const char *s, const char *newext); FILE *res_open(char *path, const char *tag); FILE *path_open(const char *tag, const char *fmt, ...); char *make_path(const char *file); -char **ls(char *path); -int cp(char *p1, char *p2); -int fexists(char *path); -FILE *fopen_mkdir(char *path, char *mode); +char **ls(const char *path); +int cp(const char *p1, const char *p2); +int fexists(const char *path); +FILE *fopen_mkdir(const char *path, const char *mode); + +char *dirname(const char *path); void *slurp_file(const char *filename, size_t *size); char *slurp_text(const char *filename, size_t *size); diff --git a/source/engine/script.c b/source/engine/script.c index a3f7415..fadf16c 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -193,7 +193,7 @@ time_t jso_file(const char *file) JSValue script_runfile(const char *file) { size_t len; - const char *script = slurp_text(file, &len); + char *script = slurp_text(file, &len); if (!script) return JS_UNDEFINED; JSValue obj = JS_Eval(js, script, len, file, JS_EVAL_FLAGS); diff --git a/source/engine/sound/dsp.c b/source/engine/sound/dsp.c index b9e21e3..a361c0b 100644 --- a/source/engine/sound/dsp.c +++ b/source/engine/sound/dsp.c @@ -24,7 +24,7 @@ void iir_free(struct dsp_iir *iir) free(iir); } -void interleave(soundbyte *a, soundbyte *b, soundbyte *stereo, int frames) +void interleave(soundbyte *a, soundbyte *b, soundbyte *restrict stereo, int frames) { for (int i = 0; i < frames; i++) { stereo[i*2] = a[i]; @@ -32,7 +32,7 @@ void interleave(soundbyte *a, soundbyte *b, soundbyte *stereo, int frames) } } -void deinterleave(soundbyte *stereo, soundbyte *out, int frames, int channels, int chout) +void deinterleave(soundbyte *restrict stereo, soundbyte *restrict out, int frames, int channels, int chout) { chout--; for (int i = 0; i < frames; i++) @@ -44,7 +44,7 @@ void mono_to_stero(soundbyte *a, soundbyte *stereo, int frames) interleave(a,a,stereo, frames); } -void mono_expand(soundbyte *buffer, int to, int frames) +void mono_expand(soundbyte *restrict buffer, int to, int frames) { soundbyte hold[frames]; memcpy(hold, buffer, sizeof(soundbyte)*frames); @@ -86,7 +86,7 @@ soundbyte *dsp_node_out(dsp_node *node) return node->cache; } -void filter_am_mod(dsp_node *mod, soundbyte *buffer, int frames) +void filter_am_mod(dsp_node *restrict mod, soundbyte *restrict buffer, int frames) { soundbyte *m = dsp_node_out(mod); for (int i = 0; i < frames*CHANNELS; i++) buffer[i] *= m[i]; @@ -98,7 +98,7 @@ dsp_node *dsp_am_mod(dsp_node *mod) } /* Add b into a */ -void sum_soundbytes(soundbyte *a, soundbyte *b, int samples) +void sum_soundbytes(soundbyte *restrict a, soundbyte *restrict b, int samples) { for (int i = 0; i < samples; i++) a[i] += b[i]; } @@ -107,7 +107,7 @@ void norm_soundbytes(soundbyte *a, float lvl, int samples) { float tar = lvl; float max = 0 ; - for (int i = 0; i < samples; i++) max = (fabsf(a[i] > max) ? fabsf(a[i]) : max); + for (int i = 0; i < samples; i++) max = fabsf(a[i]) > max ? fabsf(a[i]) : max; float mult = max/tar; scale_soundbytes(a, mult, samples); } @@ -118,7 +118,7 @@ void scale_soundbytes(soundbyte *a, float scale, int samples) for (int i = 0; i < samples; i++) a[i] *= scale; } -void zero_soundbytes(soundbyte *a, int samples) { memset(a, 0, sizeof(soundbyte)*samples); } +void zero_soundbytes(soundbyte *restrict a, int samples) { memset(a, 0, sizeof(soundbyte)*samples); } void set_soundbytes(soundbyte *a, soundbyte *b, int samples) { @@ -152,9 +152,12 @@ void node_free(dsp_node *node) if (node == masterbus) return; /* Simple check to not delete the masterbus */ pthread_mutex_lock(&soundrun); unplug_node(node); - if (node->data) - if (node->data_free) node->data_free(node->data); - else free(node->data); + if (node->data) { + if (node->data_free) + node->data_free(node->data); + else + free(node->data); + } free(node); pthread_mutex_unlock(&soundrun); @@ -229,9 +232,9 @@ dsp_node *dsp_phasor(float amp, float freq, float (*filter)(float)) return make_node(p, filter_phasor, NULL); } -void filter_rectify(void *data, soundbyte *out, int n) +void filter_rectify(void *restrict data, soundbyte *restrict out, int n) { - for (int i = 0; i < n; i++) out[i] = abs(out[i]); + for (int i = 0; i < n; i++) out[i] = fabsf(out[i]); } dsp_node *dsp_rectify() @@ -246,7 +249,9 @@ soundbyte sample_whitenoise() void gen_whitenoise(void *data, soundbyte *out, int n) { - for (int i = 0; i < n; i++) out[i] = sample_whitenoise(); + for (int i = 0; i < n; i++) + out[i] = sample_whitenoise(); + mono_expand(out, CHANNELS, n); } @@ -295,7 +300,7 @@ dsp_node *dsp_pinknoise() return make_node(pink, gen_pinknoise, iir_free); } -void filter_rednoise(soundbyte *last, soundbyte *out, int frames) +void filter_rednoise(soundbyte *restrict last, soundbyte *out, int frames) { gen_whitenoise(NULL, out, frames); for (int i = 0; i < frames*CHANNELS; i++) { @@ -312,7 +317,7 @@ dsp_node *dsp_rednoise() return make_node(last,filter_rednoise, NULL); } -void filter_pitchshift(float *octaves, soundbyte *buffer, int frames) +void filter_pitchshift(float *restrict octaves, soundbyte *buffer, int frames) { soundbyte ch1[frames]; for (int i = 0; i < frames; i++) @@ -336,6 +341,7 @@ struct timescale static long *src_cb(struct timescale *ts, float **data) { + return NULL; } void filter_timescale(struct timescale *ts, soundbyte *buffer, int frames) @@ -419,7 +425,7 @@ dsp_node *dsp_delay(double sec, double decay) return make_node(d, filter_delay, delay_free); } -void filter_fwd_delay(delay *d, soundbyte *buf, int frames) +void filter_fwd_delay(delay *restrict d, soundbyte *restrict buf, int frames) { for (int i = 0; i < frames*CHANNELS; i++) { ringpush(d->ring, buf[i]); @@ -505,7 +511,7 @@ dsp_node *dsp_adsr(unsigned int atk, unsigned int dec, unsigned int sus, unsigne return make_node(adsr, dsp_adsr_fillbuf, NULL); } -void filter_noise_gate(float *floor, soundbyte *out, int frames) +void filter_noise_gate(float *restrict floor, soundbyte *restrict out, int frames) { for (int i = 0; i < frames*CHANNELS; i++) out[i] = fabsf(out[i]) < *floor ? 0.0 : out[i]; } @@ -517,7 +523,7 @@ dsp_node *dsp_noise_gate(float floor) return make_node(v, filter_noise_gate, NULL); } -void filter_limiter(float *ceil, soundbyte *out, int n) +void filter_limiter(float *restrict ceil, soundbyte *restrict out, int n) { for (int i = 0; i < n*CHANNELS; i++) out[i] = fabsf(out[i]) > *ceil ? *ceil : out[i]; } @@ -598,7 +604,7 @@ void pan_frames(soundbyte *out, float deg, int frames) } } -void dsp_mono(void *p, soundbyte *out, int n) +void dsp_mono(void *p, soundbyte *restrict out, int n) { for (int i = 0; i < n; i++) { soundbyte val = (out[i*CHANNELS] + out[i*CHANNELS+1]) / 2; @@ -614,7 +620,7 @@ struct bitcrush { }; #define ROUND(f) ((float)((f>0.0)?floor(f+0.5):ceil(f-0.5))) -void filter_bitcrush(struct bitcrush *b, soundbyte *out, int frames) +void filter_bitcrush(struct bitcrush *restrict b, soundbyte *restrict out, int frames) { int max = pow(2,b->depth) - 1; int step = SAMPLERATE/b->sr; diff --git a/source/engine/sound/music.c b/source/engine/sound/music.c index c8ed15a..640ac7e 100644 --- a/source/engine/sound/music.c +++ b/source/engine/sound/music.c @@ -13,10 +13,10 @@ static struct { char *key; - tsf **value; + tsf *value; } *sf_hash = NULL; -void dsp_midi_fillbuf(struct dsp_midi_song *song, void *out, int n) +void dsp_midi_fillbuf(struct dsp_midi_song *restrict song, void *restrict out, int n) { soundbyte *o = out; tml_message *midi = song->midi; @@ -60,7 +60,7 @@ tsf *make_soundfont(const char *path) int idx = shgeti(sf_hash, path); if (idx != -1) return sf_hash[idx].value; - long rawlen; + size_t rawlen; void *raw = slurp_file(path, &rawlen); tsf *sf = tsf_load_memory(raw,rawlen); free(raw); @@ -83,7 +83,7 @@ void dsp_midi_free(struct dsp_midi_song *ms) dsp_node *dsp_midi(const char *midi, tsf *sf) { - long rawlen; + size_t rawlen; void *raw = slurp_file(midi, &rawlen); struct dsp_midi_song *ms = malloc(sizeof(*ms)); ms->time = 0.0; diff --git a/source/engine/sound/sound.c b/source/engine/sound/sound.c index 0f6f630..641c674 100644 --- a/source/engine/sound/sound.c +++ b/source/engine/sound/sound.c @@ -9,6 +9,7 @@ #include "time.h" #include #include "pthread.h" +#include "debug.h" pthread_mutex_t soundrun = PTHREAD_MUTEX_INITIALIZER; @@ -121,7 +122,7 @@ void filter_mod(pocketmod_context *mod, soundbyte *buffer, int frames) dsp_node *dsp_mod(const char *path) { - long modsize; + size_t modsize; void *data = slurp_file(path, &modsize); pocketmod_context *mod = malloc(sizeof(*mod)); pocketmod_init(mod, data, modsize, SAMPLERATE); @@ -171,7 +172,7 @@ struct wav *make_sound(const char *wav) { } struct wav *mwav = malloc(sizeof(*mwav)); - long rawlen; + size_t rawlen; void *raw = slurp_file(wav, &rawlen); if (!raw) { YughError("Could not find file %s.", wav); @@ -282,7 +283,7 @@ static long src_cb(struct sound *s, float **data) return needed; } -struct dsp_node *dsp_source(char *path) +struct dsp_node *dsp_source(const char *path) { struct sound *self = malloc(sizeof(*self)); self->frame = 0; diff --git a/source/engine/sound/sound.h b/source/engine/sound/sound.h index 0b06446..2709882 100644 --- a/source/engine/sound/sound.h +++ b/source/engine/sound/sound.h @@ -44,7 +44,7 @@ void audio_close(); struct wav *make_sound(const char *wav); void free_sound(const char *wav); void wav_norm_gain(struct wav *w, double lv); -struct dsp_node *dsp_source(char *path); +struct dsp_node *dsp_source(const char *path); struct dsp_node *dsp_mod(const char *path); int sound_finished(const struct sound *s); diff --git a/source/engine/spline.c b/source/engine/spline.c index 89c8d07..9253722 100644 --- a/source/engine/spline.c +++ b/source/engine/spline.c @@ -138,7 +138,7 @@ HMM_Vec4 spline_CT(HMM_Mat4 *C, float t) return HMM_MulM4V4(*C, T); } -HMM_Mat4 make_C(HMM_Vec2 *p, HMM_Mat4 *B) +HMM_Mat4 make_C(const HMM_Vec2 *p, const HMM_Mat4 *B) { HMM_Mat4 G; G.Columns[0].xy = p[0]; @@ -179,6 +179,8 @@ HMM_Vec2 *spline2d_min_seg(float u0, float u1, float min_seg, HMM_Mat4 *C, HMM_V } else arrput(ret, b); + + return ret; } HMM_Vec2 *catmull_rom_min_seg(HMM_Vec2 *a, HMM_Vec2 *b, HMM_Vec2 *c, HMM_Vec2 *d, float min_seg) @@ -216,7 +218,7 @@ HMM_Vec2 *spline2d_min_angle_2(float u0, float u1, float max_angle, HMM_Mat4 *C, return arr; } -HMM_Vec2 *spline_min_angle(HMM_Vec2 *p, HMM_Mat4 *B, float min_angle, HMM_Vec2 *arr) +HMM_Vec2 *spline_min_angle(HMM_Vec2 *p, const HMM_Mat4 *B, float min_angle, HMM_Vec2 *arr) { HMM_Mat4 C = make_C(p, B); arr = spline2d_min_angle_2(0,1,min_angle, &C, arr); @@ -250,7 +252,7 @@ HMM_Vec2 *bezier_cb_ma_v2(HMM_Vec2 *cp, float ma) return ret; } -HMM_Vec2 catmull_rom_query(HMM_Vec2 *cp, float d, HMM_Mat4 *G) +HMM_Vec2 catmull_rom_query(HMM_Vec2 *cp, float d, const HMM_Mat4 *G) { if (arrlen(cp) < 4 || d < 0 || d > 1) return HMM_V2(0,0); @@ -315,5 +317,5 @@ HMM_Vec2 catmull_rom_wig(HMM_Vec2 *cp, float d) { return catmull_rom_query(cp,d, HMM_Vec2 catmull_rom_closest(HMM_Vec2 *cp, HMM_Vec2 p) { - + return p; } diff --git a/source/engine/texture.c b/source/engine/texture.c index fe4cf71..1ad8914 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -79,12 +79,13 @@ int gif_nframes(const char *path) /* If an empty string or null is put for path, loads default texture */ struct Texture *texture_pullfromfile(const char *path) { if (!path) return texture_notex(); + if (shlen(texhash) == 0) sh_new_arena(texhash); int index = shgeti(texhash, path); if (index != -1) return texhash[index].value; - long rawlen; + size_t rawlen; unsigned char *raw = slurp_file(path, &rawlen); if (!raw) return texture_notex(); @@ -109,8 +110,7 @@ struct Texture *texture_pullfromfile(const char *path) { tex->height = qoi.height; n = qoi.channels; } else if (!strcmp(ext, ".gif")) { - int *delays; - data = stbi_load_gif_from_memory(raw, rawlen, &delays, &tex->width, &tex->height, &tex->frames, &n, 4); + data = stbi_load_gif_from_memory(raw, rawlen, NULL, &tex->width, &tex->height, &tex->frames, &n, 4); tex->height *= tex->frames; } else if (!strcmp(ext, ".svg")) { #ifndef NSVG @@ -187,9 +187,6 @@ struct Texture *texture_pullfromfile(const char *path) { .data = sg_img_data }); - if (shlen(texhash) == 0) - sh_new_arena(texhash); - shput(texhash, path, tex); for (int i = 1; i < mips; i++) @@ -282,9 +279,7 @@ struct Texture *texture_fromdata(void *raw, long size) return tex; } -struct Texture *texture_loadfromfile(const char *path) { - struct Texture *new = texture_pullfromfile(path); -} +struct Texture *texture_loadfromfile(const char *path) { return texture_pullfromfile(path); } struct glrect tex_get_rect(struct Texture *tex) { return ST_UNIT; } diff --git a/source/engine/texture.h b/source/engine/texture.h index 2855d45..600d07b 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -44,8 +44,8 @@ struct TextureOptions { /* Represents an actual texture on the GPU */ struct Texture { sg_image id; /* ID reference for the GPU memory location of the texture */ - uint16_t width; - uint16_t height; + int width; + int height; unsigned char *data; struct TextureOptions opts; int frames; diff --git a/source/engine/transform.c b/source/engine/transform.c index e84c534..8a082ec 100644 --- a/source/engine/transform.c +++ b/source/engine/transform.c @@ -32,7 +32,7 @@ HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) { return HMM_MulM4V4(m, (HMM_Vec4) HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir) { - m.Columns[4] = (HMM_Vec4){0,0,0,1}; + m.Columns[3] = (HMM_Vec4){0,0,0,1}; return mat3_t_pos(m, dir); } @@ -60,4 +60,5 @@ transform3d mat2transform3d(HMM_Mat4 m) // for (int i = 0; i < 2; i++) // m.Columns[i].xyz = HMM_MulV3(m.Columns[i].xyz, t.scale.Elements[i]); t.rotation = HMM_M4ToQ_RH(m); + return t; } diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 0b272f2..b8ad2d1 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -119,10 +119,10 @@ static void process_frame() if (stm_sec(stm_diff(frame_t, updatelast)) > updateMS) { double dt = stm_sec(stm_diff(frame_t, updatelast)); updatelast = frame_t; - prof_start(&prof_update); +// prof_start(&prof_update); call_updates(dt * timescale); - prof(&prof_update); +// prof_lap(&prof_update); if (sim_play == SIM_STEP) sim_pause(); @@ -131,18 +131,18 @@ static void process_frame() physlag += elapsed; while (physlag > physMS) { physlag -= physMS; - prof_start(&prof_physics); +// prof_start(&prof_physics); phys_step = 1; phys2d_update(physMS * timescale); call_physics(physMS * timescale); phys_step = 0; - prof(&prof_physics); +// prof_lap(&prof_physics); } } - prof_start(&prof_draw); +// prof_start(&prof_draw); window_render(&mainwin); - prof(&prof_draw); +// prof_lap(&prof_draw); gameobjects_cleanup(); } @@ -224,6 +224,8 @@ void c_event(const sapp_event *e) input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy, e->modifiers); input_dropped_files(sapp_get_num_dropped_files()); break; + default: + break; } if (editor_mode) @@ -258,7 +260,7 @@ static sapp_desc start_desc = { .logger.func = sg_logging, }; -void app_name(char *name) { start_desc.window_title = strdup(name); } +void app_name(const char *name) { start_desc.window_title = strdup(name); } int main(int argc, char **argv) { #ifndef NDEBUG @@ -267,7 +269,7 @@ int main(int argc, char **argv) { if (logout) { time_t now = time(NULL); char fname[100]; - snprintf(fname, 100, "yugine-%d.log", now); + snprintf(fname, 100, "yugine-%ld.log", now); log_setfile(fname); } #endif diff --git a/source/engine/yugine.h b/source/engine/yugine.h index ba13a36..637848d 100644 --- a/source/engine/yugine.h +++ b/source/engine/yugine.h @@ -13,7 +13,7 @@ void print_stacktrace(); void yugine_init(); const char *engine_info(); -void app_name(char *name); +void app_name(const char *name); int frame_fps(); double get_timescale();