mirror of
https://github.com/devZoGok/gameBase.git
synced 2026-08-26 19:43:28 +00:00
@@ -4,3 +4,6 @@
|
||||
[submodule "external/lua-cmake"]
|
||||
path = external/lua-cmake
|
||||
url = https://github.com/lubgr/lua-cmake
|
||||
[submodule "external/sol2"]
|
||||
path = external/sol2
|
||||
url = https://github.com/ThePhD/sol2
|
||||
|
||||
+7
-2
@@ -6,8 +6,8 @@ cmake_policy(SET CMP0015 NEW)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(states abstractAppState.cpp)
|
||||
set(core inputManager.cpp mapping.h stateManager.cpp luaManager.cpp)
|
||||
set(util util.cpp)
|
||||
set(core inputManager.cpp mapping.h stateManager.cpp)
|
||||
set(util solUtil.cpp util.cpp)
|
||||
|
||||
set(LIB_NAME gameBase)
|
||||
add_library(${LIB_NAME} STATIC ${states} ${core} ${util})
|
||||
@@ -20,4 +20,9 @@ target_link_directories(${LIB_NAME} PUBLIC ${GLFW_LIB_DIR})
|
||||
set(LUA_DIR external/lua-cmake)
|
||||
add_subdirectory(${LUA_DIR})
|
||||
target_include_directories(${LIB_NAME} PUBLIC ${LUA_DIR}/external/upstream)
|
||||
|
||||
set(SOL_DIR external/sol2)
|
||||
add_subdirectory(${SOL_DIR})
|
||||
target_include_directories(${LIB_NAME} PUBLIC ${SOL_DIR}/include)
|
||||
|
||||
target_link_libraries(${LIB_NAME} glfw lua::lib)
|
||||
|
||||
+8
-15
@@ -1,13 +1,12 @@
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
#include "abstractAppState.h"
|
||||
#include "util.h"
|
||||
#include "mapping.h"
|
||||
#include "luaManager.h"
|
||||
|
||||
namespace gameBase {
|
||||
using namespace std;
|
||||
|
||||
typedef LuaManager::Index Index;
|
||||
|
||||
AbstractAppState::AbstractAppState(int type, int firstMapping, int numMappings, string optionsFile){
|
||||
this->type = type;
|
||||
this->firstMapping = firstMapping;
|
||||
@@ -18,22 +17,16 @@ namespace gameBase {
|
||||
void AbstractAppState::onAttached(){
|
||||
attached = true;
|
||||
|
||||
LuaManager *luaManager = LuaManager::getSingleton();
|
||||
luaManager->buildScript(vector<string>{optionsFile});
|
||||
sol::state_view SOL_LUA_STATE = generateView();
|
||||
SOL_LUA_STATE.script_file(optionsFile);
|
||||
string table = "mappings";
|
||||
|
||||
for(int i = firstMapping; i < firstMapping + numMappings; i++){
|
||||
Index idIndex = Index(i + 1);
|
||||
int bind = luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("bind")});
|
||||
Mapping::BindType type = (Mapping::BindType)luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("bindType")});
|
||||
bool action = luaManager->getBoolFromTable(table, vector<Index>{idIndex, Index("action")});
|
||||
int trigger = luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("trigger")});
|
||||
|
||||
Mapping *m = new Mapping;
|
||||
m->bind = bind;
|
||||
m->type = type;
|
||||
m->action = action;
|
||||
m->trigger = trigger;
|
||||
m->bind = SOL_LUA_STATE[table][i + 1]["bind"];
|
||||
m->type = (Mapping::BindType)SOL_LUA_STATE[table][i + 1]["bindType"];
|
||||
m->action = SOL_LUA_STATE[table][i + 1]["action"];
|
||||
m->trigger = SOL_LUA_STATE[table][i + 1]["trigger"];
|
||||
mappings.push_back(m);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,15 @@
|
||||
#define ABSTRACT_APP_STATE_H
|
||||
|
||||
#include "mapping.h"
|
||||
#include "solUtil.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace sol{
|
||||
class state;
|
||||
}
|
||||
|
||||
namespace gameBase {
|
||||
class AbstractAppState{
|
||||
public:
|
||||
|
||||
+1
Submodule external/sol2 added at 9c882a28fd
-112
@@ -1,112 +0,0 @@
|
||||
#include "luaManager.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace gameBase{
|
||||
static LuaManager *luaManager = nullptr;
|
||||
|
||||
LuaManager* LuaManager::getSingleton(){
|
||||
if(!luaManager) luaManager = new LuaManager();
|
||||
|
||||
return luaManager;
|
||||
}
|
||||
|
||||
LuaManager::LuaManager(){
|
||||
state = luaL_newstate();
|
||||
luaL_openlibs(state);
|
||||
}
|
||||
|
||||
void LuaManager::buildScript(vector<string> files){
|
||||
string script = "";
|
||||
|
||||
for(string f : files)
|
||||
script += "dofile(\"" + f + "\");";
|
||||
|
||||
executeCode(script, "Error building script(s)\n");
|
||||
}
|
||||
|
||||
void LuaManager::executeCode(string code, string errMsg){
|
||||
if(luaL_dostring(state, code.c_str()) && lua_pcall(state, 0, 0, 0))
|
||||
cout << errMsg;
|
||||
}
|
||||
|
||||
int LuaManager::getInt(string varName){
|
||||
lua_getglobal(state, varName.c_str());
|
||||
int isNum;
|
||||
int var = (int)lua_tointegerx(state, -1, &isNum);
|
||||
lua_pop(state, 1);
|
||||
return var;
|
||||
}
|
||||
|
||||
float LuaManager::getFloat(string varName){
|
||||
lua_getglobal(state, varName.c_str());
|
||||
int isNum;
|
||||
float var = (int)lua_tonumberx(state, -1, &isNum);
|
||||
lua_pop(state, 1);
|
||||
return var;
|
||||
}
|
||||
|
||||
string LuaManager::getString(string varName){
|
||||
lua_getglobal(state, varName.c_str());
|
||||
string result = lua_tostring(state, -1);
|
||||
lua_pop(state, 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void LuaManager::prepareTableForRetrieval(string table, vector<Index> &indices){
|
||||
lua_getglobal(state, table.c_str());
|
||||
|
||||
if(!lua_istable(state, -1))
|
||||
cout << table << " is not a table\n";
|
||||
|
||||
for(int i = 0; i < indices.size(); i++){
|
||||
if(indices[i].string)
|
||||
lua_pushstring(state, indices[i].index.c_str());
|
||||
else
|
||||
lua_pushnumber(state, atoi(indices[i].index.c_str()));
|
||||
|
||||
lua_gettable(state, -2);
|
||||
}
|
||||
}
|
||||
|
||||
int LuaManager::getIntFromTable(string table, vector<Index> indices){
|
||||
prepareTableForRetrieval(table, indices);
|
||||
|
||||
int isNum;
|
||||
int result = lua_tointegerx(state, -1, &isNum);
|
||||
|
||||
lua_pop(state, 1 + indices.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
float LuaManager::getFloatFromTable(string table, vector<Index> indices){
|
||||
prepareTableForRetrieval(table, indices);
|
||||
|
||||
int isNum;
|
||||
float result = lua_tonumberx(state, -1, &isNum);
|
||||
|
||||
lua_pop(state, 1 + indices.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LuaManager::getBoolFromTable(string table, vector<Index> indices){
|
||||
prepareTableForRetrieval(table, indices);
|
||||
bool result = lua_toboolean(state, -1);
|
||||
lua_pop(state, 1 + indices.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
string LuaManager::getStringFromTable(string table, vector<Index> indices){
|
||||
prepareTableForRetrieval(table, indices);
|
||||
string result = lua_tostring(state, -1);
|
||||
lua_pop(state, 1 + indices.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
#ifndef LUA_MANAGER_H
|
||||
#define LUA_MANAGER_H
|
||||
|
||||
extern "C"{
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
}
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace gameBase{
|
||||
class LuaManager{
|
||||
public:
|
||||
struct Index{
|
||||
std::string index;
|
||||
bool string;
|
||||
|
||||
Index(std::string index){
|
||||
this->index = index;
|
||||
this->string = true;
|
||||
}
|
||||
|
||||
Index(int index){
|
||||
this->index = std::to_string(index);
|
||||
this->string = false;
|
||||
}
|
||||
|
||||
Index(float index){
|
||||
this->index = std::to_string(index);
|
||||
this->string = false;
|
||||
}
|
||||
};
|
||||
|
||||
static LuaManager* getSingleton();
|
||||
inline lua_State* getState(){return state;}
|
||||
void buildScript(std::vector<std::string>);
|
||||
void executeCode(std::string, std::string = "Error executing code\n");
|
||||
int getInt(std::string);
|
||||
float getFloat(std::string);
|
||||
int getIntFromTable(std::string, std::vector<Index>);
|
||||
bool getBoolFromTable(std::string, std::vector<Index>);
|
||||
float getFloatFromTable(std::string, std::vector<Index>);
|
||||
std::string getStringFromTable(std::string, std::vector<Index>);
|
||||
std::string getString(std::string);
|
||||
private:
|
||||
LuaManager();
|
||||
void prepareTableForRetrieval(std::string, std::vector<Index>&);
|
||||
|
||||
lua_State *state = nullptr;
|
||||
};
|
||||
|
||||
typedef LuaManager::Index Index;
|
||||
}
|
||||
|
||||
#endif
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
#include "solUtil.h"
|
||||
|
||||
namespace gameBase{
|
||||
static lua_State *state = nullptr;
|
||||
|
||||
sol::state_view generateView(){
|
||||
if(!state){
|
||||
state = luaL_newstate();
|
||||
luaL_openlibs(state);
|
||||
}
|
||||
|
||||
sol::state_view view(state);
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef SOL_UTIL_H
|
||||
#define SOL_UTIL_H
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
}
|
||||
|
||||
namespace gameBase{
|
||||
sol::state_view generateView();
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user