add Heightmap class & rename bytearray to Bytearray

This commit is contained in:
MihailRis 2024-08-13 03:09:52 +03:00
parent 1a50e69769
commit da4b8bd7d4
6 changed files with 122 additions and 19 deletions

View File

@ -30,6 +30,7 @@ extern const luaL_Reg jsonlib[];
extern const luaL_Reg mat4lib[];
extern const luaL_Reg packlib[];
extern const luaL_Reg playerlib[];
extern const luaL_Reg generationlib[];
extern const luaL_Reg quatlib[]; // quat.cpp
extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[];

View File

@ -0,0 +1,4 @@
#include "api_lua.hpp"
const luaL_Reg generationlib[] = {
{NULL, NULL}};

View File

@ -29,4 +29,27 @@ namespace lua {
static int createMetatable(lua::State*);
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";
};
}

View File

@ -37,6 +37,7 @@ static void create_libs(lua::State* L) {
openlib(L, "gui", guilib);
openlib(L, "input", inputlib);
openlib(L, "inventory", inventorylib);
openlib(L, "generation", generationlib);
openlib(L, "item", itemlib);
openlib(L, "json", jsonlib);
openlib(L, "mat4", mat4lib);
@ -95,7 +96,8 @@ void lua::initialize() {
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() {

View File

@ -16,7 +16,7 @@ Bytearray::Bytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
Bytearray::~Bytearray() {
}
static int l_bytearray_append(lua::State* L) {
static int l_append(lua::State* L) {
if (auto buffer = touserdata<Bytearray>(L, 1)) {
auto value = tointeger(L, 2);
buffer->data().push_back(static_cast<ubyte>(value));
@ -24,7 +24,7 @@ static int l_bytearray_append(lua::State* L) {
return 0;
}
static int l_bytearray_insert(lua::State* L) {
static int l_insert(lua::State* L) {
auto buffer = touserdata<Bytearray>(L, 1);
if (buffer == nullptr) {
return 0;
@ -39,7 +39,7 @@ static int l_bytearray_insert(lua::State* L) {
return 0;
}
static int l_bytearray_remove(lua::State* L) {
static int l_remove(lua::State* L) {
auto buffer = touserdata<Bytearray>(L, 1);
if (buffer == nullptr) {
return 0;
@ -54,12 +54,12 @@ static int l_bytearray_remove(lua::State* L) {
}
static std::unordered_map<std::string, lua_CFunction> bytearray_methods {
{"append", lua::wrap<l_bytearray_append>},
{"insert", lua::wrap<l_bytearray_insert>},
{"remove", lua::wrap<l_bytearray_remove>},
{"append", lua::wrap<l_append>},
{"insert", lua::wrap<l_insert>},
{"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)) {
size_t len = objlen(L, 2);
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));
}
static int l_bytearray_meta_index(lua::State* L) {
static int l_meta_index(lua::State* L) {
auto buffer = touserdata<Bytearray>(L, 1);
if (buffer == nullptr) {
return 0;
@ -97,7 +97,7 @@ static int l_bytearray_meta_index(lua::State* L) {
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);
if (buffer == nullptr) {
return 0;
@ -115,14 +115,14 @@ static int l_bytearray_meta_newindex(lua::State* L) {
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)) {
return pushinteger(L, buffer->data().size());
}
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);
if (buffer == nullptr) {
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 bufferB = touserdata<Bytearray>(L, 2);
if (bufferA == nullptr || bufferB == nullptr) {
@ -164,19 +164,19 @@ static int l_bytearray_meta_add(lua::State* L) {
int Bytearray::createMetatable(lua::State* L) {
createtable(L, 0, 6);
pushcfunction(L, lua::wrap<l_bytearray_meta_index>);
pushcfunction(L, lua::wrap<l_meta_index>);
setfield(L, "__index");
pushcfunction(L, lua::wrap<l_bytearray_meta_newindex>);
pushcfunction(L, lua::wrap<l_meta_newindex>);
setfield(L, "__newindex");
pushcfunction(L, lua::wrap<l_bytearray_meta_len>);
pushcfunction(L, lua::wrap<l_meta_len>);
setfield(L, "__len");
pushcfunction(L, lua::wrap<l_bytearray_meta_tostring>);
pushcfunction(L, lua::wrap<l_meta_tostring>);
setfield(L, "__tostring");
pushcfunction(L, lua::wrap<l_bytearray_meta_add>);
pushcfunction(L, lua::wrap<l_meta_add>);
setfield(L, "__add");
createtable(L, 0, 1);
pushcfunction(L, lua::wrap<l_bytearray_meta_meta_call>);
pushcfunction(L, lua::wrap<l_meta_meta_call>);
setfield(L, "__call");
setmetatable(L);
return 1;

View 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;
}