1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-04 13:33:34 +00:00
lovr/src/lovr.c

164 lines
4.2 KiB
C
Raw Normal View History

2016-07-07 07:04:24 +00:00
#include "lovr.h"
2016-08-10 06:28:17 +00:00
#include "lovr/event.h"
2016-11-01 00:14:31 +00:00
#include "lovr/filesystem.h"
2016-08-10 06:28:17 +00:00
#include "lovr/graphics.h"
2016-08-11 04:17:14 +00:00
#include "lovr/headset.h"
2016-08-10 06:28:17 +00:00
#include "lovr/timer.h"
2016-11-29 06:59:27 +00:00
#include "glfw.h"
2016-11-19 09:28:01 +00:00
#include "util.h"
#include <stdlib.h>
2016-07-16 05:39:17 +00:00
2016-11-29 06:59:27 +00:00
static int handleLuaError(lua_State* L) {
const char* message = luaL_checkstring(L, -1);
lua_getglobal(L, "lovr");
lua_getfield(L, -1, "errhand");
2016-11-01 00:14:31 +00:00
2016-11-29 06:59:27 +00:00
if (lua_isfunction(L, -1)) {
lua_pushstring(L, message);
lua_call(L, 1, 0);
} else {
error(message);
}
return 0;
}
void lovrInit(lua_State* L, int argc, char** argv) {
initGlfw();
2016-11-26 01:31:41 +00:00
2016-11-01 00:14:31 +00:00
// arg global
lua_newtable(L);
if (argc > 0) {
lua_pushstring(L, argv[0]);
lua_rawseti(L, -2, -2);
}
lua_pushstring(L, "lovr");
lua_rawseti(L, -2, -1);
for (int i = 1; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
2016-07-07 07:04:24 +00:00
2016-11-29 09:33:53 +00:00
// lovr
2016-07-07 07:04:24 +00:00
lua_newtable(L);
2016-11-29 09:33:53 +00:00
lua_pushstring(L, LOVR_VERSION);
lua_setfield(L, -2, "_version");
2016-07-07 07:04:24 +00:00
lua_setglobal(L, "lovr");
2016-08-05 21:02:09 +00:00
// Preload modules
2016-11-19 08:37:43 +00:00
luax_preloadmodule(L, "lovr.event", l_lovrEventInit);
luax_preloadmodule(L, "lovr.filesystem", l_lovrFilesystemInit);
luax_preloadmodule(L, "lovr.graphics", l_lovrGraphicsInit);
luax_preloadmodule(L, "lovr.headset", l_lovrHeadsetInit);
luax_preloadmodule(L, "lovr.timer", l_lovrTimerInit);
2016-08-01 00:21:04 +00:00
// Bootstrap
2016-11-01 00:14:31 +00:00
char buffer[2048];
2016-07-16 06:43:57 +00:00
snprintf(buffer, sizeof(buffer), "%s",
2016-08-01 00:21:04 +00:00
"local conf = { "
" modules = { "
" event = true, "
" graphics = true, "
2016-08-11 04:17:14 +00:00
" headset = true, "
2016-08-01 00:21:04 +00:00
" timer = true "
" } "
"} "
2016-08-08 07:34:19 +00:00
2016-11-01 00:14:31 +00:00
"lovr.filesystem = require('lovr.filesystem') "
"lovr.filesystem.init(arg[-2]) "
"if not lovr.filesystem.setSource(lovr.filesystem.getExecutablePath()) and arg[1] then "
" lovr.filesystem.setSource(arg[1]) "
2016-11-01 00:14:31 +00:00
"end "
2016-08-01 00:21:04 +00:00
"local success, err = pcall(require, 'conf') "
"if lovr.conf then "
" success, err = pcall(lovr.conf, conf) "
"end "
2016-08-08 07:34:19 +00:00
2016-09-06 17:13:49 +00:00
"local modules = { 'event', 'graphics', 'headset', 'timer' } "
2016-08-01 00:21:04 +00:00
"for _, module in ipairs(modules) do "
" if conf.modules[module] then "
" lovr[module] = require('lovr.' .. module) "
" end "
"end "
2016-08-08 07:34:19 +00:00
2016-11-07 22:30:32 +00:00
"lovr.filesystem.setIdentity(conf.identity or 'default') "
2016-11-29 06:59:27 +00:00
"lovr.handlers = setmetatable({ "
2016-12-01 07:03:58 +00:00
" quit = function() end, "
2016-12-01 22:55:47 +00:00
" controlleradded = function(c) "
" if lovr.controlleradded then lovr.controlleradded(c) end "
" end, "
" controllerremoved = function(c) "
" if lovr.controllerremoved then lovr.controllerremoved(c) end "
" end "
2016-11-29 06:59:27 +00:00
"}, { "
" __index = function(self, event) "
" error('Unknown event: ', event) "
" end "
"}) "
2016-07-16 06:43:57 +00:00
"function lovr.run() "
" if lovr.load then lovr.load() end "
" while true do "
2016-11-29 06:59:27 +00:00
" lovr.event.pump() "
" for name, a, b, c, d in lovr.event.poll() do "
" if name == 'quit' and (not lovr.quit or not lovr.quit()) then "
" return a "
" end "
" lovr.handlers[name](a, b, c, d) "
" end "
2016-07-23 22:41:15 +00:00
" local dt = lovr.timer.step() "
" if lovr.update then lovr.update(dt) end "
2016-07-16 06:43:57 +00:00
" lovr.graphics.clear() "
2016-11-08 06:38:45 +00:00
" lovr.graphics.origin() "
2016-09-15 04:42:38 +00:00
" if lovr.draw then "
" if lovr.headset and lovr.headset.isPresent() then "
" lovr.headset.renderTo(lovr.draw) "
" else "
" lovr.draw() "
" end "
2016-09-15 04:42:38 +00:00
" end "
2016-07-16 06:43:57 +00:00
" lovr.graphics.present() "
2016-10-01 22:11:33 +00:00
" lovr.timer.sleep(.001) "
2016-07-16 06:43:57 +00:00
" end "
2016-11-02 03:27:15 +00:00
"end "
2016-11-15 04:14:36 +00:00
"function lovr.errhand(message, layer) "
" local stackTrace = debug.traceback('Error: ' .. tostring(message), 1 + (layer or 1)) "
2016-11-29 06:59:27 +00:00
" print((stackTrace:gsub('\\n[^\\n]+$', ''):gsub('stack traceback', 'Stack'))) "
2016-11-15 04:14:36 +00:00
"end "
"if lovr.filesystem.isFile('main.lua') then "
" require('main') "
"end"
2016-07-16 06:43:57 +00:00
);
2016-08-01 00:21:04 +00:00
if (luaL_dostring(L, buffer)) {
const char* message = luaL_checkstring(L, 1);
error("Unable to bootstrap LOVR: %s", message);
2016-07-07 07:04:24 +00:00
}
2016-08-08 19:22:33 +00:00
2016-11-29 06:59:27 +00:00
lua_atpanic(L, handleLuaError);
2016-07-07 07:04:24 +00:00
}
void lovrDestroy(int exitCode) {
2016-07-09 05:27:34 +00:00
glfwTerminate();
exit(exitCode);
2016-07-09 05:27:34 +00:00
}
2016-11-01 00:14:31 +00:00
void lovrRun(lua_State* L) {
2016-07-07 07:04:24 +00:00
// lovr.run()
lua_getglobal(L, "lovr");
lua_getfield(L, -1, "run");
2016-11-29 06:59:27 +00:00
lua_call(L, 0, 1);
2016-07-09 05:27:34 +00:00
2016-11-29 06:59:27 +00:00
// Exit with return value from lovr.run
int exitCode = luaL_optint(L, -1, 0);
lovrDestroy(exitCode);
2016-07-09 05:27:34 +00:00
}