Reorganize core

This commit is contained in:
Dane Johnson
2021-01-19 16:16:54 -06:00
parent ddb10abb03
commit a0620bd760
7 changed files with 1 additions and 1 deletions

90
core/Scripting/Lua.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "Lua.h"
#ifdef LUA_SCRIPTING
lua_State *Lua::L { luaL_newstate() };
#endif // LUA_SCRIPTING
void Lua::Initialize() {
#ifdef LUA_SCRIPTING
language = this;
int err;
// Initialize Lua
luaL_openlibs(L);
luaopen_couch(L);
err = luaL_loadfile(L, "main.lua");
if (err == LUA_OK) {
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
}
lua_getglobal(L, "init");
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
}
} else if (err == LUA_ERRFILE) {
std::cerr << "Could not find main.lua." << std::endl;
exit(1);
} else {
// Syntax error
Error();
}
// Bind input functions
//glfwSetWindowUserPointer(window, (void*) L);
Input *input = Input::GetInstance();
input->keyHandlers.push_back(LuaKeyHandler);
input->mousePositionHandlers.push_back(LuaMousePositionHandler);
#else // LUA_SCRIPTING
std::cerr << "Lua is selected as scripting language, but this binary was built without Lua support." << std::endl;
exit(1);
#endif // LUA_SCRIPTING
}
void Lua::Update(double delta) {
#ifdef LUA_SCRIPTING
lua_getglobal(L, "update");
lua_pushnumber(L, delta);
lua_call(L, 1, 0);
#endif // LUA_SCRIPTING
}
void Lua::Close() {
#ifdef LUA_SCRIPTING
lua_close(L);
#endif // LUA_SCRIPTING
}
void Lua::Error() {
#ifdef LUA_SCRIPTING
const char *error = lua_tolstring(L, -1, 0);
std::cerr << error << std::endl;
#endif // LUA_SCRIPTING
exit(1);
}
void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) {
#ifdef LUA_SCRIPTING
// lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);
lua_getglobal(L, "onkey");
lua_pushinteger(L, key);
lua_pushinteger(L, code);
lua_pushinteger(L, action);
lua_pushinteger(L, mods);
lua_call(L, 4, 0);
#endif // LUA_SCRIPTING
}
void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, double relx, double rely) {
#ifdef LUA_SCRIPTING
// lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);
lua_getglobal(L, "onmousemotion");
lua_pushnumber(L, xpos);
lua_pushnumber(L, ypos);
lua_pushnumber(L, relx);
lua_pushnumber(L, rely);
lua_call(L, 4, 0);
#endif // LUA_SCRIPTING
}

35
core/Scripting/Lua.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef LUA_H
#define LUA_H
#include <iostream>
#include <stdlib.h>
#include "Input.h"
#ifdef LUA_SCRIPTING
// Lua includes
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
extern "C" int luaopen_couch(lua_State* L);
#endif // LUA_SCRIPTING
#include "ScriptingLanguage.h"
class Lua : public ScriptingLanguage {
public:
void Initialize();
void Update(double delta);
void Close();
void Error();
private:
#ifdef LUA_SCRIPTING
static lua_State *L;
static void LuaKeyHandler(Window *window, int key, int code, int action, int mods);
static void LuaMousePositionHandler(Window *window, double xpos, double ypos, double xrel, double yrel);
#endif // LUA_SCRIPTING
};
#endif /* LUA_H */

View File

@@ -0,0 +1,8 @@
#include "ScriptingLanguage.h"
ScriptingLanguage *ScriptingLanguage::language { nullptr };
ScriptingLanguage *ScriptingLanguage::GetCurrentLanguage() {
return language;
}

View File

@@ -0,0 +1,15 @@
#ifndef SCRIPTINGLANGUAGE_H
#define SCRIPTINGLANGUAGE_H
class ScriptingLanguage {
public:
virtual void Initialize() = 0;
virtual void Update(double delta) = 0;
virtual void Close() = 0;
virtual void Error() = 0;
static ScriptingLanguage *GetCurrentLanguage();
protected:
static ScriptingLanguage *language;
};
#endif /* SCRIPTINGLANGUAGE_H */