diff --git a/.gitmodules b/.gitmodules index 1c4ef8f..f70a6e3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "external/glfw"] path = external/glfw url = https://github.com/glfw/glfw -[submodule "external/lua"] - path = external/lua - url = https://github.com/lua/lua +[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 diff --git a/CMakeLists.txt b/CMakeLists.txt index a1f8509..ba3646b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,26 +6,23 @@ 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(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) +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_link_directories(${LIB_NAME} PUBLIC ${GLFW_LIB_DIR}) -target_link_libraries(${LIB_NAME} lua) -target_link_libraries(gameBase glfw) +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) diff --git a/abstractAppState.cpp b/abstractAppState.cpp index 23a09d0..3d67521 100644 --- a/abstractAppState.cpp +++ b/abstractAppState.cpp @@ -1,13 +1,12 @@ +#include + #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{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{idIndex, Index("bind")}); - Mapping::BindType type = (Mapping::BindType)luaManager->getIntFromTable(table, vector{idIndex, Index("bindType")}); - bool action = luaManager->getBoolFromTable(table, vector{idIndex, Index("action")}); - int trigger = luaManager->getIntFromTable(table, vector{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); } } diff --git a/abstractAppState.h b/abstractAppState.h index 9a8d3fd..d08c71e 100644 --- a/abstractAppState.h +++ b/abstractAppState.h @@ -2,10 +2,15 @@ #define ABSTRACT_APP_STATE_H #include "mapping.h" +#include "solUtil.h" #include #include +namespace sol{ + class state; +} + namespace gameBase { class AbstractAppState{ public: diff --git a/external/lua b/external/lua deleted file mode 160000 index 8426d9b..0000000 --- a/external/lua +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8426d9b4d4df1da3c5b2d759e509ae1c50a86667 diff --git a/external/lua-cmake b/external/lua-cmake new file mode 160000 index 0000000..ca210c5 --- /dev/null +++ b/external/lua-cmake @@ -0,0 +1 @@ +Subproject commit ca210c56ae41ff13b20490cca8be5ead47b24196 diff --git a/external/sol2 b/external/sol2 new file mode 160000 index 0000000..9c882a2 --- /dev/null +++ b/external/sol2 @@ -0,0 +1 @@ +Subproject commit 9c882a28fdb6f4ad79a53a4191b43ce48a661175 diff --git a/luaManager.cpp b/luaManager.cpp deleted file mode 100644 index 2fe81c2..0000000 --- a/luaManager.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#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 + "\");"; - - 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 &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 deleted file mode 100644 index fcd0826..0000000 --- a/luaManager.h +++ /dev/null @@ -1,55 +0,0 @@ -#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){ - 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); - void executeCode(std::string, std::string = "Error executing code\n"); - int getInt(std::string); - float getFloat(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); - std::string getString(std::string); - private: - LuaManager(); - void prepareTableForRetrieval(std::string, std::vector&); - - lua_State *state = nullptr; - }; - - typedef LuaManager::Index Index; -} - -#endif diff --git a/solUtil.cpp b/solUtil.cpp new file mode 100644 index 0000000..3993562 --- /dev/null +++ b/solUtil.cpp @@ -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; + } +} + diff --git a/solUtil.h b/solUtil.h new file mode 100644 index 0000000..e02682a --- /dev/null +++ b/solUtil.h @@ -0,0 +1,17 @@ +#ifndef SOL_UTIL_H +#define SOL_UTIL_H +#define SOL_ALL_SAFETIES_ON 1 + +#include + +extern "C" { + #include + #include + #include +} + +namespace gameBase{ + sol::state_view generateView(); +} + +#endif diff --git a/util.h b/util.h index 50c1e32..39394a8 100644 --- a/util.h +++ b/util.h @@ -1,5 +1,5 @@ -#ifndef UTILS_H -#define UTILS_H +#ifndef GAME_BASE_UTIL_H +#define GAME_BASE_UTIL_H #include #include