From 740077fae3708b9fb16d44014c8f807cda2b1c1c Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 3 Jan 2023 23:13:31 +0000 Subject: [PATCH] scheme --- source/engine/2dphysics.c | 2 - source/engine/3d/static_actor.c | 78 --------------------------------- source/engine/3d/static_actor.h | 24 ---------- source/engine/editor/editor.c | 9 ++-- source/engine/font.c | 22 +++++++--- source/engine/gameobject.c | 43 ++++++++---------- source/engine/gameobject.h | 11 +++-- source/engine/openglrender.c | 1 - source/engine/registry.c | 8 ++-- source/engine/registry.h | 6 +-- source/engine/shader.c | 8 +++- 11 files changed, 57 insertions(+), 155 deletions(-) delete mode 100644 source/engine/3d/static_actor.c delete mode 100644 source/engine/3d/static_actor.h diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 64bfbd0..e157ca2 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -17,8 +17,6 @@ #include "registry.h" -register_component(0, 0, 0, 0, 0, 0, 0, 0, 0); - cpSpace *space = NULL; float phys2d_gravity = -50.f; diff --git a/source/engine/3d/static_actor.c b/source/engine/3d/static_actor.c deleted file mode 100644 index 948e3b8..0000000 --- a/source/engine/3d/static_actor.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "static_actor.h" - -//ADDMAKE(StaticActor); - -static struct mStaticActor *models[100]; -static int numModels = 0; -static struct mStaticActor *shadow_casters[100]; -static int numShadowCasters = 0; - -struct mStaticActor *curActor = NULL; - -void staticactor_draw_dbg_color_pick(struct shader *s) -{ - for (int i = 0; i < numModels; i++) { - shader_setvec3(s, "PickingColor", models[i]->obj.editor.color); - setup_model_transform(&models[i]->obj.transform, s, 1.f); - //models[i]->obj.draw(s); - } - -} - -void staticactor_draw_models(struct shader *s) -{ - for (int i = 0; i < numModels; i++) { - setup_model_transform(&models[i]->obj.transform, s, 1.f); - draw_model(models[i]->model, s); - } -} - -void staticactor_draw_shadowcasters(struct shader *s) -{ - for (int i = 0; i < numShadowCasters; i++) { - setup_model_transform(&shadow_casters[i]->obj.transform, s, 1.f); - //models[i]->obj.draw(s); - } -} - -/* -void StaticActor::serialize(FILE * file) -{ - GameObject::serialize(file); - SerializeBool(file, &castShadows); - Serializecstr(file, currentModelPath); -} - -void StaticActor::deserialize(FILE * file) -{ - GameObject::deserialize(file); - DeserializeBool(file, &castShadows); - - Deserializecstr(file, currentModelPath, MAXPATH); - curActor = this; - set_new_model(currentModelPath); -} -*/ - - - -struct mStaticActor *MakeStaticActor(const char *modelPath) -{ - struct mStaticActor *newsa = - (struct mStaticActor *) malloc(sizeof(struct mStaticActor)); - newsa->model = GetExistingModel(modelPath); - models[numModels++] = newsa; - - return newsa; -} - -/* -Serialize *make_staticactor() -{ - StaticActor *nactor = (StaticActor *) malloc(sizeof(StaticActor)); - - return nactor; -} -*/ - -#include "nuke.h" diff --git a/source/engine/3d/static_actor.h b/source/engine/3d/static_actor.h deleted file mode 100644 index 1a5a93a..0000000 --- a/source/engine/3d/static_actor.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef STATIC_ACTOR_H -#define STATIC_ACTOR_H - -#include "gameobject.h" -#include "model.h" -#include "shader.h" - -struct mStaticActor { - struct gameobject obj; - struct model *model; - char *modelPath; - char currentModelPath[MAXPATH]; - bool castShadows; -}; - -void staticactor_draw_dbg_color_pick(struct shader *s); -void staticactor_draw_models(struct shader *s); -void staticactor_draw_shadowcasters(struct shader *s); -struct mStaticActor *MakeStaticActor(); -void staticactor_gui(struct mStaticActor *sa); - -extern struct mStaticActor *curActor; - -#endif diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c index cfd33dc..73d3cac 100644 --- a/source/engine/editor/editor.c +++ b/source/engine/editor/editor.c @@ -679,7 +679,8 @@ void editor_project_gui() { startobjectgui: if (selectedobject) { - draw_point(selectedobject->transform.position[0], selectedobject->transform.position[1], 5); + cpVect pos = cpBodyGetPosition(selectedobject->body); + draw_point(pos.x, pos.y, 5); NK_FORCE(gameobject) @@ -711,9 +712,9 @@ startobjectgui: // nuke_label("Components"); nuke_nel(3); - for (int i = 0; i < ncomponent; i++) { - if (nuke_btn(components[i].name)) { - gameobject_addcomponent(selectedobject, &components[i]); + for (int i = 0; i < gameobject_ncomponents(selectedobject); i++) { + if (nuke_btn(selectedobject->components[i].ref->name)) { + gameobject_addcomponent(selectedobject, &selectedobject->components[i]); } } diff --git a/source/engine/font.c b/source/engine/font.c index a11d106..44790d4 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -12,7 +12,7 @@ #include "openglrender.h" -#include +#include "stb_truetype.h" #include "stb_rect_pack.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" @@ -20,8 +20,6 @@ static uint32_t VBO = 0; static uint32_t VAO = 0; -unsigned char ttf_buffer[1<<25]; - struct sFont *font; static struct shader *shader; @@ -61,7 +59,14 @@ struct sFont *MakeFont(const char *fontfile, int height) char fontpath[256]; snprintf(fontpath, 256, "fonts/%s", fontfile); - fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb")); + + FILE *f = fopen(fontpath, "rb"); + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); + unsigned char *ttf_buffer = malloc(fsize+1); + fread(ttf_buffer, fsize, 1, f); + fclose(f); unsigned char *bitmap = malloc(packsize*packsize); @@ -73,25 +78,30 @@ struct sFont *MakeFont(const char *fontfile, int height) stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs); stbtt_PackEnd(&pc); - stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char)*packsize); + //stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char)*packsize); stbtt_fontinfo fontinfo; if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) { YughError("Failed to make font %s", fontfile); } + + float scale = stbtt_ScaleForPixelHeight(&fontinfo, height); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &newfont->texID); glBindTexture(GL_TEXTURE_2D, newfont->texID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap); + + //glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - + free(ttf_buffer); + free(bitmap); for (unsigned char c = 32; c < 127; c++) { stbtt_packedchar glyph = glyphs[c-32]; diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 0f75504..5972bfa 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -27,7 +27,7 @@ struct gameobject *get_gameobject_from_id(int id) int id_from_gameobject(struct gameobject *go) { for (int i = 0; i < arrlen(gameobjects); i++) { - if (&gameobjects[i] == i) return i; + if (&gameobjects[i] == go) return i; } return -1; @@ -51,7 +51,6 @@ int MakeGameobject() YughInfo("Making new gameobject"); struct gameobject go = { .editor.id = arrlen(gameobjects), - .transform.scale = 1.f, .scale = 1.f, .bodytype = CP_BODY_TYPE_STATIC, .mass = 1.f @@ -71,10 +70,10 @@ int MakeGameobject() void gameobject_addcomponent(struct gameobject *go, struct component_interface *c) { struct component new; - new.interface = c; + new.ref = c; new.data = c->make(go); new.go = go; - arrput(go->components, c); + arrput(go->components, new); } void gameobject_delete(int id) @@ -82,7 +81,7 @@ void gameobject_delete(int id) YughInfo("Deleting gameobject with id %d.", id); struct gameobject *go = &gameobjects[id]; for (int i = 0; i < arrlen(go->components); i++) { - go->components[i].delete(go->components[i].data); + comp_delete(&go->components[i]); arrdel(go->components, i); } @@ -93,24 +92,13 @@ void gameobject_delete(int id) void gameobject_delcomponent(struct gameobject *go, int n) { - go->components[n].delete(go->components[n].data); + comp_delete(&go->components[n]); arrdel(go->components, n); } -void setup_model_transform(struct mTransform *t, struct shader *s, float scale) -{ - mfloat_t modelT[16] = { 0.f }; - mfloat_t matbuff[16] = { 0.f }; - memcpy(modelT, UNITMAT4, sizeof(modelT)); - mat4_translate_vec3(modelT, t->position); - mat4_multiply(modelT, modelT, mat4_rotation_quat(matbuff, t->rotation)); - mat4_scale_vec3f(modelT, scale); - shader_setmat4(s, "model", modelT); - -} - void gameobject_save(struct gameobject *go, FILE * file) { +/* fwrite(go, sizeof(*go), 1, file); YughInfo("Number of components is %d.", arrlen(go->components)); @@ -125,6 +113,7 @@ void gameobject_save(struct gameobject *go, FILE * file) else go->components[i].io(go->components[i].data, file, 0); } + */ } int gameobject_makefromprefab(char *path) @@ -150,6 +139,7 @@ int gameobject_makefromprefab(char *path) void gameobject_init(struct gameobject *go, FILE * fprefab) { +/* go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f)); cpBodySetType(go->body, go->bodytype); cpBodySetUserData(go->body, go); @@ -173,6 +163,7 @@ void gameobject_init(struct gameobject *go, FILE * fprefab) newc->init(newc->data, go); } + */ } @@ -221,6 +212,11 @@ void toggleprefab(struct gameobject *go) } } +int gameobject_ncomponents(struct gameobject *go) +{ + return arrlen(go->components); +} + void gameobject_move(struct gameobject *go, cpVect vec) { cpVect p = cpBodyGetPosition(go->body); @@ -300,15 +296,13 @@ void object_gui(struct gameobject *go) for (int i = 0; i < arrlen(go->components); i++) { struct component *c = &go->components[i]; - if (c->draw_debug) - c->draw_debug(c->data); - + comp_draw_debug(c); nuke_nel(5); if (nuke_btn("Del")) n = i; - if (nuke_push_tree_id(c->name, i)) { - c->draw_gui(c->data); + if (nuke_push_tree_id(c->ref->name, i)) { + comp_draw_gui(c); nuke_tree_pop(); } @@ -324,8 +318,7 @@ void gameobject_draw_debugs() { for (int i = 0; i < arrlen(gameobjects); i++) { for (int j = 0; j < arrlen(gameobjects[i].components); j++) { struct component *c = &gameobjects[i].components[j]; - - if (c->draw_debug) c->draw_debug(c->data); + comp_draw_debug(c); } } } diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 3e5db50..377c104 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -3,11 +3,11 @@ #include #include "mathc.h" -#include "transform.h" #include "config.h" #include #include #include "2dphysics.h" +#include "registry.h" struct shader; struct sprite; @@ -29,16 +29,15 @@ struct go_temp { }; struct gameobject { - struct mTransform transform; - struct editor editor; cpBodyType bodytype; float scale; float mass; - cpBody *body; float f; /* friction */ float e; /* elasticity */ + cpBody *body; struct component *components; struct phys_cbs *cbs; + struct editor editor; }; extern struct gameobject *gameobjects; @@ -49,16 +48,16 @@ void gameobject_delete(int id); void clear_gameobjects(); int number_of_gameobjects(); void set_n_gameobjects(int n); -void setup_model_transform(struct mTransform *t, struct shader *s, float scale); void toggleprefab(struct gameobject *go); struct gameobject *get_gameobject_from_id(int id); int id_from_gameobject(struct gameobject *go); void gameobject_save(struct gameobject *go, FILE * file); -void gameobject_addcomponent(struct gameobject *go, struct component *c); +void gameobject_addcomponent(struct gameobject *go, struct component_interface *c); void gameobject_delcomponent(struct gameobject *go, int n); void gameobject_loadcomponent(struct gameobject *go, int id); +int gameobject_ncomponents(struct gameobject *go); void gameobject_saveprefab(struct gameobject *go); int gameobject_makefromprefab(char *path); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 54d34f3..fc9fb09 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -4,7 +4,6 @@ #include "shader.h" #include "font.h" #include "config.h" -#include "static_actor.h" #include "gameobject.h" #include "camera.h" #include "window.h" diff --git a/source/engine/registry.c b/source/engine/registry.c index 8d0870e..6d7447b 100644 --- a/source/engine/registry.c +++ b/source/engine/registry.c @@ -76,12 +76,12 @@ void comp_delete(struct component *c) c->ref->delete(c->data); } -void comp_init(struct component *c) +void comp_init(struct component *c, struct gameobject *go) { - c->ref->init(c->data); + c->ref->init(c->data, go); } -void comp_io(struct component *c, int read) +void comp_io(struct component *c, FILE *f, int read) { - c->ref->io(c->data, read); + c->ref->io(c->data, f, read); } \ No newline at end of file diff --git a/source/engine/registry.h b/source/engine/registry.h index 13f1a0b..c227db1 100644 --- a/source/engine/registry.h +++ b/source/engine/registry.h @@ -28,15 +28,15 @@ struct component comp_make(struct component_interface *interface); void comp_draw_debug(struct component *c); void comp_draw_gui(struct component *c); void comp_delete(struct component *c); -void comp_init(struct component *c); -void comp_io(struct component *c, int read); +void comp_init(struct component *c, struct gameobject *go); +void comp_io(struct component *c, FILE *f, int read); void comp_update(struct component *c, struct gameobject *go); void registry_init(); void register_component(const char *name, size_t size, - void (*make)(struct gameobject * go, struct component * c), + void (*make)(struct gameobject * go), void (*delete)(void *data), void (*io)(void *data, FILE *f, int read), void(*draw_debug)(void *data), diff --git a/source/engine/shader.c b/source/engine/shader.c index 76768fb..812056b 100644 --- a/source/engine/shader.c +++ b/source/engine/shader.c @@ -66,12 +66,14 @@ GLuint load_shader_from_file(const char *path, int type) if (!path) perror(spath), exit(1); - char buf[SHADER_BUF] = {'\0'}; + char *buf; long int fsize; fseek(f, 0, SEEK_END); fsize = ftell(f); + buf = malloc(fsize+1); rewind(f); - fread(buf, fsize, 1, f); + size_t r = fread(buf, sizeof(char), fsize, f); + buf[r] = '\0'; fclose(f); @@ -85,6 +87,8 @@ GLuint load_shader_from_file(const char *path, int type) return 0; } + free(buf); + return id; }