Create utility function for adding components to entity

This commit is contained in:
SeanOMik 2022-09-16 17:01:52 -04:00
parent 3f39e27174
commit ca6c2337d6
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
4 changed files with 31 additions and 7 deletions

2
cmrc

@ -1 +1 @@
Subproject commit e386a629eb537d384811e598a3c96b9ca928f65e Subproject commit a64bea50c05594c8e7cf1f08e441bb9507742e2e

View File

@ -170,14 +170,9 @@ int main(int argc, char *argv[]) {
se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f); se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f);
// Create a model component
se::gfx::Model model(cube_vertices, cube_indicies, std::optional<se::gfx::Material>(material));
model.calculate_normals();
se::ModelComponent model_component(model);
// Create the entity and add the model component to it. // Create the entity and add the model component to it.
auto entity = std::make_shared<simpleengine::Entity>(); auto entity = std::make_shared<simpleengine::Entity>();
entity->add_component(model_component); entity->add_component<se::ModelComponent>(cube_vertices, cube_indicies, material, true);
entity->translate(3.5f, 0.f, 0.f); entity->translate(3.5f, 0.f, 0.f);
// Create a renderer and submit the entity into it. // Create a renderer and submit the entity into it.

View File

@ -21,6 +21,23 @@ namespace simpleengine {
} }
ModelComponent(std::vector<LitVertex> vertices, std::vector<GLuint> indicies, gfx::Material material,
bool calculate_normals = false): model(vertices, indicies, material) {
if (calculate_normals) {
model.calculate_normals();
}
}
ModelComponent(std::vector<LitVertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>(),
std::optional<gfx::Material> material = std::nullopt, bool calculate_normals = false) :
model(vertices, indicies, material) {
if (calculate_normals) {
model.calculate_normals();
}
}
virtual void update(const float& delta_time) override { virtual void update(const float& delta_time) override {
std::cout << "Model Component update" << std::endl; std::cout << "Model Component update" << std::endl;
} }

View File

@ -102,5 +102,17 @@ namespace simpleengine {
assert(!has_component<T>()); // TODO: Don't assert, give an error assert(!has_component<T>()); // TODO: Don't assert, give an error
components.push_back(std::make_shared<T>(component)); components.push_back(std::make_shared<T>(component));
} }
template<typename T, typename ...Args>
std::shared_ptr<T> add_component(Args&&... args) {
static_assert(std::is_base_of_v<Component, T>, "Component class must derive from simpleengine::Component");
// Only allow one type of the same component
assert(!has_component<T>()); // TODO: Don't assert, give an error
auto comp = std::make_shared<T>(std::forward<Args>(args)...);
components.push_back(comp);
return comp;
}
}; };
} }