diff --git a/src/engine.cpp b/src/engine.cpp index f1d0665e..f8d88015 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -165,6 +165,12 @@ void Engine::mainloop() { } else { Window::swapInterval(1); } + + while (!postRunnables.empty()) { + postRunnables.front()(); + postRunnables.pop(); + } + Window::swapBuffers(); Events::pollEvents(); } @@ -311,3 +317,7 @@ ResPaths* Engine::getResPaths() { std::shared_ptr Engine::getScreen() { return screen; } + +void Engine::postRunnable(runnable callback) { + postRunnables.push(callback); +} diff --git a/src/engine.h b/src/engine.h index 9545efdf..b8509cef 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,19 +1,22 @@ #ifndef SRC_ENGINE_H_ #define SRC_ENGINE_H_ -#include -#include -#include -#include -#include -#include "typedefs.h" +#include "delegates.h" #include "settings.h" +#include "typedefs.h" #include "assets/Assets.h" #include "content/Content.h" #include "content/ContentPack.h" #include "files/engine_paths.h" +#include +#include +#include +#include +#include +#include + class Level; class Screen; class EnginePaths; @@ -39,6 +42,7 @@ class Engine { std::vector contentPacks; std::unique_ptr content = nullptr; std::unique_ptr resPaths = nullptr; + std::queue postRunnables; uint64_t frame = 0; double lastTime = 0.0; @@ -106,6 +110,8 @@ public: /// @brief Get current screen std::shared_ptr getScreen(); + + void postRunnable(runnable callback); }; #endif // SRC_ENGINE_H_ diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 07d0208f..ae550e38 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -3,6 +3,7 @@ #include "../../../engine.h" #include "../../../files/engine_paths.h" +#include "../../../frontend/menu/menu.h" #include "../scripting.h" #include @@ -19,7 +20,15 @@ static int l_get_worlds_list(lua_State* L) { return 1; } +static int l_open_world(lua_State* L) { + auto name = lua_tostring(L, 1); + scripting::engine->setScreen(nullptr); + menus::open_world(name, scripting::engine, false); + return 0; +} + const luaL_Reg corelib [] = { {"get_worlds_list", lua_wrap_errors}, + {"open_world", lua_wrap_errors}, {NULL, NULL} };