add Heightmap class & rename bytearray to Bytearray
This commit is contained in:
parent
1a50e69769
commit
da4b8bd7d4
@ -30,6 +30,7 @@ extern const luaL_Reg jsonlib[];
|
|||||||
extern const luaL_Reg mat4lib[];
|
extern const luaL_Reg mat4lib[];
|
||||||
extern const luaL_Reg packlib[];
|
extern const luaL_Reg packlib[];
|
||||||
extern const luaL_Reg playerlib[];
|
extern const luaL_Reg playerlib[];
|
||||||
|
extern const luaL_Reg generationlib[];
|
||||||
extern const luaL_Reg quatlib[]; // quat.cpp
|
extern const luaL_Reg quatlib[]; // quat.cpp
|
||||||
extern const luaL_Reg timelib[];
|
extern const luaL_Reg timelib[];
|
||||||
extern const luaL_Reg tomllib[];
|
extern const luaL_Reg tomllib[];
|
||||||
|
|||||||
4
src/logic/scripting/lua/libgeneration.cpp
Normal file
4
src/logic/scripting/lua/libgeneration.cpp
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "api_lua.hpp"
|
||||||
|
|
||||||
|
const luaL_Reg generationlib[] = {
|
||||||
|
{NULL, NULL}};
|
||||||
@ -29,4 +29,27 @@ namespace lua {
|
|||||||
static int createMetatable(lua::State*);
|
static int createMetatable(lua::State*);
|
||||||
inline static std::string TYPENAME = "bytearray";
|
inline static std::string TYPENAME = "bytearray";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Heightmap : public Userdata {
|
||||||
|
std::vector<float> buffer;
|
||||||
|
uint width, height;
|
||||||
|
public:
|
||||||
|
Heightmap(uint width, uint height);
|
||||||
|
virtual ~Heightmap();
|
||||||
|
|
||||||
|
uint getWidth() const {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint getHeight() const {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& getTypeName() const override {
|
||||||
|
return TYPENAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int createMetatable(lua::State*);
|
||||||
|
inline static std::string TYPENAME = "heightmap";
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,6 +37,7 @@ static void create_libs(lua::State* L) {
|
|||||||
openlib(L, "gui", guilib);
|
openlib(L, "gui", guilib);
|
||||||
openlib(L, "input", inputlib);
|
openlib(L, "input", inputlib);
|
||||||
openlib(L, "inventory", inventorylib);
|
openlib(L, "inventory", inventorylib);
|
||||||
|
openlib(L, "generation", generationlib);
|
||||||
openlib(L, "item", itemlib);
|
openlib(L, "item", itemlib);
|
||||||
openlib(L, "json", jsonlib);
|
openlib(L, "json", jsonlib);
|
||||||
openlib(L, "mat4", mat4lib);
|
openlib(L, "mat4", mat4lib);
|
||||||
@ -95,7 +96,8 @@ void lua::initialize() {
|
|||||||
|
|
||||||
initialize_libs_extends(L);
|
initialize_libs_extends(L);
|
||||||
|
|
||||||
newusertype<Bytearray, Bytearray::createMetatable>(L, "bytearray");
|
newusertype<Bytearray, Bytearray::createMetatable>(L, "Bytearray");
|
||||||
|
newusertype<Heightmap, Heightmap::createMetatable>(L, "Heightmap");
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::finalize() {
|
void lua::finalize() {
|
||||||
|
|||||||
@ -16,7 +16,7 @@ Bytearray::Bytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
|
|||||||
Bytearray::~Bytearray() {
|
Bytearray::~Bytearray() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_append(lua::State* L) {
|
static int l_append(lua::State* L) {
|
||||||
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
||||||
auto value = tointeger(L, 2);
|
auto value = tointeger(L, 2);
|
||||||
buffer->data().push_back(static_cast<ubyte>(value));
|
buffer->data().push_back(static_cast<ubyte>(value));
|
||||||
@ -24,7 +24,7 @@ static int l_bytearray_append(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_insert(lua::State* L) {
|
static int l_insert(lua::State* L) {
|
||||||
auto buffer = touserdata<Bytearray>(L, 1);
|
auto buffer = touserdata<Bytearray>(L, 1);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -39,7 +39,7 @@ static int l_bytearray_insert(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_remove(lua::State* L) {
|
static int l_remove(lua::State* L) {
|
||||||
auto buffer = touserdata<Bytearray>(L, 1);
|
auto buffer = touserdata<Bytearray>(L, 1);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -54,12 +54,12 @@ static int l_bytearray_remove(lua::State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static std::unordered_map<std::string, lua_CFunction> bytearray_methods {
|
static std::unordered_map<std::string, lua_CFunction> bytearray_methods {
|
||||||
{"append", lua::wrap<l_bytearray_append>},
|
{"append", lua::wrap<l_append>},
|
||||||
{"insert", lua::wrap<l_bytearray_insert>},
|
{"insert", lua::wrap<l_insert>},
|
||||||
{"remove", lua::wrap<l_bytearray_remove>},
|
{"remove", lua::wrap<l_remove>},
|
||||||
};
|
};
|
||||||
|
|
||||||
static int l_bytearray_meta_meta_call(lua::State* L) {
|
static int l_meta_meta_call(lua::State* L) {
|
||||||
if (lua_istable(L, 2)) {
|
if (lua_istable(L, 2)) {
|
||||||
size_t len = objlen(L, 2);
|
size_t len = objlen(L, 2);
|
||||||
std::vector<ubyte> buffer(len);
|
std::vector<ubyte> buffer(len);
|
||||||
@ -78,7 +78,7 @@ static int l_bytearray_meta_meta_call(lua::State* L) {
|
|||||||
return newuserdata<Bytearray>(L, static_cast<size_t>(size));
|
return newuserdata<Bytearray>(L, static_cast<size_t>(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_meta_index(lua::State* L) {
|
static int l_meta_index(lua::State* L) {
|
||||||
auto buffer = touserdata<Bytearray>(L, 1);
|
auto buffer = touserdata<Bytearray>(L, 1);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -97,7 +97,7 @@ static int l_bytearray_meta_index(lua::State* L) {
|
|||||||
return pushinteger(L, data[index]);
|
return pushinteger(L, data[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_meta_newindex(lua::State* L) {
|
static int l_meta_newindex(lua::State* L) {
|
||||||
auto buffer = touserdata<Bytearray>(L, 1);
|
auto buffer = touserdata<Bytearray>(L, 1);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -115,14 +115,14 @@ static int l_bytearray_meta_newindex(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_meta_len(lua::State* L) {
|
static int l_meta_len(lua::State* L) {
|
||||||
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
||||||
return pushinteger(L, buffer->data().size());
|
return pushinteger(L, buffer->data().size());
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_meta_tostring(lua::State* L) {
|
static int l_meta_tostring(lua::State* L) {
|
||||||
auto buffer = touserdata<Bytearray>(L, 1);
|
auto buffer = touserdata<Bytearray>(L, 1);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -146,7 +146,7 @@ static int l_bytearray_meta_tostring(lua::State* L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bytearray_meta_add(lua::State* L) {
|
static int l_meta_add(lua::State* L) {
|
||||||
auto bufferA = touserdata<Bytearray>(L, 1);
|
auto bufferA = touserdata<Bytearray>(L, 1);
|
||||||
auto bufferB = touserdata<Bytearray>(L, 2);
|
auto bufferB = touserdata<Bytearray>(L, 2);
|
||||||
if (bufferA == nullptr || bufferB == nullptr) {
|
if (bufferA == nullptr || bufferB == nullptr) {
|
||||||
@ -164,19 +164,19 @@ static int l_bytearray_meta_add(lua::State* L) {
|
|||||||
|
|
||||||
int Bytearray::createMetatable(lua::State* L) {
|
int Bytearray::createMetatable(lua::State* L) {
|
||||||
createtable(L, 0, 6);
|
createtable(L, 0, 6);
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_index>);
|
pushcfunction(L, lua::wrap<l_meta_index>);
|
||||||
setfield(L, "__index");
|
setfield(L, "__index");
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_newindex>);
|
pushcfunction(L, lua::wrap<l_meta_newindex>);
|
||||||
setfield(L, "__newindex");
|
setfield(L, "__newindex");
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_len>);
|
pushcfunction(L, lua::wrap<l_meta_len>);
|
||||||
setfield(L, "__len");
|
setfield(L, "__len");
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_tostring>);
|
pushcfunction(L, lua::wrap<l_meta_tostring>);
|
||||||
setfield(L, "__tostring");
|
setfield(L, "__tostring");
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_add>);
|
pushcfunction(L, lua::wrap<l_meta_add>);
|
||||||
setfield(L, "__add");
|
setfield(L, "__add");
|
||||||
|
|
||||||
createtable(L, 0, 1);
|
createtable(L, 0, 1);
|
||||||
pushcfunction(L, lua::wrap<l_bytearray_meta_meta_call>);
|
pushcfunction(L, lua::wrap<l_meta_meta_call>);
|
||||||
setfield(L, "__call");
|
setfield(L, "__call");
|
||||||
setmetatable(L);
|
setmetatable(L);
|
||||||
return 1;
|
return 1;
|
||||||
73
src/logic/scripting/lua/lua_type_heightmap.cpp
Normal file
73
src/logic/scripting/lua/lua_type_heightmap.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include "lua_custom_types.hpp"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
#include "lua_util.hpp"
|
||||||
|
|
||||||
|
using namespace lua;
|
||||||
|
|
||||||
|
Heightmap::Heightmap(uint width, uint height) : width(width), height(height) {
|
||||||
|
buffer.resize(width*height);
|
||||||
|
}
|
||||||
|
|
||||||
|
Heightmap::~Heightmap() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_meta_meta_call(lua::State* L) {
|
||||||
|
auto width = tointeger(L, 2);
|
||||||
|
auto height = tointeger(L, 3);
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
throw std::runtime_error("width and height must be greather than 0");
|
||||||
|
}
|
||||||
|
return newuserdata<Heightmap>(
|
||||||
|
L, static_cast<uint>(width), static_cast<uint>(height)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_meta_index(lua::State* L) {
|
||||||
|
auto map = touserdata<Heightmap>(L, 1);
|
||||||
|
if (map == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (isstring(L, 2)) {
|
||||||
|
auto fieldname = tostring(L, 2);
|
||||||
|
if (!std::strcmp(fieldname, "width")) {
|
||||||
|
return pushinteger(L, map->getWidth());
|
||||||
|
} else if (!std::strcmp(fieldname, "height")) {
|
||||||
|
return pushinteger(L, map->getHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_meta_tostring(lua::State* L) {
|
||||||
|
auto map = touserdata<Heightmap>(L, 1);
|
||||||
|
if (map == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream stream;
|
||||||
|
stream << std::hex << reinterpret_cast<ptrdiff_t>(map);
|
||||||
|
auto ptrstr = stream.str();
|
||||||
|
|
||||||
|
return pushstring(
|
||||||
|
L, "Heightmap(" + std::to_string(map->getWidth()) +
|
||||||
|
"*" + std::to_string(map->getHeight()) + " at 0x" + ptrstr + ")"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Heightmap::createMetatable(lua::State* L) {
|
||||||
|
createtable(L, 0, 2);
|
||||||
|
pushcfunction(L, lua::wrap<l_meta_tostring>);
|
||||||
|
setfield(L, "__tostring");
|
||||||
|
pushcfunction(L, lua::wrap<l_meta_index>);
|
||||||
|
setfield(L, "__index");
|
||||||
|
|
||||||
|
createtable(L, 0, 1);
|
||||||
|
pushcfunction(L, lua::wrap<l_meta_meta_call>);
|
||||||
|
setfield(L, "__call");
|
||||||
|
setmetatable(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user