diff --git a/example/main.lua b/example/main.lua index 5a5e763..2a7f7b1 100644 --- a/example/main.lua +++ b/example/main.lua @@ -103,5 +103,5 @@ end) -- show the main layout layout:show() --- only needed when using LuaJIT/SDL -Backend.run() +-- only needed when using LuaJIT/SDL and not using launch.lua +-- Backend.run() diff --git a/luigi/backend/ffisdl/font.lua b/luigi/backend/ffisdl/font.lua index dc0525b..aabd869 100644 --- a/luigi/backend/ffisdl/font.lua +++ b/luigi/backend/ffisdl/font.lua @@ -1,4 +1,5 @@ local REL = (...):gsub('[^.]*$', '') +local APP_ROOT = rawget(_G, 'LUIGI_APP_ROOT') or '' local ffi = require 'ffi' local sdl = require(REL .. 'sdl') @@ -207,7 +208,7 @@ function Font:constructor (path, size) local key = path .. '_' .. size if not fontCache[key] then - local font = SDL2_ttf.TTF_OpenFont(path, size) + local font = SDL2_ttf.TTF_OpenFont(APP_ROOT .. path, size) if font == nil then error(ffi.string(sdl.getError())) diff --git a/luigi/backend/ffisdl/image.lua b/luigi/backend/ffisdl/image.lua index da341d5..805da32 100644 --- a/luigi/backend/ffisdl/image.lua +++ b/luigi/backend/ffisdl/image.lua @@ -1,4 +1,5 @@ local REL = (...):gsub('[^.]*$', '') +local APP_ROOT = rawget(_G, 'LUIGI_APP_ROOT') or '' local ffi = require 'ffi' local sdl = require(REL .. 'sdl') @@ -15,7 +16,7 @@ end }) function Image:constructor (renderer, path) self.sdlRenderer = renderer self.sdlSurface = ffi.gc( - SDL2_image.IMG_Load(path), + SDL2_image.IMG_Load(APP_ROOT .. path), sdl.freeSurface) if self.sdlSurface == nil then diff --git a/luigi/launch.lua b/luigi/launch.lua new file mode 100644 index 0000000..29259fa --- /dev/null +++ b/luigi/launch.lua @@ -0,0 +1,54 @@ +--[[-- +Launcher for the LuaJIT backend. + +Looks for main.lua. Launch it like this: + + luajit myapp/lib/luigi/launch.lua + +If luigi isn't inside the project directory, pass +the path containing main.lua as the second argument. +The path must end with a directory separator. + + luajit /opt/luigi/launch.lua ./myapp/ + +If the app prefixes luigi modules with something +other then 'luigi', pass that prefix as the third +argument. + + luajit /opt/luigi/launch.lua ./myapp/ lib.luigi + +--]]-- +local packagePath = package.path +local libRoot = arg[0]:gsub('[^/\\]*%.lua$', '') +local appRoot, modPath = ... + +local function run (appRoot, modPath) + package.path = packagePath .. ';' .. appRoot .. '?.lua' + rawset(_G, 'LUIGI_APP_ROOT', appRoot) + require 'main' + require (modPath .. '.backend').run() +end + +-- expect to find main.lua in appRoot if specified +if appRoot then + return run(appRoot, modPath or 'luigi') +end + +-- try to find main.lua in a parent of this library, recursively. +local lastLibRoot = libRoot +repeat + if io.open(libRoot .. 'main.lua') then + return run(libRoot, modPath) + end + lastLibRoot = libRoot + libRoot = libRoot:gsub('([^/\\]*).$', function (m) + modPath = modPath and (m .. '.' .. modPath) or m + return '' + end) +until libRoot == lastLibRoot + +error 'main.lua not found' + + + +