revert back to sol2

This commit is contained in:
Hartmut Seichter 2020-12-11 22:54:27 +01:00
parent ce8e89af51
commit f3c17f6d03
16 changed files with 184 additions and 118 deletions

View file

@ -25,7 +25,7 @@ target_include_directories(
pwbinding pwbinding
PRIVATE PRIVATE
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.5/src ${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.5/src
${CMAKE_SOURCE_DIR}/src/deps/sol2-3.2.2/include ${CMAKE_SOURCE_DIR}/src/deps/sol2-2.20.6
PUBLIC PUBLIC
include include
) )
@ -36,4 +36,5 @@ target_link_libraries(pwbinding
pwsystem pwsystem
pwio pwio
pwscene pwscene
pwvisual) pwvisual
)

View file

@ -34,7 +34,7 @@ public:
script(); script();
~script(); ~script();
int eval(const std::string& s); int eval(const std::string_view &s);
static void initialize(); static void initialize();

View file

@ -8,7 +8,9 @@ runtime_lua &runtime_lua::get()
return instance; return instance;
} }
void runtime_lua::add(const std::string &name, runtime_lua::register_function_t f) { void runtime_lua::add(const std::string &name
,runtime_lua::register_function_t f)
{
_register_function_list[name] = f; _register_function_list[name] = f;
} }

View file

@ -14,6 +14,24 @@ PW_REGISTER_DECL_LUA(io)
PW_REGISTER_DECL_LUA(scene) PW_REGISTER_DECL_LUA(scene)
PW_REGISTER_DECL_LUA(visual) PW_REGISTER_DECL_LUA(visual)
void static_example()
{
// in SOL 2.20.6
// in SOL 3.2.2
}
struct script::state { struct script::state {
sol::state _state; sol::state _state;
@ -24,13 +42,19 @@ struct script::state {
void load_all(); void load_all();
void load(const std::string& name); void load(const std::string& name);
int run(const std::string &s); int eval(const std::string_view &code);
}; };
int script::state::run(const std::string &s) int script::state::eval(const std::string_view &code)
{ {
return _state.script(s.c_str()).get<int>(); auto results = _state.safe_script(code);
return 0;
// sol2
// return _state.script(s.c_str()).get<int>();
} }
void script::state::load(const std::string& name) void script::state::load(const std::string& name)
@ -80,20 +104,18 @@ script::~script()
{ {
} }
int script::eval(const std::string &s) int script::eval(const std::string_view &s)
{ {
return _state->run(s); return _state->eval(s);
} }
void script::initialize() void script::initialize()
{ {
PW_REGISTER_USE_LUA(core) PW_REGISTER_USE_LUA(core)
PW_REGISTER_USE_LUA(system) PW_REGISTER_USE_LUA(system)
PW_REGISTER_USE_LUA(io) PW_REGISTER_USE_LUA(io)
PW_REGISTER_USE_LUA(scene) PW_REGISTER_USE_LUA(scene)
PW_REGISTER_USE_LUA(visual) PW_REGISTER_USE_LUA(visual)
} }

View file

@ -13,9 +13,9 @@
// seems CRTP magic doesnt work with SOL // seems CRTP magic doesnt work with SOL
namespace sol { namespace sol {
template <> struct is_automagical<pw::matrix4x4> : std::false_type {}; template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {}; template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {}; template <> struct is_automagical<pw::quaternion> : std::false_type {};
} }
namespace pw { namespace pw {
@ -23,109 +23,113 @@ namespace pw {
void register_core_function(sol::state& lua,sol::table& ns) void register_core_function(sol::state& lua,sol::table& ns)
{ {
typedef real_t Scalar; typedef real_t Scalar;
ns.set("pi",pw::pi<Scalar>()); ns.set("pi",pw::pi<Scalar>());
ns.new_usertype<matrix4x4>("matrix4x4" ns.new_usertype<matrix4x4>("matrix4x4"
, sol::constructors<matrix4x4()>() , sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row , "row",&matrix4x4::row
, "column",&matrix4x4::column , "column",&matrix4x4::column
, "set_identity",&matrix4x4::set_identity , "set_identity",&matrix4x4::set_identity
, "inverse",&matrix4x4::inverse , "inverse",&matrix4x4::inverse
); );
ns.new_usertype<vector3>("vector3" ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>() ,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}) ,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}) ,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}) ,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
,"cross",&vector3::cross ,"cross",&vector3::cross
,"transposed",&vector3::transposed ,"transposed",&vector3::transposed
,"lerp",&vector3::lerp ,"lerp",&vector3::lerp
); );
ns.new_usertype<quaternion>("quaternion" ns.new_usertype<quaternion>("quaternion"
, sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>() ,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}) ,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}) ,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}) ,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}) ,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;})
,"identity",sol::readonly_property(&quaternion::identity) ,"identity",sol::readonly_property(&quaternion::identity)
,"dot",&quaternion::dot ,"dot",&quaternion::dot
,"inverse",sol::readonly_property(&quaternion::inverse) ,"inverse",sol::readonly_property(&quaternion::inverse)
,"normalized",&quaternion::normalized ,"normalized",&quaternion::normalized
,"lerp",&quaternion::lerp ,"lerp",&quaternion::lerp
,"slerp",&quaternion::slerp ,"slerp",&quaternion::slerp
,"matrix",&quaternion::to_matrix ,"matrix",&quaternion::to_matrix
); );
ns.new_usertype<axisangle> ns.new_usertype<axisangle>
("axisangle", ("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(), sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",&axisangle::axis, "axis",&axisangle::axis,
"angle",&axisangle::angle, "angle",&axisangle::angle,
"from_matrix",&axisangle::from_matrix, "from_matrix",&axisangle::from_matrix,
"to_matrix",&axisangle::to_matrix "to_matrix",&axisangle::to_matrix
); );
ns.new_usertype<size>("size" ns.new_usertype<size>("size"
, sol::constructors<size(),size(Scalar,Scalar)>() , sol::constructors<size(),size(Scalar,Scalar)>()
, "width",&size::width , "width",&size::width
, "height",&size::height , "height",&size::height
); );
ns.new_usertype<sizei>("sizei" ns.new_usertype<sizei>("sizei"
, sol::constructors<sizei(),sizei(int,int)>() , sol::constructors<sizei(),sizei(int,int)>()
, "width",&sizei::width , "width",&sizei::width
, "height",&sizei::height , "height",&sizei::height
);
ns.new_usertype<point>("point",
sol::constructors<point(),point(Scalar,Scalar)>(),
"x",&point::x,
"y",&point::y
);
ns.new_usertype<debug>("debug",
"new",sol::no_constructor,
"get",&debug::get,
"write",&debug::write
// "none",sol::debug::level::none
);
ns.new_usertype<time>("time",
"now",sol::readonly_property(&time::now),
"elapsed",sol::readonly_property(&time::elapsed),
"reset",&time::reset
); );
ns.new_usertype<geometry>("geometry"
ns.new_usertype<point>("point", , sol::constructors<geometry()>()
sol::constructors<point(),point(Scalar,Scalar)>(), , "topology", sol::property(&geometry::topology,&geometry::set_topology)
"x",&point::x, , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
"y",&point::y , "indices", sol::property(&geometry::indices,&geometry::set_indices)
); ).new_enum<false>("topology_type"
,"points", geometry::topology_type::points
, "lines", geometry::topology_type::lines
, "line_strip", geometry::topology_type::line_strip);
ns.new_usertype<debug>("debug", // SOL3
"new",sol::no_constructor, // geoom_type["type"] = lua.create_table_with(
"get",&debug::get, // "points", geometry::topology_type::points
"write",&debug::write // , "lines", geometry::topology_type::lines
// "none",sol::debug::level::none // , "line_strip", geometry::topology_type::line_strip
); // );
ns.new_usertype<image>("image"
ns.new_usertype<time>("time", , sol::constructors<image()>()
"now",sol::readonly_property(&time::now),
"elapsed",sol::readonly_property(&time::elapsed),
"reset",&time::reset
);
auto geoom_type = ns.new_usertype<geometry>("geometry"
, sol::constructors<geometry()>()
, "topology", sol::property(&geometry::topology,&geometry::set_topology)
, "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
, "indices", sol::property(&geometry::indices,&geometry::set_indices)
);
geoom_type["type"] = lua.create_table_with(
"points", geometry::topology_type::points
, "lines", geometry::topology_type::lines
, "line_strip", geometry::topology_type::line_strip
);
auto image_type = ns.new_usertype<image>("image"
,"create",&image::create ,"create",&image::create
,"size",sol::readonly_property(&image::size) ,"size",sol::readonly_property(&image::size)
,"change_count",sol::property(&image::change_count,&image::set_change_count) ,"change_count",sol::property(&image::change_count,&image::set_change_count)
); ).new_enum<false>("layout"
,"rgb8", image::RGB8
// image_type.new_enum("layout" ,"rgb32", image::RGBA8
// ,"rgb8", image::RGB8 ,"gray", image::LUM);
// ,"rgb32", image::RGBA8
// ,"gray", image::LUM);
} }

View file

@ -12,7 +12,7 @@ void register_io_function(sol::state&,sol::table& ns)
,"new", sol::no_constructor ,"new", sol::no_constructor
,"get",&image_io::get ,"get",&image_io::get
,"read",&image_io::read ,"read",&image_io::read
, "write",&image_io::write ,"write",&image_io::write
); );
} }

View file

@ -6,19 +6,27 @@
#include "runtime_lua.hpp" #include "runtime_lua.hpp"
namespace sol {
template <> struct is_automagical<pw::entity> : std::false_type {};
}
namespace pw { namespace pw {
void register_scene_function(sol::state&,sol::table &ns) void register_scene_function(sol::state&,sol::table &ns)
{ {
ns.new_usertype<scene>("scene", ns.new_usertype<scene>("scene",
sol::constructors<scene()>()); sol::constructors<scene()>()
,"count_all_entities",sol::readonly_property(&scene::count_all_enties)
,"count_alive_entities",sol::readonly_property(&scene::count_alive_enties)
);
ns.new_usertype<entity>("entity", ns.new_usertype<entity>("entity"
sol::constructors</*entity(),*/entity(scene&)>(), ,sol::constructors<entity(),entity(const entity&),entity(scene&)>()
"add_child",&entity::add_child, ,"add_child",&entity::add_child
"child_count",sol::readonly_property(&entity::child_count) ,"child_count",sol::readonly_property(&entity::child_count)
); );

View file

@ -39,12 +39,12 @@ void register_system_function(sol::state&, sol::table &ns)
"name",sol::readonly_property(&display::name) "name",sol::readonly_property(&display::name)
); );
ns.new_usertype<path>("path" ns.new_usertype<path>("path"
,"new", sol::no_constructor ,"new", sol::no_constructor
,"get",&path::get ,"get",&path::get
,"separator",sol::readonly_property(&path::separator) ,"separator",sol::readonly_property(&path::separator)
,"executable_path",sol::readonly_property(&path::executable_path) ,"executable_path",sol::readonly_property(&path::executable_path)
); );
} }

View file

@ -62,6 +62,7 @@ public:
stream& operator << (const bool &value); stream& operator << (const bool &value);
stream& operator << (const char *value); stream& operator << (const char *value);
stream& operator << (const std::string& value); ///! log a string stream& operator << (const std::string& value); ///! log a string
stream& operator << (const std::string_view& value); ///! log a string_view
stream& operator << (const float &value); ///! log a float value stream& operator << (const float &value); ///! log a float value
stream& operator << (const double &value); ///! log a double value stream& operator << (const double &value); ///! log a double value

View file

@ -22,13 +22,6 @@ template <typename T> struct module {
template <typename T> typename module<T>::proxy module<T>::_proxy; template <typename T> typename module<T>::proxy module<T>::_proxy;
struct stuff {
};
module<stuff> mod;
} }

View file

@ -140,6 +140,12 @@ debug::stream &debug::stream::operator <<(const std::string &value)
return *this; return *this;
} }
debug::stream &debug::stream::operator <<(const std::string_view &value)
{
_line.append(value.data());
return *this;
}
debug::stream &debug::stream::operator <<(const float &value) debug::stream &debug::stream::operator <<(const float &value)
{ {
std::stringstream ss; std::stringstream ss;

View file

@ -64,7 +64,7 @@ public:
} }
template<typename T> template<typename T>
bool has_component() const { return _registry->valid(_entity);} bool has_component() const { return _registry->has<T>(_entity);}
void destroy(); void destroy();

View file

@ -38,6 +38,9 @@ public:
scene(); scene();
~scene() = default; ~scene() = default;
size_t count_all_enties() const;
size_t count_alive_enties() const;
protected: protected:
std::shared_ptr<entt::registry> _registry; std::shared_ptr<entt::registry> _registry;

View file

@ -8,5 +8,17 @@ scene::scene()
{ {
} }
size_t scene::count_all_enties() const
{
return _registry->size();
}
size_t scene::count_alive_enties() const
{
return _registry->alive();
}
} }

View file

@ -42,6 +42,7 @@ void test_stack()
e.add_child(e2); e.add_child(e2);
e.add_child(e3); e.add_child(e3);
std::cout << e.child_count() << std::endl; std::cout << e.child_count() << std::endl;
} }
@ -55,6 +56,13 @@ void test_heap()
auto e = std::make_unique<entity>(*s); auto e = std::make_unique<entity>(*s);
auto e2 = std::make_unique<entity>(*s);
e->add_child(*e2);
pw::debug::d() << e->child_count();
auto t = e->add_component<test>(112); auto t = e->add_component<test>(112);
std::cout << t.val << std::endl; std::cout << t.val << std::endl;

View file

@ -2,20 +2,26 @@
-- small demonstrator for Lua binding on pixwerx -- small demonstrator for Lua binding on pixwerx
-- --
-- loading our libraries
pw.script:load_all() pw.script:load_all()
print("hello pixwerx!") print("hello pixwerx!")
local s = pw.scene.new() local s = pw.scene.new()
e = pw.entity.new(s) print(s)
e2 = pw.entity.new(s)
local e = pw.entity.new(s)
c = pw.entity.new(s)
print(e) print(e)
e2.add_child(e) print(e.child_count)
-- print(s) -- add child
-- print(e2.child_count) e.add_child(c)
print(e.child_count)
print("bye, bye pixwerx!")
return 1