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..3f93d13f 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) -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 - ) +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 +) -target_link_libraries(bomberman wal ray) \ No newline at end of file +add_executable(bomberman + sources/main.cpp + ${SOURCES} +) +target_include_directories(bomberman PUBLIC sources) +target_link_libraries(bomberman PUBLIC wal ray) + + +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/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..2f90a19f 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -10,25 +10,6 @@ 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(); diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 18bca212..c7c02e9d 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 { @@ -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/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index dfdc8d5e..e69de29b 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -1,23 +0,0 @@ -// -// Created by Zoe Roux on 5/17/21. -// - -#include "MovableComponent.hpp" -#include "Entity/Entity.hpp" - -namespace BBM -{ - MovableComponent::MovableComponent(WAL::Entity &entity) - : Component(entity) - {} - - WAL::Component *MovableComponent::clone(WAL::Entity &entity) const - { - return new MovableComponent(entity); - } - - void MovableComponent::addForce(WAL::Vector3f force) - { - this->_acceleration += force; - } -} \ No newline at end of file diff --git a/sources/Component/Position/PositionComponent.cpp b/sources/Component/Position/PositionComponent.cpp index 0fcb7f70..e69de29b 100644 --- a/sources/Component/Position/PositionComponent.cpp +++ b/sources/Component/Position/PositionComponent.cpp @@ -1,45 +0,0 @@ -// -// Created by Zoe Roux on 5/17/21. -// - -#include "PositionComponent.hpp" -#include "Entity/Entity.hpp" -#include "Component/Component.hpp" - -namespace BBM -{ - PositionComponent::PositionComponent(WAL::Entity &entity) - : WAL::Component(entity), - position() - {} - - PositionComponent::PositionComponent(WAL::Entity &entity, WAL::Vector3f pos) - : WAL::Component(entity), - position(pos) - {} - - PositionComponent::PositionComponent(WAL::Entity &entity, float x, float y, float z) - : WAL::Component(entity), - position(x, y, z) - {} - - WAL::Component *PositionComponent::clone(WAL::Entity &entity) const - { - return new PositionComponent(entity, this->position); - } - - float PositionComponent::getX() const - { - return this->position.x; - } - - float PositionComponent::getY() const - { - return this->position.y; - } - - float PositionComponent::getZ() const - { - return this->position.z; - } -} \ No newline at end of file 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 97% rename from lib/wal/sources/Models/Vector3.hpp rename to sources/Models/Vector3.hpp index 0a43bbe7..81a61cf3 100644 --- a/lib/wal/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -8,7 +8,7 @@ #include #include -namespace WAL +namespace BBM { //! @brief A Vector3 data type. (templated to allow any kind of vector3) template @@ -160,7 +160,7 @@ namespace WAL } // 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/main.cpp b/sources/main.cpp index 55095958..aa9c8d8f 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -6,7 +6,10 @@ */ -#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" @@ -35,7 +38,7 @@ std::string get_full_path(const std::string &color) return path; } -int main() +int demo() { WAL::Wal wal; const int screenWidth = 800; @@ -50,10 +53,10 @@ int main() window.setIcon(icon); 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({300, 300, 300}, 50, 0XFFFFFFF, {0, 0, 0}, 0); BBM::Drawable3DComponent circleComponent(entityPlayer, circle); @@ -105,3 +108,22 @@ int main() 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 98% rename from lib/wal/tests/EntityTests.cpp rename to tests/EntityTests.cpp index ae9c1900..5e1edb86 100644 --- a/lib/wal/tests/EntityTests.cpp +++ b/tests/EntityTests.cpp @@ -8,6 +8,7 @@ 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