Reworking drawable components

This commit is contained in:
Zoe Roux
2021-06-02 16:18:26 +02:00
parent 1cc6907ff0
commit 09d8b1a90b
15 changed files with 118 additions and 268 deletions
+4 -8
View File
@@ -28,17 +28,13 @@ set(SOURCES
sources/System/Movable/MovableSystem.cpp
sources/Models/Vector3.hpp
sources/Models/Vector2.hpp
sources/Component/Renderer/Drawable3DComponent.hpp
sources/Component/Renderer/Drawable2DComponent.hpp
sources/System/Renderer/Renderer3DSystem.hpp
sources/System/Renderer/Renderer2DSystem.hpp
sources/System/Renderer/RenderScreenSystem.hpp
sources/System/Renderer/RenderScreenSystem.cpp
sources/Component/Renderer/Drawable3DComponent.hpp
sources/System/Renderer/RenderSystem.hpp
sources/System/Renderer/RenderSystem.cpp
sources/Component/Renderer/CameraComponent.cpp
sources/Component/Renderer/CameraComponent.hpp
sources/System/Renderer/Render2DScreenSystem.cpp
sources/System/Renderer/Render2DScreenSystem.hpp
)
)
add_executable(bomberman
sources/main.cpp
+1 -1
View File
@@ -17,6 +17,6 @@ add_library(wal
sources/Component/Component.cpp
sources/System/System.cpp
sources/Models/Callback.hpp
)
sources/Models/TypeHolder.hpp)
target_include_directories(wal PUBLIC sources)
+13
View File
@@ -10,6 +10,7 @@
#include <memory>
#include "Component/Component.hpp"
#include "Exception/WalError.hpp"
#include "Models/TypeHolder.hpp"
namespace WAL
{
@@ -83,6 +84,18 @@ namespace WAL
return *this;
}
//! @brief Add a component to this entity. The component is constructed in place.
//! @throw DuplicateError is thrown if a component with the same type already exist.
//! @return This entity is returned
template<typename T, typename TNested, typename ...Types>
Entity &addComponent(Types &&...params)
{
if (this->hasComponent<T>())
throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists.");
this->_components.push_back(std::make_unique<T>(*this, TypeHolder<TNested>(), std::forward<Types>(params)...));
return *this;
}
//! @brief Copy a component to this entity.
//! @return This entity is returned.
Entity &addComponent(const Component &component);
+13
View File
@@ -0,0 +1,13 @@
//
// Created by Zoe Roux on 2021-06-02.
//
#pragma once
namespace WAL
{
//! @brief A class only used to specify template arguments.
template<typename T>
class TypeHolder {};
}
@@ -5,35 +5,35 @@
#pragma once
#include "Component/Component.hpp"
#include "Drawables/ADrawable2D.hpp"
#include "Drawables/ADrawable3D.hpp"
#include "Model/Model.hpp"
namespace BBM
{
template <class T>
class Drawable2DComponent : public WAL::Component
{
public:
//! @brief The type of the component
T member;
std::shared_ptr<RAY::Drawables::ADrawable2D> drawable;
//! ctor
Drawable2DComponent(WAL::Entity &entity, T member)
//! @brief ctor
Drawable2DComponent(WAL::Entity &entity, std::shared_ptr<RAY::Drawables::ADrawable2D> drawable)
: WAL::Component(entity),
member(std::move(member))
drawable(std::move(drawable))
{}
//! ctor
template<typename ...Params>
template<typename T, typename ...Params>
explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params)
: WAL::Component(entity),
member(std::forward<Params>(params)...)
drawable(std::move(T(std::forward<Params>(params)...)))
{}
//! @brief Clone a component for another or the same entity.
//! @param entity The entity that owns the ne component.
WAL::Component *clone(WAL::Entity &entity) const override
{
return new Drawable2DComponent(entity, this->member);
return new Drawable2DComponent(entity, this->drawable);
}
//! @brief Default copy ctor
@@ -42,7 +42,5 @@ namespace BBM
~Drawable2DComponent() override = default;
//! @brief Default assignment operator
Drawable2DComponent &operator=(const Drawable2DComponent &) = delete;
};
}
@@ -4,37 +4,37 @@
#pragma once
#include <Models/TypeHolder.hpp>
#include "Component/Component.hpp"
#include "Drawables/ADrawable3D.hpp"
#include "Model/Model.hpp"
namespace BBM
{
template <class T>
class Drawable3DComponent : public WAL::Component
{
public:
//! @brief The type of the component
T member;
std::shared_ptr<RAY::Drawables::ADrawable3D> drawable;
//! @brief ctor
Drawable3DComponent(WAL::Entity &entity, T member)
Drawable3DComponent(WAL::Entity &entity, std::shared_ptr<RAY::Drawables::ADrawable3D> drawable)
: WAL::Component(entity),
member(std::move(member))
drawable(std::move(drawable))
{}
//! ctor
template<typename ...Params>
explicit Drawable3DComponent(WAL::Entity &entity, Params &&...params)
template<typename T, typename ...Params>
explicit Drawable3DComponent(WAL::Entity &entity, WAL::TypeHolder<T>, Params &&...params)
: WAL::Component(entity),
member(std::forward<Params>(params)...)
drawable(new T(std::forward<Params>(params)...))
{}
//! @brief Clone a component for another or the same entity.
//! @param entity The entity that owns the ne component.
WAL::Component *clone(WAL::Entity &entity) const override
{
return new Drawable3DComponent(entity, this->member);
return new Drawable3DComponent(entity, this->drawable);
}
//! @brief Default copy ctor
+12 -15
View File
@@ -4,16 +4,16 @@
#include <Wal.hpp>
#include <iostream>
#include <System/Movable/MovableSystem.hpp>
#include <System/Renderer/RenderScreenSystem.hpp>
#include <System/Renderer/Render2DScreenSystem.hpp>
#include <System/Renderer/Renderer2DSystem.hpp>
#include "System/Movable/MovableSystem.hpp"
#include "System/Renderer/RenderSystem.hpp"
#include <Model/Model.hpp>
#include <Drawables/2D/Rectangle.hpp>
#include <TraceLog.hpp>
#include <System/Renderer/Renderer3DSystem.hpp>
#include "Component/Position/PositionComponent.hpp"
#include "Models/Vector2.hpp"
#include "Component/Renderer/CameraComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Runner.hpp"
#include "Models/GameState.hpp"
@@ -36,23 +36,20 @@ namespace BBM
{
RAY::TraceLog::setLevel(LOG_WARNING);
RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE);
wal.addSystem<Renderer3DSystem<RAY3D::Model>>();
wal.addSystem<Render2DScreenSystem>(window)
.addSystem<Renderer2DSystem<RAY2D::Rectangle>>();
wal.addSystem<RenderScreenSystem>(window);
wal.addSystem<RenderSystem>(wal, window);
}
std::shared_ptr<WAL::Scene> loadGameScene()
{
// Drawable2DComponent cmp = Drawable2DComponent(Vector2f(), Vector2f(), RED);
auto scene = std::make_shared<WAL::Scene>();
scene->addEntity("cube")
.addComponent<PositionComponent>()
.addComponent<Drawable2DComponent<RAY2D::Rectangle>>(Vector2f(), Vector2f(10, 10), RED);
// scene->addEntity("cube")
// .addComponent<PositionComponent>()
// .addComponent<Drawable2DComponent>(Vector2f(), Vector2f(10, 10), RED);
scene->addEntity("player")
.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"));
scene->addEntity("camera")
.addComponent<PositionComponent>(10, 10, 10)
.addComponent<CameraComponent>();
@@ -1,19 +0,0 @@
//
// Created by Zoe Roux on 5/27/21.
//
#include "Render2DScreenSystem.hpp"
namespace BBM
{
Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window)
: WAL::System({}),
_window(window),
_camera(RAY::Vector2(10, 10), RAY::Vector2(), 0)
{}
void Render2DScreenSystem::onSelfUpdate()
{
this->_window.useCamera(this->_camera);
}
}
@@ -1,33 +0,0 @@
//
// Created by Zoe Roux on 5/27/21.
//
#pragma once
#include <System/System.hpp>
#include <Window.hpp>
namespace BBM
{
class Render2DScreenSystem : public WAL::System
{
//! @brief The window to render on
RAY::Window &_window;
//! @brief The camera used to render.
RAY::Camera::Camera2D _camera;
public:
//! @brief A method called after all entities that this system manage has been updated.
//! @note render on screen here
void onSelfUpdate() override;
//! @brief ctor
explicit Render2DScreenSystem(RAY::Window &window);
//! @brief Default copy ctor
Render2DScreenSystem(const Render2DScreenSystem &) = default;
//! @brief Default dtor
~Render2DScreenSystem() override = default;
//! @brief A render screen system can't be assigned.
Render2DScreenSystem &operator=(const Render2DScreenSystem &) = delete;
};
}
@@ -1,32 +0,0 @@
//
// Created by Zoe Roux on 5/27/21.
//
#include "RenderScreenSystem.hpp"
#include "Component/Renderer/CameraComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
namespace BBM
{
RenderScreenSystem::RenderScreenSystem(RAY::Window &window)
: WAL::System({
typeid(CameraComponent),
typeid(PositionComponent)
}),
_window(window),
_camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE)
{}
void RenderScreenSystem::onSelfUpdate()
{
this->_window.draw();
this->_window.clear();
this->_window.useCamera(this->_camera);
}
void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime)
{
const auto &pos = entity.getComponent<PositionComponent>();
_camera.setPosition(pos.position);
}
}
+49
View File
@@ -0,0 +1,49 @@
//
// Created by Zoe Roux on 5/27/21.
//
#undef INTERNAL
#define INTERNAL public
#include <Component/Renderer/Drawable2DComponent.hpp>
#include "RenderSystem.hpp"
#include "Component/Renderer/CameraComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
namespace BBM
{
RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window)
: WAL::System({
typeid(CameraComponent),
typeid(PositionComponent)
}),
_wal(wal),
_window(window),
_camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE)
{}
void RenderSystem::onSelfUpdate()
{
this->_camera.update();
BeginDrawing();
ClearBackground(BLACK);
BeginMode3D(this->_camera);
for (auto &entity : this->_wal.scene->getEntities()) {
if (!entity.hasComponent<Drawable2DComponent>()
|| !entity.hasComponent<PositionComponent>())
continue;
auto &drawable = entity.getComponent<Drawable2DComponent>();
auto &pos = entity.getComponent<PositionComponent>();
// drawable.drawable->setPosition(static_cast<RAY::Vector3>(pos.position));
// drawable.drawable->drawOn(this->_window);
}
EndMode3D();
EndDrawing();
}
void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime)
{
const auto &pos = entity.getComponent<PositionComponent>();
_camera.setPosition(pos.position);
}
}
@@ -7,11 +7,15 @@
#include "System/System.hpp"
#include "Camera/Camera2D.hpp"
#include "Window.hpp"
#include "Wal.hpp"
namespace BBM
{
class RenderScreenSystem : public WAL::System
class RenderSystem : public WAL::System
{
//! @brief The ECS to update.
WAL::Wal &_wal;
//! @brief The window to render on
RAY::Window &_window;
@@ -26,12 +30,12 @@ namespace BBM
void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override;
//! @brief ctor
explicit RenderScreenSystem(RAY::Window &window);
RenderSystem(WAL::Wal &wal, RAY::Window &window);
//! @brief Default copy ctor
RenderScreenSystem(const RenderScreenSystem &) = default;
RenderSystem(const RenderSystem &) = default;
//! @brief Default dtor
~RenderScreenSystem() override = default;
~RenderSystem() override = default;
//! @brief A render screen system can't be assigned.
RenderScreenSystem &operator=(const RenderScreenSystem &) = delete;
RenderSystem &operator=(const RenderSystem &) = delete;
};
}
@@ -1,48 +0,0 @@
//
// Created by cbihan on 24/05/2021.
//
#pragma once
#include <type_traits>
#include "System/System.hpp"
#include "Entity/Entity.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Window.hpp"
namespace BBM
{
template <class T>
class Renderer2DSystem : public WAL::System
{
private:
//! @brief The class to render
RAY::Window &_window;
public:
explicit Renderer2DSystem()
: WAL::System({typeid(PositionComponent), typeid(Drawable2DComponent<T>)}),
_window(RAY::Window::getInstance())
{
}
//! @brief Update the corresponding component of the given entity
//! @param entity The entity to update.
//! @param dtime The delta time.
void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override
{
auto &comp = entity.getComponent<Drawable2DComponent<T>>();
auto &pos = entity.getComponent<PositionComponent>();
comp.member.setPosition({pos.getX(), pos.getY()});
comp.member.drawOn(this->_window);
}
//! @brief default copy ctor
Renderer2DSystem(const Renderer2DSystem &) = default;
//! @brief default dtor
~Renderer2DSystem() override = default;
//! @brief Default assignment operator
Renderer2DSystem &operator=(const Renderer2DSystem &) = delete;
};
}
@@ -1,48 +0,0 @@
//
// Created by cbihan on 24/05/2021.
//
#pragma once
#include <type_traits>
#include "System/System.hpp"
#include "Entity/Entity.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Window.hpp"
namespace BBM
{
template <class T>
class Renderer3DSystem : public WAL::System
{
private:
//! @brief The class to render
RAY::Window &_window;
public:
//! @brief ctor
explicit Renderer3DSystem()
: WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent<T>)}),
_window(RAY::Window::getInstance())
{}
//! @brief Update the corresponding component of the given entity
//! @param entity The entity to update.
//! @param dtime The delta time.
void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override
{
auto &comp = entity.getComponent<Drawable3DComponent<T>>();
auto &pos = entity.getComponent<PositionComponent>();
comp.member.setPosition(static_cast<RAY::Vector3>(pos.position));
comp.member.drawOn(this->_window);
}
//! @brief Default copy ctor
Renderer3DSystem(const Renderer3DSystem &) = default;
//! @brief Default dtor
~Renderer3DSystem() override = default;
//! @brief Default assignment operator
Renderer3DSystem &operator=(const Renderer3DSystem &) = delete;
};
}
-40
View File
@@ -9,45 +9,6 @@
#include <iostream>
#include "Runner/Runner.hpp"
// Dependencies of the demo
#include "Camera/Camera3D.hpp"
#include "Controllers/Keyboard.hpp"
#include "Drawables/2D/Text.hpp"
#include "Drawables/Image.hpp"
#include "Drawables/3D/Grid.hpp"
#include "Drawables/Texture.hpp"
#include "Drawables/3D/Circle.hpp"
#include "Drawables/2D/Circle.hpp"
#include "Drawables/3D/Cube.hpp"
#include "Drawables/3D/Sphere.hpp"
#include "Model/Model.hpp"
#include "Model/ModelAnimations.hpp"
#include "System/Renderer/Renderer3DSystem.hpp"
#include "System/Renderer/Renderer2DSystem.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "System/Renderer/RenderScreenSystem.hpp"
#include "Vector/Vector3.hpp"
#include "Window.hpp"
#include "TraceLog.hpp"
#include "Wal.hpp"
const std::vector<std::string>textures = {
"blue", "cyan", "green", "purple", "red", "yellow"
};
std::string get_full_path(const std::string &color)
{
std::string path = "assets/player/";
path += color;
path += ".png";
return path;
}
void usage(const std::string &bin)
{
std::cout << "Bomberman." << std::endl
@@ -62,6 +23,5 @@ int main(int argc, char **argv)
usage(argv[0]);
return 1;
}
// return demo();
return BBM::run();
}