added new GLFW
This commit is contained in:
parent
a9626cc33f
commit
e01df69575
383 changed files with 130844 additions and 1 deletions
39
src/binding/CMakeLists.txt
Normal file
39
src/binding/CMakeLists.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
set(hdrs
|
||||
include/pw/scripting/script.hpp
|
||||
)
|
||||
|
||||
set(srcs
|
||||
src/script.cpp
|
||||
src/script_core.cpp
|
||||
src/script_system.cpp
|
||||
src/script_io.cpp
|
||||
src/script_scene.cpp
|
||||
src/script_visual.cpp
|
||||
src/runtime_lua.hpp
|
||||
src/runtime_lua.cpp
|
||||
)
|
||||
|
||||
add_library(pwscripting
|
||||
STATIC
|
||||
${hdrs}
|
||||
${srcs}
|
||||
)
|
||||
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.5/src
|
||||
${CMAKE_SOURCE_DIR}/src/deps/sol2-2.20.6
|
||||
PUBLIC
|
||||
include
|
||||
)
|
||||
|
||||
target_link_libraries(pwscripting
|
||||
lualib
|
||||
pwcore
|
||||
pwsystem
|
||||
pwio
|
||||
pwscene
|
||||
pwvisual)
|
51
src/binding/include/pw/scripting/script.hpp
Normal file
51
src/binding/include/pw/scripting/script.hpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 1999-2019 Hartmut Seichter
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
#ifndef PW_SCRIPTING_SCRIPT_HPP
|
||||
#define PW_SCRIPTING_SCRIPT_HPP
|
||||
|
||||
#include <pw/core/globals.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class script {
|
||||
public:
|
||||
|
||||
|
||||
script();
|
||||
~script();
|
||||
|
||||
int eval(const std::string& s);
|
||||
|
||||
static void initialize();
|
||||
|
||||
protected:
|
||||
|
||||
struct state;
|
||||
std::unique_ptr<state> _state;
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
0
src/binding/src/CMakeLists.txt
Normal file
0
src/binding/src/CMakeLists.txt
Normal file
19
src/binding/src/runtime_lua.cpp
Normal file
19
src/binding/src/runtime_lua.cpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "runtime_lua.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
runtime_lua &runtime_lua::get()
|
||||
{
|
||||
static runtime_lua instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void runtime_lua::add(const std::string &name, runtime_lua::register_function_t f) {
|
||||
_register_function_list[name] = f;
|
||||
}
|
||||
|
||||
runtime_lua::runtime_lua()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
42
src/binding/src/runtime_lua.hpp
Normal file
42
src/binding/src/runtime_lua.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef PW_SCRIPTING_RUNTIME_LUA_HPP
|
||||
#define PW_SCRIPTING_RUNTIME_LUA_HPP
|
||||
|
||||
#include "sol/sol.hpp"
|
||||
#include <lua.hpp>
|
||||
#include <map>
|
||||
|
||||
#include <pw/core/debug.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class runtime_lua {
|
||||
public:
|
||||
typedef std::function<void(sol::state&,sol::table&)> register_function_t;
|
||||
typedef std::map<std::string,register_function_t> register_func_list_t;
|
||||
|
||||
static runtime_lua& get();
|
||||
|
||||
register_func_list_t register_functions() { return _register_function_list; }
|
||||
|
||||
void add(const std::string& name,register_function_t f);
|
||||
|
||||
protected:
|
||||
|
||||
runtime_lua();
|
||||
|
||||
register_func_list_t _register_function_list;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#define PW_REGISTER_LUA(name) void register_##name##_lua() { runtime_lua::get().add(#name,register_##name##_function); }
|
||||
|
||||
#define PW_REGISTER_DECL_LUA(name) void register_##name##_lua();
|
||||
|
||||
#define PW_REGISTER_USE_LUA(name) register_##name##_lua();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
104
src/binding/src/script.cpp
Normal file
104
src/binding/src/script.cpp
Normal file
|
@ -0,0 +1,104 @@
|
|||
#include "pw/scripting/script.hpp"
|
||||
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
#include "pw/core/debug.hpp"
|
||||
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
||||
PW_REGISTER_DECL_LUA(core)
|
||||
PW_REGISTER_DECL_LUA(system)
|
||||
PW_REGISTER_DECL_LUA(io)
|
||||
PW_REGISTER_DECL_LUA(scene)
|
||||
PW_REGISTER_DECL_LUA(visual)
|
||||
|
||||
struct script::state {
|
||||
|
||||
sol::state _state;
|
||||
sol::table _namespace;
|
||||
|
||||
state();
|
||||
|
||||
void load_all();
|
||||
void load(const std::string& name);
|
||||
|
||||
int run(const std::string &s);
|
||||
|
||||
};
|
||||
|
||||
int script::state::run(const std::string &s)
|
||||
{
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
}
|
||||
|
||||
void script::state::load(const std::string& name)
|
||||
{
|
||||
auto f = runtime_lua::get().register_functions()[name];
|
||||
f(_state,_namespace);
|
||||
}
|
||||
|
||||
script::state::state()
|
||||
{
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
|
||||
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<state>("script",
|
||||
"load_all",&state::load_all,
|
||||
"load",&state::load
|
||||
);
|
||||
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
|
||||
}
|
||||
|
||||
void script::state::load_all() {
|
||||
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
//
|
||||
for (auto m : runtime_lua::get().register_functions()) {
|
||||
m.second(_state,_namespace);
|
||||
// debug::d() << "loading module " << m.first;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// script
|
||||
//
|
||||
|
||||
script::script()
|
||||
: _state(make_unique<script::state>())
|
||||
{
|
||||
}
|
||||
|
||||
script::~script()
|
||||
{
|
||||
}
|
||||
|
||||
int script::eval(const std::string &s)
|
||||
{
|
||||
return _state->run(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)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
135
src/binding/src/script_core.cpp
Normal file
135
src/binding/src/script_core.cpp
Normal file
|
@ -0,0 +1,135 @@
|
|||
#include "pw/core/vector.hpp"
|
||||
#include "pw/core/quaternion.hpp"
|
||||
#include "pw/core/axisangle.hpp"
|
||||
#include "pw/core/debug.hpp"
|
||||
#include "pw/core/size.hpp"
|
||||
#include "pw/core/point.hpp"
|
||||
#include "pw/core/time.hpp"
|
||||
#include "pw/core/mesh.hpp"
|
||||
#include "pw/core/image.hpp"
|
||||
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
|
||||
// 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 {};
|
||||
}
|
||||
|
||||
namespace pw {
|
||||
|
||||
void register_core_function(sol::state& lua,sol::table& ns)
|
||||
{
|
||||
|
||||
typedef real_t 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<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)>()
|
||||
,"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
|
||||
);
|
||||
|
||||
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<sizei>("sizei"
|
||||
, 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
|
||||
);
|
||||
|
||||
auto mesh_type = ns.new_usertype<mesh>("mesh"
|
||||
, sol::constructors<mesh()>()
|
||||
, "topology", sol::property(&mesh::topology,&mesh::set_topology)
|
||||
, "vertices", sol::property(&mesh::vertices,&mesh::set_vertices)
|
||||
, "indices", sol::property(&mesh::indices,&mesh::set_indices)
|
||||
);
|
||||
|
||||
mesh_type.new_enum("topology"
|
||||
, "points", mesh::topology_type::points
|
||||
, "lines", mesh::topology_type::lines
|
||||
, "line_strip", mesh::topology_type::line_strip
|
||||
);
|
||||
|
||||
ns.new_usertype<image>("image"
|
||||
,"create",&image::create
|
||||
,"size",sol::readonly_property(&image::size)
|
||||
,"change_count",sol::property(&image::change_count,&image::set_change_count)
|
||||
|
||||
).new_enum("layout"
|
||||
,"rgb8", image::RGB8
|
||||
,"rgb32", image::RGBA8
|
||||
,"gray", image::LUM);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
PW_REGISTER_LUA(core)
|
||||
|
||||
}
|
21
src/binding/src/script_io.cpp
Normal file
21
src/binding/src/script_io.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "pw/io/image_io.hpp"
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
||||
void register_io_function(sol::state&,sol::table& ns)
|
||||
{
|
||||
ns.new_usertype<image_io>("imageio"
|
||||
,"new", sol::no_constructor
|
||||
,"get",&image_io::get
|
||||
,"read",&image_io::read
|
||||
, "write",&image_io::write
|
||||
);
|
||||
}
|
||||
|
||||
PW_REGISTER_LUA(io)
|
||||
|
||||
}
|
27
src/binding/src/script_scene.cpp
Normal file
27
src/binding/src/script_scene.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
//#include "script_scene.hpp"
|
||||
|
||||
#include "pw/scene/node.hpp"
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
void register_scene_function(sol::state&,sol::table &ns)
|
||||
{
|
||||
|
||||
ns.new_usertype<node>("node",
|
||||
sol::constructors<node()>(),
|
||||
"add_child",&node::add_child,
|
||||
"children",sol::readonly_property(&node::children),
|
||||
"parent",sol::readonly_property(&node::parent),
|
||||
"child_count",sol::readonly_property(&node::child_count),
|
||||
"create", []() -> std::shared_ptr<node> { return std::make_shared<node>(); },
|
||||
"is_leaf", sol::readonly_property(&node::is_leaf),
|
||||
"is_root", sol::readonly_property(&node::is_root),
|
||||
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
|
||||
"name",sol::property(&node::name,&node::set_name)
|
||||
);
|
||||
}
|
||||
|
||||
PW_REGISTER_LUA(scene)
|
||||
|
||||
}
|
56
src/binding/src/script_system.cpp
Normal file
56
src/binding/src/script_system.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
//#include "script_system.hpp"
|
||||
|
||||
#include "pw/system/window.hpp"
|
||||
#include "pw/system/input.hpp"
|
||||
#include "pw/system/display.hpp"
|
||||
#include "pw/system/path.hpp"
|
||||
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
#include "pw/core/debug.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
void register_system_function(sol::state&, sol::table &ns)
|
||||
{
|
||||
ns.new_usertype<window>("window",
|
||||
"update",&window::update,
|
||||
"on_update",sol::writeonly_property(&window::set_on_update),
|
||||
"title",sol::writeonly_property(&window::set_title),
|
||||
"size",sol::property(&window::size,&window::set_size),
|
||||
"client_size",sol::readonly_property(&window::client_size),
|
||||
"position",sol::property(&window::position,&window::set_position),
|
||||
"fullscreen",sol::property(&window::fullscreen,&window::set_fullscreen),
|
||||
"visible",sol::property(&window::visible,&window::set_visible)
|
||||
);
|
||||
|
||||
|
||||
ns.new_usertype<input>("input",
|
||||
"new", sol::no_constructor,
|
||||
"get",&input::get,
|
||||
"mouse_position",sol::readonly_property(&input::mouse_position),
|
||||
"mouse_button",sol::readonly_property(&input::mouse_button),
|
||||
"mouse_pressed",sol::readonly_property(&input::mouse_pressed),
|
||||
"input_string",sol::readonly_property(&input::input_string)
|
||||
);
|
||||
|
||||
ns.new_usertype<display>("display",
|
||||
"all",&display::all,
|
||||
"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)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PW_REGISTER_LUA(system)
|
||||
|
||||
|
||||
}
|
31
src/binding/src/script_visual.cpp
Normal file
31
src/binding/src/script_visual.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
#include "pw/core/debug.hpp"
|
||||
#include "pw/visual/pipeline.hpp"
|
||||
#include "pw/visual/shader.hpp"
|
||||
|
||||
#include "runtime_lua.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
void register_visual_function(sol::state&,sol::table &ns)
|
||||
{
|
||||
|
||||
ns.new_usertype<pipeline>("pipeline"
|
||||
,"create",&pipeline::create
|
||||
,"draw",&pipeline::draw
|
||||
);
|
||||
|
||||
ns.new_usertype<shader>("shader"
|
||||
,"ready",sol::readonly_property(&shader::ready)
|
||||
,"use",&shader::use
|
||||
,"build",&shader::build
|
||||
,"set_source",&shader::set_source
|
||||
,"source",&shader::source
|
||||
).new_enum("shader_type"
|
||||
,"fragment",shader::code_type::fragment
|
||||
,"vertex",shader::code_type::vertex);
|
||||
}
|
||||
|
||||
PW_REGISTER_LUA(visual)
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue