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

111 lines
2.4 KiB
C
Raw Normal View History

2016-07-07 07:04:24 +00:00
#include "lovr.h"
#include "util.h"
2016-07-10 21:56:29 +00:00
#include "event.h"
#include "device.h"
2016-07-07 07:04:24 +00:00
#include "graphics.h"
2016-07-23 22:41:15 +00:00
#include "timer.h"
2016-07-16 05:39:17 +00:00
2016-07-09 05:27:34 +00:00
extern lua_State* L;
2016-07-07 07:04:24 +00:00
void lovrInit(lua_State* L) {
// Write top-level lovr global
lua_newtable(L);
lua_setglobal(L, "lovr");
// Register modules
2016-08-01 00:21:04 +00:00
luaPreloadModule(L, "lovr.event", lovrPushEvent);
luaPreloadModule(L, "lovr.device", lovrPushDevice);
luaPreloadModule(L, "lovr.graphics", lovrPushGraphics);
luaPreloadModule(L, "lovr.timer", lovrPushTimer);
// Bootstrap
char buffer[1024];
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, "
" device = true, "
" graphics = true, "
" timer = true "
" } "
"} "
" "
"local success, err = pcall(require, 'conf') "
"if lovr.conf then "
" success, err = pcall(lovr.conf, conf) "
"end "
" "
"if not success and err then "
" error(err, -1) "
"end "
" "
"local modules = { 'event', 'device', 'graphics', 'timer' } "
"for _, module in ipairs(modules) do "
" if conf.modules[module] then "
" lovr[module] = require('lovr.' .. module) "
" end "
"end "
" "
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() "
" if lovr.draw then lovr.draw() end "
" lovr.graphics.present() "
" end "
"end"
);
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-07-09 05:27:34 +00:00
void lovrDestroy() {
glfwTerminate();
exit(EXIT_SUCCESS);
}
2016-07-07 07:04:24 +00:00
void lovrRun(lua_State* L) {
2016-08-01 00:21:04 +00:00
// Run "main.lua" which will override/define pieces of lovr
if (luaL_dofile(L, "main.lua")) {
error("Failed to run main.lua");
lua_pop(L, 1);
exit(EXIT_FAILURE);
}
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
void lovrOnError(int code, const char* description) {
error(description);
}
void lovrOnClose(GLFWwindow* _window) {
if (_window == window) {
// 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);
lovrDestroy();
}
}
}