diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7bc5e5c..f24a0564 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: run: | mkdir build && cd build cmake .. - cmake --build . -t wal_tests + cmake --build . -t unit_tests - name: Run tests run: | - ./build/lib/wal/wal_tests + ./build/unit_tests diff --git a/CMakeLists.txt b/CMakeLists.txt index a3726bdf..4efd04dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,18 +10,44 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +set(SOURCES + sources/Models/GameState.hpp + sources/Runner/Runner.cpp + sources/Runner/Runner.hpp + sources/Component/Position/PositionComponent.cpp + sources/Component/Position/PositionComponent.hpp + sources/Component/Movable/MovableComponent.cpp + sources/Component/Movable/MovableComponent.hpp + sources/System/Movable/MovableSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/Models/Vector3.hpp + sources/Component/Drawable/Drawable3DComponent.hpp + sources/Component/Drawable/Drawable2DComponent.hpp + sources/System/Renderer/Renderer3DSystem.hpp + sources/System/Renderer/Renderer2DSystem.hpp + sources/System/Renderer/RenderScreenSystem.hpp +) + add_executable(bomberman sources/main.cpp - sources/Component/Drawable/Drawable3DComponent.hpp - sources/Component/Drawable/Drawable2DComponent.hpp - sources/Component/Movable/MovableComponent.cpp - sources/Component/Movable/MovableComponent.hpp - sources/Component/Position/PositionComponent.cpp - sources/Component/Position/PositionComponent.hpp - sources/System/Movable/MovableSystem.cpp - sources/System/Movable/MovableSystem.hpp - sources/System/Renderer/Renderer3DSystem.hpp - sources/System/Renderer/Renderer2DSystem.hpp - ) + ${SOURCES}) +target_include_directories(bomberman PUBLIC sources) +target_link_libraries(bomberman PUBLIC wal ray) -target_link_libraries(bomberman wal ray) \ No newline at end of file + +add_executable(unit_tests EXCLUDE_FROM_ALL + ${SOURCES} + tests/EntityTests.cpp + tests/MainTest.cpp + tests/EngineTests.cpp + tests/CallbackTest.cpp +) +target_include_directories(unit_tests PUBLIC sources) +target_link_libraries(unit_tests PUBLIC wal ray) + +find_package(Catch2 QUIET) +if (NOT Catch2_FOUND) + set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/lib/catch2) + find_package(Catch2 REQUIRED) +endif() +target_link_libraries(unit_tests PRIVATE Catch2::Catch2) diff --git a/assets/guy/guy.iqm b/assets/guy/guy.iqm deleted file mode 100644 index 36bed5e0..00000000 Binary files a/assets/guy/guy.iqm and /dev/null differ diff --git a/assets/guy/guytex.png b/assets/guy/guytex.png deleted file mode 100644 index 05a58eea..00000000 Binary files a/assets/guy/guytex.png and /dev/null differ diff --git a/lib/Ray/sources/Camera/Camera3D.cpp b/lib/Ray/sources/Camera/Camera3D.cpp index 2482df6e..aa2b2a9b 100644 --- a/lib/Ray/sources/Camera/Camera3D.cpp +++ b/lib/Ray/sources/Camera/Camera3D.cpp @@ -80,4 +80,4 @@ void RAY::Camera::Camera3D::update(void) RAY::Camera::Camera3D::operator ::Camera3D() const { return this->_camera; -} +} \ No newline at end of file diff --git a/lib/Ray/sources/Drawables/ADrawable2D.hpp b/lib/Ray/sources/Drawables/ADrawable2D.hpp index bfdba5a3..46a9407b 100644 --- a/lib/Ray/sources/Drawables/ADrawable2D.hpp +++ b/lib/Ray/sources/Drawables/ADrawable2D.hpp @@ -10,6 +10,7 @@ #include #include "Vector/Vector2.hpp" +#include "Image.hpp" #include "Drawables/IDrawable.hpp" #include "Color.hpp" diff --git a/lib/Ray/sources/Drawables/IDrawable.hpp b/lib/Ray/sources/Drawables/IDrawable.hpp index 97c0a110..9e3b403b 100644 --- a/lib/Ray/sources/Drawables/IDrawable.hpp +++ b/lib/Ray/sources/Drawables/IDrawable.hpp @@ -8,13 +8,11 @@ #ifndef IDRAWABLE_HPP_ #define IDRAWABLE_HPP_ -#include "Drawables/Image.hpp" #include "Window.hpp" namespace RAY { class Window; - class Image; namespace Drawables { //! @brief Interface for any drawable class IDrawable { diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index a372c7df..6f16418b 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -146,12 +146,7 @@ void RAY::Window::setTitle(const std::string &title) this->_title = title; } -void RAY::Window::draw(RAY::Drawables::ADrawable2D &drawable) -{ - drawable.drawOn(*this); -} - -void RAY::Window::draw(RAY::Drawables::ADrawable3D &drawable) +void RAY::Window::draw(RAY::Drawables::IDrawable &drawable) { drawable.drawOn(*this); } diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index b8508167..0b6d7609 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -10,6 +10,7 @@ #include #include +#include "Drawables/Image.hpp" #include "Vector/Vector2.hpp" #include "Vector/Vector3.hpp" #include "Controllers/Keyboard.hpp" @@ -22,6 +23,7 @@ namespace RAY { class Model; //! @brief Window manager namespace Drawables { + class IDrawable; class ADrawable3D; } class Window { @@ -65,7 +67,7 @@ namespace RAY { bool cursorIsVisible(void) const; //! @brief set the window icon - void setIcon(Image &img); + void setIcon(RAY::Image &img); //! @brief Get the cursor position Vector2 getCursorPosition() const; @@ -113,11 +115,7 @@ namespace RAY { //! @brief draw drawable //! @param drawable The drawable to render on screen - void draw(RAY::Drawables::ADrawable2D &drawable); - - //! @brief draw drawable - //! @param drawable The drawable to render on screen - void draw(RAY::Drawables::ADrawable3D &drawable); + void draw(RAY::Drawables::IDrawable &drawable); //! @brief draw texture at position //! @param texture The object to render diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index b3cc8845..41a455d0 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -4,37 +4,19 @@ project(wal) set(CMAKE_CXX_STANDARD 20) add_library(wal - sources/Entity/Entity.hpp - sources/Component/Component.hpp - sources/System/System.hpp - sources/Wal.cpp - sources/Wal.hpp - sources/Scene/Scene.cpp - sources/Scene/Scene.hpp - sources/Exception/WalError.cpp - sources/Exception/WalError.hpp - sources/Entity/Entity.cpp - sources/Component/Component.cpp - sources/Models/Vector3.hpp - sources/System/System.cpp - sources/Models/Callback.hpp - ) - -target_include_directories(wal PUBLIC sources) - -add_executable(wal_tests EXCLUDE_FROM_ALL - tests/EntityTests.cpp - tests/MainTest.cpp - tests/EngineTests.cpp - tests/CallbackTest.cpp + sources/Entity/Entity.hpp + sources/Component/Component.hpp + sources/System/System.hpp + sources/Wal.cpp + sources/Wal.hpp + sources/Scene/Scene.cpp + sources/Scene/Scene.hpp + sources/Exception/WalError.cpp + sources/Exception/WalError.hpp + sources/Entity/Entity.cpp + sources/Component/Component.cpp + sources/System/System.cpp + sources/Models/Callback.hpp ) -target_link_libraries(wal_tests PRIVATE wal) - -find_package(Catch2 QUIET) -if (NOT Catch2_FOUND) - set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../catch2) - find_package(Catch2 REQUIRED) -endif() - -target_link_libraries(wal_tests PRIVATE Catch2::Catch2) +target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index 26dbe575..be2c092f 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -52,5 +52,11 @@ namespace WAL ~Callback() = default; //! @brief A default assignment operator Callback &operator=(const Callback &) = default; + + //! @brief Implicitly transform a function into a callback. + Callback(std::function callback) // NOLINT(google-explicit-constructor) + { + this->addCallback(callback); + } }; } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Scene/SceneManager.hpp b/lib/wal/sources/Scene/SceneManager.hpp deleted file mode 100644 index a0f017db..00000000 --- a/lib/wal/sources/Scene/SceneManager.hpp +++ /dev/null @@ -1,43 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - - -#pragma once - -#include -#include "Scene/Scene.hpp" - -namespace WAL -{ - //! @brief A class to manage scenes - class SceneManager - { - private: - std::deque _scenes = {}; - public: - //! @brief Add a scene to the container and move to it. - //! @return The manager instance used to call this function is returned. This allow method chaining. - SceneManager &addScene(Scene &&scene); - - //! @brief Add a scene before the current scene. This could be useful for lobbies or scene where the next scene can be constructed. - //! @return The manager instance used to call this function is returned. This allow method chaining. - SceneManager &addBackScene(Scene &&scene); - - //! @brief Get the current scene - Scene &getCurrent(); - - //! @brief Remove the current scene and switch to the previous scene on the stack. - //! @return The manager instance used to call this function is returned. This allow method chaining. - SceneManager &closeCurrent(); - - //! @brief A default constructor - SceneManager() = default; - //! @brief A scene manager is copy constructable - SceneManager(const SceneManager &) = default; - //! @brief A default destructor. - ~SceneManager() = default; - //! @brief A scene manager is assignable - SceneManager &operator=(const SceneManager &) = default; - }; -} diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index a3b73d17..5bab4a43 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -10,28 +10,9 @@ namespace WAL { std::chrono::nanoseconds Wal::timestep = std::chrono::milliseconds(8); - void Wal::run() - { - auto lastTick = std::chrono::steady_clock::now(); - std::chrono::nanoseconds fBehind(0); - - while (!this->_shouldClose) { - auto now = std::chrono::steady_clock::now(); - std::chrono::nanoseconds dtime = now - lastTick; - fBehind += dtime; - lastTick = now; - - while (fBehind > Wal::timestep) { - fBehind -= Wal::timestep; - this->_fixedUpdate(); - } - this->_update(dtime); - } - } - void Wal::_update(std::chrono::nanoseconds dtime) { - auto &entities = this->_scene.getEntities(); + auto &entities = this->scene.getEntities(); for (auto &system : this->_systems) { for (auto &entity : entities) { @@ -45,7 +26,7 @@ namespace WAL void Wal::_fixedUpdate() { - auto &entities = this->_scene.getEntities(); + auto &entities = this->scene.getEntities(); for (auto &system : this->_systems) { for (auto &entity : entities) { diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 18bca212..47c5027f 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -9,10 +9,11 @@ #include #include #include -#include +#include "Exception/WalError.hpp" #include "Scene/Scene.hpp" #include "Entity/Entity.hpp" #include "System/System.hpp" +#include "Models/Callback.hpp" namespace WAL { @@ -20,8 +21,6 @@ namespace WAL class Wal { private: - //! @brief The scene manager that allow multiple scene to work together. - Scene _scene; //! @brief The list of registered systems std::vector> _systems = {}; //! @brief True if the engine should close after the end of the current tick. @@ -39,6 +38,8 @@ namespace WAL //! @return True if all dependencies are met, false otherwise. static bool _hasDependencies(const Entity &entity, const System &system); public: + //! @brief The scene manager that allow multiple scene to work together. + Scene scene; //! @brief The time between each fixed update. static std::chrono::nanoseconds timestep; @@ -101,7 +102,40 @@ namespace WAL } //! @brief Start the game loop - void run(); + //! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here) + //! @param state An initial game state. If not specified, it will be defaulted. + //! @tparam T A type used to track your game state. It must be default constructable. + template + void run(const std::function &callback, T state = T()) + { + Callback update(callback); + return this->run(update, state); + } + + //! @brief Start the game loop + //! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here) + //! @param state An initial game state. If not specified, it will be defaulted. + //! @tparam T A type used to track your game state. It must be default constructable. + template + void run(const Callback &callback, T state = T()) + { + auto lastTick = std::chrono::steady_clock::now(); + std::chrono::nanoseconds fBehind(0); + + while (!this->_shouldClose) { + auto now = std::chrono::steady_clock::now(); + std::chrono::nanoseconds dtime = now - lastTick; + fBehind += dtime; + lastTick = now; + + while (fBehind > Wal::timestep) { + fBehind -= Wal::timestep; + this->_fixedUpdate(); + } + this->_update(dtime); + callback(*this, state); + } + } //! @brief A default constructor Wal() = default; diff --git a/sources/Component/Drawable/Drawable2DComponent.hpp b/sources/Component/Drawable/Drawable2DComponent.hpp index c59e3c0c..0e383701 100644 --- a/sources/Component/Drawable/Drawable2DComponent.hpp +++ b/sources/Component/Drawable/Drawable2DComponent.hpp @@ -13,20 +13,30 @@ namespace BBM class Drawable2DComponent : public WAL::Component { public: - + //! @brief The type of the component T member; + //! ctor explicit Drawable2DComponent(WAL::Entity &entity, T member) : WAL::Component(entity), member(std::move(member)) { } + //! @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); } + //! @brief Default copy ctor + Drawable2DComponent(const Drawable2DComponent &) = default; + //! @brief Default dtor + ~Drawable2DComponent() override = default; + //! @brief Default assignment operator + Drawable2DComponent &operator=(const Drawable2DComponent &) = delete; + }; } \ No newline at end of file diff --git a/sources/Component/Drawable/Drawable3DComponent.hpp b/sources/Component/Drawable/Drawable3DComponent.hpp index 8807d76a..f221e7de 100644 --- a/sources/Component/Drawable/Drawable3DComponent.hpp +++ b/sources/Component/Drawable/Drawable3DComponent.hpp @@ -13,17 +13,28 @@ namespace BBM class Drawable3DComponent : public WAL::Component { public: + //! @brief The type of the component T member; + //! @brief ctor explicit Drawable3DComponent(WAL::Entity &entity, T member) : WAL::Component(entity), member(std::move(member)) { } + //! @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); + return new Drawable3DComponent(entity, this->member); } + + //! @brief Default copy ctor + Drawable3DComponent(const Drawable3DComponent &) = default; + //! @brief Default dtor + ~Drawable3DComponent() override = default; + //! @brief Default assignment operator + Drawable3DComponent &operator=(const Drawable3DComponent &) = delete; }; } \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index dfdc8d5e..4fc9bc0c 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -3,7 +3,6 @@ // #include "MovableComponent.hpp" -#include "Entity/Entity.hpp" namespace BBM { @@ -16,8 +15,8 @@ namespace BBM return new MovableComponent(entity); } - void MovableComponent::addForce(WAL::Vector3f force) + void MovableComponent::addForce(Vector3f force) { this->_acceleration += force; } -} \ No newline at end of file +} // namespace WAL \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index aee21495..7656a960 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -14,13 +14,13 @@ namespace BBM { private: //! @brief The acceleration of this entity. - WAL::Vector3f _acceleration; + Vector3f _acceleration; //! @brief The velocity of the entity. - WAL::Vector3f _velocity; + Vector3f _velocity; public: //! @brief Add an instant force to this entity. //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. - void addForce(WAL::Vector3f force); + void addForce(Vector3f force); //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/Component/Position/PositionComponent.cpp b/sources/Component/Position/PositionComponent.cpp index 0fcb7f70..b1b88f43 100644 --- a/sources/Component/Position/PositionComponent.cpp +++ b/sources/Component/Position/PositionComponent.cpp @@ -3,23 +3,21 @@ // #include "PositionComponent.hpp" -#include "Entity/Entity.hpp" -#include "Component/Component.hpp" namespace BBM { PositionComponent::PositionComponent(WAL::Entity &entity) - : WAL::Component(entity), + : Component(entity), position() {} - PositionComponent::PositionComponent(WAL::Entity &entity, WAL::Vector3f pos) - : WAL::Component(entity), + PositionComponent::PositionComponent(WAL::Entity &entity, Vector3f pos) + : Component(entity), position(pos) {} PositionComponent::PositionComponent(WAL::Entity &entity, float x, float y, float z) - : WAL::Component(entity), + : Component(entity), position(x, y, z) {} @@ -42,4 +40,4 @@ namespace BBM { return this->position.z; } -} \ No newline at end of file +} // namespace WAL \ No newline at end of file diff --git a/sources/Component/Position/PositionComponent.hpp b/sources/Component/Position/PositionComponent.hpp index 647314fd..0a34a8ca 100644 --- a/sources/Component/Position/PositionComponent.hpp +++ b/sources/Component/Position/PositionComponent.hpp @@ -14,7 +14,7 @@ namespace BBM { public: //! @brief Get the editable position of this entity - WAL::Vector3f position; + Vector3f position; //! @brief Get the X position of this entity. float getX() const; @@ -29,7 +29,7 @@ namespace BBM //! @brief Create a new PositionComponent linked to a specific entity explicit PositionComponent(WAL::Entity &entity); //! @brief Create a new PositionComponent at a certain position - PositionComponent(WAL::Entity &entity, WAL::Vector3f pos); + PositionComponent(WAL::Entity &entity, Vector3f pos); //! @brief Create a new PositionComponent at a certain position PositionComponent(WAL::Entity &entity, float x, float y, float z); //! @brief A position component is copy constructable diff --git a/sources/Models/GameState.hpp b/sources/Models/GameState.hpp new file mode 100644 index 00000000..883578bf --- /dev/null +++ b/sources/Models/GameState.hpp @@ -0,0 +1,31 @@ +// +// Created by Zoe Roux on 5/24/21. +// + + +#pragma once + +#include +#include + + +namespace BBM +{ + //! @brief A class representing the current game state. This allow one to retain information between update calls. + class GameState + { + //! @brief The list of scenes available. + enum SceneID + { + MainMenu, + GameScene + }; + + + //! @brief The currently loaded scene + SceneID currentScene = MainMenu; + + //! @brief The list of loaded scenes. + std::unordered_map _loadedScenes = {}; + }; +} \ No newline at end of file diff --git a/lib/wal/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp similarity index 94% rename from lib/wal/sources/Models/Vector3.hpp rename to sources/Models/Vector3.hpp index 0a43bbe7..93327c70 100644 --- a/lib/wal/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -7,8 +7,9 @@ #include #include +#include "Vector/Vector3.hpp" -namespace WAL +namespace BBM { //! @brief A Vector3 data type. (templated to allow any kind of vector3) template @@ -152,15 +153,22 @@ namespace WAL { return (point * this) / std::pow(this->magnitude(), 2) * this; } + + explicit operator RAY::Vector3() const { return {this->x, this->y, this->z};} }; typedef Vector3 Vector3f; typedef Vector3 Vector3u; typedef Vector3 Vector3i; -} // namespace WAL + + +} + + + template -std::ostream &operator<<(std::ostream &s, const WAL::Vector3 &v) +std::ostream &operator<<(std::ostream &s, const BBM::Vector3 &v) { s << "Vector3<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ", " << v.z << ")"; return s; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp new file mode 100644 index 00000000..ff2dc933 --- /dev/null +++ b/sources/Runner/Runner.cpp @@ -0,0 +1,32 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#include +#include +#include "Runner.hpp" +#include "Models/GameState.hpp" + +namespace BBM +{ + void updateState(WAL::Wal &engine, GameState &state) + { + // You can change the scene here or update the game state based on entities values. + + // If you want to keep a scene loaded but not running, store it in the state.loadedScenes. + // If you don't need the scene anymore, remember to remove it from the loadedScene array. + } + + int run() + { + WAL::Wal wal; + + try { + wal.run(updateState); + return 0; + } catch (const std::exception &ex) { + std::cerr << ex.what() << std::endl; + return 1; + } + } +} \ No newline at end of file diff --git a/sources/Runner/Runner.hpp b/sources/Runner/Runner.hpp new file mode 100644 index 00000000..0f622f11 --- /dev/null +++ b/sources/Runner/Runner.hpp @@ -0,0 +1,12 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#pragma once + +namespace BBM +{ + //! @brief Start the game and run a Bomberman. + //! @return 0 on success, another value on error. + int run(); +} \ No newline at end of file diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index 34d5b300..5b57ebba 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -23,6 +23,6 @@ namespace BBM position.position += movable._velocity * WAL::Wal::timestep.count(); movable._velocity = movable._acceleration * WAL::Wal::timestep.count(); - movable._acceleration = WAL::Vector3f(); + movable._acceleration = Vector3f(); } } // namespace WAL \ No newline at end of file diff --git a/sources/System/Movable/MovableSystem.hpp b/sources/System/Movable/MovableSystem.hpp index 62c69ddb..51a56ea9 100644 --- a/sources/System/Movable/MovableSystem.hpp +++ b/sources/System/Movable/MovableSystem.hpp @@ -11,7 +11,7 @@ namespace BBM { //! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity. -class MovableSystem : public WAL::System + class MovableSystem : public WAL::System { public: //! @inherit diff --git a/sources/System/Renderer/RenderScreenSystem.hpp b/sources/System/Renderer/RenderScreenSystem.hpp new file mode 100644 index 00000000..04f8e5e2 --- /dev/null +++ b/sources/System/Renderer/RenderScreenSystem.hpp @@ -0,0 +1,48 @@ +// +// Created by cbihan on 26/05/2021. +// + +#pragma once + +#include "System/System.hpp" +#include "Camera/Camera2D.hpp" +#include "Window.hpp" + +namespace BBM +{ + template + class RenderScreenSystem : public WAL::System + { + //! @brief The window to render on + RAY::Window &_window; + //! @brief The camera + T &_camera; + public: + //! @brief ctor + explicit RenderScreenSystem(RAY::Window &window, T &camera) + : WAL::System({}), + _window(window), + _camera(camera) + { + } + + //! @brief A method called after all entities that this system manage has been updated. + //! @note render on screen here + void onSelfUpdate() override + { + this->_window.unuseCamera(); + this->_window.setDrawingState(RAY::Window::IDLE); + this->_window.setDrawingState(RAY::Window::DRAWING); + this->_window.clear(); + this->_window.useCamera(_camera); + } + + //! @brief Default copy ctor + RenderScreenSystem(const RenderScreenSystem &) = default; + //! @brief Default dtor + ~RenderScreenSystem() override = default; + //! @brief Default assignment operator + RenderScreenSystem &operator=(const RenderScreenSystem &) = default; + }; + +} diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp index 69e94e96..59518e68 100644 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ b/sources/System/Renderer/Renderer2DSystem.hpp @@ -26,17 +26,23 @@ namespace BBM { } - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override + //! @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.setPosition({pos.getX(), pos.getY()}); - comp.drawOn(this->_window); + 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 index 83e91ff3..dd55e584 100644 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ b/sources/System/Renderer/Renderer3DSystem.hpp @@ -20,23 +20,30 @@ namespace BBM //! @brief The class to render RAY::Window &_window; public: + //! @brief ctor explicit Renderer3DSystem(RAY::Window &window) : WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent)}), _window(window) { } - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override + //! @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); + 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/Util/vector.cpp b/sources/Util/vector.cpp deleted file mode 100644 index ff4c15e9..00000000 --- a/sources/Util/vector.cpp +++ /dev/null @@ -1,14 +0,0 @@ -/* -** EPITECH PROJECT, 2021 -** Bomberman -** File description: -** Vector -*/ - -#include "Vector/Vector3.hpp" -#include "Models/Vector3.hpp" - -RAY::Vector3 toRAY(const WAL::Vector3f &wal) -{ - return RAY::Vector3(wal.x, wal.y, wal.y); -} \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index f1aaf9fa..5abe9795 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -6,21 +6,31 @@ */ -#include "Wal.hpp" +#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/Drawable/Drawable3DComponent.hpp" #include "Component/Drawable/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", "orange", "purple", "red", "yellow" @@ -35,10 +45,9 @@ std::string get_full_path(const std::string &color) return path; } -int main() +int demo() { - // Initialization - //-------------------------------------------------------------------------------------- + WAL::Wal wal; const int screenWidth = 800; const int screenHeight = 450; std::vector::const_iterator iterator = textures.begin(); @@ -48,10 +57,19 @@ int main() RAY::Image icon("assets/icon.png"); RAY::Model model(modelPath); RAY::Camera::Camera3D camera(RAY::Vector3(10.0f, 10.0f, 10.0f), - RAY::Vector3(0.0f, 0.0f, 0.0f), - RAY::Vector3(0.0f, 1.0f, 0.0f), - 45.0f, CAMERA_PERSPECTIVE - ); + RAY::Vector3(0.0f, 0.0f, 0.0f), + RAY::Vector3(0.0f, 1.0f, 0.0f), + 45.0f, CAMERA_PERSPECTIVE + ); + WAL::Entity entityPlayer("roger"); + RAY::Drawables::Drawables3D::Circle circle({0, 0, 0}, 5, MAROON, {0, 0, 0}, 0); + BBM::Drawable3DComponent circleComponent(entityPlayer, circle); + + BBM::Renderer3DSystem circleSystem(window); + + wal.addSystem(circleSystem); + entityPlayer.addComponent(circleComponent); + RAY::Texture texture(get_full_path(*iterator)); RAY::ModelAnimations animations(modelPath); RAY::Drawables::Drawables3D::Grid grid(10, 1.0f); @@ -64,14 +82,10 @@ int main() camera.setMode(CAMERA_FREE); // Set free camera mode float y_rotation = 0; - window.setFPS(60); // Set our game to run at 60 frames-per-second - //-------------------------------------------------------------------------------------- + window.setFPS(60); - // Main game loop - while (!window.shouldClose()) // Detect window close button or ESC key + while (!window.shouldClose()) { - // Update - //---------------------------------------------------------------------------------- camera.update(); // Play animation when spacebar is held down @@ -99,33 +113,39 @@ int main() animationIndex = ++animationIndex % animations.getAnimationsCount(); model.setAnimation(animations[animationIndex]); } - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- window.setDrawingState(RAY::Window::DRAWING); - window.clear(); - window.useCamera(camera); window.draw(model, position, RAY::Vector3(1.0f, 20, 0.0f), -180.0f, RAY::Vector3( 3.0f, 3.0f, 3.0f )); window.draw(grid); - + window.draw(circle); window.unuseCamera(); - window.draw(instructionText); - window.setDrawingState(RAY::Window::IDLE); - //---------------------------------------------------------------------------------- } + window.close(); - // De-Initialization - //-------------------------------------------------------------------------------------- - - window.close(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- return 0; } + + +void usage(const std::string &bin) +{ + std::cout << "Bomberman." << std::endl + << "\tUsage: " << bin << " [options]" << std::endl + << "Options:" << std::endl + << "\t-h:\tPrint this help message" << std::endl; +} + +int main(int argc, char **argv) +{ + if (argc == 2 && std::string(argv[1]) == "-h") { + usage(argv[0]); + return 1; + } + return demo(); + return BBM::run(); +} diff --git a/lib/wal/tests/CallbackTest.cpp b/tests/CallbackTest.cpp similarity index 100% rename from lib/wal/tests/CallbackTest.cpp rename to tests/CallbackTest.cpp diff --git a/lib/wal/tests/EngineTests.cpp b/tests/EngineTests.cpp similarity index 100% rename from lib/wal/tests/EngineTests.cpp rename to tests/EngineTests.cpp diff --git a/lib/wal/tests/EntityTests.cpp b/tests/EntityTests.cpp similarity index 100% rename from lib/wal/tests/EntityTests.cpp rename to tests/EntityTests.cpp index ae9c1900..f58fd0c1 100644 --- a/lib/wal/tests/EntityTests.cpp +++ b/tests/EntityTests.cpp @@ -6,8 +6,8 @@ #include "Component/Position/PositionComponent.hpp" #include -using namespace BBM; using namespace WAL; +using namespace BBM; TEST_CASE("Component", "[Entity]") { diff --git a/lib/wal/tests/MainTest.cpp b/tests/MainTest.cpp similarity index 100% rename from lib/wal/tests/MainTest.cpp rename to tests/MainTest.cpp