From c56e2841b6607b370f4caeaa178abd09942990a2 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 26 Aug 2022 16:38:35 +0000 Subject: [PATCH] ?? --- source/engine/engine.c | 1 - source/engine/gameobject.c | 9 ++++++--- source/engine/level.c | 2 -- source/engine/mrbffi.c | 6 ------ source/engine/registry.c | 12 ++++++------ source/engine/registry.h | 2 ++ source/engine/sprite.c | 26 +++++++++----------------- source/engine/sprite.h | 7 +------ source/engine/texture.c | 1 - 9 files changed, 24 insertions(+), 42 deletions(-) diff --git a/source/engine/engine.c b/source/engine/engine.c index c0d2959..68ae2aa 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -64,7 +64,6 @@ void engine_init() //stbi_set_flip_vertically_on_load(1); phys2d_init(); - gui_init(); sound_init(); } diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 233652f..b33d57b 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -94,7 +94,10 @@ void gameobject_save(struct mGameObject *go, FILE * file) { fwrite(go, sizeof(*go), 1, file); - fwrite(arrlen(go->components), sizeof(int), 1, file); + printf("Number of components is %d.\n", arrlen(go->components)); + + int n = arrlen(go->components); + fwrite(&n, sizeof(n), 1, file); for (int i = 0; i < arrlen(go->components); i++) { fwrite(go->components[i].id, sizeof(int), 1, file); fwrite(go->components[i].data, go->components[i].datasize, 1, file); @@ -131,8 +134,8 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab) for (int i = 0; i < comp_n; i++) { fread(&n, sizeof(int), 1, fprefab); - arrput(go->components, components[n]); - struct component *newc = &arrlast(go->components); + go->components[i] = components[n]; + struct component *newc = &go->components[i]; newc->go = go; newc->data = malloc(newc->datasize); fread(newc->data, newc->datasize, 1, fprefab); diff --git a/source/engine/level.c b/source/engine/level.c index 644b910..d9299fe 100644 --- a/source/engine/level.c +++ b/source/engine/level.c @@ -24,8 +24,6 @@ void save_level(char name[MAXNAME]) void load_level(char name[MAXNAME]) { - - FILE *lfile = fopen(name, "rb"); if (!lfile) return; diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index a9c429e..01ea9df 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -192,10 +192,6 @@ mrb_value mrb_settings_cmd(mrb_state *mrb, mrb_value self) { return self; } -mrb_value mrb_sprite_make(mrb_state *mrb, mrb_value self) { - struct mSprite *new = gui_makesprite(); -} - mrb_value mrb_editor_render(mrb_state *mrb, mrb_value self) { editor_render(); return self; @@ -216,8 +212,6 @@ void ffi_load() { MRB_FUNC(ui_rendertext, MRB_ARGS_REQ(5)); - MRB_FUNC(sprite_make, MRB_ARGS_REQ(1)); - MRB_FUNC(c_reload, MRB_ARGS_REQ(1)); MRB_FUNC(win_make, MRB_ARGS_REQ(1)); diff --git a/source/engine/registry.c b/source/engine/registry.c index 021e72e..fee8f0f 100644 --- a/source/engine/registry.c +++ b/source/engine/registry.c @@ -21,12 +21,12 @@ void registry_init() REGISTER_COMP(2d_edge); */ - register_component("Sprite", sizeof(struct mSprite), MakeSprite, NULL, sprite_gui, sprite_init); - register_component("2D Circle Collider", sizeof(struct phys2d_circle), Make2DCircle, phys2d_dbgdrawcircle, circle_gui, phys2d_circleinit); - register_component("2D Segment", sizeof(struct phys2d_segment), Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit); - register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, phys2d_dbgdrawbox, box_gui, phys2d_boxinit); - register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit); - register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit); + register_component("Sprite", sizeof(struct mSprite), MakeSprite, sprite_delete, NULL, sprite_gui, sprite_init); + register_component("2D Circle Collider", sizeof(struct phys2d_circle), Make2DCircle, NULL, phys2d_dbgdrawcircle, circle_gui, phys2d_circleinit); + register_component("2D Segment", sizeof(struct phys2d_segment), NULL, Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit); + register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, NULL, phys2d_dbgdrawbox, box_gui, phys2d_boxinit); + register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, NULL, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit); + register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, NULL, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit); } void register_component(const char *name, size_t size, diff --git a/source/engine/registry.h b/source/engine/registry.h index 99b82f0..116b87d 100644 --- a/source/engine/registry.h +++ b/source/engine/registry.h @@ -13,6 +13,7 @@ struct component { struct mGameObject *go; void (*draw_debug)(void *data); void (*draw_gui)(void *data); + void (*delete)(void *data); int id; int datasize; void (*init)(void *data, struct mGameObject * go); @@ -29,6 +30,7 @@ void comp_update(struct component *c, struct mGameObject *go); void registry_init(); void register_component(const char *name, size_t size, void (*make)(struct mGameObject * go, struct component * c), + void (*delete)(void *data), void(*draw_debug)(void *data), void(*draw_gui)(void *data), void(*init)(void *data, struct mGameObject * go)); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 5fecfad..31cb47b 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -11,9 +11,6 @@ #include #include "stb_ds.h" - -static struct mGameObject *gui_go = NULL; - struct TextureOptions TEX_SPRITE = { 1, 0, 0 }; struct mSprite *sprites; @@ -22,6 +19,9 @@ static uint32_t quadVAO; struct mSprite *MakeSprite(struct mGameObject *go) { + if (arrcap(sprites) == 0) + arrsetcap(sprites, 100); + struct mSprite sprite = { .color = {1.f, 1.f, 1.f}, .size = {1.f, 1.f}, @@ -31,17 +31,20 @@ struct mSprite *MakeSprite(struct mGameObject *go) sprite_init(&sprite, go); arrput(sprites, sprite); return &arrlast(sprites); + sprite->go = go; } -void sprite_init(struct mSprite *sprite, struct mGameObject *go) +void sprite_delete(struct mSprite *sprite) +.3+ { - sprite->go = go; + } void sprite_draw_all() { //shader_use(spriteShader); - arrwalk(sprites, sprite_draw); + for (int i = 0; i < arrlen(sprites); i++) + sprite_draw(&sprites[i]); } void sprite_loadtex(struct mSprite *sprite, const char *path) @@ -199,14 +202,3 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } -void gui_init() -{ - gui_go = MakeGameobject(); -} - -struct mSprite *gui_makesprite() -{ - struct mSprite *new = MakeSprite(gui_go); - return new; -} - diff --git a/source/engine/sprite.h b/source/engine/sprite.h index a3052c8..94eea78 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -31,6 +31,7 @@ struct mSprite { }; struct mSprite *MakeSprite(struct mGameObject *go); +void sprite_delete(struct mSprite *sprite); void sprite_init(struct mSprite *sprite, struct mGameObject *go); void sprite_loadtex(struct mSprite *sprite, const char *path); void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim); @@ -42,11 +43,5 @@ void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float void sprite_draw_all(); unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite); -struct mSprite *gui_makesprite(); -void gui_init(); - -void sprite_draw_all(); - - #endif diff --git a/source/engine/texture.c b/source/engine/texture.c index 2cdbb19..0a79edb 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -19,7 +19,6 @@ struct Texture *texture_pullfromfile(const char *path) if (index != -1) return texhash[index].value; - struct Texture *tex = calloc(1, sizeof(*tex)); tex->flipy = 0; tex->opts.sprite = 1;