Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+144
View File
@@ -0,0 +1,144 @@
#include "texture/sub_image.h"
#include <fstream>
#include <sstream>
#include <algorithm>
namespace metin2::texture {
namespace {
inline std::string Trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, (last - first + 1));
}
inline std::string ToLower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return str;
}
} // namespace
bool SubImage::LoadFromFile(const std::string& file_path) {
std::ifstream file(file_path);
if (!file.is_open()) return false;
std::stringstream buffer;
buffer << file.rdbuf();
return LoadFromString(buffer.str());
}
bool SubImage::LoadFromString(const std::string& content) {
std::istringstream stream(content);
std::string line;
m_image_path.clear();
m_rect = Rect2D();
while (std::getline(stream, line)) {
std::string trimmed = Trim(line);
if (trimmed.empty() || trimmed[0] == '#') continue;
std::istringstream ls(trimmed);
std::string key;
ls >> key;
key = ToLower(key);
std::string val;
ls >> std::ws;
std::getline(ls, val);
val = Trim(val);
if (val.size() >= 2 && val.front() == '"' && val.back() == '"') {
val = val.substr(1, val.size() - 2);
}
if (key == "image") {
m_image_path = val;
} else if (key == "left") {
m_rect.left = std::stoi(val);
} else if (key == "top") {
m_rect.top = std::stoi(val);
} else if (key == "right") {
m_rect.right = std::stoi(val);
} else if (key == "bottom") {
m_rect.bottom = std::stoi(val);
}
}
return !m_image_path.empty() && m_rect.GetWidth() > 0 && m_rect.GetHeight() > 0;
}
UVRect SubImage::ComputeUV(uint32_t atlas_width, uint32_t atlas_height) const {
UVRect uv;
if (atlas_width == 0 || atlas_height == 0) return uv;
float inv_w = 1.0f / static_cast<float>(atlas_width);
float inv_h = 1.0f / static_cast<float>(atlas_height);
uv.u0 = static_cast<float>(m_rect.left) * inv_w;
uv.v0 = static_cast<float>(m_rect.top) * inv_h;
uv.u1 = static_cast<float>(m_rect.right) * inv_w;
uv.v1 = static_cast<float>(m_rect.bottom) * inv_h;
return uv;
}
NineSliceMesh SubImage::BuildNineSliceMesh(float x, float y, float width, float height,
float border_l, float border_t, float border_r, float border_b,
uint32_t atlas_width, uint32_t atlas_height,
uint32_t color) const {
NineSliceMesh mesh;
if (atlas_width == 0 || atlas_height == 0) return mesh;
float inv_w = 1.0f / static_cast<float>(atlas_width);
float inv_h = 1.0f / static_cast<float>(atlas_height);
float xs[4] = { x, x + border_l, x + width - border_r, x + width };
float ys[4] = { y, y + border_t, y + height - border_b, y + height };
float us[4] = {
static_cast<float>(m_rect.left) * inv_w,
static_cast<float>(m_rect.left + border_l) * inv_w,
static_cast<float>(m_rect.right - border_r) * inv_w,
static_cast<float>(m_rect.right) * inv_w
};
float vs[4] = {
static_cast<float>(m_rect.top) * inv_h,
static_cast<float>(m_rect.top + border_t) * inv_h,
static_cast<float>(m_rect.bottom - border_b) * inv_h,
static_cast<float>(m_rect.bottom) * inv_h
};
mesh.vertices.reserve(16);
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
mesh.vertices.push_back({xs[c], ys[r], us[c], vs[r], color});
}
}
mesh.indices.reserve(54); // 9 quads * 6 indices
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
uint32_t i0 = r * 4 + c;
uint32_t i1 = r * 4 + (c + 1);
uint32_t i2 = (r + 1) * 4 + c;
uint32_t i3 = (r + 1) * 4 + (c + 1);
mesh.indices.push_back(i0);
mesh.indices.push_back(i2);
mesh.indices.push_back(i1);
mesh.indices.push_back(i1);
mesh.indices.push_back(i2);
mesh.indices.push_back(i3);
}
}
return mesh;
}
} // namespace metin2::texture