ref: libcore, libvecn

This commit is contained in:
Cogi Asd 2024-07-08 20:20:37 +03:00
parent 1858a59dce
commit e7501a17ac
2 changed files with 27 additions and 85 deletions

View File

@ -190,37 +190,6 @@ static int l_get_generators(lua::State* L) {
return 1;
}
/// @brief Get engine constants
/// @return A table with engine constants
static int l_get_constants(lua::State* L) {
lua::createtable(L, 0, 20);
const std::pair<const char*, int> numberConstants[] = {
{"ENGINE_VERSION_MAJOR", ENGINE_VERSION_MAJOR},
{"ENGINE_VERSION_MINOR", ENGINE_VERSION_MINOR},
{"BLOCK_AIR", BLOCK_AIR},
{"ITEM_EMPTY", ITEM_EMPTY},
{"CHUNK_W", CHUNK_W},
{"CHUNK_H", CHUNK_H},
{"CHUNK_D", CHUNK_D},
{"VOXEL_USER_BITS", VOXEL_USER_BITS},
{"VOXEL_USER_BITS_OFFSET", VOXEL_USER_BITS_OFFSET},
{"ITEM_ICON_SIZE", ITEM_ICON_SIZE},
{"CHUNK_VOL", CHUNK_VOL},
{"BLOCK_VOID", BLOCK_VOID},
{"ITEM_VOID", ITEM_VOID},
{"MAX_BLOCKS", MAX_BLOCKS}
};
for (const auto& constant : numberConstants) {
lua::pushnumber(L, constant.second);
lua::setfield(L, constant.first);
}
return 1;
}
const luaL_Reg corelib [] = {
{"new_world", lua::wrap<l_new_world>},
{"open_world", lua::wrap<l_open_world>},
@ -235,6 +204,5 @@ const luaL_Reg corelib [] = {
{"quit", lua::wrap<l_quit>},
{"get_default_generator", lua::wrap<l_get_default_generator>},
{"get_generators", lua::wrap<l_get_generators>},
{"get_constants", lua::wrap<l_get_constants>},
{NULL, NULL}
};

View File

@ -1,43 +1,41 @@
#include "api_lua.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
template<int n, template<class> class Operation>
template<int n, template<class> class Op>
static int l_binop(lua::State* L) {
uint argc = lua::gettop(L);
if (argc != 2 && argc != 3) {
throw std::runtime_error("invalid arguments number (2 or 3 expected)");
}
const auto& a = lua::tovec<n>(L, 1);
auto a = lua::tovec<n>(L, 1);
if (lua::isnumber(L, 2)) { // scalar second operand overload
const auto& b = lua::tonumber(L, 2);
Operation oper;
auto b = lua::tonumber(L, 2);
Op op;
if (argc == 2) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, oper(a[i], b));
lua::pushnumber(L, op(a[i], b));
lua::rawseti(L, i+1);
}
return 1;
} else {
return lua::setvec(L, 3, oper(a, glm::vec<n, float>(b)));
return lua::setvec(L, 3, op(a, glm::vec<n, float>(b)));
}
} else {
const auto& b = lua::tovec<n>(L, 2);
Operation oper;
auto b = lua::tovec<n>(L, 2);
Op op;
if (argc == 2) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, oper(a[i], b[i]));
lua::pushnumber(L, op(a[i], b[i]));
lua::rawseti(L, i+1);
}
return 1;
} else {
return lua::setvec(L, 3, oper(a, b));
return lua::setvec(L, 3, op(a, b));
}
}
}
@ -45,7 +43,7 @@ static int l_binop(lua::State* L) {
template<int n, glm::vec<n, float>(*func)(const glm::vec<n, float>&)>
static int l_unaryop(lua::State* L) {
uint argc = lua::gettop(L);
const auto& vec = func(lua::tovec<n>(L, 1));
auto vec = func(lua::tovec<n>(L, 1));
switch (argc) {
case 1:
lua::createtable(L, n, 0);
@ -64,10 +62,11 @@ static int l_unaryop(lua::State* L) {
template<int n, float(*func)(const glm::vec<n, float>&)>
static int l_scalar_op(lua::State* L) {
auto vec = lua::tovec<n>(L, 1);
if (lua::gettop(L) != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
return lua::pushnumber(L, func(lua::tovec<n>(L, 1)/*vector a*/));
return lua::pushnumber(L, func(vec));
}
template<int n>
@ -93,30 +92,6 @@ static int l_dot(lua::State* L) {
lua::tovec<n>(L, 2)));
}
template<int n>
static int l_rotate(lua::State* L) {
if (lua::gettop(L) != 2) {
throw std::runtime_error("invalid arguments number (2 expected)");
}
const auto& vec = lua::tovec<n>(L, 1); // vector
const auto& angle = lua::tonumber(L, 2); // scalar (angle)
const auto _cos = std::cos(angle);
const auto _sin = std::sin(angle);
glm::vec<n, float> result_vector;
switch (n) {
case 2:
result_vector.x = vec.x * _cos - vec.y * _sin;
result_vector.y = vec.x * _sin + vec.y * _cos;
break;
// TODO
}
return lua::setvec(L, 1, result_vector);
}
template<int n>
static int l_inverse(lua::State* L) {
if (lua::gettop(L) != 1) {
@ -132,7 +107,7 @@ static int l_inverse(lua::State* L) {
template<int n>
static int l_tostring(lua::State* L) {
const auto& vec = lua::tovec<n>(L, 1);
auto vec = lua::tovec<n>(L, 1);
if (lua::gettop(L) != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
@ -153,13 +128,12 @@ const luaL_Reg vec2lib [] = {
{"sub", lua::wrap<l_binop<2, std::minus>>},
{"mul", lua::wrap<l_binop<2, std::multiplies>>},
{"div", lua::wrap<l_binop<2, std::divides>>},
{"norm", lua::wrap<l_unaryop<2, glm::normalize>>},
{"len", lua::wrap<l_scalar_op<2, glm::length>>},
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
{"tostring", lua::wrap<l_tostring<2>>},
{"abs", lua::wrap<l_unaryop<2, glm::abs>>},
{"round", lua::wrap<l_unaryop<2, glm::round>>},
{"tostring", lua::wrap<l_tostring<2>>},
{"inv", lua::wrap<l_inverse<2>>},
{"rot", lua::wrap<l_rotate<2>>},
{"inverse", lua::wrap<l_inverse<2>>},
{"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>},
{NULL, NULL}
@ -170,12 +144,12 @@ const luaL_Reg vec3lib [] = {
{"sub", lua::wrap<l_binop<3, std::minus>>},
{"mul", lua::wrap<l_binop<3, std::multiplies>>},
{"div", lua::wrap<l_binop<3, std::divides>>},
{"norm", lua::wrap<l_unaryop<3, glm::normalize>>},
{"len", lua::wrap<l_scalar_op<3, glm::length>>},
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
{"tostring", lua::wrap<l_tostring<3>>},
{"abs", lua::wrap<l_unaryop<3, glm::abs>>},
{"round", lua::wrap<l_unaryop<3, glm::round>>},
{"tostring", lua::wrap<l_tostring<3>>},
{"inv", lua::wrap<l_inverse<3>>},
{"inverse", lua::wrap<l_inverse<3>>},
{"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>},
{NULL, NULL}
@ -186,12 +160,12 @@ const luaL_Reg vec4lib [] = {
{"sub", lua::wrap<l_binop<4, std::minus>>},
{"mul", lua::wrap<l_binop<4, std::multiplies>>},
{"div", lua::wrap<l_binop<4, std::divides>>},
{"abs", lua::wrap<l_unaryop<4, glm::abs>>},
{"norm", lua::wrap<l_unaryop<4, glm::normalize>>},
{"len", lua::wrap<l_scalar_op<4, glm::length>>},
{"round", lua::wrap<l_unaryop<4, glm::round>>},
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
{"tostring", lua::wrap<l_tostring<4>>},
{"inv", lua::wrap<l_inverse<4>>},
{"abs", lua::wrap<l_unaryop<4, glm::abs>>},
{"round", lua::wrap<l_unaryop<4, glm::round>>},
{"inverse", lua::wrap<l_inverse<4>>},
{"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>},
{NULL, NULL}