diff --git a/src/main.cpp b/src/main.cpp index 4bd56a2..37fd96d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,68 @@ -#include -#include -#include +// #include +// #include +// #include + +#define SOL_ALL_SAFETIES_ON 1 +#include + +void some_function() { + std::cout << "some function!" << std::endl; +} + +void some_other_function() { + std::cout << "some other function!" << std::endl; +} + +struct some_class { + int variable = 30; + + double member_function() { + return 24.5; + } +}; int main(int argc, char** argv) { // init GL context - td::Display display(1920, 1080, "Tower-Defense 2"); + // td::Display display(1920, 1080, "Tower-Defense 2"); - display.ChangeState(); + // display.ChangeState(); - td::Timer timer; - while (!display.IsCloseRequested()) { - display.PollEvents(); - display.Update(timer.GetDelta()); - } + // td::Timer timer; + // while (!display.IsCloseRequested()) { + // display.PollEvents(); + // display.Update(timer.GetDelta()); + // } + + std::cout << "=== functions (all) ===" << std::endl; + + sol::state lua; + lua.open_libraries(sol::lib::base); + + // put an instance of "some_class" into lua + // (we'll go into more detail about this later + // just know here that it works and is + // put into lua as a userdata + lua.set("sc", some_class()); + + // binds a plain function + lua["f1"] = some_function; + lua.set_function("f2", &some_other_function); + + // binds just the member function + lua["m1"] = &some_class::member_function; + + // binds the class to the type + lua.set_function("m2", &some_class::member_function, some_class{}); + + // binds just the member variable as a function + lua["v1"] = &some_class::variable; + + // binds class with member variable as function + lua.set_function("v2", &some_class::variable, some_class{}); + + lua.script_file("test/main.lua"); + + std::cout << std::endl; return 0; } diff --git a/test/main.lua b/test/main.lua new file mode 100644 index 0000000..0a61f16 --- /dev/null +++ b/test/main.lua @@ -0,0 +1,24 @@ +f1() -- some function! +f2() -- some other function! + +-- need class instance if you don't bind it with the function +print(m1(sc)) -- 24.5 +-- does not need class instance: was bound to lua with one +print(m2()) -- 24.5 + +-- need class instance if you +-- don't bind it with the function +print(v1(sc)) -- 30 +-- does not need class instance: +-- it was bound with one +print(v2()) -- 30 + +-- can set, still +-- requires instance +v1(sc, 212) +-- can set, does not need +-- class instance: was bound with one +v2(254) + +print(v1(sc)) -- 212 +print(v2()) -- 254 \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 0b93abe..1a48414 100644 --- a/xmake.lua +++ b/xmake.lua @@ -3,7 +3,7 @@ add_rules("mode.debug", "mode.release") add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git") add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}}) -add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6") +add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6", "sol2") set_languages("c++20") @@ -21,7 +21,7 @@ target("Tower-Defense2") add_includedirs("include", {public = true}) set_kind("binary") add_files("src/**.cpp") - add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", {public = true}) + add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", "sol2", {public = true}) set_rundir(".") add_defines("TD_GL_LOADER_GLEW")