我在C ++引擎和Lua之间做了一个包装器,我正在使用LuaJIT,因此我使用ffi作为这两者之间的“包装器”,因为引擎有很多不同的部分我在想它将它们分成文件然后需要它们会很好,但是,在阅读了一些关于LuaJIT之后,我发现对于外部库你必须加载库,所以我带着这个:我何时何地加载库?在“胶水”代码(统一所有模块的那个)?,在每个人?,或者最好将它保存为单个文件? 另外,为了确定加载库有多慢?
答案 0 :(得分:3)
您可以创建一个加载库的“核心”模块:engine / core.lua
local ffi = require'ffi'
local C = ffi.load('engine') -- loads .so/.dll
ffi.cdef[[
/* put common function/type definitions here. */
]]
-- return native module handle
return C
然后,您将为每个引擎部件创建一个模块:engine / graphics.lua
local ffi = require'ffi' -- still need to load ffi here.
local C = require'engine.core'
-- load other dependent parts.
-- If this module uses types from other parts of the engine they most
-- be defined/loaded here before the call to the ffi.cdef below.
require'engine.types'
ffi.cdef[[
/* define function/types for this part of the engine here. */
]]
local _M = {}
-- Add glue functions to _M module.
function _M.glue_function()
return C.engine_function()
end
return _M
'engine.core'模块中的代码只会执行一次。将引擎分成多个部分的最大问题是处理交叉类型依赖性。要解决此问题,请添加'typedef struct name name;'到'engine.core'模块,用于多个部分中使用的类型。