Merge branch 'main' into heightmaps

This commit is contained in:
MihailRis 2024-09-06 18:38:53 +03:00
commit feb86a547f
7 changed files with 100 additions and 22 deletions

View File

@ -42,7 +42,7 @@ jobs:
cp -r build/* packaged/
working-directory: ${{ github.workspace }}
- name: Run tests
run: ctest --test-dir build
run: ctest --output-on-failure --test-dir build
- uses: actions/upload-artifact@v2
with:
name: Windows-Build

View File

@ -22,6 +22,9 @@ local Camera = {__index={
local wrappers = {}
cameras.get = function(name)
if type(name) == 'number' then
return cameras.get(cameras.name(name))
end
local wrapper = wrappers[name]
if wrapper ~= nil then
return wrapper

View File

@ -102,6 +102,7 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
}
break;
case BlockModel::xsprite: {
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f));
batch->sprite(
right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0),

View File

@ -62,8 +62,6 @@ static int l_set_matrix(lua::State* L) {
static int l_get_texture(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
skeleton.textures[lua::require_string(L, 2)] =
lua::require_string(L, 3);
const auto& found = skeleton.textures.find(lua::require_string(L, 2));
if (found != skeleton.textures.end()) {
return lua::pushstring(L, found->second);

View File

@ -116,20 +116,22 @@ const utf_t utf[] = {
};
inline uint utf8_len(ubyte cp) {
uint len = 0;
for (const utf_t* u = utf; u->mask; ++u) {
if ((cp >= u->beg) && (cp <= u->end)) {
break;
if ((cp & 0x80) == 0) {
return 1;
}
++len;
if ((cp & 0xE0) == 0xC0) {
return 2;
}
if (len > 4) /* Out of bounds */
throw std::runtime_error("utf-8 decode error");
return len;
if ((cp & 0xF0) == 0xE0) {
return 3;
}
if ((cp & 0xF8) == 0xF0) {
return 4;
}
return 0;
}
extern uint32_t util::decode_utf8(uint& size, const char* chr) {
uint32_t util::decode_utf8(uint& size, const char* chr) {
size = utf8_len(*chr);
int shift = utf[0].bits_stored * (size - 1);
uint32_t code = (*chr++ & utf[size].mask) << shift;
@ -141,11 +143,26 @@ extern uint32_t util::decode_utf8(uint& size, const char* chr) {
return code;
}
std::string util::wstr2str_utf8(const std::wstring& ws) {
size_t util::crop_utf8(std::string_view s, size_t maxSize) {
size_t pos = 0;
uint size = 0;
while (pos < s.length()) {
decode_utf8(size, s.data() + pos);
if (pos + size > maxSize) {
return pos;
}
pos += size;
}
return pos;
}
template<class C>
std::string xstr2str_utf8(const std::basic_string<C>& xs) {
std::vector<char> chars;
char buffer[4];
for (wchar_t wc : ws) {
uint size = encode_utf8((uint)wc, (ubyte*)buffer);
ubyte buffer[4];
for (C xc : xs) {
uint size = util::encode_utf8(
static_cast<uint>(xc), buffer);
for (uint i = 0; i < size; i++) {
chars.push_back(buffer[i]);
}
@ -153,15 +170,32 @@ std::string util::wstr2str_utf8(const std::wstring& ws) {
return std::string(chars.data(), chars.size());
}
std::wstring util::str2wstr_utf8(const std::string& s) {
std::vector<wchar_t> chars;
std::string util::wstr2str_utf8(const std::wstring& ws) {
return xstr2str_utf8(ws);
}
std::string util::u32str2str_utf8(const std::u32string& ws) {
return xstr2str_utf8(ws);
}
template<class C>
std::basic_string<C> str2xstr_utf8(const std::string& s) {
std::vector<C> chars;
size_t pos = 0;
uint size = 0;
while (pos < s.length()) {
chars.push_back(decode_utf8(size, &s.at(pos)));
chars.push_back(util::decode_utf8(size, &s.at(pos)));
pos += size;
}
return std::wstring(chars.data(), chars.size());
return std::basic_string<C>(chars.data(), chars.size());
}
std::wstring util::str2wstr_utf8(const std::string& s) {
return str2xstr_utf8<wchar_t>(s);
}
std::u32string util::str2u32str_utf8(const std::string& s) {
return str2xstr_utf8<char32_t>(s);
}
bool util::is_integer(const std::string& text) {

View File

@ -17,8 +17,33 @@ namespace util {
uint encode_utf8(uint32_t c, ubyte* bytes);
uint32_t decode_utf8(uint& size, const char* bytes);
/// @brief Encode raw wstring to UTF-8
/// @param ws source raw wstring
/// @return new UTF-8 encoded string
std::string wstr2str_utf8(const std::wstring& ws);
/// @brief Decode UTF-8 string
/// @param s source encoded string
/// @return new raw decoded wstring
std::wstring str2wstr_utf8(const std::string& s);
/// @brief Encode raw u32string to UTF-8
/// @param ws source raw wstring
/// @return new UTF-8 encoded string
std::string u32str2str_utf8(const std::u32string& ws);
/// @brief Decode UTF-8 string
/// @param s source encoded string
/// @return new raw decoded u32string
std::u32string str2u32str_utf8(const std::string& s);
/// @brief Calculated length of UTF-8 encoded string that fits into maxSize
/// @param s source UTF-8 encoded string view
/// @param maxSize max encoded string length after crop
/// @return cropped string size (less or equal to maxSize)
size_t crop_utf8(std::string_view s, size_t maxSize);
bool is_integer(const std::string& text);
bool is_integer(const std::wstring& text);
bool is_valid_filename(const std::wstring& name);

17
test/util/stringutil.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "util/stringutil.hpp"
#include <gtest/gtest.h>
TEST(stringutil, crop_utf8) {
// Project source files must be UTF-8 encoded
std::string str = u8"пример";
str = str.substr(0, util::crop_utf8(str, 7));
EXPECT_EQ(str, u8"при");
}
TEST(stringutil, utf8) {
std::string str = u8"テキストデモ";
auto u32str = util::str2u32str_utf8(str);
std::string str2 = util::u32str2str_utf8(u32str);
EXPECT_EQ(str, str2);
}