Merge pull request #9 from devZoGok/develop

Merge into master
This commit is contained in:
devZoGok
2023-12-30 12:57:19 +00:00
committed by GitHub
12 changed files with 69 additions and 204 deletions
+6 -3
View File
@@ -1,6 +1,9 @@
[submodule "external/glfw"] [submodule "external/glfw"]
path = external/glfw path = external/glfw
url = https://github.com/glfw/glfw url = https://github.com/glfw/glfw
[submodule "external/lua"] [submodule "external/lua-cmake"]
path = external/lua path = external/lua-cmake
url = https://github.com/lua/lua url = https://github.com/lubgr/lua-cmake
[submodule "external/sol2"]
path = external/sol2
url = https://github.com/ThePhD/sol2
+13 -16
View File
@@ -6,26 +6,23 @@ cmake_policy(SET CMP0015 NEW)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(states abstractAppState.cpp) set(states abstractAppState.cpp)
set(core inputManager.cpp mapping.h stateManager.cpp luaManager.cpp) set(core inputManager.cpp mapping.h stateManager.cpp)
set(util util.cpp) set(util solUtil.cpp util.cpp)
set(GLFW_DIR vb01/external/glfw)
set(LUA_DIR external/lua)
include_directories(../${GLFW_DIR}/include/GLFW)
set(LUA_LIB_DIR ${LUA_DIR})
include_directories(${LUA_DIR})
link_directories(${LUA_DIR})
add_library(gameBase STATIC ${states} ${core} ${util})
set(GLFW_DIR ../vb01/external/glfw)
set(LIB_NAME gameBase) set(LIB_NAME gameBase)
add_library(${LIB_NAME} STATIC ${states} ${core} ${util})
set(GLFW_LIB_DIR ../vb01/build/external/glfw/src) set(GLFW_DIR external/glfw)
set(GLFW_LIB_DIR build/external/glfw/src)
target_include_directories(${LIB_NAME} PUBLIC ${GLFW_DIR}/include/GLFW) target_include_directories(${LIB_NAME} PUBLIC ${GLFW_DIR}/include/GLFW)
target_link_directories(${LIB_NAME} PUBLIC ${GLFW_LIB_DIR}) target_link_directories(${LIB_NAME} PUBLIC ${GLFW_LIB_DIR})
target_link_libraries(${LIB_NAME} lua) set(LUA_DIR external/lua-cmake)
target_link_libraries(gameBase glfw) 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
View File
@@ -1,13 +1,12 @@
#include <sol/sol.hpp>
#include "abstractAppState.h" #include "abstractAppState.h"
#include "util.h" #include "util.h"
#include "mapping.h" #include "mapping.h"
#include "luaManager.h"
namespace gameBase { namespace gameBase {
using namespace std; using namespace std;
typedef LuaManager::Index Index;
AbstractAppState::AbstractAppState(int type, int firstMapping, int numMappings, string optionsFile){ AbstractAppState::AbstractAppState(int type, int firstMapping, int numMappings, string optionsFile){
this->type = type; this->type = type;
this->firstMapping = firstMapping; this->firstMapping = firstMapping;
@@ -18,22 +17,16 @@ namespace gameBase {
void AbstractAppState::onAttached(){ void AbstractAppState::onAttached(){
attached = true; attached = true;
LuaManager *luaManager = LuaManager::getSingleton(); sol::state_view SOL_LUA_STATE = generateView();
luaManager->buildScript(vector<string>{optionsFile}); SOL_LUA_STATE.script_file(optionsFile);
string table = "mappings"; string table = "mappings";
for(int i = firstMapping; i < firstMapping + numMappings; i++){ 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; Mapping *m = new Mapping;
m->bind = bind; m->bind = SOL_LUA_STATE[table][i + 1]["bind"];
m->type = type; m->type = (Mapping::BindType)SOL_LUA_STATE[table][i + 1]["bindType"];
m->action = action; m->action = SOL_LUA_STATE[table][i + 1]["action"];
m->trigger = trigger; m->trigger = SOL_LUA_STATE[table][i + 1]["trigger"];
mappings.push_back(m); mappings.push_back(m);
} }
} }
+5
View File
@@ -2,10 +2,15 @@
#define ABSTRACT_APP_STATE_H #define ABSTRACT_APP_STATE_H
#include "mapping.h" #include "mapping.h"
#include "solUtil.h"
#include <string> #include <string>
#include <vector> #include <vector>
namespace sol{
class state;
}
namespace gameBase { namespace gameBase {
class AbstractAppState{ class AbstractAppState{
public: public:
-1
Submodule external/lua deleted from 8426d9b4d4
Vendored Submodule
+1
Submodule external/lua-cmake added at ca210c56ae
Vendored Submodule
+1
Submodule external/sol2 added at 9c882a28fd
-112
View File
@@ -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;
}
}
-55
View File
@@ -1,55 +0,0 @@
#ifndef LUA_MANAGER_H
#define LUA_MANAGER_H
#include <lua.hpp>
#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
View File
@@ -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;
}
}
+17
View File
@@ -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
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef UTILS_H #ifndef GAME_BASE_UTIL_H
#define UTILS_H #define GAME_BASE_UTIL_H
#include <vector> #include <vector>
#include <fstream> #include <fstream>