From 225cda4a145b0dcb93d7efe434625de256862b23 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 30 Jul 2023 17:15:02 +0300 Subject: [PATCH 1/8] removed CMake-less Lua submodule --- .gitmodules | 3 --- external/lua | 1 - 2 files changed, 4 deletions(-) delete mode 160000 external/lua diff --git a/.gitmodules b/.gitmodules index 1c4ef8f..6a3c5b3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "external/glfw"] path = external/glfw url = https://github.com/glfw/glfw -[submodule "external/lua"] - path = external/lua - url = https://github.com/lua/lua 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 From 84d70b281b863e57cfacecbb9623faddc2ec05ce Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 30 Jul 2023 17:25:31 +0300 Subject: [PATCH 2/8] using Lua dep as a CMake project --- .gitmodules | 3 +++ CMakeLists.txt | 13 +++++-------- external/lua-cmake | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) create mode 160000 external/lua-cmake diff --git a/.gitmodules b/.gitmodules index 6a3c5b3..f71aab3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "external/glfw"] path = external/glfw url = https://github.com/glfw/glfw +[submodule "external/lua-cmake"] + path = external/lua-cmake + url = https://github.com/lubgr/lua-cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a1f8509..924d6c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,22 +10,19 @@ 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) +set(LUA_DIR external/lua-cmake) include_directories(../${GLFW_DIR}/include/GLFW) set(LUA_LIB_DIR ${LUA_DIR}) -include_directories(${LUA_DIR}) -link_directories(${LUA_DIR}) +add_subdirectory(${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_DIR ../vb01/external/glfw) set(GLFW_LIB_DIR ../vb01/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) - +target_link_libraries(${LIB_NAME} glfw lua::lib) 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 From b311ea23af0595d983ba5685be5ea54fc568f5fa Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 31 Jul 2023 20:01:37 +0300 Subject: [PATCH 3/8] replaced lua.hpp include with externed *.h includes --- CMakeLists.txt | 13 ++++--------- luaManager.h | 8 +++++--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 924d6c3..dcd234a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,20 +9,15 @@ set(states abstractAppState.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-cmake) - -include_directories(../${GLFW_DIR}/include/GLFW) - -set(LUA_LIB_DIR ${LUA_DIR}) -add_subdirectory(${LUA_DIR}) - set(LIB_NAME gameBase) add_library(${LIB_NAME} STATIC ${states} ${core} ${util}) -set(GLFW_DIR ../vb01/external/glfw) +set(GLFW_DIR external/glfw) set(GLFW_LIB_DIR ../vb01/build/external/glfw/src) target_include_directories(${LIB_NAME} PUBLIC ${GLFW_DIR}/include/GLFW) 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) target_link_libraries(${LIB_NAME} glfw lua::lib) diff --git a/luaManager.h b/luaManager.h index fcd0826..f65fdd5 100644 --- a/luaManager.h +++ b/luaManager.h @@ -1,9 +1,11 @@ #ifndef LUA_MANAGER_H #define LUA_MANAGER_H -#include -#include -#include +extern "C"{ + #include + #include + #include +} #include #include From 3760eca97453665199c6faf21880f5b4e49ea3dc Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 15 Aug 2023 15:00:19 +0300 Subject: [PATCH 4/8] updated GLFW link dir --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dcd234a..664b70a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(LIB_NAME gameBase) add_library(${LIB_NAME} STATIC ${states} ${core} ${util}) set(GLFW_DIR external/glfw) -set(GLFW_LIB_DIR ../vb01/build/external/glfw/src) +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}) From 83c34e373a04c6ecc09c20da5814e428be7b9d8c Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 15 Aug 2023 15:11:19 +0300 Subject: [PATCH 5/8] added sol submodule --- .gitmodules | 3 +++ CMakeLists.txt | 5 +++++ external/sol2 | 1 + 3 files changed, 9 insertions(+) create mode 160000 external/sol2 diff --git a/.gitmodules b/.gitmodules index f71aab3..f70a6e3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 664b70a..540fc92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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/sol) + target_link_libraries(${LIB_NAME} glfw lua::lib) 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 From 53934504a7c8c1aa34c9688022dcffe68499a622 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 15 Aug 2023 15:14:06 +0300 Subject: [PATCH 6/8] removed LuaManager class --- CMakeLists.txt | 2 +- luaManager.cpp | 112 ------------------------------------------------- luaManager.h | 57 ------------------------- 3 files changed, 1 insertion(+), 170 deletions(-) delete mode 100644 luaManager.cpp delete mode 100644 luaManager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 540fc92..a2d2821 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ 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(core inputManager.cpp mapping.h stateManager.cpp) set(util util.cpp) set(LIB_NAME gameBase) 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 f65fdd5..0000000 --- a/luaManager.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef LUA_MANAGER_H -#define LUA_MANAGER_H - -extern "C"{ - #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 From aba7c30b082b7c7ab083cce2af4d3be11198d875 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 2 Sep 2023 18:31:11 +0300 Subject: [PATCH 7/8] using sol in code --- CMakeLists.txt | 2 +- abstractAppState.cpp | 21 ++++++--------------- solUtil.cpp | 0 solUtil.h | 11 +++++++++++ util.h | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 solUtil.cpp create mode 100644 solUtil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a2d2821..933bedb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,6 @@ 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/sol) +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..97ec8cb 100644 --- a/abstractAppState.cpp +++ b/abstractAppState.cpp @@ -1,13 +1,11 @@ #include "abstractAppState.h" #include "util.h" +#include "solUtil.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 +16,15 @@ namespace gameBase { void AbstractAppState::onAttached(){ attached = true; - LuaManager *luaManager = LuaManager::getSingleton(); - luaManager->buildScript(vector{optionsFile}); + 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/solUtil.cpp b/solUtil.cpp new file mode 100644 index 0000000..e69de29 diff --git a/solUtil.h b/solUtil.h new file mode 100644 index 0000000..a5f660c --- /dev/null +++ b/solUtil.h @@ -0,0 +1,11 @@ +#ifndef SOL_UTIL_H +#define SOL_UTIL_H +#define SOL_ALL_SAFETIES_ON 1 + +#include + +namespace gameBase{ + static sol::state sol, &SOL_LUA_STATE = sol; +} + +#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 From 3f0a70a681a2ee337ea7dc47e0f504f8f66bdc9d Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 3 Sep 2023 09:01:49 +0300 Subject: [PATCH 8/8] generating sol views from one static lua state --- CMakeLists.txt | 2 +- abstractAppState.cpp | 4 +++- abstractAppState.h | 5 +++++ solUtil.cpp | 16 ++++++++++++++++ solUtil.h | 8 +++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 933bedb..ba3646b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(states abstractAppState.cpp) set(core inputManager.cpp mapping.h stateManager.cpp) -set(util util.cpp) +set(util solUtil.cpp util.cpp) set(LIB_NAME gameBase) add_library(${LIB_NAME} STATIC ${states} ${core} ${util}) diff --git a/abstractAppState.cpp b/abstractAppState.cpp index 97ec8cb..3d67521 100644 --- a/abstractAppState.cpp +++ b/abstractAppState.cpp @@ -1,6 +1,7 @@ +#include + #include "abstractAppState.h" #include "util.h" -#include "solUtil.h" #include "mapping.h" namespace gameBase { @@ -16,6 +17,7 @@ namespace gameBase { void AbstractAppState::onAttached(){ attached = true; + sol::state_view SOL_LUA_STATE = generateView(); SOL_LUA_STATE.script_file(optionsFile); string table = "mappings"; 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/solUtil.cpp b/solUtil.cpp index e69de29..3993562 100644 --- a/solUtil.cpp +++ 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 index a5f660c..e02682a 100644 --- a/solUtil.h +++ b/solUtil.h @@ -4,8 +4,14 @@ #include +extern "C" { + #include + #include + #include +} + namespace gameBase{ - static sol::state sol, &SOL_LUA_STATE = sol; + sol::state_view generateView(); } #endif