Fixed crash on launch bug by changing init/de-init of entities

dev
Shariq Shah 5 years ago
parent 61afe3d0d7
commit 9f45cd7190
  1. 4
      src/common/version.h
  2. 3
      src/game/entity.c
  3. 8
      src/game/game.c
  4. 59
      src/game/scene.c
  5. 9
      src/game/transform.c

@ -4,7 +4,7 @@
/* Auto generated version file. DO NOT MODIFY */ /* Auto generated version file. DO NOT MODIFY */
#define SYMMETRY_VERSION_MAJOR 0 #define SYMMETRY_VERSION_MAJOR 0
#define SYMMETRY_VERSION_MINOR 2 #define SYMMETRY_VERSION_MINOR 2
#define SYMMETRY_VERSION_REVISION 364 #define SYMMETRY_VERSION_REVISION 365
#define SYMMETRY_VERSION_BRANCH "dev" #define SYMMETRY_VERSION_BRANCH "HEAD"
#endif #endif

@ -42,7 +42,7 @@ void entity_init(struct Entity* entity, const char* name, struct Entity* parent)
entity->name[MAX_ENTITY_NAME_LEN - 1] = '\0'; entity->name[MAX_ENTITY_NAME_LEN - 1] = '\0';
entity->type = ET_DEFAULT; entity->type = ET_DEFAULT;
entity->archetype_index = -1; entity->archetype_index = -1;
entity->flags = EF_ACTIVE; entity->flags = EF_NONE;
entity_bounding_box_reset(entity, false); entity_bounding_box_reset(entity, false);
entity->derived_bounding_box.min = (vec3){ -0.5f, -0.5f, -0.5f }; entity->derived_bounding_box.min = (vec3){ -0.5f, -0.5f, -0.5f };
entity->derived_bounding_box.max = (vec3){ 0.5f, 0.5f, 0.5f }; entity->derived_bounding_box.max = (vec3){ 0.5f, 0.5f, 0.5f };
@ -59,6 +59,7 @@ void entity_reset(struct Entity* entity, int id)
entity_bounding_box_reset(entity, false); entity_bounding_box_reset(entity, false);
entity->derived_bounding_box.min = (vec3){ -0.5f, -0.5f, -0.5f }; entity->derived_bounding_box.min = (vec3){ -0.5f, -0.5f, -0.5f };
entity->derived_bounding_box.max = (vec3){ 0.5f, 0.5f, 0.5f }; entity->derived_bounding_box.max = (vec3){ 0.5f, 0.5f, 0.5f };
transform_destroy(entity);
memset(entity->name, '\0', MAX_ENTITY_NAME_LEN); memset(entity->name, '\0', MAX_ENTITY_NAME_LEN);
} }

@ -123,15 +123,15 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
scene_init(game_state->scene); scene_init(game_state->scene);
editor_init(game_state->editor); editor_init(game_state->editor);
}
/* Debug scene setup */
//game_scene_setup();
event_manager_subscribe(game_state->event_manager, EVT_SCENE_LOADED, &game_on_scene_loaded); event_manager_subscribe(game_state->event_manager, EVT_SCENE_LOADED, &game_on_scene_loaded);
event_manager_subscribe(game_state->event_manager, EVT_SCENE_CLEARED, &game_on_scene_cleared); event_manager_subscribe(game_state->event_manager, EVT_SCENE_CLEARED, &game_on_scene_cleared);
event_manager_subscribe(game_state->event_manager, EVT_PLAYER_DIED, &game_on_player_death); event_manager_subscribe(game_state->event_manager, EVT_PLAYER_DIED, &game_on_player_death);
game_state->is_initialized = scene_load(game_state->scene, "scene_1", DIRT_INSTALL) ? true : false; game_state->is_initialized = scene_load(game_state->scene, "scene_1", DIRT_INSTALL) ? true : false;
/* Debug scene setup */
//game_scene_setup();
}
return game_state->is_initialized; return game_state->is_initialized;
} }

@ -46,22 +46,33 @@ void scene_init(struct Scene* scene)
scene->root_entity.id = 0; scene->root_entity.id = 0;
scene->root_entity.type = ET_ROOT; scene->root_entity.type = ET_ROOT;
for(int i = 0; i < MAX_SCENE_ENTITIES; i++) entity_reset(&scene->entities[i], i); for(int i = 0; i < MAX_SCENE_ENTITIES; i++)
{
entity_init(&scene->entities[i], NULL, NULL);
scene->entities[i].id = i;
}
for(int i = 0; i < MAX_SCENE_LIGHTS; i++) for(int i = 0; i < MAX_SCENE_LIGHTS; i++)
{ {
entity_reset(&scene->lights[i], i); entity_init(&scene->lights[i], NULL, NULL);
scene->lights[i].type = ET_LIGHT; scene->lights[i].base.id = i;
} }
for(int i = 0; i < MAX_SCENE_STATIC_MESHES; i++) for(int i = 0; i < MAX_SCENE_STATIC_MESHES; i++)
{ {
entity_reset(&scene->static_meshes[i], i); entity_init(&scene->static_meshes[i], NULL, NULL);
scene->static_meshes[i].base.id = i;
struct Static_Mesh* mesh = &scene->static_meshes[i]; struct Static_Mesh* mesh = &scene->static_meshes[i];
mesh->model.geometry_index = -1; mesh->model.geometry_index = -1;
mesh->model.material = NULL; mesh->model.material = NULL;
} }
for(int i = 0; i < MAX_SCENE_SOUND_SOURCES; i++) entity_reset(&scene->sound_sources[i], i); for(int i = 0; i < MAX_SCENE_SOUND_SOURCES; i++)
{
entity_init(&scene->sound_sources[i], NULL, NULL);
scene->sound_sources[i].base.id = i;
}
int width = 1280, height = 720; int width = 1280, height = 720;
window_get_drawable_size(game_state->window, &width, &height); window_get_drawable_size(game_state->window, &width, &height);
@ -78,26 +89,26 @@ void scene_init(struct Scene* scene)
for(int i = 0; i < MAX_SCENE_ENEMIES; i++) for(int i = 0; i < MAX_SCENE_ENEMIES; i++)
{ {
entity_reset(&scene->enemies[i], i); entity_init(&scene->enemies[i], NULL, NULL);
scene->enemies[i].base.type = ET_ENEMY; scene->enemies[i].base.id = i;
} }
for(int i = 0; i < MAX_SCENE_TRIGGERS; i++) for(int i = 0; i < MAX_SCENE_TRIGGERS; i++)
{ {
entity_reset(&scene->triggers[i], i); entity_init(&scene->triggers[i], NULL, NULL);
scene->triggers[i].base.type = ET_TRIGGER; scene->triggers[i].base.id = i;
} }
for(int i = 0; i < MAX_SCENE_DOORS; i++) for(int i = 0; i < MAX_SCENE_DOORS; i++)
{ {
entity_reset(&scene->doors[i], i); entity_init(&scene->doors[i], NULL, NULL);
scene->doors[i].base.type = ET_DOOR; scene->doors[i].base.id = i;
} }
for(int i = 0; i < MAX_SCENE_PICKUPS; i++) for(int i = 0; i < MAX_SCENE_PICKUPS; i++)
{ {
entity_reset(&scene->pickups[i], i); entity_init(&scene->pickups[i], NULL, NULL);
scene->pickups[i].base.type = ET_PICKUP; scene->pickups[i].base.id = i;
} }
player_init(&scene->player, scene); player_init(&scene->player, scene);
@ -506,7 +517,7 @@ void scene_write_entity_entry(struct Scene* scene, struct Entity* entity, struct
void scene_destroy(struct Scene* scene) void scene_destroy(struct Scene* scene)
{ {
assert(scene); assert(scene);
scene->cleanup(scene); if(scene->cleanup) scene->cleanup(scene);
for(int i = 0; i < MAX_SCENE_ENTITIES; i++) scene_entity_base_remove(scene, &scene->entities[i]); for(int i = 0; i < MAX_SCENE_ENTITIES; i++) scene_entity_base_remove(scene, &scene->entities[i]);
for(int i = 0; i < MAX_SCENE_CAMERAS; i++) scene_camera_remove(scene, &scene->cameras[i]); for(int i = 0; i < MAX_SCENE_CAMERAS; i++) scene_camera_remove(scene, &scene->cameras[i]);
@ -728,7 +739,9 @@ struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct
{ {
if(!parent) if(!parent)
parent = &scene->root_entity; parent = &scene->root_entity;
entity_reset(new_entity, new_entity->id);
entity_init(new_entity, name, parent); entity_init(new_entity, name, parent);
new_entity->flags |= EF_ACTIVE;
} }
else else
{ {
@ -754,8 +767,10 @@ struct Light* scene_light_create(struct Scene* scene, const char* name, struct E
if(new_light) if(new_light)
{ {
entity_reset(new_light, new_light->base.id);
entity_init(&new_light->base, name, parent ? parent : &scene->root_entity); entity_init(&new_light->base, name, parent ? parent : &scene->root_entity);
new_light->base.type = ET_LIGHT; new_light->base.type = ET_LIGHT;
new_light->base.flags |= EF_ACTIVE;
light_init(new_light, light_type); light_init(new_light, light_type);
} }
else else
@ -782,8 +797,10 @@ struct Camera* scene_camera_create(struct Scene* scene, const char* name, struct
if(new_camera) if(new_camera)
{ {
entity_reset(new_camera, new_camera->base.id);
entity_init(&new_camera->base, name, parent ? parent : &scene->root_entity); entity_init(&new_camera->base, name, parent ? parent : &scene->root_entity);
new_camera->base.type = ET_CAMERA; new_camera->base.type = ET_CAMERA;
new_camera->base.flags |= EF_ACTIVE;
camera_init(new_camera, width, height); camera_init(new_camera, width, height);
} }
else else
@ -810,8 +827,10 @@ struct Static_Mesh* scene_static_mesh_create(struct Scene* scene, const char* na
if(new_static_mesh) if(new_static_mesh)
{ {
entity_reset(new_static_mesh, new_static_mesh->base.id);
entity_init(&new_static_mesh->base, name, parent ? parent : &scene->root_entity); entity_init(&new_static_mesh->base, name, parent ? parent : &scene->root_entity);
new_static_mesh->base.type = ET_STATIC_MESH; new_static_mesh->base.type = ET_STATIC_MESH;
new_static_mesh->base.flags |= EF_ACTIVE;
model_init(&new_static_mesh->model, new_static_mesh, geometry_name, material_type); model_init(&new_static_mesh->model, new_static_mesh, geometry_name, material_type);
vec3_assign(&new_static_mesh->base.bounding_box.min, &geom_get(new_static_mesh->model.geometry_index)->bounding_box.min); vec3_assign(&new_static_mesh->base.bounding_box.min, &geom_get(new_static_mesh->model.geometry_index)->bounding_box.min);
vec3_assign(&new_static_mesh->base.bounding_box.max, &geom_get(new_static_mesh->model.geometry_index)->bounding_box.max); vec3_assign(&new_static_mesh->base.bounding_box.max, &geom_get(new_static_mesh->model.geometry_index)->bounding_box.max);
@ -819,7 +838,7 @@ struct Static_Mesh* scene_static_mesh_create(struct Scene* scene, const char* na
} }
else else
{ {
log_error("scene:model_create", "Max model limit reached!"); log_error("scene:static_mesh_create", "Max static mesh limit reached!");
} }
return new_static_mesh; return new_static_mesh;
@ -842,7 +861,9 @@ struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char*
if(new_sound_source) if(new_sound_source)
{ {
entity_reset(new_sound_source, new_sound_source->base.id);
entity_init(&new_sound_source->base, name, parent ? parent : &scene->root_entity); entity_init(&new_sound_source->base, name, parent ? parent : &scene->root_entity);
new_sound_source->base.flags |= EF_ACTIVE;
new_sound_source->base.type = ET_SOUND_SOURCE; new_sound_source->base.type = ET_SOUND_SOURCE;
struct Entity* entity = &new_sound_source->base; struct Entity* entity = &new_sound_source->base;
@ -902,7 +923,9 @@ struct Enemy* scene_enemy_create(struct Scene* scene, const char* name, struct E
if(new_enemy) if(new_enemy)
{ {
entity_reset(new_enemy, new_enemy->base.id);
entity_init(&new_enemy->base, name, parent ? parent : &scene->root_entity); entity_init(&new_enemy->base, name, parent ? parent : &scene->root_entity);
new_enemy->base.flags |= EF_ACTIVE;
enemy_init(new_enemy, type); enemy_init(new_enemy, type);
} }
else else
@ -929,7 +952,9 @@ struct Door* scene_door_create(struct Scene* scene, const char* name, struct Ent
if(new_door) if(new_door)
{ {
entity_reset(new_door, new_door->base.id);
entity_init(&new_door->base, name, parent ? parent : &scene->root_entity); entity_init(&new_door->base, name, parent ? parent : &scene->root_entity);
new_door->base.flags |= EF_ACTIVE;
door_init(new_door, mask); door_init(new_door, mask);
} }
else else
@ -956,7 +981,9 @@ struct Pickup* scene_pickup_create(struct Scene* scene, const char* name, struct
if(new_pickup) if(new_pickup)
{ {
entity_reset(new_pickup, new_pickup->base.id);
entity_init(&new_pickup->base, name, parent ? parent : &scene->root_entity); entity_init(&new_pickup->base, name, parent ? parent : &scene->root_entity);
new_pickup->base.flags |= EF_ACTIVE;
pickup_init(new_pickup, type); pickup_init(new_pickup, type);
} }
else else
@ -983,7 +1010,9 @@ struct Trigger* scene_trigger_create(struct Scene* scene, const char* name, stru
if(new_trigger) if(new_trigger)
{ {
entity_reset(new_trigger, new_trigger->base.id);
entity_init(&new_trigger->base, name, parent ? parent : &scene->root_entity); entity_init(&new_trigger->base, name, parent ? parent : &scene->root_entity);
new_trigger->base.flags |= EF_ACTIVE;
trigger_init(new_trigger, type, mask); trigger_init(new_trigger, type, mask);
} }
else else

@ -246,6 +246,8 @@ void transform_destroy(struct Entity* entity)
{ {
struct Transform* transform = &entity->transform; struct Transform* transform = &entity->transform;
// Remove children // Remove children
if(transform->children)
{
int children = array_len(transform->children); int children = array_len(transform->children);
if(children > 0) if(children > 0)
{ {
@ -253,20 +255,23 @@ void transform_destroy(struct Entity* entity)
{ {
struct Entity* child = transform->children[i]; struct Entity* child = transform->children[i];
child->flags |= EF_MARKED_FOR_DELETION; child->flags |= EF_MARKED_FOR_DELETION;
child->transform.parent = NULL;
}
} }
array_free(transform->children);
} }
// Remove this entity from parent's children // Remove this entity from parent's children
if(entity->transform.parent) if(transform->parent)
transform_child_remove(entity->transform.parent, entity); transform_child_remove(entity->transform.parent, entity);
/* Remove transform */ /* Remove transform */
array_free(transform->children);
vec3_fill(&transform->position, 0.f, 0.f, 0.f); vec3_fill(&transform->position, 0.f, 0.f, 0.f);
vec3_fill(&transform->scale, 1.f, 1.f, 1.f); vec3_fill(&transform->scale, 1.f, 1.f, 1.f);
quat_identity(&transform->rotation); quat_identity(&transform->rotation);
mat4_identity(&transform->trans_mat); mat4_identity(&transform->trans_mat);
transform->parent = NULL; transform->parent = NULL;
transform->children = NULL;
transform->is_modified = false; transform->is_modified = false;
} }

Loading…
Cancel
Save