Scripting quality of life adjustments

This commit is contained in:
Dane Johnson
2021-01-20 21:42:14 -06:00
parent 087a75eaab
commit cf39a40f29
6 changed files with 289 additions and 17 deletions

View File

@@ -19,10 +19,12 @@ void Lua::Initialize() {
if (err != LUA_OK) {
Error();
}
lua_getglobal(L, "init");
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
if (HasHook("init")) {
lua_getglobal(L, "init");
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
}
}
} else if (err == LUA_ERRFILE) {
Util::Die("Could not find main.lua.");
@@ -34,8 +36,12 @@ void Lua::Initialize() {
// Bind input functions
//glfwSetWindowUserPointer(window, (void*) L);
Input *input = Input::GetInstance();
input->keyHandlers.push_back(LuaKeyHandler);
input->mousePositionHandlers.push_back(LuaMousePositionHandler);
if (HasHook("onkey")) {
input->keyHandlers.push_back(LuaKeyHandler);
}
if (HasHook("onmousemotion")) {
input->mousePositionHandlers.push_back(LuaMousePositionHandler);
}
#else // LUA_SCRIPTING
Util::Die("Lua is selected as scripting language, but this binary was built without Lua support.");
#endif // LUA_SCRIPTING
@@ -43,9 +49,11 @@ void Lua::Initialize() {
void Lua::Update(double delta) {
#ifdef LUA_SCRIPTING
lua_getglobal(L, "update");
lua_pushnumber(L, delta);
lua_call(L, 1, 0);
if (HasHook("update")) {
lua_getglobal(L, "update");
lua_pushnumber(L, delta);
lua_call(L, 1, 0);
}
#endif // LUA_SCRIPTING
}
@@ -63,6 +71,16 @@ void Lua::Error() {
Util::Die("Whut?");
}
bool Lua::HasHook(const char *name) {
bool exists = false;
#ifdef LUA_SCRIPTING
int type = lua_getglobal(L, name);
lua_pop(L, -1);
exists = type != LUA_TNIL;
#endif // LUA_SCRIPTING
return exists;
}
void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) {
#ifdef LUA_SCRIPTING
// lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);

View File

@@ -22,6 +22,7 @@ public:
void Update(double delta);
void Close();
void Error();
bool HasHook(const char *name);
private:
#ifdef LUA_SCRIPTING
static lua_State *L;

View File

@@ -1,12 +1,15 @@
#ifndef SCRIPTINGLANGUAGE_H
#define SCRIPTINGLANGUAGE_H
#include "types.h"
class ScriptingLanguage {
public:
virtual void Initialize() = 0;
virtual void Update(double delta) = 0;
virtual void Close() = 0;
virtual void Error() = 0;
virtual bool HasHook(const char * name) = 0;
static ScriptingLanguage *GetCurrentLanguage();
protected:
static ScriptingLanguage *language;