fix: lua::tovec + add vecn.tostring
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
template<int n, template<class> class Op>
|
template<int n, template<class> class Op>
|
||||||
@@ -53,6 +54,24 @@ static int l_scalar_op(lua::State* L) {
|
|||||||
return lua::pushnumber(L, func(vec));
|
return lua::pushnumber(L, func(vec));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<int n>
|
||||||
|
static int l_tostring(lua::State* L) {
|
||||||
|
auto vec = lua::tovec<n>(L, 1);
|
||||||
|
if (lua::gettop(L) != 1) {
|
||||||
|
throw std::runtime_error("invalid arguments number (1 expected)");
|
||||||
|
}
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "vec" << std::to_string(n) << "{";
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
ss << ", ";
|
||||||
|
}
|
||||||
|
ss << vec[i];
|
||||||
|
}
|
||||||
|
ss << "}";
|
||||||
|
return lua::pushstring(L, ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg vec2lib [] = {
|
const luaL_Reg vec2lib [] = {
|
||||||
{"add", lua::wrap<l_binop<2, std::plus>>},
|
{"add", lua::wrap<l_binop<2, std::plus>>},
|
||||||
{"sub", lua::wrap<l_binop<2, std::minus>>},
|
{"sub", lua::wrap<l_binop<2, std::minus>>},
|
||||||
@@ -60,6 +79,7 @@ const luaL_Reg vec2lib [] = {
|
|||||||
{"div", lua::wrap<l_binop<2, std::divides>>},
|
{"div", lua::wrap<l_binop<2, std::divides>>},
|
||||||
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
|
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
|
||||||
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
|
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
|
||||||
|
{"tostring", lua::wrap<l_tostring<2>>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,6 +90,7 @@ const luaL_Reg vec3lib [] = {
|
|||||||
{"div", lua::wrap<l_binop<3, std::divides>>},
|
{"div", lua::wrap<l_binop<3, std::divides>>},
|
||||||
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
|
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
|
||||||
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
|
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
|
||||||
|
{"tostring", lua::wrap<l_tostring<3>>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -80,5 +101,6 @@ const luaL_Reg vec4lib [] = {
|
|||||||
{"div", lua::wrap<l_binop<4, std::divides>>},
|
{"div", lua::wrap<l_binop<4, std::divides>>},
|
||||||
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
|
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
|
||||||
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
|
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
|
||||||
|
{"tostring", lua::wrap<l_tostring<4>>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -336,7 +336,7 @@ namespace lua {
|
|||||||
}
|
}
|
||||||
glm::vec<n, float> vec;
|
glm::vec<n, float> vec;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
rawgeti(L, 1);
|
rawgeti(L, i+1);
|
||||||
vec[i] = tonumber(L, -1);
|
vec[i] = tonumber(L, -1);
|
||||||
pop(L);
|
pop(L);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user