From 34cbe24c197e71e25fe1cd0e4eeaec689f27e026 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 2 Jun 2021 17:54:16 +0200 Subject: [PATCH] remove unwanted defines + upade encapsulation for window drawing function --- lib/Ray/sources/Window.cpp | 8 ++- lib/Ray/sources/Window.hpp | 9 ++-- .../System/Renderer/Render2DScreenSystem.cpp | 21 -------- .../System/Renderer/Render2DScreenSystem.hpp | 30 ------------ .../System/Renderer/RenderScreenSystem.cpp | 40 --------------- sources/System/Renderer/RenderSystem.cpp | 12 ++--- sources/System/Renderer/Renderer2DSystem.hpp | 49 ------------------- sources/System/Renderer/Renderer3DSystem.hpp | 48 ------------------ 8 files changed, 17 insertions(+), 200 deletions(-) delete mode 100644 sources/System/Renderer/Render2DScreenSystem.cpp delete mode 100644 sources/System/Renderer/Render2DScreenSystem.hpp delete mode 100644 sources/System/Renderer/RenderScreenSystem.cpp delete mode 100644 sources/System/Renderer/Renderer2DSystem.hpp delete mode 100644 sources/System/Renderer/Renderer3DSystem.hpp diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index 9deba6e0..c89c6363 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -107,10 +107,14 @@ void RAY::Window::clear(const RAY::Color &color) ClearBackground(color); } -void RAY::Window::draw() +void RAY::Window::beginDrawing() +{ + BeginDrawing(); +} + +void RAY::Window::endDrawing() { EndDrawing(); - BeginDrawing(); } void RAY::Window::useCamera(RAY::Camera::Camera2D &camera) diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index c16eae4b..a02c78f9 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -7,7 +7,7 @@ #ifndef WINDOW_HPP_ #define WINDOW_HPP_ -#define INTERNAL public + #include #include #include @@ -102,8 +102,10 @@ namespace RAY { NONE, }; - //! @brief Draw the content of the buffer on the screen. - void draw(); + //! @brief Setup canvas (framebuffer) to start drawing + void beginDrawing(); + //! @brief End canvas drawing and swap buffers (double buffering) + void endDrawing(); //! @brief Initialize 2D mode with custom camera (2D) void useCamera(Camera::Camera2D &camera); @@ -131,6 +133,7 @@ namespace RAY { //! @brief Draw a 3d mesh with material and transform void draw(const Mesh &mesh, const Material &material, const Matrix &transform); + //! @return true if the window's context has been correctly initialized bool isReady() const; diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp deleted file mode 100644 index b21c35da..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "Render2DScreenSystem.hpp" - -namespace BBM -{ - Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window) - : WAL::System({}), - _window(window) - {} - - void Render2DScreenSystem::onSelfUpdate() - { - EndMode3D(); - printf("EndMode3D\n"); - DrawText("Try selecting the box with mouse!", 10, 10, 20, WHITE); - //this->_window.unuseCamera(); - } -} \ 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 4fa00ba1..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ /dev/null @@ -1,30 +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; - 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 1550bd65..00000000 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ /dev/null @@ -1,40 +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) - { - this->_camera.setMode(CAMERA_FREE); - } - - void RenderScreenSystem::onSelfUpdate() - { - //this->_window.draw(); - //EndMode2D(); - EndDrawing(); - this->_camera.update(); - BeginDrawing(); - this->_window.clear(); - BeginMode3D(this->_camera); - printf("BeginMode3D\n"); - } - - void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) - { - const auto &pos = entity.getComponent(); - _camera.setPosition(pos.position); - //this->_camera.update(); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 99a94f0e..5073ee30 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -2,8 +2,6 @@ // Created by Zoe Roux on 5/27/21. // -#undef INTERNAL -#define INTERNAL public #include #include "Models/Vector2.hpp" #include "RenderSystem.hpp" @@ -28,10 +26,10 @@ namespace BBM void RenderSystem::onSelfUpdate() { this->_camera.update(); - BeginDrawing(); - ClearBackground(BLACK); + this->_window.beginDrawing(); + this->_window.clear(); - BeginMode3D(this->_camera); + this->_window.useCamera(this->_camera); for (auto &entity : this->_wal.scene->getEntities()) { if (!entity.hasComponent() || !entity.hasComponent()) @@ -42,7 +40,7 @@ namespace BBM drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); } - EndMode3D(); + this->_window.unuseCamera(); // TODO sort entities based on the Z axis for (auto &entity : this->_wal.scene->getEntities()) { @@ -55,7 +53,7 @@ namespace BBM drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); drawable.drawable->drawOn(this->_window); } - EndDrawing(); + this->_window.endDrawing(); } void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp deleted file mode 100644 index 3fd55346..00000000 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ /dev/null @@ -1,49 +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()}); - this->_window.draw(comp.member); - printf("Drawing smth\n"); - } - - //! @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 0fa3a9bb..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)); - this->_window.draw(comp.member); - } - - //! @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