diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f8313e9..105a7c68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,7 @@ message("------------------angelscript 脚本语言库-----------") add_subdirectory ("third/angelscript/angelscript/projects/cmake") message("------------------lua 脚本语言库-----------") add_subdirectory ("third/lua") +add_subdirectory ("third/LuaBridge3") message("------------------其余库以其他形式链接----------") message("------------------nlohmann-json json库---------") diff --git a/dl/CMakeLists.txt b/dl/CMakeLists.txt index fcc613e4..32bbe5a3 100644 --- a/dl/CMakeLists.txt +++ b/dl/CMakeLists.txt @@ -160,6 +160,8 @@ include_directories(${CMAKE_SOURCE_DIR}/third/angelscript/add_on) target_link_libraries(${PROJECT_NAME} PRIVATE angelscript) # lua target_link_libraries(${PROJECT_NAME} PRIVATE lua) +# lua bridge +target_link_libraries(${PROJECT_NAME} PRIVATE LuaBridge) # curl if (NOT DL_DISABLE_CURL) @@ -227,8 +229,6 @@ include_directories(${CMAKE_SOURCE_DIR}/third/sqlite) include_directories(${CMAKE_SOURCE_DIR}/third/tinyobjloader) # tinygltf include_directories(${CMAKE_SOURCE_DIR}/third/tinygltf) -# luaaa -include_directories(${CMAKE_SOURCE_DIR}/third/luaaa) if (CMAKE_SYSTEM_NAME MATCHES "Android") find_library(log-lib log) diff --git a/dl/script/dl_lua.cpp b/dl/script/dl_lua.cpp index 9ef86f4d..7c3d7d45 100644 --- a/dl/script/dl_lua.cpp +++ b/dl/script/dl_lua.cpp @@ -15,6 +15,8 @@ namespace dl luaL_openlibs(L); lua_register_base(L); + lua_register_system(L); + lua_register_gfx(L); } LuaScript::~LuaScript() diff --git a/dl/script/dl_lua.h b/dl/script/dl_lua.h index 0968b538..90cb316b 100644 --- a/dl/script/dl_lua.h +++ b/dl/script/dl_lua.h @@ -9,12 +9,18 @@ * * @note 例如 res/script/、as、mod * 在 res/script/ui/main.as的脚本,模块名为 mod:ui/main +* +* 直接暴露lua与luabridge */ #pragma once #include #include +#define LUA_USE_APICHECK +#include +#include + #include "dl_io.h" struct lua_State; @@ -26,9 +32,44 @@ public: LuaScript(); ~LuaScript(); + struct Env + { + //std::string _funcUpdate; + }; + bool DoFile(std::string_view path_name); + + bool ExcuteFunction(std::string_view name) + { + luabridge::LuaRef v = luabridge::getGlobal(L, name.data()); + luabridge::LuaResult ret = v(); + if (ret.hasFailed()) + { + log_warn("{}", ret.errorMessage()); + return false; + } + // todo ret + return true; + } + template + bool ExcuteFunction(std::string_view name, Args&&... args) + { + luabridge::LuaRef v = luabridge::getGlobal(L, name.data()); + luabridge::LuaResult ret = v(args); + if (ret.hasFailed()) + { + log_warn("{}", ret.errorMessage()); + return false; + } + // todo ret + return true; + } + + Env& GetEnv() { return _env; } + lua_State* GetL() { return L; } private: lua_State* L; + Env _env; }; extern LuaScript* g_lua; diff --git a/dl/script/dl_lua_register.cpp b/dl/script/dl_lua_register.cpp index df289c03..519d1e9b 100644 --- a/dl/script/dl_lua_register.cpp +++ b/dl/script/dl_lua_register.cpp @@ -1,27 +1,47 @@ #include "dl_lua_register.h" -#include - #include "dl_io.h" +#include "render/dl_graphics.h" +#include "system/dl_system.h" +#include "script/dl_lua.h" namespace dl { - void lua_log(const std::string& str) + void lua_log(std::string_view str) { log(str); } + void lua_register_base(lua_State* L) { - /*luaaa::LuaClass class_string_view(L, "string_view"); - class_string_view.ctor();*/ - /*luaaa::LuaClass< Lua_dl> lua_dl(L, "dl"); - lua_dl.ctor(); - lua_dl.def("gfx", g_gfx); - lua_dl.fun("log", log);*/ + luabridge::getGlobalNamespace(L). + addFunction("log", lua_log); + } - luaaa::LuaModule m{ L, "dl" }; - m.fun("log", lua_log); + void lua_register_system(lua_State* L) + { + luabridge::getGlobalNamespace(L) + .beginClass("ParamWindow") + .addConstructor() // 忽略这个构造会出错…… + .addProperty("title", &ParamWindow::_strTitle) + .endClass(); + + luabridge::getGlobalNamespace(L) + .beginClass("System") + .addFunction("SetFuncRun", &System::lua_SetFuncUpdate) + .addFunction("Start", (void(System::*)(const ParamWindow&)) & System::Start) + .endClass(); + + luabridge::push(L, g_system); + lua_setglobal(L, "system"); + + } + + void lua_register_gfx(lua_State* L) + { + /*luaaa::LuaClass c{ L, "gfx"}; + c.fun("Rectangle2D", &Graphics::Rectangle2D);*/ } } \ No newline at end of file diff --git a/dl/script/dl_lua_register.h b/dl/script/dl_lua_register.h index 0df20ea0..4b7ae2a4 100644 --- a/dl/script/dl_lua_register.h +++ b/dl/script/dl_lua_register.h @@ -14,7 +14,8 @@ struct lua_State; namespace dl { - void lua_register_base(lua_State* L); + void lua_register_system(lua_State* L); + void lua_register_gfx(lua_State* L); } \ No newline at end of file diff --git a/dl/system/dl_system.cpp b/dl/system/dl_system.cpp index 3242255f..a54bcb2e 100644 --- a/dl/system/dl_system.cpp +++ b/dl/system/dl_system.cpp @@ -26,6 +26,7 @@ #include "ui/dl_ui_manager.h" #include "system/dl_audio.h" #include "ui/dl_ui_scene.h" +#include "script/dl_lua.h" namespace dl { @@ -207,6 +208,10 @@ void System::Start(const ParamWindow& param) g_vk->GfxLock(); g_scene->_update(); if (_funcRun) _funcRun();//帧函数 + if (!_lua._funcUpdate.empty()) + { + g_lua->ExcuteFunction(_lua._funcUpdate); + } g_vk->GfxUnlock(); g_ui->_render(); @@ -395,6 +400,11 @@ void System::_delete_glfw() glfwTerminate(); } +void System::lua_SetFuncUpdate(std::string_view str) +{ + _lua._funcUpdate = str; +} + const RenderInfo2D& System::GetRenderInfo2D() { return g_vk->GetRenderInfo2D(); diff --git a/dl/system/dl_system.h b/dl/system/dl_system.h index 006acf81..248236d5 100644 --- a/dl/system/dl_system.h +++ b/dl/system/dl_system.h @@ -161,6 +161,9 @@ public: // 提前初始化,以便使用glfw的一些接口,例如显示器相关 void _init_glfw(); void _delete_glfw(); + + //--------------------------lua---------------------------- + void lua_SetFuncUpdate(std::string_view str); private: SystemImp* _imp; Func _funcInit; @@ -172,6 +175,11 @@ private: void* _nativeWindow; std::vector _vecDropFile; + + struct + { + std::string _funcUpdate; + }_lua; }; extern System* g_system; } diff --git a/project/dl_console/CMakeLists.txt b/project/dl_console/CMakeLists.txt index 757a4682..a83301b6 100644 --- a/project/dl_console/CMakeLists.txt +++ b/project/dl_console/CMakeLists.txt @@ -42,9 +42,10 @@ include_directories(${CMAKE_SOURCE_DIR}/third/tinyxml2) include_directories(${CMAKE_SOURCE_DIR}/third/asio/include) # angelscript include_directories(${CMAKE_SOURCE_DIR}/third/angelscript/angelscript/include) -# luaaa -include_directories(${CMAKE_SOURCE_DIR}/third/luaaa) - +# lua +include_directories(${CMAKE_SOURCE_DIR}/third/lua) +# lua bridge +include_directories(${CMAKE_SOURCE_DIR}/third/LuaBridge3/Source) # vld资源文件夹 if(CMAKE_CL_64) set(DL_PATH_VLD ${CMAKE_SOURCE_DIR}/third/vld/bin/Win64) diff --git a/project/dl_console/res/main.lua b/project/dl_console/res/main.lua index 4fb474d3..993f4a03 100644 --- a/project/dl_console/res/main.lua +++ b/project/dl_console/res/main.lua @@ -1,2 +1,15 @@ +function update() + log("hello dl! 22") + gfx:Rectangle2D({100, 100}, {500, 500}) +end + print "xxxx" -dl.log("hello dl!") \ No newline at end of file +log("hello dl!") + +local param = ParamWindow() +param.title = "dl lua" +system:SetFuncRun("update") +system:Start(param) + + +log("end") \ No newline at end of file