From a5213c552a325340abe29e2656a854a8a6701146 Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Sat, 30 Jul 2022 01:39:39 +0800 Subject: [PATCH] fixed lua dependency --- .gitmodules | 5 +-- CMakeLists.txt | 9 ++-- ReadMe.md | 4 +- binding/lua/CMakeLists.txt | 5 +++ binding/lua/bridge.c | 21 ++++++++++ binding/lua/{executor => }/executor.c | 4 +- binding/lua/executor/CMakeLists.txt | 2 - binding/lua/main.lua | 6 ++- cmake/FindGLFW.cmake | 2 + cmake/FindLua.cmake | 59 +++++++++++++++------------ hazel/hazel.h | 2 +- src/hazel.c | 2 +- 12 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 binding/lua/CMakeLists.txt create mode 100644 binding/lua/bridge.c rename binding/lua/{executor => }/executor.c (87%) delete mode 100644 binding/lua/executor/CMakeLists.txt diff --git a/.gitmodules b/.gitmodules index 3d031b0..76e0c33 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "3rdlibs/glfw"] path = 3rdlibs/glfw - url = https://github.com/glfw/glfw.git -[submodule "3rdlibs/lua"] - path = 3rdlibs/lua - url = https://github.com/lua/lua.git + url = https://github.com/glfw/glfw.git \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ae9d2f..91d5531 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,11 +9,8 @@ project(HazelLuaEngine ) set(HAZEL_CORE_NAME hazel_core) -set(HAZEL_EXECUTOR_NAME hazel) - -# glfw -set(GLFW_BUILD_DOCS OFF) -set(GLFW_INSTALL OFF) +set(HAZEL_EXECUTOR_NAME HazelRunner) +set(HAZEL_LUA_BRIDGE_NAME hazel) include(cmake/FindLua.cmake) include(cmake/FindGLFW.cmake) @@ -31,4 +28,4 @@ if(WIN32) endif(WIN32) # sandbox -add_subdirectory(binding/lua/executor) \ No newline at end of file +add_subdirectory(binding/lua) \ No newline at end of file diff --git a/ReadMe.md b/ReadMe.md index c0c9578..48df028 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1 +1,3 @@ -A tiny engine made with C and Lua for 1MGames \ No newline at end of file +A tiny engine made with C and Lua for 1MGames + +NOTE: **Under Windows, you must use MinGW to compile** diff --git a/binding/lua/CMakeLists.txt b/binding/lua/CMakeLists.txt new file mode 100644 index 0000000..929fcef --- /dev/null +++ b/binding/lua/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(${HAZEL_LUA_BRIDGE_NAME} SHARED bridge.c) +target_link_libraries(${HAZEL_LUA_BRIDGE_NAME} PUBLIC ${HAZEL_CORE_NAME} lua) + +add_executable(${HAZEL_EXECUTOR_NAME} executor.c) +target_link_libraries(${HAZEL_EXECUTOR_NAME} PUBLIC lua) \ No newline at end of file diff --git a/binding/lua/bridge.c b/binding/lua/bridge.c new file mode 100644 index 0000000..8212f08 --- /dev/null +++ b/binding/lua/bridge.c @@ -0,0 +1,21 @@ +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "luaconf.h" + +#include "hazel/hazel.h" + +static int LuaBridge_RunExampleWindow(lua_State* L) { + Hazel_RunExampleWindow(); + return 0; +} + +static const struct luaL_Reg hazel[] = { + {"RunExampleWindow", LuaBridge_RunExampleWindow}, + {NULL, NULL}, +}; + +DLLEXPORT int luaopen_hazel(lua_State* L) { + luaL_newlib(L, hazel); + return 1; +} \ No newline at end of file diff --git a/binding/lua/executor/executor.c b/binding/lua/executor.c similarity index 87% rename from binding/lua/executor/executor.c rename to binding/lua/executor.c index 0e6093e..fba36d0 100644 --- a/binding/lua/executor/executor.c +++ b/binding/lua/executor.c @@ -1,5 +1,3 @@ -#include "hazel/hazel.h" - #include "lua.h" #include "lualib.h" #include "lauxlib.h" @@ -10,7 +8,7 @@ int main() { printf("lua init failed\n"); return 1; } - // luaL_openlibs(L); + luaL_openlibs(L); if (luaL_loadfile(L, "main.lua") || lua_pcall(L, 0, 0, 0)) { printf("main.lua load and execute failed\n"); diff --git a/binding/lua/executor/CMakeLists.txt b/binding/lua/executor/CMakeLists.txt deleted file mode 100644 index d851eca..0000000 --- a/binding/lua/executor/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(${HAZEL_EXECUTOR_NAME} ./executor.c) -target_link_libraries(${HAZEL_EXECUTOR_NAME} PUBLIC ${HAZEL_CORE_NAME} lua) \ No newline at end of file diff --git a/binding/lua/main.lua b/binding/lua/main.lua index ce47b77..eb333b3 100644 --- a/binding/lua/main.lua +++ b/binding/lua/main.lua @@ -1 +1,5 @@ -print("hello") \ No newline at end of file +---@class hazel +---@field RunExampleWindow function +local hazel = require "hazel" + +hazel.RunExampleWindow() \ No newline at end of file diff --git a/cmake/FindGLFW.cmake b/cmake/FindGLFW.cmake index c303c1e..d2160a4 100644 --- a/cmake/FindGLFW.cmake +++ b/cmake/FindGLFW.cmake @@ -1 +1,3 @@ +set(GLFW_BUILD_DOCS OFF) +set(GLFW_INSTALL OFF) add_subdirectory(3rdlibs/glfw) \ No newline at end of file diff --git a/cmake/FindLua.cmake b/cmake/FindLua.cmake index 4250d1f..7746891 100644 --- a/cmake/FindLua.cmake +++ b/cmake/FindLua.cmake @@ -1,31 +1,40 @@ set(LUA_SRC - 3rdlibs/lua/lapi.c - 3rdlibs/lua/lcode.c - 3rdlibs/lua/lctype.c - 3rdlibs/lua/ldebug.c - 3rdlibs/lua/ldo.c - 3rdlibs/lua/ldump.c - 3rdlibs/lua/lfunc.c - 3rdlibs/lua/lgc.c - 3rdlibs/lua/llex.c - - 3rdlibs/lua/lmem.c - 3rdlibs/lua/lobject.c - 3rdlibs/lua/lopcodes.c - 3rdlibs/lua/lparser.c - 3rdlibs/lua/lstate.c - 3rdlibs/lua/lstring.c - 3rdlibs/lua/ltable.c - - 3rdlibs/lua/ltm.c - 3rdlibs/lua/lundump.c - 3rdlibs/lua/lvm.c - 3rdlibs/lua/lzio.c - 3rdlibs/lua/ltests.c - 3rdlibs/lua/lauxlib.c + 3rdlibs/lua/src/lapi.c + 3rdlibs/lua/src/lcode.c + 3rdlibs/lua/src/lctype.c + 3rdlibs/lua/src/ldebug.c + 3rdlibs/lua/src/ldo.c + 3rdlibs/lua/src/ldump.c + 3rdlibs/lua/src/lfunc.c + 3rdlibs/lua/src/lgc.c + 3rdlibs/lua/src/llex.c + 3rdlibs/lua/src/lmem.c + 3rdlibs/lua/src/lobject.c + 3rdlibs/lua/src/lopcodes.c + 3rdlibs/lua/src/lparser.c + 3rdlibs/lua/src/lstate.c + 3rdlibs/lua/src/lstring.c + 3rdlibs/lua/src/ltable.c + 3rdlibs/lua/src/ltm.c + 3rdlibs/lua/src/lundump.c + 3rdlibs/lua/src/lvm.c + 3rdlibs/lua/src/lzio.c + 3rdlibs/lua/src/lauxlib.c + 3rdlibs/lua/src/lbaselib.c + 3rdlibs/lua/src/lcorolib.c + 3rdlibs/lua/src/ldblib.c + 3rdlibs/lua/src/liolib.c + 3rdlibs/lua/src/lmathlib.c + 3rdlibs/lua/src/loadlib.c + 3rdlibs/lua/src/loslib.c + 3rdlibs/lua/src/lstrlib.c + 3rdlibs/lua/src/ltablib.c + 3rdlibs/lua/src/lutf8lib.c + 3rdlibs/lua/src/linit.c ) + add_library(lua STATIC ${LUA_SRC}) -target_include_directories(lua PUBLIC ./3rdlibs/lua) +target_include_directories(lua PUBLIC ./3rdlibs/lua/src) target_compile_features(lua PRIVATE c_std_99) # TODO if you use MinGW, maybe you need this diff --git a/hazel/hazel.h b/hazel/hazel.h index b2ee12f..3609691 100644 --- a/hazel/hazel.h +++ b/hazel/hazel.h @@ -3,6 +3,6 @@ #include "pch.h" -DLLEXPORT void RunExampleWindow(); +DLLEXPORT void Hazel_RunExampleWindow(); #endif \ No newline at end of file diff --git a/src/hazel.c b/src/hazel.c index 49abcf1..1a1cdb8 100644 --- a/src/hazel.c +++ b/src/hazel.c @@ -1,6 +1,6 @@ #include "hazel/hazel.h" -void RunExampleWindow() { +void Hazel_RunExampleWindow() { GLFWwindow* window; /* Initialize the library */