Improved log; fixed texture loading and hash table

This commit is contained in:
John Alanbrook 2022-11-17 22:48:20 +00:00
parent 36277d5c7b
commit 5386e19e17
12 changed files with 78 additions and 46 deletions

View file

@ -1007,23 +1007,28 @@ void game_stop() { physOn = 0; }
void game_pause() { physOn = 0; } void game_pause() { physOn = 0; }
void sprite_gui(struct mSprite *sprite) { void sprite_gui(struct sprite *sprite) {
nuke_nel(2); 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) { if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) {
sprite_loadtex(sprite, selected_asset->filename); sprite_loadtex(sprite, selected_asset->filename);
} }
if (sprite->tex != NULL) { 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_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); nk_layout_row_static(ctx, sprite->tex->height, sprite->tex->width, 1);
if (nk_button_image(ctx, nk_image_id(sprite->tex->id))) if (nk_button_image(ctx, nk_image_id(sprite->tex->id)))
editor_selectasset_str(tex_get_path(sprite->tex)); editor_selectasset_str(tex_get_path(sprite->tex));
} }
nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f); nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f);
nuke_nel(3); nuke_nel(3);
@ -1041,5 +1046,6 @@ void sprite_gui(struct mSprite *sprite) {
sprite->pos[0] = -0.5f; sprite->pos[0] = -0.5f;
sprite->pos[1] = 0.f; sprite->pos[1] = 0.f;
} }
} }

View file

@ -65,7 +65,7 @@ extern int show_desktop;
struct vec; struct vec;
struct gameproject; struct gameproject;
struct mSprite; struct sprite;
extern struct gameproject *cur_project; extern struct gameproject *cur_project;
extern struct vec *projects; extern struct vec *projects;
@ -104,6 +104,6 @@ void get_levels();
int obj_gui_hierarchy(struct mGameObject *selected); int obj_gui_hierarchy(struct mGameObject *selected);
void sprite_gui(struct mSprite *sprite); void sprite_gui(struct sprite *sprite);
#endif #endif

View file

@ -7,6 +7,7 @@
#include <stb_truetype.h> #include <stb_truetype.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#include "stb_image.h" #include "stb_image.h"
#define PL_MPEG_IMPLEMENTATION #define PL_MPEG_IMPLEMENTATION

View file

@ -140,7 +140,7 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab)
go->components[i] = components[n]; go->components[i] = components[n];
struct component *newc = &go->components[i]; struct component *newc = &go->components[i];
newc->go = go; newc->go = go;
newc->data = malloc(newc->datasize); newc->data = calloc(1, newc->datasize);
if (newc->io == NULL) if (newc->io == NULL)
fread(newc->data, newc->datasize, 1, fprefab); fread(newc->data, newc->datasize, 1, fprefab);

View file

@ -9,7 +9,7 @@
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
struct mShader; struct mShader;
struct mSprite; struct sprite;
struct component; struct component;
struct editor { struct editor {

View file

@ -9,6 +9,10 @@
#define logLevel 0 #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, ...) void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
{ {
if (priority >= logLevel) { if (priority >= logLevel) {
@ -23,8 +27,7 @@ void mYughLog(int category, int priority, int line, const char *file, const char
va_end(args); va_end(args);
char buffer[ERROR_BUFFER] = { '\0' }; char buffer[ERROR_BUFFER] = { '\0' };
snprintf(buffer, ERROR_BUFFER, "%s\n[ %s:%d ] %s\n", snprintf(buffer, ERROR_BUFFER, "%s | %s | %s [ %s:%d ] %s\n", logstr[priority], catstr[0], dt, file, line, msgbuffer);
msgbuffer, file, line, dt);
printf("%s", buffer); printf("%s", buffer);
fflush(stdout); fflush(stdout);

View file

@ -88,7 +88,7 @@ GLuint debugColorPickBO = 0;
GLuint debugColorPickTEX = 0; GLuint debugColorPickTEX = 0;
struct mSprite *tsprite = NULL; struct sprite *tsprite = NULL;
static unsigned int projUBO; static unsigned int projUBO;

View file

@ -9,7 +9,7 @@ struct window;
extern struct mShader *spriteShader; extern struct mShader *spriteShader;
extern struct mShader *animSpriteShader; extern struct mShader *animSpriteShader;
extern struct mSprite *tsprite; extern struct sprite *tsprite;
extern int renderMode; extern int renderMode;

View file

@ -12,6 +12,7 @@ int ncomponent = 0;
void registry_init() void registry_init()
{ {
/* /*
REGISTER_COMP(sprite); REGISTER_COMP(sprite);
REGISTER_COMP(2d_circle); REGISTER_COMP(2d_circle);
@ -22,8 +23,8 @@ void registry_init()
*/ */
register_component("Sprite", register_component("Sprite",
sizeof(struct mSprite), sizeof(struct sprite),
MakeSprite, make_sprite,
sprite_delete, sprite_delete,
sprite_io, sprite_io,
NULL, NULL,

View file

@ -13,16 +13,16 @@
struct TextureOptions TEX_SPRITE = { 1, 0, 0 }; struct TextureOptions TEX_SPRITE = { 1, 0, 0 };
struct mSprite *sprites; struct sprite *sprites;
static uint32_t quadVAO; static uint32_t quadVAO;
struct mSprite *MakeSprite(struct mGameObject *go) struct sprite *make_sprite(struct mGameObject *go)
{ {
if (arrcap(sprites) == 0) if (arrcap(sprites) == 0)
arrsetcap(sprites, 100); arrsetcap(sprites, 100);
struct mSprite sprite = { struct sprite sprite = {
.color = {1.f, 1.f, 1.f}, .color = {1.f, 1.f, 1.f},
.size = {1.f, 1.f}, .size = {1.f, 1.f},
.tex = texture_loadfromfile("ph.png"), .tex = texture_loadfromfile("ph.png"),
@ -33,26 +33,33 @@ struct mSprite *MakeSprite(struct mGameObject *go)
return &arrlast(sprites); return &arrlast(sprites);
} }
void sprite_init(struct mSprite *sprite, struct mGameObject *go) void sprite_init(struct sprite *sprite, struct mGameObject *go)
{ {
sprite->go = 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]; char path[100];
if (read) { 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); fread(sprite, sizeof(*sprite), 1, f);
sprite_loadtex(sprite, path); sprite_loadtex(sprite, path);
printf("Tex was %s.\n", path);
} else { } else {
// fprintf(f, "%s", tex_get_path(sprite->tex));
fputs(tex_get_path(sprite->tex), f); fputs(tex_get_path(sprite->tex), f);
fputc('\0', f);
fwrite(sprite, sizeof(*sprite), 1, 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++) for (int i = 0; i < arrlen(sprites); i++)
if (&sprites[i] == sprite) { if (&sprites[i] == sprite) {
@ -68,12 +75,12 @@ void sprite_draw_all()
sprite_draw(&sprites[i]); 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); 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->tex = texture_loadfromfile(path);
sprite->anim = anim; 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; 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; sprite->anim.frame = (sprite->anim.frame + 1) % sprite->anim.frames;
return interval; return interval;
@ -124,7 +131,7 @@ void sprite_initialize()
glBindVertexArray(0); glBindVertexArray(0);
} }
void sprite_draw(struct mSprite *sprite) void sprite_draw(struct sprite *sprite)
{ {
if (sprite->tex != NULL) { 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); shader_use(animSpriteShader);

View file

@ -19,7 +19,7 @@ struct Anim2D {
int ms; int ms;
}; };
struct mSprite { struct sprite {
mfloat_t pos[2]; mfloat_t pos[2];
mfloat_t size[2]; mfloat_t size[2];
float rotation; float rotation;
@ -31,19 +31,19 @@ struct mSprite {
struct Texture *tex; struct Texture *tex;
}; };
struct mSprite *MakeSprite(struct mGameObject *go); struct sprite *make_sprite(struct mGameObject *go);
void sprite_delete(struct mSprite *sprite); void sprite_delete(struct sprite *sprite);
void sprite_init(struct mSprite *sprite, struct mGameObject *go); void sprite_init(struct sprite *sprite, struct mGameObject *go);
void sprite_io(struct mSprite *sprite, FILE *f, int read); void sprite_io(struct sprite *sprite, FILE *f, int read);
void sprite_loadtex(struct mSprite *sprite, const char *path); void sprite_loadtex(struct sprite *sprite, const char *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);
void sprite_settex(struct mSprite *sprite, struct Texture *tex); void sprite_settex(struct sprite *sprite, struct Texture *tex);
void sprite_initialize(); void sprite_initialize();
void sprite_draw(struct mSprite *sprite); void sprite_draw(struct sprite *sprite);
void spriteanim_draw(struct mSprite *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 video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]);
void sprite_draw_all(); void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite); unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite);
#endif #endif

View file

@ -30,21 +30,30 @@ struct Texture *texture_pullfromfile(const char *path)
stbi_set_flip_vertically_on_load(0); stbi_set_flip_vertically_on_load(0);
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4); unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
if (stbi_failure_reason()) while (data == NULL) {
YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason()); YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
return NULL;
}
tex->data = data; tex->data = data;
if (shlen(texhash) == 0)
sh_new_arena(texhash);
shput(texhash, path, tex); shput(texhash, path, tex);
tex->id = 0;
return tex; return tex;
} }
char *tex_get_path(struct Texture *tex) { char *tex_get_path(struct Texture *tex) {
for (int i = 0; i < shlen(texhash); i++) { 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 texhash[i].key;
} }
}
return NULL; return NULL;
} }
@ -54,13 +63,18 @@ struct Texture *texture_loadfromfile(const char *path)
struct Texture *new = texture_pullfromfile(path); struct Texture *new = texture_pullfromfile(path);
if (new == NULL) { if (new == NULL) {
YughInfo("Texture not loaded!", 0); YughError("Texture not loaded!", 0);
return new;
} }
if (new->id == 0) {
glGenTextures(1, &new->id); glGenTextures(1, &new->id);
tex_gpu_load(new); tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
return new; return new;
} }
@ -76,8 +90,8 @@ void tex_pull(struct Texture *tex)
stbi_set_flip_vertically_on_load(0); stbi_set_flip_vertically_on_load(0);
tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4); tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4);
if (stbi_failure_reason()) if (tex->data == NULL)
YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason()); YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
} }
void tex_flush(struct Texture *tex) void tex_flush(struct Texture *tex)