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
+253
View File
@@ -0,0 +1,253 @@
#include "texture/dxt.h"
#include <cstring>
#include <algorithm>
namespace metin2::texture {
namespace {
inline uint16_t ReadU16(const uint8_t* p) {
return static_cast<uint16_t>(p[0]) | (static_cast<uint16_t>(p[1]) << 8);
}
inline uint32_t ReadU32(const uint8_t* p) {
return static_cast<uint32_t>(p[0]) |
(static_cast<uint32_t>(p[1]) << 8) |
(static_cast<uint32_t>(p[2]) << 16) |
(static_cast<uint32_t>(p[3]) << 24);
}
inline void DecodeRGB565(uint16_t c, uint8_t& r, uint8_t& g, uint8_t& b) {
r = static_cast<uint8_t>(((c >> 11) & 0x1F) * 255 / 31);
g = static_cast<uint8_t>(((c >> 5) & 0x3F) * 255 / 63);
b = static_cast<uint8_t>((c & 0x1F) * 255 / 31);
}
void DecodeDXT1Block(const uint8_t* block, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t* dst_rgba) {
uint16_t c0 = ReadU16(block);
uint16_t c1 = ReadU16(block + 2);
uint32_t code = ReadU32(block + 4);
uint8_t colors[4][4];
DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]);
colors[0][3] = 255;
DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]);
colors[1][3] = 255;
if (c0 > c1) {
colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3;
colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3;
colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3;
colors[2][3] = 255;
colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3;
colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3;
colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3;
colors[3][3] = 255;
} else {
colors[2][0] = (colors[0][0] + colors[1][0]) / 2;
colors[2][1] = (colors[0][1] + colors[1][1]) / 2;
colors[2][2] = (colors[0][2] + colors[1][2]) / 2;
colors[2][3] = 255;
colors[3][0] = 0;
colors[3][1] = 0;
colors[3][2] = 0;
colors[3][3] = 0;
}
for (int py = 0; py < 4; ++py) {
if (y + py >= height) continue;
for (int px = 0; px < 4; ++px) {
if (x + px >= width) continue;
int bit_idx = (py * 4 + px) * 2;
int color_idx = (code >> bit_idx) & 3;
size_t dst_offset = ((y + py) * width + (x + px)) * 4;
dst_rgba[dst_offset + 0] = colors[color_idx][0];
dst_rgba[dst_offset + 1] = colors[color_idx][1];
dst_rgba[dst_offset + 2] = colors[color_idx][2];
dst_rgba[dst_offset + 3] = colors[color_idx][3];
}
}
}
} // namespace
bool DxtDecoder::DecompressDXT1(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) {
if (!src || !dst_rgba || width == 0 || height == 0) return false;
uint32_t block_x = (width + 3) / 4;
uint32_t block_y = (height + 3) / 4;
size_t src_offset = 0;
for (uint32_t by = 0; by < block_y; ++by) {
for (uint32_t bx = 0; bx < block_x; ++bx) {
DecodeDXT1Block(src + src_offset, bx * 4, by * 4, width, height, dst_rgba);
src_offset += 8;
}
}
return true;
}
bool DxtDecoder::DecompressDXT3(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) {
if (!src || !dst_rgba || width == 0 || height == 0) return false;
uint32_t block_x = (width + 3) / 4;
uint32_t block_y = (height + 3) / 4;
size_t src_offset = 0;
for (uint32_t by = 0; by < block_y; ++by) {
for (uint32_t bx = 0; bx < block_x; ++bx) {
const uint8_t* alpha_block = src + src_offset;
const uint8_t* color_block = src + src_offset + 8;
uint16_t c0 = ReadU16(color_block);
uint16_t c1 = ReadU16(color_block + 2);
uint32_t code = ReadU32(color_block + 4);
uint8_t colors[4][3];
DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]);
DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]);
colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3;
colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3;
colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3;
colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3;
colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3;
colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3;
for (int py = 0; py < 4; ++py) {
if (by * 4 + py >= height) continue;
uint16_t row_alpha = ReadU16(alpha_block + py * 2);
for (int px = 0; px < 4; ++px) {
if (bx * 4 + px >= width) continue;
int color_idx = (code >> ((py * 4 + px) * 2)) & 3;
uint8_t a4 = (row_alpha >> (px * 4)) & 0x0F;
uint8_t alpha = a4 * 17;
size_t dst_offset = ((by * 4 + py) * width + (bx * 4 + px)) * 4;
dst_rgba[dst_offset + 0] = colors[color_idx][0];
dst_rgba[dst_offset + 1] = colors[color_idx][1];
dst_rgba[dst_offset + 2] = colors[color_idx][2];
dst_rgba[dst_offset + 3] = alpha;
}
}
src_offset += 16;
}
}
return true;
}
bool DxtDecoder::DecompressDXT5(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) {
if (!src || !dst_rgba || width == 0 || height == 0) return false;
uint32_t block_x = (width + 3) / 4;
uint32_t block_y = (height + 3) / 4;
size_t src_offset = 0;
for (uint32_t by = 0; by < block_y; ++by) {
for (uint32_t bx = 0; bx < block_x; ++bx) {
uint8_t a0 = src[src_offset + 0];
uint8_t a1 = src[src_offset + 1];
uint64_t a_bits = 0;
for (int k = 0; k < 6; ++k) {
a_bits |= (static_cast<uint64_t>(src[src_offset + 2 + k]) << (k * 8));
}
uint8_t alphas[8];
alphas[0] = a0;
alphas[1] = a1;
if (a0 > a1) {
alphas[2] = (6 * a0 + 1 * a1) / 7;
alphas[3] = (5 * a0 + 2 * a1) / 7;
alphas[4] = (4 * a0 + 3 * a1) / 7;
alphas[5] = (3 * a0 + 4 * a1) / 7;
alphas[6] = (2 * a0 + 5 * a1) / 7;
alphas[7] = (1 * a0 + 6 * a1) / 7;
} else {
alphas[2] = (4 * a0 + 1 * a1) / 5;
alphas[3] = (3 * a0 + 2 * a1) / 5;
alphas[4] = (2 * a0 + 3 * a1) / 5;
alphas[5] = (1 * a0 + 4 * a1) / 5;
alphas[6] = 0;
alphas[7] = 255;
}
const uint8_t* color_block = src + src_offset + 8;
uint16_t c0 = ReadU16(color_block);
uint16_t c1 = ReadU16(color_block + 2);
uint32_t code = ReadU32(color_block + 4);
uint8_t colors[4][3];
DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]);
DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]);
colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3;
colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3;
colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3;
colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3;
colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3;
colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3;
for (int py = 0; py < 4; ++py) {
if (by * 4 + py >= height) continue;
for (int px = 0; px < 4; ++px) {
if (bx * 4 + px >= width) continue;
int p_idx = py * 4 + px;
int color_idx = (code >> (p_idx * 2)) & 3;
int alpha_idx = (a_bits >> (p_idx * 3)) & 7;
size_t dst_offset = ((by * 4 + py) * width + (bx * 4 + px)) * 4;
dst_rgba[dst_offset + 0] = colors[color_idx][0];
dst_rgba[dst_offset + 1] = colors[color_idx][1];
dst_rgba[dst_offset + 2] = colors[color_idx][2];
dst_rgba[dst_offset + 3] = alphas[alpha_idx];
}
}
src_offset += 16;
}
}
return true;
}
bool DxtDecoder::DecodeDDS(const uint8_t* dds_data, size_t size, ImageBuffer& out_image) {
if (!dds_data || size < 128) return false;
// Magic 'DDS '
if (dds_data[0] != 'D' || dds_data[1] != 'D' || dds_data[2] != 'S' || dds_data[3] != ' ') {
return false;
}
uint32_t height = ReadU32(dds_data + 12);
uint32_t width = ReadU32(dds_data + 16);
uint32_t fourcc = ReadU32(dds_data + 84);
if (width == 0 || height == 0) return false;
out_image.width = width;
out_image.height = height;
out_image.format = EPixelFormat::RGBA8888;
out_image.pixels.resize(width * height * 4);
const uint8_t* pixel_data = dds_data + 128;
if (fourcc == 0x31545844) { // DXT1
return DecompressDXT1(pixel_data, width, height, out_image.pixels.data());
} else if (fourcc == 0x33545844) { // DXT3
return DecompressDXT3(pixel_data, width, height, out_image.pixels.data());
} else if (fourcc == 0x35545844) { // DXT5
return DecompressDXT5(pixel_data, width, height, out_image.pixels.data());
}
return false;
}
} // namespace metin2::texture
+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
+88
View File
@@ -0,0 +1,88 @@
#include "texture/tga.h"
#include <cstring>
#include <algorithm>
namespace metin2::texture {
bool TgaDecoder::DecodeTGA(const uint8_t* data, size_t size, ImageBuffer& out_image) {
if (!data || size < 18) return false;
uint8_t id_length = data[0];
uint8_t image_type = data[2];
uint16_t width = static_cast<uint16_t>(data[12]) | (static_cast<uint16_t>(data[13]) << 8);
uint16_t height = static_cast<uint16_t>(data[14]) | (static_cast<uint16_t>(data[15]) << 8);
uint8_t bpp = data[16];
uint8_t descriptor = data[17];
if (width == 0 || height == 0) return false;
if (bpp != 24 && bpp != 32) return false;
if (image_type != 2 && image_type != 10) return false; // Only uncompressed or RLE truecolor
bool is_rle = (image_type == 10);
bool flip_vertically = !(descriptor & 0x20); // Bit 5: 0 means origin is bottom-left
size_t pixel_bytes = bpp / 8;
size_t offset = 18 + id_length;
out_image.width = width;
out_image.height = height;
out_image.format = EPixelFormat::RGBA8888;
out_image.pixels.resize(width * height * 4);
std::vector<uint8_t> raw_pixels(width * height * pixel_bytes);
if (!is_rle) {
if (size < offset + width * height * pixel_bytes) return false;
std::memcpy(raw_pixels.data(), data + offset, width * height * pixel_bytes);
} else {
size_t current_pixel = 0;
size_t total_pixels = width * height;
while (current_pixel < total_pixels && offset < size) {
uint8_t header = data[offset++];
int count = (header & 0x7F) + 1;
if (header & 0x80) { // RLE packet
if (offset + pixel_bytes > size) return false;
const uint8_t* p = data + offset;
offset += pixel_bytes;
for (int i = 0; i < count && current_pixel < total_pixels; ++i) {
std::memcpy(raw_pixels.data() + current_pixel * pixel_bytes, p, pixel_bytes);
current_pixel++;
}
} else { // Raw packet
for (int i = 0; i < count && current_pixel < total_pixels; ++i) {
if (offset + pixel_bytes > size) return false;
std::memcpy(raw_pixels.data() + current_pixel * pixel_bytes, data + offset, pixel_bytes);
offset += pixel_bytes;
current_pixel++;
}
}
}
}
// Convert BGR/BGRA to RGBA with vertical flip consideration
for (uint32_t y = 0; y < height; ++y) {
uint32_t src_y = flip_vertically ? (height - 1 - y) : y;
for (uint32_t x = 0; x < width; ++x) {
size_t src_idx = (src_y * width + x) * pixel_bytes;
size_t dst_idx = (y * width + x) * 4;
uint8_t b = raw_pixels[src_idx + 0];
uint8_t g = raw_pixels[src_idx + 1];
uint8_t r = raw_pixels[src_idx + 2];
uint8_t a = (bpp == 32) ? raw_pixels[src_idx + 3] : 255;
out_image.pixels[dst_idx + 0] = r;
out_image.pixels[dst_idx + 1] = g;
out_image.pixels[dst_idx + 2] = b;
out_image.pixels[dst_idx + 3] = a;
}
}
return true;
}
} // namespace metin2::texture