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
PRIVATE
${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
include
)
@ -36,4 +36,5 @@ target_link_libraries(pwbinding
pwsystem
pwio
pwscene
pwvisual)
pwvisual
)

View file

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

View file

@ -8,7 +8,9 @@ runtime_lua &runtime_lua::get()
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;
}

View file

@ -14,6 +14,24 @@ PW_REGISTER_DECL_LUA(io)
PW_REGISTER_DECL_LUA(scene)
PW_REGISTER_DECL_LUA(visual)
void static_example()
{
// in SOL 2.20.6
// in SOL 3.2.2
}
struct script::state {
sol::state _state;
@ -24,13 +42,19 @@ struct script::state {
void load_all();
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)
@ -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()
{
PW_REGISTER_USE_LUA(core)
PW_REGISTER_USE_LUA(system)
PW_REGISTER_USE_LUA(io)
PW_REGISTER_USE_LUA(scene)
PW_REGISTER_USE_LUA(visual)
}

View file

@ -13,9 +13,9 @@
// seems CRTP magic doesnt work with SOL
namespace sol {
template <> struct is_automagical<pw::matrix4x4> : 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::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {};
}
namespace pw {
@ -23,109 +23,113 @@ namespace pw {
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"
, sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row
, "column",&matrix4x4::column
, "set_identity",&matrix4x4::set_identity
, "inverse",&matrix4x4::inverse
);
ns.new_usertype<matrix4x4>("matrix4x4"
, sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row
, "column",&matrix4x4::column
, "set_identity",&matrix4x4::set_identity
, "inverse",&matrix4x4::inverse
);
ns.new_usertype<vector3>("vector3"
,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;})
,"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;})
,"cross",&vector3::cross
,"transposed",&vector3::transposed
,"lerp",&vector3::lerp
);
ns.new_usertype<vector3>("vector3"
,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;})
,"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;})
,"cross",&vector3::cross
,"transposed",&vector3::transposed
,"lerp",&vector3::lerp
);
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;})
,"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;})
,"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)
,"dot",&quaternion::dot
,"inverse",sol::readonly_property(&quaternion::inverse)
,"normalized",&quaternion::normalized
,"lerp",&quaternion::lerp
,"slerp",&quaternion::slerp
,"inverse",sol::readonly_property(&quaternion::inverse)
,"normalized",&quaternion::normalized
,"lerp",&quaternion::lerp
,"slerp",&quaternion::slerp
,"matrix",&quaternion::to_matrix
);
);
ns.new_usertype<axisangle>
("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",&axisangle::axis,
"angle",&axisangle::angle,
"from_matrix",&axisangle::from_matrix,
"to_matrix",&axisangle::to_matrix
);
ns.new_usertype<axisangle>
("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",&axisangle::axis,
"angle",&axisangle::angle,
"from_matrix",&axisangle::from_matrix,
"to_matrix",&axisangle::to_matrix
);
ns.new_usertype<size>("size"
, sol::constructors<size(),size(Scalar,Scalar)>()
, "width",&size::width
, "height",&size::height
);
ns.new_usertype<size>("size"
, sol::constructors<size(),size(Scalar,Scalar)>()
, "width",&size::width
, "height",&size::height
);
ns.new_usertype<sizei>("sizei"
, sol::constructors<sizei(),sizei(int,int)>()
, "width",&sizei::width
, "height",&sizei::height
, sol::constructors<sizei(),sizei(int,int)>()
, "width",&sizei::width
, "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<point>("point",
sol::constructors<point(),point(Scalar,Scalar)>(),
"x",&point::x,
"y",&point::y
);
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)
).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",
"new",sol::no_constructor,
"get",&debug::get,
"write",&debug::write
// "none",sol::debug::level::none
);
// SOL3
// geoom_type["type"] = lua.create_table_with(
// "points", geometry::topology_type::points
// , "lines", geometry::topology_type::lines
// , "line_strip", geometry::topology_type::line_strip
// );
ns.new_usertype<time>("time",
"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"
ns.new_usertype<image>("image"
, sol::constructors<image()>()
,"create",&image::create
,"size",sol::readonly_property(&image::size)
,"change_count",sol::property(&image::change_count,&image::set_change_count)
);
// image_type.new_enum("layout"
// ,"rgb8", image::RGB8
// ,"rgb32", image::RGBA8
// ,"gray", image::LUM);
).new_enum<false>("layout"
,"rgb8", image::RGB8
,"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
,"get",&image_io::get
,"read",&image_io::read
, "write",&image_io::write
,"write",&image_io::write
);
}

View file

@ -6,19 +6,27 @@
#include "runtime_lua.hpp"
namespace sol {
template <> struct is_automagical<pw::entity> : std::false_type {};
}
namespace pw {
void register_scene_function(sol::state&,sol::table &ns)
{
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",
sol::constructors</*entity(),*/entity(scene&)>(),
"add_child",&entity::add_child,
"child_count",sol::readonly_property(&entity::child_count)
ns.new_usertype<entity>("entity"
,sol::constructors<entity(),entity(const entity&),entity(scene&)>()
,"add_child",&entity::add_child
,"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)
);
ns.new_usertype<path>("path"
,"new", sol::no_constructor
,"get",&path::get
,"separator",sol::readonly_property(&path::separator)
,"executable_path",sol::readonly_property(&path::executable_path)
);
ns.new_usertype<path>("path"
,"new", sol::no_constructor
,"get",&path::get
,"separator",sol::readonly_property(&path::separator)
,"executable_path",sol::readonly_property(&path::executable_path)
);
}

View file

@ -62,6 +62,7 @@ public:
stream& operator << (const bool &value);
stream& operator << (const char *value);
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 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;
struct stuff {
};
module<stuff> mod;
}

View file

@ -140,6 +140,12 @@ debug::stream &debug::stream::operator <<(const std::string &value)
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)
{
std::stringstream ss;

View file

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

View file

@ -38,6 +38,9 @@ public:
scene();
~scene() = default;
size_t count_all_enties() const;
size_t count_alive_enties() const;
protected:
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(e3);
std::cout << e.child_count() << std::endl;
}
@ -55,6 +56,13 @@ void test_heap()
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);
std::cout << t.val << std::endl;

View file

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