~vladh/peony

38f98f7e0f47c852dc8cf825658a429329ab8e9c — Vlad-Stefan Harbuz 1 year, 7 months ago 73699ee
refactor drawable state
M src/core.cpp => src/core.cpp +1 -0
@@ 131,6 131,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);
    drawable::init(&state->drawable_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 +1 -1
@@ 144,7 144,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u

    bool has_spatial_component = spatial::is_spatial_component_valid(spatial_component);
    bool has_drawable_component = drawable::is_component_valid(
        engine_state->drawable_component_set.components[handle]);
        drawable::get_component(handle));
    bool has_light_component = lights::is_light_component_valid(
        engine_state->light_component_set.components[handle]);
    bool has_behavior_component = behavior::is_behavior_component_valid(

M src/drawable.cpp => src/drawable.cpp +39 -0
@@ 6,6 6,9 @@
#include "models.hpp"


drawable::State *drawable::state = nullptr;


char const *
drawable::render_pass_to_string(drawable::Pass render_pass)
{


@@ 90,3 93,39 @@ drawable::destroy_component(drawable::Component *drawable_component)
    geom::destroy_mesh(&drawable_component->mesh);
}


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


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


u32
drawable::get_last_drawn_shader_program()
{
    return drawable::state->last_drawn_shader_program;
}


void
drawable::set_last_drawn_shader_program(u32 val)
{
    drawable::state->last_drawn_shader_program = val;
}


void
drawable::init(drawable::State *drawable_state, memory::Pool *asset_memory_pool)
{
    drawable::state = drawable_state;
    drawable::state->components = Array<drawable::Component>(
        asset_memory_pool, MAX_N_ENTITIES, "drawable_components", true, 1);
}

M src/drawable.hpp => src/drawable.hpp +9 -1
@@ 30,7 30,7 @@ public:
        Pass target_render_pass = Pass::none;
    };

    struct ComponentSet {
    struct State {
        Array<drawable::Component> components;
        u32 last_drawn_shader_program;
    };


@@ 39,4 39,12 @@ public:
    static drawable::Pass render_pass_from_string(const char* str);
    static bool is_component_valid(drawable::Component *drawable_component);
    static void destroy_component(drawable::Component *drawable_component);
    static Array<drawable::Component> * get_components();
    static drawable::Component * get_component(entities::Handle entity_handle);
    static u32 get_last_drawn_shader_program();
    static void set_last_drawn_shader_program(u32 val);
    static void init(drawable::State *drawable_state, memory::Pool *asset_memory_pool);

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

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


u32
engine::get_last_drawn_shader_program()
{
    return engine::state->drawable_component_set.last_drawn_shader_program;
}


void
engine::set_last_drawn_shader_program(u32 val)
{
    engine::state->drawable_component_set.last_drawn_shader_program = val;
}


void
engine::mark_first_non_internal_handle()
{


@@ 78,13 64,6 @@ engine::get_entities()
}


Array<drawable::Component> *
engine::get_drawable_components()
{
    return &engine::state->drawable_component_set.components;
}


Array<lights::Component> *
engine::get_light_components()
{


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


drawable::Component *
engine::get_drawable_component(entities::Handle entity_handle)
{
    return engine::state->drawable_component_set.components[entity_handle];
}


lights::Component *
engine::get_light_component(entities::Handle entity_handle)
{


@@ 228,10 200,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->drawable_component_set = {
        .components = Array<drawable::Component>(
            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)


@@ 266,8 234,7 @@ engine::destroy_non_internal_entities()
        idx < engine::state->entity_set.entities.length;
        idx++
    ) {
        drawable::destroy_component(
            engine::state->drawable_component_set.components[idx]);
        drawable::destroy_component(drawable::get_component(idx));
    }

    engine::state->entity_set.next_handle =


@@ 280,7 247,7 @@ engine::destroy_non_internal_entities()
        engine::state->entity_set.first_non_internal_handle);
    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(
    drawable::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->behavior_component_set.components.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);

M src/engine.hpp => src/engine.hpp +0 -8
@@ 6,7 6,6 @@
namespace chrono = std::chrono;
#include "types.hpp"
#include "entities.hpp"
#include "spatial.hpp"
#include "lights.hpp"
#include "behavior.hpp"
#include "physics.hpp"


@@ 59,7 58,6 @@ public:
        Array<models::ModelLoader> model_loaders;
        models::EntityLoaderSet entity_loader_set;
        entities::Set entity_set;
        drawable::ComponentSet drawable_component_set;
        lights::ComponentSet light_component_set;
        behavior::ComponentSet behavior_component_set;
        anim::ComponentSet animation_component_set;


@@ 69,21 67,15 @@ public:
    static engine::State * debug_get_engine_state();
    static models::EntityLoader * get_entity_loader(entities::Handle entity_handle);
    static models::ModelLoader * push_model_loader();
    static u32 get_last_drawn_shader_program();
    static void set_last_drawn_shader_program(u32 val);
    static void mark_first_non_internal_handle();
    static entities::Set * get_entity_set();
    static Array<entities::Entity> * get_entities();
    static Array<drawable::Component> * get_drawable_components();
    static Array<lights::Component> * get_light_components();
    static Array<spatial::Component> * get_spatial_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 drawable::Component * get_drawable_component(entities::Handle entity_handle);
    static lights::Component * get_light_component(entities::Handle entity_handle);
    static spatial::Component * get_spatial_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/models.cpp => src/models.cpp +2 -2
@@ 104,7 104,7 @@ models::prepare_entity_loader_and_check_if_done(

        // drawable::Component
        if (model_loader->n_meshes == 1) {
            drawable::Component *drawable_component = engine::get_drawable_component(entity_loader->entity_handle);
            drawable::Component *drawable_component = drawable::get_component(entity_loader->entity_handle);
            assert(drawable_component);
            *drawable_component = {
                .entity_handle = entity_loader->entity_handle,


@@ 129,7 129,7 @@ models::prepare_entity_loader_and_check_if_done(
                    };
                }

                drawable::Component *drawable_component = engine::get_drawable_component(child_entity->handle);
                drawable::Component *drawable_component = drawable::get_component(child_entity->handle);
                assert(drawable_component);
                *drawable_component = {
                    .entity_handle = child_entity->handle,

M src/renderer.cpp => src/renderer.cpp +3 -3
@@ 1240,9 1240,9 @@ renderer::draw(
    assert(shader_asset);

    // If our shader program has changed since our last mesh, tell OpenGL about it.
    if (shader_asset->program != engine::get_last_drawn_shader_program()) {
    if (shader_asset->program != drawable::get_last_drawn_shader_program()) {
        glUseProgram(shader_asset->program);
        engine::set_last_drawn_shader_program(shader_asset->program);
        drawable::set_last_drawn_shader_program(shader_asset->program);

        if (render_mode == drawable::Mode::regular) {
            for (


@@ 1292,7 1292,7 @@ renderer::draw_all(
) {
    spatial::ModelMatrixCache cache = { m4(1.0f), nullptr };

    each (drawable_component, *engine::get_drawable_components()) {
    each (drawable_component, *drawable::get_components()) {
        if (!drawable::is_component_valid(drawable_component)) {
            continue;
        }

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