From 09d8b1a90b23ef8b9423dd491d3c446cc2b24a29 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 2 Jun 2021 16:18:26 +0200 Subject: [PATCH] Reworking drawable components --- CMakeLists.txt | 12 ++--- lib/wal/CMakeLists.txt | 2 +- lib/wal/sources/Entity/Entity.hpp | 13 +++++ lib/wal/sources/Models/TypeHolder.hpp | 13 +++++ .../Renderer/Drawable2DComponent.hpp | 20 ++++---- .../Renderer/Drawable3DComponent.hpp | 16 +++--- sources/Runner/Runner.cpp | 27 +++++----- .../System/Renderer/Render2DScreenSystem.cpp | 19 ------- .../System/Renderer/Render2DScreenSystem.hpp | 33 ------------- .../System/Renderer/RenderScreenSystem.cpp | 32 ------------ sources/System/Renderer/RenderSystem.cpp | 49 +++++++++++++++++++ ...enderScreenSystem.hpp => RenderSystem.hpp} | 14 ++++-- sources/System/Renderer/Renderer2DSystem.hpp | 48 ------------------ sources/System/Renderer/Renderer3DSystem.hpp | 48 ------------------ sources/main.cpp | 40 --------------- 15 files changed, 118 insertions(+), 268 deletions(-) create mode 100644 lib/wal/sources/Models/TypeHolder.hpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.cpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.hpp delete mode 100644 sources/System/Renderer/RenderScreenSystem.cpp create mode 100644 sources/System/Renderer/RenderSystem.cpp rename sources/System/Renderer/{RenderScreenSystem.hpp => RenderSystem.hpp} (69%) delete mode 100644 sources/System/Renderer/Renderer2DSystem.hpp delete mode 100644 sources/System/Renderer/Renderer3DSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 84be48cd..01c04fb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index e8cd8655..d216b9f3 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..b64bfb82 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -10,6 +10,7 @@ #include #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 + Entity &addComponent(Types &&...params) + { + if (this->hasComponent()) + throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); + this->_components.push_back(std::make_unique(*this, TypeHolder(), std::forward(params)...)); + return *this; + } + //! @brief Copy a component to this entity. //! @return This entity is returned. Entity &addComponent(const Component &component); diff --git a/lib/wal/sources/Models/TypeHolder.hpp b/lib/wal/sources/Models/TypeHolder.hpp new file mode 100644 index 00000000..45057ed1 --- /dev/null +++ b/lib/wal/sources/Models/TypeHolder.hpp @@ -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 + class TypeHolder {}; +} \ No newline at end of file diff --git a/sources/Component/Renderer/Drawable2DComponent.hpp b/sources/Component/Renderer/Drawable2DComponent.hpp index 63aaaf2b..0fc75f26 100644 --- a/sources/Component/Renderer/Drawable2DComponent.hpp +++ b/sources/Component/Renderer/Drawable2DComponent.hpp @@ -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 Drawable2DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; - //! ctor - Drawable2DComponent(WAL::Entity &entity, T member) + //! @brief ctor + Drawable2DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template + template explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(std::move(T(std::forward(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; - - }; } \ No newline at end of file diff --git a/sources/Component/Renderer/Drawable3DComponent.hpp b/sources/Component/Renderer/Drawable3DComponent.hpp index e4f85321..5a04da07 100644 --- a/sources/Component/Renderer/Drawable3DComponent.hpp +++ b/sources/Component/Renderer/Drawable3DComponent.hpp @@ -4,37 +4,37 @@ #pragma once +#include #include "Component/Component.hpp" #include "Drawables/ADrawable3D.hpp" #include "Model/Model.hpp" namespace BBM { - template class Drawable3DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; //! @brief ctor - Drawable3DComponent(WAL::Entity &entity, T member) + Drawable3DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template - explicit Drawable3DComponent(WAL::Entity &entity, Params &&...params) + template + explicit Drawable3DComponent(WAL::Entity &entity, WAL::TypeHolder, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(new T(std::forward(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 diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9e256b05..d45a9273 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,16 +4,16 @@ #include #include -#include -#include -#include -#include +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include #include #include -#include +#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>(); - - wal.addSystem(window) - .addSystem>(); - wal.addSystem(window); + wal.addSystem(wal, window); } std::shared_ptr loadGameScene() { +// Drawable2DComponent cmp = Drawable2DComponent(Vector2f(), Vector2f(), RED); + auto scene = std::make_shared(); - scene->addEntity("cube") - .addComponent() - .addComponent>(Vector2f(), Vector2f(10, 10), RED); +// scene->addEntity("cube") +// .addComponent() +// .addComponent(Vector2f(), Vector2f(10, 10), RED); scene->addEntity("player") .addComponent() - .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); + .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); scene->addEntity("camera") .addComponent(10, 10, 10) .addComponent(); diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp deleted file mode 100644 index 938d107f..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/Render2DScreenSystem.hpp b/sources/System/Renderer/Render2DScreenSystem.hpp deleted file mode 100644 index 12adaf34..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#pragma once - -#include -#include - -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; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp deleted file mode 100644 index 5523ee48..00000000 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ /dev/null @@ -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(); - _camera.setPosition(pos.position); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp new file mode 100644 index 00000000..590a9262 --- /dev/null +++ b/sources/System/Renderer/RenderSystem.cpp @@ -0,0 +1,49 @@ +// +// Created by Zoe Roux on 5/27/21. +// + +#undef INTERNAL +#define INTERNAL public +#include +#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() + || !entity.hasComponent()) + continue; + auto &drawable = entity.getComponent(); + auto &pos = entity.getComponent(); + +// drawable.drawable->setPosition(static_cast(pos.position)); +// drawable.drawable->drawOn(this->_window); + } + EndMode3D(); + EndDrawing(); + } + + void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + { + const auto &pos = entity.getComponent(); + _camera.setPosition(pos.position); + } +} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.hpp b/sources/System/Renderer/RenderSystem.hpp similarity index 69% rename from sources/System/Renderer/RenderScreenSystem.hpp rename to sources/System/Renderer/RenderSystem.hpp index e9b2ea85..5524f614 100644 --- a/sources/System/Renderer/RenderScreenSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -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; }; } diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp deleted file mode 100644 index a1622c0d..00000000 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#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 Renderer2DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - explicit Renderer2DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable2DComponent)}), - _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>(); - auto &pos = entity.getComponent(); - - 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; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/Renderer3DSystem.hpp b/sources/System/Renderer/Renderer3DSystem.hpp deleted file mode 100644 index 8dba98f6..00000000 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#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 Renderer3DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - //! @brief ctor - explicit Renderer3DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent)}), - _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>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition(static_cast(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; - }; -} \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 43dccd7d..bd13441a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -9,45 +9,6 @@ #include #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::vectortextures = { - "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(); }