~vladh/peony

1791a546a0c34aaea2f93b005c3c7a0503b7fa22 — Vlad-Stefan Harbuz 1 year, 7 months ago ba1aa49
refactor entity state
M src/core.cpp => src/core.cpp +1 -0
@@ 135,6 135,7 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)
    lights::init(&state->lights_state, asset_memory_pool);
    anim::init(&state->anim_state, asset_memory_pool);
    physics::init(&state->physics_state, asset_memory_pool);
    entities::init(&state->entities_state, asset_memory_pool);
    behavior::init(
        &state->behavior_state,
        asset_memory_pool,

M src/debug_ui.cpp => src/debug_ui.cpp +4 -8
@@ 45,7 45,7 @@ void debug_ui::render_debug_ui() {
        snprintf(debug_text, dt_size, "%u", mats::get_n_materials());
        gui::draw_named_value(container, "materials.length", debug_text);

        snprintf(debug_text, dt_size, "%u", engine_state->entity_set.entities.length);
        snprintf(debug_text, dt_size, "%u", entities::get_n_entities());
        gui::draw_named_value(container, "entities.length", debug_text);

        snprintf(debug_text, dt_size, "%u", engine_state->model_loaders.length);


@@ 128,8 128,6 @@ void debug_ui::render_debug_ui() {
void
debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u8 depth)
{
    engine::State *engine_state = engine::debug_get_engine_state();

    entities::Handle handle = entity->handle;
    spatial::Component *spatial_component = spatial::get_component(handle);



@@ 201,7 199,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
                    continue;
                }
                entities::Handle child_handle = child_spatial_component->entity_handle;
                entities::Entity *child_entity = engine_state->entity_set.entities[child_handle];
                entities::Entity *child_entity = entities::get_entity(child_handle);

                if (text[strlen(text) - 1] != '\n') {
                    strcat(text, "\n");


@@ 228,17 226,15 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
void
debug_ui::get_scene_text_representation(char *text)
{
    engine::State *engine_state = engine::debug_get_engine_state();

    text[0] = '\0';

    constexpr u32 const MAX_N_SHOWN_ENTITIES = 35;
    u32 idx_entity = 0;
    each (entity, engine_state->entity_set.entities) {
    each (entity, *entities::get_entities()) {
        if (idx_entity > MAX_N_SHOWN_ENTITIES) {
            sprintf(text + strlen(text),
                "...and %d more\n",
                engine_state->entity_set.entities.length - idx_entity);
                entities::get_n_entities() - idx_entity);
            break;;
        }
        get_entity_text_representation(text, entity, 0);

M src/engine.cpp => src/engine.cpp +4 -64
@@ 42,35 42,6 @@ engine::push_model_loader()
}


void
engine::mark_first_non_internal_handle()
{
    engine::state->entity_set.first_non_internal_handle =
        engine::state->entity_set.next_handle;
}


entities::Set *
engine::get_entity_set()
{
    return &engine::state->entity_set;
}


Array<entities::Entity> *
engine::get_entities()
{
    return &engine::state->entity_set.entities;
}


entities::Entity *
engine::get_entity(entities::Handle entity_handle)
{
    return engine::state->entity_set.entities[entity_handle];
}


f64
engine::get_t()
{


@@ 140,10 111,6 @@ void engine::init(engine::State *engine_state, memory::Pool *asset_memory_pool) 
        .loaders = Array<models::EntityLoader>(
            asset_memory_pool, MAX_N_ENTITIES, "entity_loaders", true, 1)
    };
    engine::state->entity_set = {
        .entities = Array<entities::Entity>(
            asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1)
    };
}




@@ 155,36 122,6 @@ engine::destroy_model_loaders()


void
engine::destroy_non_internal_entities()
{
    for (
        u32 idx = engine::state->entity_set.first_non_internal_handle;
        idx < engine::state->entity_set.entities.length;
        idx++
    ) {
        drawable::destroy_component(drawable::get_component(idx));
    }

    engine::state->entity_set.next_handle =
        engine::state->entity_set.first_non_internal_handle;
    engine::state->entity_set.entities.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->entity_loader_set.loaders.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    lights::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    spatial::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    drawable::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    behavior::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    anim::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
}


void
engine::destroy_scene()
{
    // If the current scene has not finished loading, we can neither


@@ 198,7 135,10 @@ engine::destroy_scene()
    // end up overflowing.
    destroy_model_loaders();
    mats::destroy_non_internal_materials();
    destroy_non_internal_entities();

    entities::destroy_non_internal_entities();
    engine::state->entity_loader_set.loaders.delete_elements_after_index(
        entities::get_first_non_internal_handle());
}



M src/engine.hpp => src/engine.hpp +0 -6
@@ 57,16 57,11 @@ public:
        bool was_world_ever_loaded;
        Array<models::ModelLoader> model_loaders;
        models::EntityLoaderSet entity_loader_set;
        entities::Set entity_set;
    };

    static engine::State * debug_get_engine_state();
    static models::EntityLoader * get_entity_loader(entities::Handle entity_handle);
    static models::ModelLoader * push_model_loader();
    static void mark_first_non_internal_handle();
    static entities::Set * get_entity_set();
    static Array<entities::Entity> * get_entities();
    static entities::Entity * get_entity(entities::Handle entity_handle);
    static f64 get_t();
    static f64 get_dt();
    static void run_main_loop(GLFWwindow *window);


@@ 75,7 70,6 @@ public:
private:
    static engine::State *state;
    static void destroy_model_loaders();
    static void destroy_non_internal_entities();
    static void destroy_scene();
    static bool load_scene(const char *scene_name);
    static void handle_console_command();

M src/entities.cpp => src/entities.cpp +80 -6
@@ 4,24 4,98 @@
#include "engine.hpp"


entities::State *entities::state = nullptr;


entities::Handle
entities::make_handle()
{
    entities::Set *entity_set = engine::get_entity_set();
    if (entity_set->next_handle == NO_ENTITY_HANDLE) {
        entity_set->next_handle++;
    if (entities::state->next_handle == NO_ENTITY_HANDLE) {
        entities::state->next_handle++;
    }
    return entity_set->next_handle++;
    return entities::state->next_handle++;
}


entities::Entity *
entities::add_entity_to_set(char const *debug_name)
{
    entities::Set *entity_set = engine::get_entity_set();
    Handle new_handle = make_handle();
    Entity *new_entity = entity_set->entities[new_handle];
    Entity *new_entity = get_entity(new_handle);
    new_entity->handle = new_handle;
    strcpy(new_entity->debug_name, debug_name);
    return new_entity;
}


void
entities::mark_first_non_internal_handle()
{
    entities::state->first_non_internal_handle = entities::state->next_handle;
}


entities::Handle
entities::get_first_non_internal_handle()
{
    return entities::state->first_non_internal_handle;
}


void
entities::destroy_non_internal_entities()
{
    for (
        u32 idx = entities::state->first_non_internal_handle;
        idx < entities::state->entities.length;
        idx++
    ) {
        drawable::destroy_component(drawable::get_component(idx));
    }

    entities::state->next_handle = entities::state->first_non_internal_handle;

    entities::state->entities.delete_elements_after_index(
        entities::state->first_non_internal_handle);

    lights::get_components()->delete_elements_after_index(
        entities::state->first_non_internal_handle);
    spatial::get_components()->delete_elements_after_index(
        entities::state->first_non_internal_handle);
    drawable::get_components()->delete_elements_after_index(
        entities::state->first_non_internal_handle);
    behavior::get_components()->delete_elements_after_index(
        entities::state->first_non_internal_handle);
    anim::get_components()->delete_elements_after_index(
        entities::state->first_non_internal_handle);
}


u32
entities::get_n_entities()
{
    return entities::state->entities.length;
}


Array<entities::Entity> *
entities::get_entities()
{
    return &entities::state->entities;
}


entities::Entity *
entities::get_entity(entities::Handle entity_handle)
{
    return entities::state->entities[entity_handle];
}


void
entities::init(entities::State *entities_state, memory::Pool *asset_memory_pool)
{
    entities::state = entities_state;
    entities::state->entities = Array<entities::Entity>(
        asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1);
}

M src/entities.hpp => src/entities.hpp +11 -1
@@ 16,7 16,7 @@ public:
        char debug_name[MAX_DEBUG_NAME_LENGTH];
    };

    struct Set {
    struct State {
        Array<Entity> entities;
        // The handle of the next entity which has not yet been created.
        // NOTE: 0 is an invalid handle.


@@ 33,4 33,14 @@ public:

    static Handle make_handle();
    static Entity * add_entity_to_set(char const *debug_name);
    static void mark_first_non_internal_handle();
    static entities::Handle get_first_non_internal_handle();
    static void destroy_non_internal_entities();
    static u32 get_n_entities();
    static Array<entities::Entity> * get_entities();
    static entities::Entity * get_entity(entities::Handle entity_handle);
    static void init(entities::State *entities_state, memory::Pool *asset_memory_pool);

private:
    static entities::State *state;
};

M src/internals.cpp => src/internals.cpp +1 -1
@@ 274,7 274,7 @@ internals::create_internal_entities()
    // We've created all internal entities, so we will mark the next position
    // in the entities::Set, to know that that position is where the non-internal
    // entities start.
    engine::mark_first_non_internal_handle();
    entities::mark_first_non_internal_handle();

    memory::destroy_memory_pool(&temp_memory_pool);
}

M src/state.hpp => src/state.hpp +1 -0
@@ 27,6 27,7 @@ struct State {
    lights::State lights_state;
    anim::State anim_state;
    physics::State physics_state;
    entities::State entities_state;
    behavior::State behavior_state;
    engine::State engine_state;
    renderer::State renderer_state;