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(); }