From 00abbbebbd0ad989be59602e5b01dc9f50e8f991 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Sun, 12 Dec 2021 23:36:13 -0500 Subject: [PATCH] Fixed an issue with lua context manager, tested to make sure it worked, started a document for the algorithm lua structure, updated engine loop --- documents/algorithms.txt | 14 ++++++++++++++ src/engine/engine.cpp | 3 ++- src/engine/scripting/luacontextmanager.cpp | 9 +++++++++ src/engine/scripting/luacontextmanager.hpp | 13 ++++++++++++- src/game/dungeonalgorithm.hpp | 2 ++ src/game/visualizer.cpp | 14 ++++++++++++++ src/game/visualizer.hpp | 3 +++ 7 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 documents/algorithms.txt diff --git a/documents/algorithms.txt b/documents/algorithms.txt new file mode 100644 index 0000000..b2a8829 --- /dev/null +++ b/documents/algorithms.txt @@ -0,0 +1,14 @@ +Dungeon generation algorithms are written in lua and dropped into scripting->algorithms. They are automatically parsed by the visualizer and assigned keys in the program to run them. +Algorithms are made up of a single lua file (possibly more with requires) with the following functions: + +initialize() +Initializes the algorithm and returns a structure/table with the following fields: +{ + updates_per_second: int // Number of times the algorithm's update function should be called per second. Minimum of 1, converted into frames per update with a minimum of 1. + free_data: table // Free table that the developer can store necessary state data in +} + +update(state) + +The following functions are available to algorithms in either their update or initialize functions: + diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index e3f36e6..a81cff5 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -33,10 +33,11 @@ void Engine::start_loop() { } game->render(renderer.get()); auto endtime = std::chrono::high_resolution_clock::now(); + auto update_duration = std::chrono::duration_cast(endtime - starttime); last_frame = render_fps(endtime, last_frame); renderer->flush(); // not technically correct to do this here, but is done so we can put the fps over the display - std::this_thread::sleep_for(duration_per_frame); + std::this_thread::sleep_for(duration_per_frame - update_duration); } } diff --git a/src/engine/scripting/luacontextmanager.cpp b/src/engine/scripting/luacontextmanager.cpp index fedb0af..4a30fb8 100644 --- a/src/engine/scripting/luacontextmanager.cpp +++ b/src/engine/scripting/luacontextmanager.cpp @@ -4,3 +4,12 @@ #include "luacontextmanager.hpp" +std::string LuaContextManager::get_key(std::string args) { + return args; +} + +std::shared_ptr LuaContextManager::load_resource(std::string args) { + auto lua = std::make_shared(); + lua->open_libraries(sol::lib::base, sol::lib::package); + return lua; +} diff --git a/src/engine/scripting/luacontextmanager.hpp b/src/engine/scripting/luacontextmanager.hpp index 669467e..80d17df 100644 --- a/src/engine/scripting/luacontextmanager.hpp +++ b/src/engine/scripting/luacontextmanager.hpp @@ -5,11 +5,22 @@ #ifndef RLA_IIPP_LUACONTEXTMANAGER_HPP #define RLA_IIPP_LUACONTEXTMANAGER_HPP #include +#include "../resources/resourcemanager.hpp" +#define DEFAULT_CONTEXT_KEY "default" -class LuaContextManager { +class LuaContextManager : public ResourceManager, std::string, std::string> { public: + std::shared_ptr get_default_context() { + return fetch_resource(DEFAULT_CONTEXT_KEY); + } + void clear_default_context() { + clear_resource(DEFAULT_CONTEXT_KEY); + } +protected: + std::string get_key(std::string args) override; + std::shared_ptr load_resource(std::string args) override; }; diff --git a/src/game/dungeonalgorithm.hpp b/src/game/dungeonalgorithm.hpp index d2e60d4..b68a53b 100644 --- a/src/game/dungeonalgorithm.hpp +++ b/src/game/dungeonalgorithm.hpp @@ -12,6 +12,8 @@ struct DungeonAlgorithm { std::string path; std::optional algorithm_data; + const int frames_per_update; + int frames_until_update=0; }; #endif //RLA_IIPP_DUNGEONALGORITHM_HPP diff --git a/src/game/visualizer.cpp b/src/game/visualizer.cpp index a60fc08..ecebb1a 100644 --- a/src/game/visualizer.cpp +++ b/src/game/visualizer.cpp @@ -20,6 +20,14 @@ bool Visualizer::update(InputResult input) { this->y = std::clamp(this->y, 0, (int)this->tile_map.get_height()-1); this->camera.move_camera(Point{x,y}); + if(current_algorithm.has_value()) { + current_algorithm->frames_until_update -= 1; + if(current_algorithm->frames_until_update <= 0) { + update_algorithm(current_algorithm.value()); + current_algorithm->frames_until_update = current_algorithm->frames_per_update; + } + } + return false; } @@ -53,6 +61,8 @@ void Visualizer::initialize(GameInitArgs args) { int tilesy = (window_height / TILE_HEIGHT)+1; initialize_map(MAP_SIZE_W, MAP_SIZE_H); this->camera = Camera(Point{x,y}, tilesx, tilesy, tile_map.get_width(), tile_map.get_height()); + + } void Visualizer::initialize_map(int width, int height) { @@ -64,4 +74,8 @@ void Visualizer::initialize_map(int width, int height) { } } +void Visualizer::update_algorithm(DungeonAlgorithm algorithm) { + +} + Visualizer::~Visualizer() = default; diff --git a/src/game/visualizer.hpp b/src/game/visualizer.hpp index b6633ab..f750bf4 100644 --- a/src/game/visualizer.hpp +++ b/src/game/visualizer.hpp @@ -6,6 +6,7 @@ #include "../engine/utility/camera.hpp" #include "../engine/utility/grid2d.hpp" #include "../engine/scripting/luacontextmanager.hpp" +#include "dungeonalgorithm.hpp" #include #include #include @@ -36,12 +37,14 @@ private: Grid2D tile_map = Grid2D(0, 0); Camera camera = Camera(); LuaContextManager lua_context_manager = LuaContextManager(); + std::optional current_algorithm; int x = 0; int y=0; int sprite_index = 1; void initialize_map(int width, int height); + void update_algorithm(DungeonAlgorithm algorithm); };