Moved embedded boot script from main.c to src/embed/boot.lua
This commit is contained in:
64
src/embed/boot.lua
Normal file
64
src/embed/boot.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
function love.run()
|
||||
if love.load then love.load() end
|
||||
love.timer.step()
|
||||
while true do
|
||||
-- Handle mouse events
|
||||
for _, e in ipairs(love.mouse.poll()) do
|
||||
if e.type == "motion" then
|
||||
if love.mousemoved then love.mousemoved(e.x, e.y, e.dx, e.dy) end
|
||||
elseif e.type == "pressed" then
|
||||
if love.mousepressed then love.mousepressed(e.x, e.y, e.button) end
|
||||
elseif e.type == "released" then
|
||||
if love.mousereleased then love.mousereleased(e.x, e.y, e.button) end
|
||||
end
|
||||
end
|
||||
-- Handle keyboard events
|
||||
for _, e in ipairs(love.keyboard.poll()) do
|
||||
if e.type == "down" then
|
||||
if love.keypressed then love.keypressed(e.key, e.code, e.isrepeat) end
|
||||
elseif e.type == "up" then
|
||||
if love.keyreleased then love.keyreleased(e.key, e.code) end
|
||||
elseif e.type == "text" then
|
||||
if love.textinput then love.textinput(e.text) end
|
||||
end
|
||||
end
|
||||
-- Update
|
||||
love.timer.step()
|
||||
local dt = love.timer.getDelta()
|
||||
if love.update then love.update(dt) end
|
||||
-- Draw
|
||||
love.graphics.clear()
|
||||
if love.draw then love.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function love.errhand(msg)
|
||||
-- Init error text
|
||||
local err = { "Error\n", msg }
|
||||
local trace = debug.traceback("", 2)
|
||||
for line in string.gmatch(trace, "([^\t]-)\n") do
|
||||
table.insert(err, line)
|
||||
end
|
||||
local str = table.concat(err, "\n")
|
||||
-- Init error state
|
||||
love.graphics.reset()
|
||||
pcall(love.graphics.setBackgroundColor, 89, 157, 220)
|
||||
-- Do error main loop
|
||||
while true do
|
||||
for _, e in ipairs(love.keyboard.poll()) do
|
||||
if e.type == "down" and e.code == 1 then
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
love.graphics.clear()
|
||||
love.graphics.print(str, 6, 6)
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
xpcall(function() require("main") end, love.errhand)
|
||||
xpcall(love.run, love.errhand)
|
||||
70
src/main.c
70
src/main.c
@@ -59,74 +59,8 @@ int main(void) {
|
||||
luaL_openlibs(L);
|
||||
luaL_requiref(L, "love", luaopen_love, 1);
|
||||
|
||||
const char boot[] =
|
||||
"package.path = package.path .. ';' .. package.path:gsub('%?', 'lua/?')\n"
|
||||
"local print = print\n"
|
||||
"local xpcall = xpcall\n"
|
||||
|
||||
"function love.run()\n"
|
||||
"if love.load then love.load() end\n"
|
||||
"love.timer.step()\n"
|
||||
"while true do\n"
|
||||
/* Handle mouse events */
|
||||
"for _, e in ipairs(love.mouse.poll()) do\n"
|
||||
"if e.type == 'motion' then\n"
|
||||
"if love.mousemoved then love.mousemoved(e.x, e.y, e.dx, e.dy) end\n"
|
||||
"elseif e.type == 'pressed' then\n"
|
||||
"if love.mousepressed then love.mousepressed(e.x, e.y, e.button) end\n"
|
||||
"elseif e.type == 'released' then\n"
|
||||
"if love.mousereleased then love.mousereleased(e.x, e.y, e.button) end\n"
|
||||
"end\n"
|
||||
"end\n"
|
||||
/* Keyboard events */
|
||||
"for _, e in ipairs(love.keyboard.poll()) do\n"
|
||||
"if e.type == 'down' then\n"
|
||||
"if love.keypressed then love.keypressed(e.key, e.code, e.isrepeat) end\n"
|
||||
"elseif e.type == 'up' then\n"
|
||||
"if love.keyreleased then love.keyreleased(e.key, e.code) end\n"
|
||||
"elseif e.type == 'text' then\n"
|
||||
"if love.textinput then love.textinput(e.text) end\n"
|
||||
"end\n"
|
||||
"end\n"
|
||||
/* Update */
|
||||
"love.timer.step()\n"
|
||||
"local dt = love.timer.getDelta()\n"
|
||||
"if love.update then love.update(dt) end\n"
|
||||
/* Draw */
|
||||
"love.graphics.clear()\n"
|
||||
"if love.draw then love.draw() end\n"
|
||||
"love.graphics.present()\n"
|
||||
"end\n"
|
||||
"end\n"
|
||||
|
||||
"function love.errhand(msg)\n"
|
||||
/* Init error text */
|
||||
"local err = { 'Error\\n', msg }\n"
|
||||
"local trace = debug.traceback('', 2)\n"
|
||||
"for line in string.gmatch(trace, '([^\\t]-)\\n') do\n"
|
||||
"table.insert(err, line)\n"
|
||||
"end\n"
|
||||
"local str = table.concat(err, '\\n')"
|
||||
/* Init error state */
|
||||
"love.graphics.reset()\n"
|
||||
"pcall(love.graphics.setBackgroundColor, 89, 157, 220)\n"
|
||||
/* Do error main loop */
|
||||
"while true do\n"
|
||||
"for _, e in ipairs(love.keyboard.poll()) do\n"
|
||||
"if e.type == 'down' and e.code == 1 then\n"
|
||||
"os.exit()\n"
|
||||
"end\n"
|
||||
"end\n"
|
||||
"love.graphics.clear()\n"
|
||||
"love.graphics.print(str, 6, 6)\n"
|
||||
"love.graphics.present()\n"
|
||||
"end\n"
|
||||
"end\n"
|
||||
|
||||
"xpcall(function() require('main') end, love.errhand)\n"
|
||||
"xpcall(love.run, love.errhand)\n";
|
||||
|
||||
int err = luaL_loadbuffer(L, boot, sizeof(boot) - 1, "=boot");
|
||||
#include "boot_lua.h"
|
||||
int err = luaL_loadbuffer(L, boot_lua, sizeof(boot_lua) - 1, "boot.lua");
|
||||
|
||||
if (err || lua_pcall(L, 0, 0, 0)) {
|
||||
vga_deinit();
|
||||
|
||||
Reference in New Issue
Block a user