~vladh/peony

5c3b6febce58c92af5e638678f29358a0ef464a9 — Vlad-Stefan Harbuz 1 year, 7 months ago 285943b
refactor spatial
M src/anim.cpp => src/anim.cpp +3 -6
@@ 37,10 37,8 @@ anim::get_bone_matrix(u32 idx, u32 idx_bone, u32 idx_anim_key)


void
anim::update_animation_components(
    anim::ComponentSet *animation_component_set,
    spatial::ComponentSet *spatial_component_set
) {
anim::update_animation_components(anim::ComponentSet *animation_component_set)
{
    each (animation_component, animation_component_set->components) {
        if (!is_animation_component_valid(animation_component)) {
            continue;


@@ 154,8 152,7 @@ anim::find_animation_component(spatial::Component *spatial_component)
    }

    if (spatial_component->parent_entity_handle != entities::NO_ENTITY_HANDLE) {
        spatial::Component *parent =
            engine::get_spatial_component(spatial_component->parent_entity_handle);
        spatial::Component *parent = spatial::get_component(spatial_component->parent_entity_handle);
        return find_animation_component(parent);
    }


M src/anim.hpp => src/anim.hpp +1 -4
@@ 56,10 56,7 @@ public:
        u32 idx_bone,
        u32 idx_anim_key
    );
    static void update_animation_components(
        ComponentSet *animation_component_set,
        spatial::ComponentSet *spatial_component_set
    );
    static void update_animation_components(ComponentSet *animation_component_set);
    static void make_bone_matrices_for_animation_bone(
        Component *animation_component,
        aiNodeAnim *ai_channel,

M src/behavior_functions.cpp => src/behavior_functions.cpp +2 -4
@@ 8,8 8,7 @@
void
behavior_functions::test(entities::Handle entity_handle)
{
    spatial::Component *spatial_component =
        engine::get_spatial_component(entity_handle);
    spatial::Component *spatial_component = spatial::get_component(entity_handle);
    if (!spatial_component) {
        logs::error("Could not get spatial::Component for behavior::Component");
        return;


@@ 24,8 23,7 @@ behavior_functions::test(entities::Handle entity_handle)
void
behavior_functions::char_movement_test(entities::Handle entity_handle)
{
    spatial::Component *spatial_component =
        engine::get_spatial_component(entity_handle);
    spatial::Component *spatial_component = spatial::get_component(entity_handle);
    if (!spatial_component) {
        logs::error("Could not get spatial::Component for behavior::Component");
        return;

M src/core.cpp => src/core.cpp +1 -0
@@ 130,6 130,7 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)

    state->asset_memory_pool = asset_memory_pool;

    spatial::init(&state->spatial_state, asset_memory_pool);
    engine::init(&state->engine_state, asset_memory_pool);
    mats::init(&state->materials_state, asset_memory_pool);
    input::init(&state->input_state, state->window);

M src/debug_ui.cpp => src/debug_ui.cpp +2 -2
@@ 131,7 131,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
    engine::State *engine_state = engine::debug_get_engine_state();

    entities::Handle handle = entity->handle;
    spatial::Component *spatial_component = engine_state->spatial_component_set.components[handle];
    spatial::Component *spatial_component = spatial::get_component(handle);

    // Children will be drawn under their parents.
    if (


@@ 191,7 191,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
    if (spatial::is_spatial_component_valid(spatial_component)) {
        // NOTE: This is super slow lol.
        u32 n_children_found = 0;
        each (child_spatial_component, engine_state->spatial_component_set.components) {
        each (child_spatial_component, *spatial::get_components()) {
            if (
                child_spatial_component->parent_entity_handle ==
                spatial_component->entity_handle

M src/engine.cpp => src/engine.cpp +10 -40
@@ 92,13 92,6 @@ engine::get_light_components()
}


Array<spatial::Component> *
engine::get_spatial_components()
{
    return &engine::state->spatial_component_set.components;
}


Array<behavior::Component> *
engine::get_behavior_components()
{


@@ 141,13 134,6 @@ engine::get_light_component(entities::Handle entity_handle)
}


spatial::Component *
engine::get_spatial_component(entities::Handle entity_handle)
{
    return engine::state->spatial_component_set.components[entity_handle];
}


behavior::Component *
engine::get_behavior_component(entities::Handle entity_handle)
{


@@ 233,47 219,34 @@ engine::run_main_loop(GLFWwindow *window)
void engine::init(engine::State *engine_state, memory::Pool *asset_memory_pool) {
    engine::state = engine_state;
    engine::state->model_loaders = Array<models::ModelLoader>(
        asset_memory_pool, MAX_N_MODELS, "model_loaders"
    );
        asset_memory_pool, MAX_N_MODELS, "model_loaders");
    engine::state->entity_loader_set = {
        .loaders = Array<models::EntityLoader>(
            asset_memory_pool, MAX_N_ENTITIES, "entity_loaders", true, 1
        )
            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
        )
            asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1)
    };
    engine::state->drawable_component_set = {
        .components = Array<drawable::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "drawable_components", true, 1
        )
            asset_memory_pool, MAX_N_ENTITIES, "drawable_components", true, 1)
    };
    engine::state->light_component_set = {
        .components = Array<lights::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "light_components", true, 1
        )
    };
    engine::state->spatial_component_set = {
        .components = Array<spatial::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "spatial_components", true, 1
        )
            asset_memory_pool, MAX_N_ENTITIES, "light_components", true, 1)
    };
    engine::state->behavior_component_set = {
        .components = Array<behavior::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "behavior_components", true, 1
        )
            asset_memory_pool, MAX_N_ENTITIES, "behavior_components", true, 1)
    };
    engine::state->animation_component_set = {
        .components = Array<anim::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "animation_components", true, 1
        )
            asset_memory_pool, MAX_N_ENTITIES, "animation_components", true, 1)
    };
    engine::state->physics_component_set = {
        .components = Array<physics::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "physics_components", true, 1
        )
            asset_memory_pool, MAX_N_ENTITIES, "physics_components", true, 1)
    };
}



@@ 305,7 278,7 @@ engine::destroy_non_internal_entities()
        engine::state->entity_set.first_non_internal_handle);
    engine::state->light_component_set.components.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->spatial_component_set.components.delete_elements_after_index(
    spatial::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->drawable_component_set.components.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);


@@ 656,14 629,11 @@ engine::update()

    lights::update_light_components(
        &engine::state->light_component_set,
        &engine::state->spatial_component_set,
        cameras::get_main()->position);

    behavior::update_behavior_components(&engine::state->behavior_component_set);

    anim::update_animation_components(
        &engine::state->animation_component_set,
        &engine::state->spatial_component_set);
    anim::update_animation_components(&engine::state->animation_component_set);

    physics::update_components();
}

M src/engine.hpp => src/engine.hpp +0 -1
@@ 61,7 61,6 @@ public:
        entities::Set entity_set;
        drawable::ComponentSet drawable_component_set;
        lights::ComponentSet light_component_set;
        spatial::ComponentSet spatial_component_set;
        behavior::ComponentSet behavior_component_set;
        anim::ComponentSet animation_component_set;
        physics::ComponentSet physics_component_set;

M src/lights.cpp => src/lights.cpp +1 -2
@@ 70,7 70,6 @@ lights::is_light_component_valid(lights::Component *light_component)
void
lights::update_light_components(
    lights::ComponentSet *light_component_set,
    spatial::ComponentSet *spatial_component_set,
    v3 camera_position
) {
    each (light_component, light_component_set->components) {


@@ 79,7 78,7 @@ lights::update_light_components(
        }

        spatial::Component *spatial_component =
            spatial_component_set->components[light_component->entity_handle];
            spatial::get_component(light_component->entity_handle);

        if (!(
            is_light_component_valid(light_component) &&

M src/lights.hpp => src/lights.hpp +0 -1
@@ 39,7 39,6 @@ public:
    static bool is_light_component_valid(Component *light_component);
    static void update_light_components(
        ComponentSet *light_component_set,
        spatial::ComponentSet *spatial_component_set,
        v3 camera_position
    );
    static void init(lights::State *state);

M src/models.cpp => src/models.cpp +2 -2
@@ 82,7 82,7 @@ models::prepare_entity_loader_and_check_if_done(
            return false;
        }

        spatial::Component *spatial_component = engine::get_spatial_component(entity_loader->entity_handle);
        spatial::Component *spatial_component = spatial::get_component(entity_loader->entity_handle);
        *spatial_component = entity_loader->spatial_component;
        spatial_component->entity_handle = entity_loader->entity_handle;



@@ 118,7 118,7 @@ models::prepare_entity_loader_and_check_if_done(
                entities::Entity *child_entity = entities::add_entity_to_set(entity_loader->name);

                if (spatial::is_spatial_component_valid(&entity_loader->spatial_component)) {
                    spatial::Component *child_spatial_component = engine::get_spatial_component(child_entity->handle);
                    spatial::Component *child_spatial_component = spatial::get_component(child_entity->handle);
                    assert(child_spatial_component);
                    *child_spatial_component = {
                        .entity_handle = child_entity->handle,

M src/physics.cpp => src/physics.cpp +2 -4
@@ 45,8 45,7 @@ physics::find_collision(
        if (!is_component_valid(candidate_physics)) {
            continue;
        }
        spatial::Component *candidate_spatial =
            engine::get_spatial_component(candidate_physics->entity_handle);
        spatial::Component *candidate_spatial = spatial::get_component(candidate_physics->entity_handle);
        if (!candidate_spatial) {
            logs::error("Could not get spatial::Component for candidate");
            return physics::CollisionManifold {};


@@ 76,8 75,7 @@ physics::update_components()
            continue;
        }

        spatial::Component *spatial_component =
            engine::get_spatial_component(physics_component->entity_handle);
        spatial::Component *spatial_component = spatial::get_component(physics_component->entity_handle);

        if (!spatial::is_spatial_component_valid(spatial_component)) {
            logs::warning("Tried to update physics component but it had no spatial component.");

M src/renderer.cpp => src/renderer.cpp +4 -5
@@ 294,7 294,7 @@ renderer::render(GLFWwindow *window)
                }

                spatial::Component *spatial_component =
                    engine::get_spatial_component(light_component->entity_handle);
                    spatial::get_component(light_component->entity_handle);

                if (!(
                        lights::is_light_component_valid(light_component) &&


@@ 350,7 350,7 @@ renderer::render(GLFWwindow *window)
                }

                spatial::Component *spatial_component =
                    engine::get_spatial_component(light_component->entity_handle);
                    spatial::get_component(light_component->entity_handle);

                if (!(
                        lights::is_light_component_valid(light_component) &&


@@ 1177,7 1177,7 @@ renderer::copy_scene_data_to_ubo(
        }

        spatial::Component *spatial_component =
            engine::get_spatial_component(light_component->entity_handle);
            spatial::get_component(light_component->entity_handle);

        if (!(
            lights::is_light_component_valid(light_component) &&


@@ 1313,8 1313,7 @@ renderer::draw_all(
            material = mats::get_material_by_name("unknown");
        }

        spatial::Component *spatial_component =
            engine::get_spatial_component(drawable_component->entity_handle);
        spatial::Component *spatial_component = spatial::get_component(drawable_component->entity_handle);

        m4 model_matrix = m4(1.0f);
        m3 model_normal_matrix = m3(1.0f);

M src/spatial.cpp => src/spatial.cpp +30 -1
@@ 5,6 5,9 @@
#include "engine.hpp"


spatial::State *spatial::state = nullptr;


void
spatial::print_spatial_component(spatial::Component *spatial_component)
{


@@ 48,7 51,7 @@ spatial::make_model_matrix(
    m4 model_matrix = m4(1.0f);

    if (spatial_component->parent_entity_handle != entities::NO_ENTITY_HANDLE) {
        spatial::Component *parent = engine::get_spatial_component(
        spatial::Component *parent = spatial::get_component(
            spatial_component->parent_entity_handle);
        model_matrix = make_model_matrix(parent, cache);
    }


@@ 72,3 75,29 @@ spatial::make_model_matrix(

    return model_matrix;
}


Array<spatial::Component> *
spatial::get_components()
{
    return &spatial::state->component_set.components;
}


spatial::Component *
spatial::get_component(entities::Handle entity_handle)
{
    return spatial::state->component_set.components[entity_handle];
}


void
spatial::init(spatial::State *spatial_state, memory::Pool *asset_memory_pool)
{
    spatial::state = spatial_state;
    spatial::state->component_set = {
        .components = Array<spatial::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "spatial_components", true, 1
        )
    };
}

M src/spatial.hpp => src/spatial.hpp +10 -0
@@ 42,6 42,10 @@ public:
        Array<Component> components;
    };

    struct State {
        ComponentSet component_set;
    };


    static void print_spatial_component(Component *spatial_component);
    static bool does_spatial_component_have_dimensions(Component *spatial_component);


@@ 50,4 54,10 @@ public:
        Component *spatial_component,
        ModelMatrixCache *cache
    );
    static Array<spatial::Component> * get_components();
    static spatial::Component * get_component(entities::Handle entity_handle);
    static void init(spatial::State *spatial_state, memory::Pool *asset_memory_pool);

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

M src/state.hpp => src/state.hpp +1 -0
@@ 22,6 22,7 @@
struct State {
    GLFWwindow *window;
    WindowSize window_size;
    spatial::State spatial_state;
    engine::State engine_state;
    renderer::State renderer_state;
    cameras::State cameras_state;