refactored scripting to hide actual script implementation

This commit is contained in:
Hartmut Seichter 2018-04-02 11:34:53 +02:00
parent 550d27273f
commit a99906317d
14 changed files with 140 additions and 62 deletions

View file

@ -1,7 +1,4 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/deps/lua-5.3.4/src
${CMAKE_CURRENT_SOURCE_DIR}/deps/sol
)
add_subdirectory(deps) add_subdirectory(deps)
add_subdirectory(core) add_subdirectory(core)

View file

@ -28,6 +28,8 @@ public:
}; };
typedef axisangle<double> axisangled;
} }

View file

@ -2,4 +2,9 @@
#define PW_CORE_GLOBALS_HPP #define PW_CORE_GLOBALS_HPP
#include <cstddef>
#include <memory>
#endif #endif

View file

@ -2,9 +2,13 @@
#define PW_CORE_IMAGE_HPP #define PW_CORE_IMAGE_HPP
#include <pw/core/globals.hpp> #include <pw/core/globals.hpp>
#include <pw/core/referenced.hpp>
namespace pw { namespace pw {
class image : public referenced<image> {
};
} }

View file

@ -58,6 +58,13 @@ public:
_q.set(x,y,z,w); _q.set(x,y,z,w);
} }
inline void set_x(const T& v) { x() = v; }
inline void set_y(const T& v) { y() = v; }
inline void set_z(const T& v) { z() = v; }
inline void set_w(const T& v) { w() = v; }
inline const vector4<T> as_vector() const { return _q; } inline const vector4<T> as_vector() const { return _q; }
inline T& x() { return _q.x(); } inline T& x() { return _q.x(); }

View file

@ -65,8 +65,7 @@ public:
// Vec3 ----------------------------------------------------------------------- // Vec3 -----------------------------------------------------------------------
template <class T> template <class T>
class vector3 : public vector<3,T> class vector3 : public vector<3,T> {
{
public: public:
using vector<3,T>::operator=; using vector<3,T>::operator=;
@ -79,6 +78,8 @@ public:
void set(T c1, T c2, T c3) { (*this)(0) = c1; (*this)(1) = c2; (*this)(2) = c3; } void set(T c1, T c2, T c3) { (*this)(0) = c1; (*this)(1) = c2; (*this)(2) = c3; }
vector3 clone() const { return vector3(*this); }
inline void set_x(const T& v) { x() = v; } inline void set_x(const T& v) { x() = v; }
inline void set_y(const T& v) { y() = v; } inline void set_y(const T& v) { y() = v; }
inline void set_z(const T& v) { z() = v; } inline void set_z(const T& v) { z() = v; }

View file

@ -1,8 +1,8 @@
//#include <GLFW/glfw3.h> //#include <GLFW/glfw3.h>
#include <lua.hpp> //#include <lua.hpp>
#include <lualib.h> //#include <lualib.h>
#include "pw/core/matrixbase.hpp" #include "pw/core/matrixbase.hpp"

View file

@ -53,14 +53,5 @@ int main(int argc,const char** argv) {
pw::script s; pw::script s;
#if 1
return s.run(sout.str().c_str()); return s.run(sout.str().c_str());
#else
s.load(sout.str());
// s.main_loop();
#endif
} }

View file

@ -1,28 +1,29 @@
#ifndef PW_SCRIPTING_SCRIPT_HPP #ifndef PW_SCRIPTING_SCRIPT_HPP
#define PW_SCRIPTING_SCRIPT_HPP #define PW_SCRIPTING_SCRIPT_HPP
#include <pw/scripting/scripting.hpp>
#include <pw/core/globals.hpp>
#include <string>
namespace pw { namespace pw {
class script { class script {
protected:
scripting::state _state;
scripting::table _namespace;
public: public:
script(); script();
~script(); ~script();
bool load(const std::string &s);
int run(const std::string& s); int run(const std::string& s);
struct state {
virtual int run(const std::string& script) = 0;
};
protected: protected:
void load_modules(); std::unique_ptr<state> _state;
}; };
} }

View file

@ -20,4 +20,11 @@ target_include_directories(
../include ../include
) )
target_include_directories(
pwscripting
PUBLIC
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src
${CMAKE_SOURCE_DIR}/src/deps/sol
)
target_link_libraries(pwscripting lua pwcore) target_link_libraries(pwscripting lua pwcore)

View file

@ -1,56 +1,101 @@
#include "pw/scripting/script.hpp" #include "pw/scripting/script.hpp"
#include "pw/scripting/scripting.hpp"
#include "pw/core/vector.hpp" #include "pw/core/vector.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/axisangle.hpp"
pw::script::script() { struct lua_state : public pw::script::state {
// create global pw namespace pw::scripting::state _state;
_namespace = _state.create_named_table("pw"); pw::scripting::table _namespace;
// add the script as a user type
_namespace.new_usertype<script>("script",
"initialize",&script::load_modules,
"run",&script::run
);
// add this instance as "script" void load_modules();
_namespace.set("script",this);
}
pw::script::~script() lua_state() {
{ // create global pw namespace
_state.collect_garbage(); _namespace = _state.create_named_table("pw");
}
bool pw::script::load(const std::string &s) // add the script as a user type
{ _namespace.new_usertype<lua_state>("script",
return _state.load(s.c_str()); "initialize",&lua_state::load_modules
} );
int pw::script::run(const std::string &s) // add this instance as "script"
_namespace.set("script",this);
}
virtual int run(const std::string &s);
};
int lua_state::run(const std::string &s)
{ {
return _state.script(s.c_str()).get<int>(); return _state.script(s.c_str()).get<int>();
} }
void pw::script::load_modules() { void lua_state::load_modules() {
// open all libraries // open all libraries
_state.open_libraries(); _state.open_libraries();
typedef double Scalar; typedef double Scalar;
_namespace.set("pi",Pi<Scalar>()); _namespace.set("pi",pw::Pi<Scalar>());
#if 0
_namespace.new_usertype<vector3d>("vector3", _namespace.new_usertype<vector3d>("vector3",
"set",&vector3d::set, "set",&vector3d::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x), "x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y), "y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z), "z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
"v",&vector3d::values "v",&vector3d::values,
"clone",&vector3d::clone
); );
_namespace.new_usertype<quaterniond>("quaternion",
"set",&quaterniond::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w)
// "v",&vector3d::values,
// "clone",&vector3d::clone
);
_namespace.new_usertype<axisangled>("axisangle",
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
);
#endif
} }
pw::script::script() {
}
pw::script::~script()
{
// _state.collect_garbage();
}
//bool pw::script::load(const std::string &s)
//{
// return _state.load(s.c_str());
//}
int pw::script::run(const std::string &s)
{
return _state->run(s);
}

View file

@ -6,9 +6,27 @@ print("hellow pixwerx")
local v = pw.vector3.new() local v = pw.vector3.new()
v:set(0,1,2) v:set(0,1,2)
-- objects need to be cloned
local v2 = v:clone()
-- manipulate stuff
v.x = 0.2 v.x = 0.2
v.y = pw.pi v.y = pw.pi
print(v.x)
print(v:v()) print("v : ", v:v())
print("v2: ", v2:v())
local q = pw.quaternion.new()
print("q",q.x,q.y,q.z,q.w)
local aa = pw.axisangle.new()
-- assign vector to axis
aa.axis = v
print("aa",aa.axis.x)
--local scene = pw:scene.new()

View file

@ -4,7 +4,7 @@
//#include "glad/glad.h" //#include "glad/glad.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "sol.hpp" //#include "sol.hpp"
namespace pw { namespace pw {
@ -34,7 +34,7 @@ public:
context* get_context(); context* get_context();
static void load(sol::table &ns); // static void load(sol::table &ns);
}; };

View file

@ -48,14 +48,14 @@ void pw::window::set_size(int w,int h) {
glfwSetWindowSize(_window,w,h); glfwSetWindowSize(_window,w,h);
} }
void pw::window::load(sol::table &ns) //void pw::window::load(sol::table &ns)
{ //{
glfwInit(); // glfwInit();
ns.new_usertype<window>("window", // ns.new_usertype<window>("window",
"update",&window::update, // "update",&window::update,
"set_title",&window::set_title, // "set_title",&window::set_title,
"set_size",&window::set_size // "set_size",&window::set_size
); // );
} //}