1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-02 12:33:52 +00:00
lovr/src/lovr.c

168 lines
3.8 KiB
C
Raw Normal View History

2016-07-07 07:04:24 +00:00
#include "lovr.h"
#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"
#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-14 06:11:03 +00:00
int luaopen_cjson(lua_State* L);
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;
2017-04-21 04:23:23 +00:00
#elif EMSCRIPTEN
lua_pushstring(L, "Web");
2017-04-29 20:33:45 +00:00
#else
2017-03-11 21:51:15 +00:00
lua_pushnil(L);
2017-04-29 20:33:45 +00:00
#endif
2017-03-11 21:51:15 +00:00
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);
}
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);
2017-05-16 04:59:53 +00:00
luax_preloadmodule(L, "lovr.physics", l_lovrPhysicsInit);
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-14 06:11:03 +00:00
// Preload libraries
luax_preloadmodule(L, "json", luaopen_cjson);
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
}
}
void lovrDestroy(int exitCode) {
2016-07-09 05:27:34 +00:00
glfwTerminate();
exit(exitCode);
2016-07-09 05:27:34 +00:00
}
2017-04-16 23:56:49 +00:00
#ifdef EMSCRIPTEN
#include <emscripten.h>
2017-04-23 00:02:41 +00:00
static int stepRef = 0;
2017-04-16 23:56:49 +00:00
2017-04-23 00:02:41 +00:00
static void emscriptenLoop(void* arg) {
lua_State* L = arg;
2017-04-16 23:56:49 +00:00
2017-04-23 00:02:41 +00:00
lua_rawgeti(L, LUA_REGISTRYINDEX, stepRef);
2017-04-16 23:56:49 +00:00
lua_call(L, 0, 0);
}
void lovrRun(lua_State* L) {
// lovr.load
lua_getglobal(L, "lovr");
if (!lua_isnil(L, -1)) {
lua_getfield(L, -1, "load");
if (!lua_isnil(L, -1)) {
lua_call(L, 0, 0);
2017-04-23 00:02:41 +00:00
} else {
lua_pop(L, 1);
2017-04-16 23:56:49 +00:00
}
2017-04-23 00:02:41 +00:00
lua_getfield(L, -1, "step");
stepRef = luaL_ref(L, LUA_REGISTRYINDEX);
2017-04-16 23:56:49 +00:00
}
emscripten_set_main_loop_arg(emscriptenLoop, (void*) L, 0, 1);
}
#else
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
}
2017-04-16 23:56:49 +00:00
#endif