refactored scripting to hide actual script implementation
This commit is contained in:
parent
550d27273f
commit
a99906317d
14 changed files with 140 additions and 62 deletions
|
@ -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(core)
|
||||
|
|
|
@ -28,6 +28,8 @@ public:
|
|||
|
||||
};
|
||||
|
||||
typedef axisangle<double> axisangled;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,4 +2,9 @@
|
|||
#define PW_CORE_GLOBALS_HPP
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -2,9 +2,13 @@
|
|||
#define PW_CORE_IMAGE_HPP
|
||||
|
||||
#include <pw/core/globals.hpp>
|
||||
#include <pw/core/referenced.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class image : public referenced<image> {
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,13 @@ public:
|
|||
_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 T& x() { return _q.x(); }
|
||||
|
|
|
@ -65,8 +65,7 @@ public:
|
|||
// Vec3 -----------------------------------------------------------------------
|
||||
|
||||
template <class T>
|
||||
class vector3 : public vector<3,T>
|
||||
{
|
||||
class vector3 : public vector<3,T> {
|
||||
public:
|
||||
|
||||
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; }
|
||||
|
||||
vector3 clone() const { return vector3(*this); }
|
||||
|
||||
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; }
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
|
||||
//#include <GLFW/glfw3.h>
|
||||
|
||||
#include <lua.hpp>
|
||||
#include <lualib.h>
|
||||
//#include <lua.hpp>
|
||||
//#include <lualib.h>
|
||||
|
||||
|
||||
#include "pw/core/matrixbase.hpp"
|
||||
|
|
|
@ -53,14 +53,5 @@ int main(int argc,const char** argv) {
|
|||
|
||||
pw::script s;
|
||||
|
||||
#if 1
|
||||
return s.run(sout.str().c_str());
|
||||
#else
|
||||
|
||||
s.load(sout.str());
|
||||
|
||||
// s.main_loop();
|
||||
|
||||
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
#ifndef PW_SCRIPTING_SCRIPT_HPP
|
||||
#define PW_SCRIPTING_SCRIPT_HPP
|
||||
|
||||
#include <pw/scripting/scripting.hpp>
|
||||
|
||||
#include <pw/core/globals.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class script {
|
||||
protected:
|
||||
|
||||
scripting::state _state;
|
||||
scripting::table _namespace;
|
||||
|
||||
public:
|
||||
|
||||
script();
|
||||
~script();
|
||||
|
||||
bool load(const std::string &s);
|
||||
|
||||
int run(const std::string& s);
|
||||
|
||||
struct state {
|
||||
virtual int run(const std::string& script) = 0;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
void load_modules();
|
||||
std::unique_ptr<state> _state;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -20,4 +20,11 @@ target_include_directories(
|
|||
../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)
|
||||
|
|
|
@ -1,56 +1,101 @@
|
|||
#include "pw/scripting/script.hpp"
|
||||
|
||||
#include "pw/scripting/scripting.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 {
|
||||
|
||||
pw::scripting::state _state;
|
||||
pw::scripting::table _namespace;
|
||||
|
||||
|
||||
void load_modules();
|
||||
|
||||
lua_state() {
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<script>("script",
|
||||
"initialize",&script::load_modules,
|
||||
"run",&script::run
|
||||
_namespace.new_usertype<lua_state>("script",
|
||||
"initialize",&lua_state::load_modules
|
||||
);
|
||||
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
}
|
||||
|
||||
pw::script::~script()
|
||||
{
|
||||
_state.collect_garbage();
|
||||
}
|
||||
virtual int run(const std::string &s);
|
||||
|
||||
bool pw::script::load(const std::string &s)
|
||||
{
|
||||
return _state.load(s.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
int pw::script::run(const std::string &s)
|
||||
int lua_state::run(const std::string &s)
|
||||
{
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
}
|
||||
|
||||
void pw::script::load_modules() {
|
||||
void lua_state::load_modules() {
|
||||
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
|
||||
typedef double Scalar;
|
||||
|
||||
_namespace.set("pi",Pi<Scalar>());
|
||||
_namespace.set("pi",pw::Pi<Scalar>());
|
||||
|
||||
#if 0
|
||||
_namespace.new_usertype<vector3d>("vector3",
|
||||
"set",&vector3d::set,
|
||||
"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),
|
||||
"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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -6,9 +6,27 @@ print("hellow pixwerx")
|
|||
local v = pw.vector3.new()
|
||||
v:set(0,1,2)
|
||||
|
||||
-- objects need to be cloned
|
||||
local v2 = v:clone()
|
||||
|
||||
-- manipulate stuff
|
||||
v.x = 0.2
|
||||
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()
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//#include "glad/glad.h"
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
#include "sol.hpp"
|
||||
//#include "sol.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
@ -34,7 +34,7 @@ public:
|
|||
|
||||
context* get_context();
|
||||
|
||||
static void load(sol::table &ns);
|
||||
// static void load(sol::table &ns);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -48,14 +48,14 @@ void pw::window::set_size(int w,int h) {
|
|||
glfwSetWindowSize(_window,w,h);
|
||||
}
|
||||
|
||||
void pw::window::load(sol::table &ns)
|
||||
{
|
||||
glfwInit();
|
||||
//void pw::window::load(sol::table &ns)
|
||||
//{
|
||||
// glfwInit();
|
||||
|
||||
ns.new_usertype<window>("window",
|
||||
"update",&window::update,
|
||||
"set_title",&window::set_title,
|
||||
"set_size",&window::set_size
|
||||
);
|
||||
}
|
||||
// ns.new_usertype<window>("window",
|
||||
// "update",&window::update,
|
||||
// "set_title",&window::set_title,
|
||||
// "set_size",&window::set_size
|
||||
// );
|
||||
//}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue