diff --git a/Makefile b/Makefile index 5564aa5..ae8e79c 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ OBJ_DIR := obj BIN_DIR := bin EXE := $(BIN_DIR)/out -OBJ_OUT_DIRS := obj/Main obj/LuaHandler obj/Components obj/Components/Objects obj/Components/Objects/TopLevel -SRC := $(SRC_DIR)/Main/Main.cpp $(SRC_DIR)/LuaHandler/LuaHandler.cpp $(SRC_DIR)/Components/GameObject.cpp -OBJ := $(OBJ_DIR)/Main/Main.o $(OBJ_DIR)/LuaHandler/LuaHandler.o $(OBJ_DIR)/Components/GameObject.o -CC := g++ +OBJ_OUT_DIRS := obj/Main obj/Components obj/Components obj/Components/TopLevel +SRC := $(SRC_DIR)/Main/Main.cpp $(SRC_DIR)/Components/TopLevel/GameObject.cpp $(SRC_DIR)/Components/TopLevel/Game.cpp +OBJ := $(OBJ_DIR)/Main/Main.o $(OBJ_DIR)/Components/TopLevel/GameObject.o $(OBJ_DIR)/Components/TopLevel/Game.o +CC := g++ -std=c++17 CPPFLAGS := -I/usr/include/$(LUA) -Iinclude -MMD -MP CFLAGS := -Wall diff --git a/Script.lua b/Script.lua index 8c29f65..646b4ab 100644 --- a/Script.lua +++ b/Script.lua @@ -1 +1,3 @@ -libepic.say_something("lol") \ No newline at end of file +for i, v in pairs(program.game:GetChildren()) do + print(v.name) +end \ No newline at end of file diff --git a/include/Components/GameObject.hpp b/include/Components/GameObject.hpp deleted file mode 100644 index 0d06c60..0000000 --- a/include/Components/GameObject.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _GAMEOBJECT_H -#define _GAMEOBJECT_H - -#include - -struct GameObject { - const char* name; - bool is_top_level; - - GameObject* parent; - std::vector children; - std::map>> handlers; - - void add(GameObject* child); - std::vector& get_children(); - GameObject* get(const char* targetedName); -}; - -#endif \ No newline at end of file diff --git a/include/Components/Objects/TopLevel/Game.hpp b/include/Components/Objects/TopLevel/Game.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/include/Components/TopLevel/Game.hpp b/include/Components/TopLevel/Game.hpp new file mode 100644 index 0000000..5d64caf --- /dev/null +++ b/include/Components/TopLevel/Game.hpp @@ -0,0 +1,14 @@ +#ifndef _GAME_H +#define _GAME_H + +struct Game : public GameObject { + Game() : GameObject("game") { + std::cout << "[+] Game" << '\n'; + } + + bool isGame(); +}; + +void registerGame(lua_State* L); + +#endif \ No newline at end of file diff --git a/include/Components/TopLevel/GameObject.hpp b/include/Components/TopLevel/GameObject.hpp new file mode 100644 index 0000000..4b332b6 --- /dev/null +++ b/include/Components/TopLevel/GameObject.hpp @@ -0,0 +1,30 @@ +#ifndef _GAMEOBJECT_H +#define _GAMEOBJECT_H + +#include +#include +#include +#include +extern "C" { +#include +#include +} + +struct GameObject { + GameObject(std::string name) : name(name) {} + + std::string name; + std::string type; + + GameObject* parent; + std::vector children; + std::map> handlers; + + GameObject& Get(std::string name); + void Add(GameObject* obj); + std::vector& GetChildren(); +}; + +void registerGameObject(lua_State* L); + +#endif \ No newline at end of file diff --git a/include/LuaHandler/LuaHandler.hpp b/include/LuaHandler/LuaHandler.hpp deleted file mode 100644 index dc9442e..0000000 --- a/include/LuaHandler/LuaHandler.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _LUAHANDLER_H -#define _LUAHANDLER_H - -extern "C"{ -#include -#include -} - -struct LuaHandler { - lua_State* L; - - LuaHandler(lua_State* L) : L(L) {}; - - void defineClass(const char* className); - void defineFunction(const char* className, const char* valueName, lua_CFunction func); -}; - -#endif \ No newline at end of file diff --git a/include/vendor/LuaBridge3 b/include/vendor/LuaBridge3 new file mode 160000 index 0000000..d0a1deb --- /dev/null +++ b/include/vendor/LuaBridge3 @@ -0,0 +1 @@ +Subproject commit d0a1debdaea96460b172c3847f0b1b7eaa6f76a2 diff --git a/src/Components/GameObject.cpp b/src/Components/GameObject.cpp deleted file mode 100644 index 88ecd3c..0000000 --- a/src/Components/GameObject.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Bruh. - -#include -#include - -// todo: might want to make these static methods inside the gameobject class to make inheritence easier -// also see this has an example http://lua-users.org/wiki/UserDataExample - -void GameObject::add(GameObject* child) { - this->children.push_back(child); -} - -std::vector& GameObject::get_children() { - return this->children; -} - -GameObject* GameObject::get(const char* targetedName) { - // Yes, using a loop is inefficient or something - // Yes, an unsigned short can be a bit too small for this - // Eh, its fine - // FIXME: Also what if no child is found at all? Hmm well we could just return the target gameobject, that could work - for (unsigned short i = 0; i < this->children.size(); i++) { - if (this->children[i]->name == targetedName) { - return this->children[i]; - } - } -} \ No newline at end of file diff --git a/src/Components/Objects/TopLevel/Game.cpp b/src/Components/Objects/TopLevel/Game.cpp deleted file mode 100644 index 701d3ef..0000000 --- a/src/Components/Objects/TopLevel/Game.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include - -class Game : public GameObject { - -} \ No newline at end of file diff --git a/src/Components/TopLevel/Game.cpp b/src/Components/TopLevel/Game.cpp new file mode 100644 index 0000000..e00ee27 --- /dev/null +++ b/src/Components/TopLevel/Game.cpp @@ -0,0 +1,17 @@ +#include +#include + +#include // Pain + +bool Game::isGame() { + return true; +} + +void registerGame(lua_State* L) { + luabridge::getGlobalNamespace (L) + .beginNamespace ("Core") + .deriveClass ("Game") + .addFunction ("isGame", &Game::isGame) + .endClass () + .endNamespace (); +} \ No newline at end of file diff --git a/src/Components/TopLevel/GameObject.cpp b/src/Components/TopLevel/GameObject.cpp new file mode 100644 index 0000000..5cb91fe --- /dev/null +++ b/src/Components/TopLevel/GameObject.cpp @@ -0,0 +1,35 @@ +#include +#include // Pain +#include // Pain + +std::vector& GameObject::GetChildren() { + return this->children; +} + +GameObject& GameObject::Get(std::string name) { + // NOTE(hippoz): This is a mess. + // Firstly, I should be using find_if, not a loop + // Secondly, why is this a short? + for (short i = this->children.size()-1; i >= 0; i--) { + if (this->children[i]->name == name) { + return *this->children[i]; + } + } +} + +void GameObject::Add(GameObject* gameObject) { + this->children.push_back(gameObject); +} + + +void registerGameObject(lua_State* L) { + luabridge::getGlobalNamespace (L) + .beginNamespace ("Core") + .beginClass ("GameObject") + .addProperty ("name", &GameObject::name) + .addFunction ("GetChildren", &GameObject::GetChildren) + .addFunction ("Add", &GameObject::Add) + .addFunction ("Get", &GameObject::Get) + .endClass () + .endNamespace (); +} \ No newline at end of file diff --git a/src/LuaHandler/LuaHandler.cpp b/src/LuaHandler/LuaHandler.cpp deleted file mode 100644 index 48bc580..0000000 --- a/src/LuaHandler/LuaHandler.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -void LuaHandler::defineClass(const char* className) { - // Create a new table and assign it to a global variable - lua_newtable(L); - lua_setglobal(L, className); -} - -void LuaHandler::defineFunction(const char* className, const char* valueName, lua_CFunction func) { - // Get the table that should have been defined with LuaHandler::defineClass - lua_getglobal(L, className); - - // Push the function name, as well as the function onto the stack - lua_pushstring(L, valueName); - lua_pushcfunction(L, func); - - // Add the function inside the table, giving it the specified name - lua_settable(L, -3); - - // Keep the stack balanced - lua_pop(L, 1); -} \ No newline at end of file diff --git a/src/Main/Main.cpp b/src/Main/Main.cpp index edb154f..1fced43 100644 --- a/src/Main/Main.cpp +++ b/src/Main/Main.cpp @@ -1,20 +1,43 @@ +extern "C" { +#include +#include +#include +} + #include -#include +#include // Pain +#include +#include -static int say_something(lua_State* L) { - std::cout << "something: " << lua_tostring(L, 1) << "\n"; - return 1; +static Game game; + +void registerInit(lua_State* L) { + registerGameObject(L); + registerGame(L); + + luabridge::getGlobalNamespace (L) + .beginNamespace ("program") + .addProperty("game", &game) + .endNamespace (); } int main() { + GameObject gaming("gamer"); + game.Add(&gaming); + lua_State* L = luaL_newstate(); - LuaHandler handler(L); + luaL_openlibs(L); // TODO: dangerous - handler.defineClass("libepic"); - handler.defineFunction("libepic", "say_something", &say_something); + registerInit(L); - luaL_dofile(handler.L, "Script.lua"); + int ret = luaL_dofile(L, "Script.lua"); + + if (ret != 0) { + std::cout << "[E] Error running main script." << '\n'; + std::cout << " -> " << lua_tostring(L, -1) << '\n'; + return 2; + } return 0; } \ No newline at end of file