Fix seg faults when loading gameobjects
This commit is contained in:
parent
4f0a59f581
commit
7aef7afc90
|
@ -388,6 +388,7 @@ static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) {
|
|||
|
||||
void editor_init(struct mSDLWindow *window) {
|
||||
levels = vec_make(MAXNAME, 10);
|
||||
get_levels();
|
||||
editor_load_projects();
|
||||
findPrefabs();
|
||||
|
||||
|
@ -453,6 +454,8 @@ void editor_project_gui() {
|
|||
*/
|
||||
NK_MENU_START(level)
|
||||
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")) {
|
||||
new_level();
|
||||
current_level[0] = '\0';
|
||||
|
@ -464,8 +467,9 @@ void editor_project_gui() {
|
|||
}
|
||||
|
||||
if (nk_button_label(ctx, "Save as")) {
|
||||
save_level(levelname);
|
||||
strcpy(current_level, levelname);
|
||||
strncat(current_level, EXT_LEVEL, MAXNAME);
|
||||
save_level(current_level);
|
||||
levelname[0] = '\0';
|
||||
get_levels();
|
||||
}
|
||||
|
@ -968,8 +972,7 @@ void sprite_gui(struct mSprite *sprite) {
|
|||
|
||||
if (sprite->tex != NULL) {
|
||||
nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path);
|
||||
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);
|
||||
if (nk_button_label(ctx, "Imgbutton"))
|
||||
editor_selectasset_str(sprite->tex->path);
|
||||
// if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50,
|
||||
|
|
|
@ -72,15 +72,13 @@ void gameobject_delcomponent(struct mGameObject *go, int n)
|
|||
vec_del_order(go->components, n);
|
||||
}
|
||||
|
||||
void setup_model_transform(struct mTransform *t, struct mShader *s,
|
||||
float scale)
|
||||
void setup_model_transform(struct mTransform *t, struct mShader *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_multiply(modelT, modelT, mat4_rotation_quat(matbuff, t->rotation));
|
||||
mat4_scale_vec3f(modelT, scale);
|
||||
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)
|
||||
{
|
||||
FILE *fprefab = fopen(path, "r");
|
||||
FILE *fprefab = fopen(path, "rb");
|
||||
if (fprefab == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct mGameObject *new = MakeGameobject();
|
||||
struct vec *hold = new->components;
|
||||
fread(new, sizeof(*new), 1, fprefab);
|
||||
new->components = hold;
|
||||
|
||||
new->editor.id = gameobjects->len - 1;
|
||||
new->body = cpSpaceAddBody(space, cpBodyNew(new->mass, 1.f));
|
||||
|
||||
gameobject_init(new, fprefab);
|
||||
|
||||
|
@ -135,10 +122,8 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab)
|
|||
vec_load(go->components, fprefab);
|
||||
|
||||
for (int i = 0; i < go->components->len; i++) {
|
||||
struct component *newc =
|
||||
vec_set(go->components, i,
|
||||
&components[((struct component *)
|
||||
vec_get(go->components, i))->id]);
|
||||
struct component *newc = vec_set(go->components, i, &components[((struct component *) vec_get(go->components, i))->id]);
|
||||
|
||||
newc->go = go;
|
||||
newc->data = malloc(newc->datasize);
|
||||
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)
|
||||
{
|
||||
/*
|
||||
|
|
|
@ -46,8 +46,7 @@ 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 mShader *s,
|
||||
float scale);
|
||||
void setup_model_transform(struct mTransform *t, struct mShader *s, float scale);
|
||||
void toggleprefab(struct mGameObject *go);
|
||||
struct mGameObject *get_gameobject_from_id(int id);
|
||||
void gameobject_save(struct mGameObject *go, FILE * file);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
void save_level(char name[MAXNAME])
|
||||
{
|
||||
FILE *lfile = res_open(name, "w+");
|
||||
FILE *lfile = res_open(name, "wb+");
|
||||
|
||||
if (!lfile) return;
|
||||
|
||||
|
@ -24,7 +24,7 @@ void save_level(char name[MAXNAME])
|
|||
|
||||
void load_level(char name[MAXNAME])
|
||||
{
|
||||
FILE *lfile = fopen(name, "r");
|
||||
FILE *lfile = fopen(name, "rb");
|
||||
|
||||
if (!lfile) return;
|
||||
|
||||
|
@ -38,6 +38,7 @@ void load_level(char name[MAXNAME])
|
|||
for (int i = 0; i < objs; i++) {
|
||||
struct mGameObject *go = vec_add(gameobjects, NULL);
|
||||
fread(go, sizeof(struct mGameObject), 1, lfile);
|
||||
go->components = vec_make(1,1);
|
||||
gameobject_init(go, lfile);
|
||||
}
|
||||
|
||||
|
|
|
@ -56,8 +56,7 @@ char *get_filename_from_path(char *path, int extension)
|
|||
printf("Making without extension ...\n");
|
||||
}
|
||||
|
||||
char *filename =
|
||||
(char *) malloc(sizeof(char) * (end - dirpos - offset + 1));
|
||||
char *filename = malloc(sizeof(char) * (end - dirpos - offset + 1));
|
||||
strncpy(filename, dirpos, end - dirpos - offset);
|
||||
return filename;
|
||||
}
|
||||
|
@ -78,8 +77,7 @@ FILE *res_open(char *path, const char *tag)
|
|||
return f;
|
||||
}
|
||||
|
||||
static int ext_check(const char *path, const struct stat *sb, int typeflag,
|
||||
struct FTW *ftwbuf)
|
||||
static int ext_check(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
|
||||
{
|
||||
if (typeflag == FTW_F) {
|
||||
const char *ext = strrchr(path, '.');
|
||||
|
|
Loading…
Reference in a new issue