私は C++ エンジンと Lua の間のラッパーを作成しています。私は LuaJIT を使用しています。このため、これら 2 つの間の「ラッパー」として ffi を使用しています。それらをファイルに分割してから要求するには、LuaJIT について少し読んだ後、外部ライブラリの場合はライブラリをロードする必要があることがわかりました。「接着剤」コード (すべてのモジュールを統合するもの)?、全員?、または単一のファイルとして保持する方がよいでしょうか? また、これを決定するために、ライブラリのロードがどれくらい遅いですか?
2985 次
1 に答える
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」モジュールのコードは 1 回だけ実行されます。エンジンをパーツに分割する際の最大の問題は、タイプ間の依存関係を処理することです。これを解決するには、「typedef struct name name;」を追加します。複数の部分で使用されるタイプの「engine.core」モジュールに。
于 2013-05-20T23:31:42.677 に答える