From 54fe8005492a912bedb483d33d418e5b140a8a62 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 24 May 2021 14:59:28 +0200 Subject: [PATCH 1/3] 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/3] 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/3] 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