Finishing to rework the drawable system

This commit is contained in:
Zoe Roux
2021-06-02 16:22:26 +02:00
parent 09d8b1a90b
commit 88e086b272
3 changed files with 27 additions and 11 deletions
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include <Models/TypeHolder.hpp>
#include "Component/Component.hpp" #include "Component/Component.hpp"
#include "Drawables/ADrawable3D.hpp" #include "Drawables/ADrawable3D.hpp"
#include "Model/Model.hpp" #include "Model/Model.hpp"
@@ -24,9 +25,9 @@ namespace BBM
//! ctor //! ctor
template<typename T, typename ...Params> template<typename T, typename ...Params>
explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params) explicit Drawable2DComponent(WAL::Entity &entity, WAL::TypeHolder<T>, Params &&...params)
: WAL::Component(entity), : WAL::Component(entity),
drawable(std::move(T(std::forward<Params>(params)...))) drawable(new T(std::forward<Params>(params)...))
{} {}
//! @brief Clone a component for another or the same entity. //! @brief Clone a component for another or the same entity.
+3 -5
View File
@@ -41,12 +41,10 @@ namespace BBM
std::shared_ptr<WAL::Scene> loadGameScene() std::shared_ptr<WAL::Scene> loadGameScene()
{ {
// Drawable2DComponent cmp = Drawable2DComponent(Vector2f(), Vector2f(), RED);
auto scene = std::make_shared<WAL::Scene>(); auto scene = std::make_shared<WAL::Scene>();
// scene->addEntity("cube") scene->addEntity("cube")
// .addComponent<PositionComponent>() .addComponent<PositionComponent>()
// .addComponent<Drawable2DComponent>(Vector2f(), Vector2f(10, 10), RED); .addComponent<Drawable2DComponent, RAY2D::Rectangle>(Vector2f(), Vector2f(10, 10), RED);
scene->addEntity("player") scene->addEntity("player")
.addComponent<PositionComponent>() .addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); .addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"));
+21 -4
View File
@@ -4,10 +4,14 @@
#undef INTERNAL #undef INTERNAL
#define INTERNAL public #define INTERNAL public
#include <Component/Renderer/Drawable2DComponent.hpp> #include <Component/Renderer/Drawable3DComponent.hpp>
#include "Models/Vector2.hpp"
#include "RenderSystem.hpp" #include "RenderSystem.hpp"
#include "Component/Renderer/CameraComponent.hpp" #include "Component/Renderer/CameraComponent.hpp"
#include "Component/Position/PositionComponent.hpp" #include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Drawables/ADrawable2D.hpp"
#include "Drawables/ADrawable3D.hpp"
namespace BBM namespace BBM
{ {
@@ -26,7 +30,21 @@ namespace BBM
this->_camera.update(); this->_camera.update();
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode3D(this->_camera); BeginMode3D(this->_camera);
for (auto &entity : this->_wal.scene->getEntities()) {
if (!entity.hasComponent<Drawable3DComponent>()
|| !entity.hasComponent<PositionComponent>())
continue;
auto &drawable = entity.getComponent<Drawable3DComponent>();
auto &pos = entity.getComponent<PositionComponent>();
drawable.drawable->setPosition(pos.position);
drawable.drawable->drawOn(this->_window);
}
EndMode3D();
// TODO sort entities based on the Z axis
for (auto &entity : this->_wal.scene->getEntities()) { for (auto &entity : this->_wal.scene->getEntities()) {
if (!entity.hasComponent<Drawable2DComponent>() if (!entity.hasComponent<Drawable2DComponent>()
|| !entity.hasComponent<PositionComponent>()) || !entity.hasComponent<PositionComponent>())
@@ -34,10 +52,9 @@ namespace BBM
auto &drawable = entity.getComponent<Drawable2DComponent>(); auto &drawable = entity.getComponent<Drawable2DComponent>();
auto &pos = entity.getComponent<PositionComponent>(); auto &pos = entity.getComponent<PositionComponent>();
// drawable.drawable->setPosition(static_cast<RAY::Vector3>(pos.position)); drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y));
// drawable.drawable->drawOn(this->_window); drawable.drawable->drawOn(this->_window);
} }
EndMode3D();
EndDrawing(); EndDrawing();
} }