2016-07-07 07:04:24 +00:00
|
|
|
#include "lovr.h"
|
2018-02-17 17:18:08 +00:00
|
|
|
#include "luax.h"
|
|
|
|
#include "api.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "resources/boot.lua.h"
|
|
|
|
#include "lib/glfw.h"
|
|
|
|
#include <stdlib.h>
|
2018-04-26 03:07:28 +00:00
|
|
|
|
2018-02-17 17:18:08 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
#include <emscripten.h>
|
2018-04-26 03:07:28 +00:00
|
|
|
static int runRef = 0;
|
|
|
|
static void emscriptenLoop(void* arg) {
|
|
|
|
lua_State* L = arg;
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, runRef);
|
|
|
|
lua_resume(L, 0);
|
2018-02-17 17:18:08 +00:00
|
|
|
}
|
2018-04-26 03:07:28 +00:00
|
|
|
#endif
|
2018-02-17 17:18:08 +00:00
|
|
|
|
|
|
|
static void onGlfwError(int code, const char* description) {
|
|
|
|
lovrThrow(description);
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:51:22 +00:00
|
|
|
int main(int argc, char** argv) {
|
2018-02-17 17:18:08 +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);
|
|
|
|
}
|
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
while (true) {
|
|
|
|
lua_State* L = lovrErrorContext = luaL_newstate();
|
|
|
|
luaL_openlibs(L);
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
glfwSetErrorCallback(onGlfwError);
|
|
|
|
lovrAssert(glfwInit(), "Error initializing GLFW");
|
2018-05-14 00:19:14 +00:00
|
|
|
glfwSetTime(0);
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
// arg
|
|
|
|
lua_newtable(L);
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
if (argc > 0) {
|
|
|
|
lua_pushstring(L, argv[0]);
|
|
|
|
lua_rawseti(L, -2, -2);
|
|
|
|
}
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
lua_pushstring(L, "lovr");
|
|
|
|
lua_rawseti(L, -2, -1);
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
lua_pushstring(L, argv[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
lua_setglobal(L, "arg");
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
l_lovrInit(L);
|
|
|
|
lua_setglobal(L, "lovr");
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
lua_pushcfunction(L, luax_getstack);
|
|
|
|
if (luaL_loadbuffer(L, (const char*) boot_lua, boot_lua_len, "boot.lua") || lua_pcall(L, 0, 1, -2)) {
|
|
|
|
fprintf(stderr, "%s\n", lua_tostring(L, -1));
|
|
|
|
lovrDestroy();
|
|
|
|
lua_close(L);
|
|
|
|
glfwTerminate();
|
|
|
|
exit(1);
|
2018-02-17 17:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
lua_newthread(L);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
runRef = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
emscripten_set_main_loop_arg(emscriptenLoop, (void*) L, 0, 1);
|
|
|
|
return 0;
|
2018-02-17 17:18:08 +00:00
|
|
|
#else
|
2018-05-14 00:19:14 +00:00
|
|
|
while (lua_resume(L, 0) == LUA_YIELD) ;
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-05-14 00:19:14 +00:00
|
|
|
int exitCode = lua_tonumber(L, -1);
|
2018-04-26 03:07:28 +00:00
|
|
|
bool isRestart = lua_type(L, -1) == LUA_TSTRING && !strcmp(lua_tostring(L, -1), "restart");
|
2018-02-17 17:18:08 +00:00
|
|
|
|
2018-02-23 03:18:36 +00:00
|
|
|
lovrDestroy();
|
2018-04-26 03:07:28 +00:00
|
|
|
lua_close(L);
|
2016-07-07 07:04:24 +00:00
|
|
|
|
2018-04-26 03:07:28 +00:00
|
|
|
if (!isRestart) {
|
|
|
|
glfwTerminate();
|
|
|
|
exit(exitCode);
|
|
|
|
}
|
2018-02-17 17:18:08 +00:00
|
|
|
#endif
|
2018-04-26 03:07:28 +00:00
|
|
|
}
|
2016-07-07 07:04:24 +00:00
|
|
|
}
|