From 5386e19e17abec593eebdaa33d6775e8f812e884 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 17 Nov 2022 22:48:20 +0000 Subject: [PATCH] Improved log; fixed texture loading and hash table --- source/engine/editor.c | 12 +++++++++--- source/engine/editor.h | 4 ++-- source/engine/engine.c | 1 + source/engine/gameobject.c | 2 +- source/engine/gameobject.h | 2 +- source/engine/log.c | 7 +++++-- source/engine/openglrender.c | 2 +- source/engine/openglrender.h | 2 +- source/engine/registry.c | 5 +++-- source/engine/sprite.c | 35 +++++++++++++++++++++-------------- source/engine/sprite.h | 22 +++++++++++----------- source/engine/texture.c | 30 ++++++++++++++++++++++-------- 12 files changed, 78 insertions(+), 46 deletions(-) diff --git a/source/engine/editor.c b/source/engine/editor.c index e6ae696..acb009b 100644 --- a/source/engine/editor.c +++ b/source/engine/editor.c @@ -1007,23 +1007,28 @@ void game_stop() { physOn = 0; } void game_pause() { physOn = 0; } -void sprite_gui(struct mSprite *sprite) { +void sprite_gui(struct sprite *sprite) { + nuke_nel(2); - nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", tex_get_path(sprite->tex)); + //nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", tex_get_path(sprite->tex)); + if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) { sprite_loadtex(sprite, selected_asset->filename); } + if (sprite->tex != NULL) { - nk_labelf(ctx, NK_TEXT_LEFT, "%s", tex_get_path(sprite->tex)); + //nk_labelf(ctx, NK_TEXT_LEFT, "%s", tex_get_path(sprite->tex)); nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height); nk_layout_row_static(ctx, sprite->tex->height, sprite->tex->width, 1); if (nk_button_image(ctx, nk_image_id(sprite->tex->id))) editor_selectasset_str(tex_get_path(sprite->tex)); } + + nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f); nuke_nel(3); @@ -1041,5 +1046,6 @@ void sprite_gui(struct mSprite *sprite) { sprite->pos[0] = -0.5f; sprite->pos[1] = 0.f; } + } diff --git a/source/engine/editor.h b/source/engine/editor.h index e72fe52..ea31a9e 100644 --- a/source/engine/editor.h +++ b/source/engine/editor.h @@ -65,7 +65,7 @@ extern int show_desktop; struct vec; struct gameproject; -struct mSprite; +struct sprite; extern struct gameproject *cur_project; extern struct vec *projects; @@ -104,6 +104,6 @@ void get_levels(); int obj_gui_hierarchy(struct mGameObject *selected); -void sprite_gui(struct mSprite *sprite); +void sprite_gui(struct sprite *sprite); #endif diff --git a/source/engine/engine.c b/source/engine/engine.c index 68ae2aa..a9bb5af 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -7,6 +7,7 @@ #include #define STB_IMAGE_IMPLEMENTATION +#define STBI_FAILURE_USERMSG #include "stb_image.h" #define PL_MPEG_IMPLEMENTATION diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index eb4a65f..ee1c7c8 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -140,7 +140,7 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab) go->components[i] = components[n]; struct component *newc = &go->components[i]; newc->go = go; - newc->data = malloc(newc->datasize); + newc->data = calloc(1, newc->datasize); if (newc->io == NULL) fread(newc->data, newc->datasize, 1, fprefab); diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 030f955..42fc4a1 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -9,7 +9,7 @@ #include struct mShader; -struct mSprite; +struct sprite; struct component; struct editor { diff --git a/source/engine/log.c b/source/engine/log.c index 1e621ed..494aee9 100644 --- a/source/engine/log.c +++ b/source/engine/log.c @@ -9,6 +9,10 @@ #define logLevel 0 +//char *logstr[] = { "INFO", "WARN", "\x1b[1;31mERROR\x1b[0m", "CRITICAL" }; +char *logstr[] = { "INFO", "WARN", "ERROR", "CRITICAL" }; +char *catstr[] = {"ENGINE"}; + void mYughLog(int category, int priority, int line, const char *file, const char *message, ...) { if (priority >= logLevel) { @@ -23,8 +27,7 @@ void mYughLog(int category, int priority, int line, const char *file, const char va_end(args); char buffer[ERROR_BUFFER] = { '\0' }; - snprintf(buffer, ERROR_BUFFER, "%s\n[ %s:%d ] %s\n", - msgbuffer, file, line, dt); + snprintf(buffer, ERROR_BUFFER, "%s | %s | %s [ %s:%d ] %s\n", logstr[priority], catstr[0], dt, file, line, msgbuffer); printf("%s", buffer); fflush(stdout); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index e34d91a..4974e8c 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -88,7 +88,7 @@ GLuint debugColorPickBO = 0; GLuint debugColorPickTEX = 0; -struct mSprite *tsprite = NULL; +struct sprite *tsprite = NULL; static unsigned int projUBO; diff --git a/source/engine/openglrender.h b/source/engine/openglrender.h index 55c26ff..1d6a75e 100644 --- a/source/engine/openglrender.h +++ b/source/engine/openglrender.h @@ -9,7 +9,7 @@ struct window; extern struct mShader *spriteShader; extern struct mShader *animSpriteShader; -extern struct mSprite *tsprite; +extern struct sprite *tsprite; extern int renderMode; diff --git a/source/engine/registry.c b/source/engine/registry.c index 90a09a4..65f0131 100644 --- a/source/engine/registry.c +++ b/source/engine/registry.c @@ -12,6 +12,7 @@ int ncomponent = 0; void registry_init() { + /* REGISTER_COMP(sprite); REGISTER_COMP(2d_circle); @@ -22,8 +23,8 @@ void registry_init() */ register_component("Sprite", - sizeof(struct mSprite), - MakeSprite, + sizeof(struct sprite), + make_sprite, sprite_delete, sprite_io, NULL, diff --git a/source/engine/sprite.c b/source/engine/sprite.c index ee62654..5dfd5e4 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -13,16 +13,16 @@ struct TextureOptions TEX_SPRITE = { 1, 0, 0 }; -struct mSprite *sprites; +struct sprite *sprites; static uint32_t quadVAO; -struct mSprite *MakeSprite(struct mGameObject *go) +struct sprite *make_sprite(struct mGameObject *go) { if (arrcap(sprites) == 0) arrsetcap(sprites, 100); - struct mSprite sprite = { + struct sprite sprite = { .color = {1.f, 1.f, 1.f}, .size = {1.f, 1.f}, .tex = texture_loadfromfile("ph.png"), @@ -33,26 +33,33 @@ struct mSprite *MakeSprite(struct mGameObject *go) return &arrlast(sprites); } -void sprite_init(struct mSprite *sprite, struct mGameObject *go) +void sprite_init(struct sprite *sprite, struct mGameObject *go) { sprite->go = go; } -void sprite_io(struct mSprite *sprite, FILE *f, int read) +void sprite_io(struct sprite *sprite, FILE *f, int read) { char path[100]; if (read) { - fgets(path,100, f); + //fscanf(f, "%s", &path); + for (int i = 0; i < 100; i++) { + path[i] = fgetc(f); + + if (path[i] == '\0') break; + } fread(sprite, sizeof(*sprite), 1, f); sprite_loadtex(sprite, path); - printf("Tex was %s.\n", path); } else { + // fprintf(f, "%s", tex_get_path(sprite->tex)); + fputs(tex_get_path(sprite->tex), f); + fputc('\0', f); fwrite(sprite, sizeof(*sprite), 1, f); } } -void sprite_delete(struct mSprite *sprite) +void sprite_delete(struct sprite *sprite) { for (int i = 0; i < arrlen(sprites); i++) if (&sprites[i] == sprite) { @@ -68,12 +75,12 @@ void sprite_draw_all() sprite_draw(&sprites[i]); } -void sprite_loadtex(struct mSprite *sprite, const char *path) +void sprite_loadtex(struct sprite *sprite, const char *path) { sprite->tex = texture_loadfromfile(path); } -void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim) +void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim) { sprite->tex = texture_loadfromfile(path); sprite->anim = anim; @@ -83,12 +90,12 @@ void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D ani */ } -void sprite_settex(struct mSprite *sprite, struct Texture *tex) +void sprite_settex(struct sprite *sprite, struct Texture *tex) { sprite->tex = tex; } -unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite) +unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite) { sprite->anim.frame = (sprite->anim.frame + 1) % sprite->anim.frames; return interval; @@ -124,7 +131,7 @@ void sprite_initialize() glBindVertexArray(0); } -void sprite_draw(struct mSprite *sprite) +void sprite_draw(struct sprite *sprite) { if (sprite->tex != NULL) { @@ -173,7 +180,7 @@ void sprite_draw(struct mSprite *sprite) } } -void spriteanim_draw(struct mSprite *sprite) +void spriteanim_draw(struct sprite *sprite) { shader_use(animSpriteShader); diff --git a/source/engine/sprite.h b/source/engine/sprite.h index a8060a3..f2de3bc 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -19,7 +19,7 @@ struct Anim2D { int ms; }; -struct mSprite { +struct sprite { mfloat_t pos[2]; mfloat_t size[2]; float rotation; @@ -31,19 +31,19 @@ struct mSprite { struct Texture *tex; }; -struct mSprite *MakeSprite(struct mGameObject *go); -void sprite_delete(struct mSprite *sprite); -void sprite_init(struct mSprite *sprite, struct mGameObject *go); -void sprite_io(struct mSprite *sprite, FILE *f, int read); -void sprite_loadtex(struct mSprite *sprite, const char *path); -void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim); -void sprite_settex(struct mSprite *sprite, struct Texture *tex); +struct sprite *make_sprite(struct mGameObject *go); +void sprite_delete(struct sprite *sprite); +void sprite_init(struct sprite *sprite, struct mGameObject *go); +void sprite_io(struct sprite *sprite, FILE *f, int read); +void sprite_loadtex(struct sprite *sprite, const char *path); +void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim); +void sprite_settex(struct sprite *sprite, struct Texture *tex); void sprite_initialize(); -void sprite_draw(struct mSprite *sprite); -void spriteanim_draw(struct mSprite *sprite); +void sprite_draw(struct sprite *sprite); +void spriteanim_draw(struct sprite *sprite); void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]); void sprite_draw_all(); -unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite); +unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite); #endif diff --git a/source/engine/texture.c b/source/engine/texture.c index 959bcf1..edfa381 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -30,20 +30,29 @@ struct Texture *texture_pullfromfile(const char *path) stbi_set_flip_vertically_on_load(0); unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4); - if (stbi_failure_reason()) - YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason()); + while (data == NULL) { + YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason()); + return NULL; + } tex->data = data; + if (shlen(texhash) == 0) + sh_new_arena(texhash); + shput(texhash, path, tex); + tex->id = 0; + return tex; } char *tex_get_path(struct Texture *tex) { for (int i = 0; i < shlen(texhash); i++) { - if (tex == texhash[i].value) + if (tex == texhash[i].value) { + YughInfo("Found key %s", texhash[i].key); return texhash[i].key; + } } return NULL; @@ -54,12 +63,17 @@ struct Texture *texture_loadfromfile(const char *path) struct Texture *new = texture_pullfromfile(path); if (new == NULL) { - YughInfo("Texture not loaded!", 0); + YughError("Texture not loaded!", 0); + return new; } - glGenTextures(1, &new->id); + if (new->id == 0) { + glGenTextures(1, &new->id); - tex_gpu_load(new); + tex_gpu_load(new); + + YughInfo("Loaded texture path %s", path); + } return new; } @@ -76,8 +90,8 @@ void tex_pull(struct Texture *tex) stbi_set_flip_vertically_on_load(0); tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4); - if (stbi_failure_reason()) - YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason()); + if (tex->data == NULL) + YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason()); } void tex_flush(struct Texture *tex)