diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dd0225..a4ba3c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,15 +5,22 @@ set(CMAKE_BUILD_TYPE Debug) cmake_policy(SET CMP0015 NEW) set(states abstractAppState.cpp) -set(core inputManager.cpp mapping.h stateManager.cpp) +set(core inputManager.cpp mapping.h stateManager.cpp luaManager.cpp) set(util util.cpp) + set(GLFW_DIR vb01/external/glfw) +set(LUA_DIR external/lua-5.4.4/src) include_directories(../${GLFW_DIR}/include/GLFW) +#set(LUA_LIB_DIR ${LUA_DIR}) +include_directories(${LUA_DIR}) +link_directories(${LUA_DIR}) + add_library(gameBase ${states} ${core} ${util}) set(LIB_NAME gameBase) link_directories(../../build/external/${GLFW_DIR}/src) -target_link_libraries(${LIB_NAME} PUBLIC glfw) +target_link_libraries(${LIB_NAME} glfw) +target_link_libraries(${LIB_NAME} lua) diff --git a/abstractAppState.cpp b/abstractAppState.cpp index 0ff855f..6248558 100644 --- a/abstractAppState.cpp +++ b/abstractAppState.cpp @@ -1,30 +1,33 @@ #include "abstractAppState.h" #include "util.h" #include "mapping.h" - -using namespace std; +#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; + this->numMappings = numMappings; + this->optionsFile = optionsFile; + } + void AbstractAppState::onAttached(){ attached = true; - for(int i = 0; i < bindingsLines.size(); i++){ - int semicolon, numFoundCommas = 0; - int commaIds[3]; + LuaManager *luaManager = LuaManager::getSingleton(); + luaManager->buildScript(vector{optionsFile}); + string table = "mappings"; - for(int j = 0; j < bindingsLines[i].length(); j++){ - if(bindingsLines[i].c_str()[j] == ':') - semicolon = j; - else if(bindingsLines[i].c_str()[j] == ','){ - commaIds[numFoundCommas] = j; - numFoundCommas++; - } - } - - int bind = atoi(bindingsLines[i].substr(semicolon + 1, commaIds[0] - semicolon).c_str()); - Mapping::BindType type = (Mapping::BindType)atoi(bindingsLines[i].substr(commaIds[0] + 1, commaIds[1] - commaIds[0]).c_str()); - bool action = atoi(bindingsLines[i].substr(commaIds[1] + 1, commaIds[2] - commaIds[1]).c_str()); - int trigger = atoi(bindingsLines[i].substr(commaIds[2] + 1, string::npos).c_str()); + for(int i = firstMapping; i < firstMapping + numMappings; i++){ + Index idIndex = Index(to_string(i + 1), false); + int bind = luaManager->getIntFromTable(table, vector{idIndex, Index("bind", true)}); + Mapping::BindType type = (Mapping::BindType)luaManager->getIntFromTable(table, vector{idIndex, Index("bindType", true)}); + bool action = luaManager->getBoolFromTable(table, vector{idIndex, Index("action", true)}); + int trigger = luaManager->getIntFromTable(table, vector{idIndex, Index("trigger", true)}); Mapping *m = new Mapping; m->bind = bind; @@ -39,7 +42,6 @@ namespace gameBase { attached = false; mappings.clear(); - bindingsLines.clear(); } void AbstractAppState::removeMapping(int bind){ diff --git a/abstractAppState.h b/abstractAppState.h index f0cc7c4..9a8d3fd 100644 --- a/abstractAppState.h +++ b/abstractAppState.h @@ -9,6 +9,7 @@ namespace gameBase { class AbstractAppState{ public: + AbstractAppState(int, int, int, std::string); virtual void update(){} virtual void onAttached(); virtual void onDettached(); @@ -30,8 +31,8 @@ namespace gameBase { ~AbstractAppState(){} std::vector mappings; - std::vector bindingsLines; - int type; + std::string optionsFile; + int type, firstMapping, numMappings; bool attached = false; }; } diff --git a/luaManager.cpp b/luaManager.cpp new file mode 100644 index 0000000..88297e3 --- /dev/null +++ b/luaManager.cpp @@ -0,0 +1,92 @@ +#include "luaManager.h" + +#include + +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 files){ + string script = ""; + + for(string f : files) + script += "dofile(\"" + f + "\");"; + + if(luaL_dostring(state, script.c_str()) && lua_pcall(state, 0, 0, 0)) + cout << "Error building Lua script\n"; + } + + 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; + } + + void LuaManager::prepareTableForRetrieval(string table, vector &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 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 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 indices){ + prepareTableForRetrieval(table, indices); + bool result = lua_toboolean(state, -1); + lua_pop(state, 1 + indices.size()); + + return result; + } + + string LuaManager::getStringFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + string result = lua_tostring(state, -1); + lua_pop(state, 1 + indices.size()); + + return result; + } +} diff --git a/luaManager.h b/luaManager.h new file mode 100644 index 0000000..fd1b146 --- /dev/null +++ b/luaManager.h @@ -0,0 +1,40 @@ +#ifndef LUA_MANAGER_H +#define LUA_MANAGER_H + +#include +#include +#include + +#include +#include + +namespace gameBase{ + class LuaManager{ + public: + struct Index{ + std::string index; + bool string; + + Index(std::string index, bool string){ + this->index = index; + this->string = string; + } + }; + + static LuaManager* getSingleton(); + inline lua_State* getState(){return state;} + void buildScript(std::vector); + int getInt(std::string); + int getIntFromTable(std::string, std::vector); + bool getBoolFromTable(std::string, std::vector); + float getFloatFromTable(std::string, std::vector); + std::string getStringFromTable(std::string, std::vector); + private: + LuaManager(); + void prepareTableForRetrieval(std::string, std::vector&); + + lua_State *state = nullptr; + }; +} + +#endif