~vladh/peony

49a30c62cf6a142eaaa3af7d3cf99c4c47433452 — Vlad-Stefan Harbuz 1 year, 7 months ago 38f98f7
refactor lights state
8 files changed, 34 insertions(+), 47 deletions(-)

M src/core.cpp
M src/debug_ui.cpp
M src/engine.cpp
M src/engine.hpp
M src/lights.cpp
M src/lights.hpp
M src/models.cpp
M src/renderer.cpp
M src/core.cpp => src/core.cpp +1 -1
@@ 132,6 132,7 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)

    spatial::init(&state->spatial_state, asset_memory_pool);
    drawable::init(&state->drawable_state, asset_memory_pool);
    lights::init(&state->lights_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);


@@ 145,7 146,6 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)
        renderer::get_gui_font_assets(),
        state->window_size.width, state->window_size.height);
    debugdraw::init(&state->debug_draw_state, asset_memory_pool);
    lights::init(&state->lights_state);
    tasks::init(&state->tasks_state, asset_memory_pool);
    anim::init(&state->anim_state, asset_memory_pool);
    cameras::init(&state->cameras_state, state->window_size.width, state->window_size.height);

M src/debug_ui.cpp => src/debug_ui.cpp +1 -1
@@ 146,7 146,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
    bool has_drawable_component = drawable::is_component_valid(
        drawable::get_component(handle));
    bool has_light_component = lights::is_light_component_valid(
        engine_state->light_component_set.components[handle]);
        lights::get_component(handle));
    bool has_behavior_component = behavior::is_behavior_component_valid(
        engine_state->behavior_component_set.components[handle]);
    bool has_animation_component = anim::is_animation_component_valid(

M src/engine.cpp => src/engine.cpp +3 -23
@@ 64,13 64,6 @@ engine::get_entities()
}


Array<lights::Component> *
engine::get_light_components()
{
    return &engine::state->light_component_set.components;
}


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


@@ 99,13 92,6 @@ engine::get_entity(entities::Handle entity_handle)
}


lights::Component *
engine::get_light_component(entities::Handle entity_handle)
{
    return engine::state->light_component_set.components[entity_handle];
}


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


@@ 200,10 186,6 @@ void engine::init(engine::State *engine_state, memory::Pool *asset_memory_pool) 
        .entities = Array<entities::Entity>(
            asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1)
    };
    engine::state->light_component_set = {
        .components = Array<lights::Component>(
            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)


@@ 243,7 225,7 @@ engine::destroy_non_internal_entities()
        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);
    engine::state->light_component_set.components.delete_elements_after_index(
    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);


@@ 404,7 386,7 @@ engine::handle_console_command()
void
engine::update_light_position(f32 amount)
{
    each (light_component, engine::state->light_component_set.components) {
    each (light_component, *lights::get_components()) {
        if (light_component->type == lights::LightType::directional) {
            lights::adjust_dir_light_angle(amount);
            break;


@@ 594,9 576,7 @@ engine::update()

    engine::state->is_world_loaded = check_all_entities_loaded();

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

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


M src/engine.hpp => src/engine.hpp +0 -3
@@ 58,7 58,6 @@ public:
        Array<models::ModelLoader> model_loaders;
        models::EntityLoaderSet entity_loader_set;
        entities::Set entity_set;
        lights::ComponentSet light_component_set;
        behavior::ComponentSet behavior_component_set;
        anim::ComponentSet animation_component_set;
        physics::ComponentSet physics_component_set;


@@ 70,12 69,10 @@ public:
    static void mark_first_non_internal_handle();
    static entities::Set * get_entity_set();
    static Array<entities::Entity> * get_entities();
    static Array<lights::Component> * get_light_components();
    static Array<behavior::Component> * get_behavior_components();
    static Array<anim::Component> * get_animation_components();
    static Array<physics::Component> * get_physics_components();
    static entities::Entity * get_entity(entities::Handle entity_handle);
    static lights::Component * get_light_component(entities::Handle entity_handle);
    static behavior::Component * get_behavior_component(entities::Handle entity_handle);
    static anim::Component * get_animation_component(entities::Handle entity_handle);
    static physics::Component * get_physics_component(entities::Handle entity_handle);

M src/lights.cpp => src/lights.cpp +20 -6
@@ 68,11 68,9 @@ lights::is_light_component_valid(lights::Component *light_component)


void
lights::update_light_components(
    lights::ComponentSet *light_component_set,
    v3 camera_position
) {
    each (light_component, light_component_set->components) {
lights::update_light_components(v3 camera_position)
{
    each (light_component, *get_components()) {
        if (light_component->entity_handle == entities::NO_ENTITY_HANDLE) {
            continue;
        }


@@ 102,9 100,25 @@ lights::update_light_components(
}


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


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


void
lights::init(lights::State *lights_state)
lights::init(lights::State *lights_state, memory::Pool *asset_memory_pool)
{
    lights::state = lights_state;
    lights::state->dir_light_angle = radians(55.0f);
    lights::state->components = Array<lights::Component>(
        asset_memory_pool, MAX_N_ENTITIES, "light_components", true, 1);
}

M src/lights.hpp => src/lights.hpp +5 -9
@@ 24,11 24,8 @@ public:
        v4 attenuation;
    };

    struct ComponentSet {
        Array<Component> components;
    };

    struct State {
        Array<Component> components;
        f32 dir_light_angle;
    };



@@ 37,11 34,10 @@ public:
    static LightType light_type_from_string(const char *str);
    static u32 light_type_to_int(LightType light_type);
    static bool is_light_component_valid(Component *light_component);
    static void update_light_components(
        ComponentSet *light_component_set,
        v3 camera_position
    );
    static void init(lights::State *state);
    static void update_light_components(v3 camera_position);
    static Array<lights::Component> * get_components();
    static lights::Component * get_component(entities::Handle entity_handle);
    static void init(lights::State *state, memory::Pool *asset_memory_pool);

private:
    static lights::State *state;

M src/models.cpp => src/models.cpp +1 -1
@@ 86,7 86,7 @@ models::prepare_entity_loader_and_check_if_done(
        *spatial_component = entity_loader->spatial_component;
        spatial_component->entity_handle = entity_loader->entity_handle;

        lights::Component *light_component = engine::get_light_component(entity_loader->entity_handle);
        lights::Component *light_component = lights::get_component(entity_loader->entity_handle);
        *light_component = entity_loader->light_component;
        light_component->entity_handle = entity_loader->entity_handle;


M src/renderer.cpp => src/renderer.cpp +3 -3
@@ 288,7 288,7 @@ renderer::render(GLFWwindow *window)

            u32 idx_light = 0;

            each (light_component, *engine::get_light_components()) {
            each (light_component, *lights::get_components()) {
                if (light_component->entity_handle == entities::NO_ENTITY_HANDLE) {
                    continue;
                }


@@ 344,7 344,7 @@ renderer::render(GLFWwindow *window)

            u32 idx_light = 0;

            each (light_component, *engine::get_light_components()) {
            each (light_component, *lights::get_components()) {
                if (light_component->entity_handle == entities::NO_ENTITY_HANDLE) {
                    continue;
                }


@@ 1171,7 1171,7 @@ renderer::copy_scene_data_to_ubo(
    u32 n_point_lights = 0;
    u32 n_directional_lights = 0;

    each (light_component, *engine::get_light_components()) {
    each (light_component, *lights::get_components()) {
        if (light_component->entity_handle == entities::NO_ENTITY_HANDLE) {
            continue;
        }