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-19 09:28:01 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include <stdlib.h>
|
2016-07-16 05:39:17 +00:00
|
|
|
|
2016-11-01 00:14:31 +00:00
|
|
|
void lovrInit(lua_State* L, int argc, char** argv) {
|
|
|
|
|
|
|
|
// 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-08-05 21:02:09 +00:00
|
|
|
// lovr = {}
|
2016-07-07 07:04:24 +00:00
|
|
|
lua_newtable(L);
|
|
|
|
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]) "
|
2016-11-18 08:33:26 +00:00
|
|
|
"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-07-16 06:43:57 +00:00
|
|
|
"function lovr.run() "
|
|
|
|
" if lovr.load then lovr.load() end "
|
|
|
|
" while true do "
|
|
|
|
" lovr.event.poll() "
|
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 "
|
2016-10-24 20:43:07 +00:00
|
|
|
" 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)) "
|
|
|
|
" print((stackTrace:gsub('\\n[^\\n]+$', ''))) "
|
|
|
|
"end "
|
|
|
|
|
2016-11-18 08:33:26 +00:00
|
|
|
"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
|
|
|
|
|
|
|
lua_atpanic(L, lovrOnLuaError);
|
2016-07-07 07:04:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:50:57 +00:00
|
|
|
void lovrDestroy(int exitCode) {
|
2016-07-09 05:27:34 +00:00
|
|
|
glfwTerminate();
|
2016-10-14 06:50:57 +00:00
|
|
|
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");
|
|
|
|
lua_call(L, 0, 0);
|
|
|
|
}
|
2016-07-09 05:27:34 +00:00
|
|
|
|
2016-08-08 19:22:33 +00:00
|
|
|
int lovrOnLuaError(lua_State* L) {
|
|
|
|
const char* message = luaL_checkstring(L, -1);
|
|
|
|
lua_getglobal(L, "lovr");
|
|
|
|
lua_getfield(L, -1, "errhand");
|
|
|
|
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
lua_pushstring(L, message);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
} else {
|
|
|
|
error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lovrOnGlfwError(int code, const char* description) {
|
2016-07-09 05:27:34 +00:00
|
|
|
error(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lovrOnClose(GLFWwindow* _window) {
|
|
|
|
if (_window == window) {
|
|
|
|
|
2016-09-18 01:15:47 +00:00
|
|
|
lua_State* L = (lua_State*) glfwGetWindowUserPointer(window);
|
|
|
|
|
2016-07-09 05:27:34 +00:00
|
|
|
// lovr.quit()
|
|
|
|
lua_getglobal(L, "lovr");
|
|
|
|
lua_getfield(L, -1, "quit");
|
2016-07-16 06:43:57 +00:00
|
|
|
|
|
|
|
if (!lua_isnil(L, -1)) {
|
|
|
|
lua_call(L, 0, 0);
|
|
|
|
}
|
2016-07-09 05:27:34 +00:00
|
|
|
|
|
|
|
if (glfwWindowShouldClose(window)) {
|
|
|
|
glfwDestroyWindow(window);
|
2016-10-14 06:50:57 +00:00
|
|
|
lovrDestroy(0);
|
2016-07-09 05:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|