Fix seg faults when loading gameobjects

This commit is contained in:
John Alanbrook 2022-08-16 04:46:06 +00:00
parent 4f0a59f581
commit 7aef7afc90
5 changed files with 35 additions and 33 deletions

View file

@ -388,6 +388,7 @@ static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) {
void editor_init(struct mSDLWindow *window) { void editor_init(struct mSDLWindow *window) {
levels = vec_make(MAXNAME, 10); levels = vec_make(MAXNAME, 10);
get_levels();
editor_load_projects(); editor_load_projects();
findPrefabs(); findPrefabs();
@ -453,6 +454,8 @@ void editor_project_gui() {
*/ */
NK_MENU_START(level) NK_MENU_START(level)
nk_layout_row_dynamic(ctx,25,3); nk_layout_row_dynamic(ctx,25,3);
nk_labelf(ctx, "Current level: %s", current_level[0] == '\0' ? "No level loaded." : current_level);
if (nk_button_label(ctx, "New")) { if (nk_button_label(ctx, "New")) {
new_level(); new_level();
current_level[0] = '\0'; current_level[0] = '\0';
@ -464,8 +467,9 @@ void editor_project_gui() {
} }
if (nk_button_label(ctx, "Save as")) { if (nk_button_label(ctx, "Save as")) {
save_level(levelname);
strcpy(current_level, levelname); strcpy(current_level, levelname);
strncat(current_level, EXT_LEVEL, MAXNAME);
save_level(current_level);
levelname[0] = '\0'; levelname[0] = '\0';
get_levels(); get_levels();
} }
@ -968,8 +972,7 @@ void sprite_gui(struct mSprite *sprite) {
if (sprite->tex != NULL) { if (sprite->tex != NULL) {
nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path); nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path);
nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height);
sprite->tex->height);
if (nk_button_label(ctx, "Imgbutton")) if (nk_button_label(ctx, "Imgbutton"))
editor_selectasset_str(sprite->tex->path); editor_selectasset_str(sprite->tex->path);
// if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50, // if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50,

View file

@ -72,15 +72,13 @@ void gameobject_delcomponent(struct mGameObject *go, int n)
vec_del_order(go->components, n); vec_del_order(go->components, n);
} }
void setup_model_transform(struct mTransform *t, struct mShader *s, void setup_model_transform(struct mTransform *t, struct mShader *s, float scale)
float scale)
{ {
mfloat_t modelT[16] = { 0.f }; mfloat_t modelT[16] = { 0.f };
mfloat_t matbuff[16] = { 0.f }; mfloat_t matbuff[16] = { 0.f };
memcpy(modelT, UNITMAT4, sizeof(modelT)); memcpy(modelT, UNITMAT4, sizeof(modelT));
mat4_translate_vec3(modelT, t->position); mat4_translate_vec3(modelT, t->position);
mat4_multiply(modelT, modelT, mat4_multiply(modelT, modelT, mat4_rotation_quat(matbuff, t->rotation));
mat4_rotation_quat(matbuff, t->rotation));
mat4_scale_vec3f(modelT, scale); mat4_scale_vec3f(modelT, scale);
shader_setmat4(s, "model", modelT); shader_setmat4(s, "model", modelT);
@ -98,30 +96,19 @@ void gameobject_save(struct mGameObject *go, FILE * file)
} }
} }
void gameobject_saveprefab(struct mGameObject *go)
{
char prefabfname[60] = { '\0' };
strncat(prefabfname, go->editor.prefabName, MAXNAME);
strncat(prefabfname, EXT_PREFAB, 10);
FILE *pfile = fopen(prefabfname, "w+");
gameobject_save(go, pfile);
fclose(pfile);
findPrefabs();
}
void gameobject_makefromprefab(char *path) void gameobject_makefromprefab(char *path)
{ {
FILE *fprefab = fopen(path, "r"); FILE *fprefab = fopen(path, "rb");
if (fprefab == NULL) { if (fprefab == NULL) {
return; return;
} }
struct mGameObject *new = MakeGameobject(); struct mGameObject *new = MakeGameobject();
struct vec *hold = new->components;
fread(new, sizeof(*new), 1, fprefab); fread(new, sizeof(*new), 1, fprefab);
new->components = hold;
new->editor.id = gameobjects->len - 1; new->editor.id = gameobjects->len - 1;
new->body = cpSpaceAddBody(space, cpBodyNew(new->mass, 1.f));
gameobject_init(new, fprefab); gameobject_init(new, fprefab);
@ -135,10 +122,8 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab)
vec_load(go->components, fprefab); vec_load(go->components, fprefab);
for (int i = 0; i < go->components->len; i++) { for (int i = 0; i < go->components->len; i++) {
struct component *newc = struct component *newc = vec_set(go->components, i, &components[((struct component *) vec_get(go->components, i))->id]);
vec_set(go->components, i,
&components[((struct component *)
vec_get(go->components, i))->id]);
newc->go = go; newc->go = go;
newc->data = malloc(newc->datasize); newc->data = malloc(newc->datasize);
fread(newc->data, newc->datasize, 1, fprefab); fread(newc->data, newc->datasize, 1, fprefab);
@ -146,6 +131,22 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab)
} }
} }
void gameobject_saveprefab(struct mGameObject *go)
{
char prefabfname[60] = { '\0' };
strncat(prefabfname, go->editor.prefabName, MAXNAME);
strncat(prefabfname, EXT_PREFAB, 10);
FILE *pfile = fopen(prefabfname, "wb+");
gameobject_save(go, pfile);
fclose(pfile);
findPrefabs();
}
void gameobject_syncprefabs(char *revertPath) void gameobject_syncprefabs(char *revertPath)
{ {
/* /*

View file

@ -46,8 +46,7 @@ void gameobject_delete(int id);
void clear_gameobjects(); void clear_gameobjects();
int number_of_gameobjects(); int number_of_gameobjects();
void set_n_gameobjects(int n); void set_n_gameobjects(int n);
void setup_model_transform(struct mTransform *t, struct mShader *s, void setup_model_transform(struct mTransform *t, struct mShader *s, float scale);
float scale);
void toggleprefab(struct mGameObject *go); void toggleprefab(struct mGameObject *go);
struct mGameObject *get_gameobject_from_id(int id); struct mGameObject *get_gameobject_from_id(int id);
void gameobject_save(struct mGameObject *go, FILE * file); void gameobject_save(struct mGameObject *go, FILE * file);

View file

@ -7,7 +7,7 @@
void save_level(char name[MAXNAME]) void save_level(char name[MAXNAME])
{ {
FILE *lfile = res_open(name, "w+"); FILE *lfile = res_open(name, "wb+");
if (!lfile) return; if (!lfile) return;
@ -24,7 +24,7 @@ void save_level(char name[MAXNAME])
void load_level(char name[MAXNAME]) void load_level(char name[MAXNAME])
{ {
FILE *lfile = fopen(name, "r"); FILE *lfile = fopen(name, "rb");
if (!lfile) return; if (!lfile) return;
@ -38,6 +38,7 @@ void load_level(char name[MAXNAME])
for (int i = 0; i < objs; i++) { for (int i = 0; i < objs; i++) {
struct mGameObject *go = vec_add(gameobjects, NULL); struct mGameObject *go = vec_add(gameobjects, NULL);
fread(go, sizeof(struct mGameObject), 1, lfile); fread(go, sizeof(struct mGameObject), 1, lfile);
go->components = vec_make(1,1);
gameobject_init(go, lfile); gameobject_init(go, lfile);
} }

View file

@ -56,8 +56,7 @@ char *get_filename_from_path(char *path, int extension)
printf("Making without extension ...\n"); printf("Making without extension ...\n");
} }
char *filename = char *filename = malloc(sizeof(char) * (end - dirpos - offset + 1));
(char *) malloc(sizeof(char) * (end - dirpos - offset + 1));
strncpy(filename, dirpos, end - dirpos - offset); strncpy(filename, dirpos, end - dirpos - offset);
return filename; return filename;
} }
@ -78,8 +77,7 @@ FILE *res_open(char *path, const char *tag)
return f; return f;
} }
static int ext_check(const char *path, const struct stat *sb, int typeflag, static int ext_check(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
struct FTW *ftwbuf)
{ {
if (typeflag == FTW_F) { if (typeflag == FTW_F) {
const char *ext = strrchr(path, '.'); const char *ext = strrchr(path, '.');