2016-07-07 07:04:24 +00:00
|
|
|
#include "lovr.h"
|
2017-04-13 02:48:17 +00:00
|
|
|
#include "util.h"
|
2017-03-11 11:08:07 +00:00
|
|
|
#include "api/lovr.h"
|
2017-04-02 12:55:21 +00:00
|
|
|
#include "data/boot.lua.h"
|
|
|
|
#include "data/logo.png.h"
|
2017-04-13 02:48:17 +00:00
|
|
|
#include "lib/glfw.h"
|
2016-11-19 09:28:01 +00:00
|
|
|
#include <stdlib.h>
|
2016-07-16 05:39:17 +00:00
|
|
|
|
2017-04-13 02:48:17 +00:00
|
|
|
static void onGlfwError(int code, const char* description) {
|
|
|
|
error(description);
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:22:06 +00:00
|
|
|
static int getStackTrace(lua_State* L) {
|
2016-11-29 06:59:27 +00:00
|
|
|
const char* message = luaL_checkstring(L, -1);
|
2017-03-31 00:22:06 +00:00
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
lua_pushstring(L, message);
|
|
|
|
lua_pushinteger(L, 3);
|
|
|
|
lua_call(L, 2, 1);
|
|
|
|
return 1;
|
2016-11-29 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 21:51:15 +00:00
|
|
|
static int lovrGetOS(lua_State* L) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
lua_pushstring(L, "Windows");
|
|
|
|
return 1;
|
|
|
|
#elif __APPLE__
|
|
|
|
lua_pushstring(L, "macOS");
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-02 01:39:10 +00:00
|
|
|
static int lovrGetVersion(lua_State* L) {
|
|
|
|
lua_pushnumber(L, LOVR_VERSION_MAJOR);
|
|
|
|
lua_pushnumber(L, LOVR_VERSION_MINOR);
|
|
|
|
lua_pushnumber(L, LOVR_VERSION_PATCH);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
void lovrInit(lua_State* L, int argc, char** argv) {
|
2017-02-18 10:48:38 +00:00
|
|
|
if (argc > 1 && strcmp(argv[1], "--version") == 0) {
|
|
|
|
printf("LOVR %d.%d.%d (%s)\n", LOVR_VERSION_MAJOR, LOVR_VERSION_MINOR, LOVR_VERSION_PATCH, LOVR_VERSION_ALIAS);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-04-13 02:48:17 +00:00
|
|
|
glfwSetErrorCallback(onGlfwError);
|
|
|
|
|
|
|
|
if (!glfwInit()) {
|
|
|
|
error("Error initializing glfw");
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwSetTime(0);
|
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);
|
2017-03-11 21:51:15 +00:00
|
|
|
lua_pushcfunction(L, lovrGetOS);
|
|
|
|
lua_setfield(L, -2, "getOS");
|
2016-12-02 01:39:10 +00:00
|
|
|
lua_pushcfunction(L, lovrGetVersion);
|
|
|
|
lua_setfield(L, -2, "getVersion");
|
2017-04-02 12:55:21 +00:00
|
|
|
lua_pushlstring(L, (const char*) logo_png, logo_png_len);
|
|
|
|
lua_setfield(L, -2, "_logo");
|
2016-07-07 07:04:24 +00:00
|
|
|
lua_setglobal(L, "lovr");
|
|
|
|
|
2016-08-05 21:02:09 +00:00
|
|
|
// Preload modules
|
2017-01-03 03:09:33 +00:00
|
|
|
luax_preloadmodule(L, "lovr.audio", l_lovrAudioInit);
|
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);
|
2017-01-19 09:56:17 +00:00
|
|
|
luax_preloadmodule(L, "lovr.math", l_lovrMathInit);
|
2016-11-19 08:37:43 +00:00
|
|
|
luax_preloadmodule(L, "lovr.timer", l_lovrTimerInit);
|
2016-08-01 00:21:04 +00:00
|
|
|
|
2017-04-02 12:55:21 +00:00
|
|
|
if (luaL_loadbuffer(L, (const char*) boot_lua, boot_lua_len, "boot.lua") || lua_pcall(L, 0, 0, 0)) {
|
2016-08-01 00:21:04 +00:00
|
|
|
const char* message = luaL_checkstring(L, 1);
|
|
|
|
error("Unable to bootstrap LOVR: %s", message);
|
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) {
|
2017-03-31 00:22:06 +00:00
|
|
|
lua_pushcfunction(L, getStackTrace);
|
2016-07-07 07:04:24 +00:00
|
|
|
|
|
|
|
// lovr.run()
|
|
|
|
lua_getglobal(L, "lovr");
|
|
|
|
lua_getfield(L, -1, "run");
|
2017-03-31 00:22:06 +00:00
|
|
|
if (lua_pcall(L, 0, 1, -3)) {
|
|
|
|
lua_getfield(L, -2, "errhand");
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
if (!lua_pcall(L, 1, 0, 0)) {
|
|
|
|
lovrDestroy(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error(lua_tostring(L, -2));
|
|
|
|
}
|
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);
|
2017-03-31 00:22:06 +00:00
|
|
|
lua_pop(L, 2);
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
lovrDestroy(exitCode);
|
2016-07-09 05:27:34 +00:00
|
|
|
}
|