From 54fe8005492a912bedb483d33d418e5b140a8a62 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 24 May 2021 14:59:28 +0200 Subject: [PATCH 1/4] Adding a state and scene utils --- CMakeLists.txt | 4 +++ lib/wal/sources/Models/Callback.hpp | 6 +++++ lib/wal/sources/Wal.cpp | 19 --------------- lib/wal/sources/Wal.hpp | 38 +++++++++++++++++++++++++++-- sources/Models/GameState.hpp | 31 +++++++++++++++++++++++ sources/Runner/Runner.cpp | 32 ++++++++++++++++++++++++ sources/Runner/Runner.hpp | 12 +++++++++ sources/main.cpp | 26 +++++++++++--------- 8 files changed, 136 insertions(+), 32 deletions(-) create mode 100644 sources/Models/GameState.hpp create mode 100644 sources/Runner/Runner.cpp create mode 100644 sources/Runner/Runner.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cd7e892..302671f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,11 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_executable(bomberman sources/main.cpp + sources/Models/GameState.hpp + sources/Runner/Runner.cpp + sources/Runner/Runner.hpp ) +target_include_directories(bomberman PUBLIC sources) find_package(raylib QUIET) if (NOT raylib_FOUND) diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index 9cce38cd..334d39a7 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -50,5 +50,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); + } }; } \ No newline at end of file diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 45d2caff..126f78dd 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -12,25 +12,6 @@ namespace WAL { std::chrono::nanoseconds Wal::timestep = 8ms; - 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 f1b61f6f..9f560ac2 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -8,10 +8,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/Models/GameState.hpp b/sources/Models/GameState.hpp new file mode 100644 index 00000000..7be6b723 --- /dev/null +++ b/sources/Models/GameState.hpp @@ -0,0 +1,31 @@ +// +// Created by Zoe Roux on 5/24/21. +// + + +#pragma once + +#include +#include + + +namespace Bomberman +{ + //! @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/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp new file mode 100644 index 00000000..837831cc --- /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 Bomberman +{ + 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..4531039a --- /dev/null +++ b/sources/Runner/Runner.hpp @@ -0,0 +1,12 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#pragma once + +namespace Bomberman +{ + //! @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 3096203d..8f963fb4 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -7,17 +7,21 @@ #include -#include +#include "Runner/Runner.hpp" -int main() +void usage(const std::string &bin) { - WAL::Wal wal; - - try { - wal.run(); - return 0; - } catch (const std::exception &ex) { - std::cerr << ex.what() << std::endl; - return 84; - } + 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 Bomberman::run(); } From 851e4e50e042ed050c21afc0519dcdcabd5465ba Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 24 May 2021 15:29:54 +0200 Subject: [PATCH 2/4] Fixing catch path --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22171362..3c6cf9dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,7 @@ target_link_libraries(unit_tests PUBLIC wal ray) find_package(Catch2 QUIET) if (NOT Catch2_FOUND) - set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../catch2) + set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/lib/catch2) find_package(Catch2 REQUIRED) endif() target_link_libraries(unit_tests PRIVATE Catch2::Catch2) From 22f61db1b3f3f3a94ca946e586b6b9a3eb58ed24 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 24 May 2021 15:34:03 +0200 Subject: [PATCH 3/4] Fixing tests ci --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 28aca385..f24a0564 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,4 +22,4 @@ jobs: cmake --build . -t unit_tests - name: Run tests run: | - ./unit_tests + ./build/unit_tests From 6deade8ddc6211be4ad1275601cd2a24a8d8bd90 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 24 May 2021 17:15:24 +0200 Subject: [PATCH 4/4] Fixing namespace --- sources/Component/Movable/MovableComponent.cpp | 2 +- sources/Component/Movable/MovableComponent.hpp | 2 +- sources/Component/Position/PositionComponent.cpp | 2 +- sources/Component/Position/PositionComponent.hpp | 2 +- sources/Models/GameState.hpp | 2 +- sources/Models/Vector3.hpp | 4 ++-- sources/Runner/Runner.cpp | 2 +- sources/Runner/Runner.hpp | 2 +- sources/System/Movable/MovableSystem.cpp | 2 +- sources/System/Movable/MovableSystem.hpp | 2 +- sources/main.cpp | 2 +- tests/EngineTests.cpp | 2 +- tests/EntityTests.cpp | 2 +- 13 files changed, 14 insertions(+), 14 deletions(-) diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index c0e9e52d..2bbbde92 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -4,7 +4,7 @@ #include "MovableComponent.hpp" -namespace Bomberman +namespace BBM { MovableComponent::MovableComponent(WAL::Entity &entity) : Component(entity) diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index 2f194378..5266232a 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -7,7 +7,7 @@ #include "Models/Vector3.hpp" #include "Entity/Entity.hpp" -namespace Bomberman +namespace BBM { //! @brief A component to place on entities that can move or be moved. class MovableComponent : public WAL::Component diff --git a/sources/Component/Position/PositionComponent.cpp b/sources/Component/Position/PositionComponent.cpp index d992a4a5..c237f845 100644 --- a/sources/Component/Position/PositionComponent.cpp +++ b/sources/Component/Position/PositionComponent.cpp @@ -4,7 +4,7 @@ #include "PositionComponent.hpp" -namespace Bomberman +namespace BBM { PositionComponent::PositionComponent(WAL::Entity &entity) : Component(entity), diff --git a/sources/Component/Position/PositionComponent.hpp b/sources/Component/Position/PositionComponent.hpp index 18c5faee..368934eb 100644 --- a/sources/Component/Position/PositionComponent.hpp +++ b/sources/Component/Position/PositionComponent.hpp @@ -7,7 +7,7 @@ #include "Models/Vector3.hpp" #include "Component/Component.hpp" -namespace Bomberman +namespace BBM { //! @brief A basic position component class PositionComponent : public WAL::Component diff --git a/sources/Models/GameState.hpp b/sources/Models/GameState.hpp index 7be6b723..883578bf 100644 --- a/sources/Models/GameState.hpp +++ b/sources/Models/GameState.hpp @@ -9,7 +9,7 @@ #include -namespace Bomberman +namespace BBM { //! @brief A class representing the current game state. This allow one to retain information between update calls. class GameState diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index ab642e94..094f6da9 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -8,7 +8,7 @@ #include #include -namespace Bomberman +namespace BBM { //! @brief A Vector3 data type. (templated to allow any kind of vector3) template @@ -160,7 +160,7 @@ namespace Bomberman } template -std::ostream &operator<<(std::ostream &s, const Bomberman::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 index 837831cc..ff2dc933 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -7,7 +7,7 @@ #include "Runner.hpp" #include "Models/GameState.hpp" -namespace Bomberman +namespace BBM { void updateState(WAL::Wal &engine, GameState &state) { diff --git a/sources/Runner/Runner.hpp b/sources/Runner/Runner.hpp index 4531039a..0f622f11 100644 --- a/sources/Runner/Runner.hpp +++ b/sources/Runner/Runner.hpp @@ -4,7 +4,7 @@ #pragma once -namespace Bomberman +namespace BBM { //! @brief Start the game and run a Bomberman. //! @return 0 on success, another value on error. diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index d8b88a48..2f699ece 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -7,7 +7,7 @@ #include "Component/Movable/MovableComponent.hpp" #include "Wal.hpp" -namespace Bomberman +namespace BBM { MovableSystem::MovableSystem() : System({ diff --git a/sources/System/Movable/MovableSystem.hpp b/sources/System/Movable/MovableSystem.hpp index 0bb89652..08555d26 100644 --- a/sources/System/Movable/MovableSystem.hpp +++ b/sources/System/Movable/MovableSystem.hpp @@ -7,7 +7,7 @@ #include "System/System.hpp" -namespace Bomberman +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 diff --git a/sources/main.cpp b/sources/main.cpp index d29f7374..cd4efc41 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -107,5 +107,5 @@ int main(int argc, char **argv) return 1; } return demo(); - return Bomberman::run(); + return BBM::run(); } diff --git a/tests/EngineTests.cpp b/tests/EngineTests.cpp index 8c908dac..804b6050 100644 --- a/tests/EngineTests.cpp +++ b/tests/EngineTests.cpp @@ -8,7 +8,7 @@ #include using namespace WAL; -using namespace Bomberman; +using namespace BBM; TEST_CASE("Create system", "[Engine][System]") { diff --git a/tests/EntityTests.cpp b/tests/EntityTests.cpp index 3b009cad..f58fd0c1 100644 --- a/tests/EntityTests.cpp +++ b/tests/EntityTests.cpp @@ -7,7 +7,7 @@ #include using namespace WAL; -using namespace Bomberman; +using namespace BBM; TEST_CASE("Component", "[Entity]") {