From 6c22b58487a47d0c40dadcde085c648c331d99a7 Mon Sep 17 00:00:00 2001 From: skymz4 <99190689+skymz4@users.noreply.github.com> Date: Sat, 23 Apr 2022 14:54:10 +0200 Subject: [PATCH] __ --- renderd7.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/renderd7.cpp b/renderd7.cpp index 01e49cd..03589cb 100644 --- a/renderd7.cpp +++ b/renderd7.cpp @@ -950,6 +950,72 @@ void RenderD7::Image::LoadPFromBuffer(const std::vector &buffer) } } +static u32 GetNextPowerOf2(u32 v) { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return (v >= 64 ? v : 64); +} + +static bool C3DTexToC2DImage(C2D_Image *texture, u32 width, u32 height, u8 *buf) { + if (width >= 1024 || height >= 1024) + return false; + + C3D_Tex *tex = new C3D_Tex[sizeof(C3D_Tex)]; + Tex3DS_SubTexture *subtex = new Tex3DS_SubTexture[sizeof(Tex3DS_SubTexture)]; + subtex->width = static_cast(width); + subtex->height = static_cast(height); + // RGBA -> ABGR + for (u32 row = 0; row < subtex->width; row++) { + for (u32 col = 0; col < subtex->height; col++) { + u32 z = (row + col * subtex->width) * BYTES_PER_PIXEL; + + u8 r = *(u8 *)(buf + z); + u8 g = *(u8 *)(buf + z + 1); + u8 b = *(u8 *)(buf + z + 2); + u8 a = *(u8 *)(buf + z + 3); + + *(buf + z) = a; + *(buf + z + 1) = b; + *(buf + z + 2) = g; + *(buf + z + 3) = r; + } + } + + u32 w_pow2 = GetNextPowerOf2(subtex->width); + u32 h_pow2 = GetNextPowerOf2(subtex->height); + subtex->left = 0.f; + subtex->top = 1.f; + subtex->right = (subtex->width /static_cast(w_pow2)); + subtex->bottom = (1.0 - (subtex->height / static_cast(h_pow2))); + C3D_TexInit(tex, static_cast(w_pow2), static_cast(h_pow2), GPU_RGBA8); + C3D_TexSetFilter(tex, GPU_NEAREST, GPU_NEAREST); + + std::memset(tex->data, 0, tex->size); + + for (u32 x = 0; x < subtex->width; x++) { + for (u32 y = 0; y < subtex->height; y++) { + u32 dst_pos = ((((y >> 3) * (w_pow2 >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) * BYTES_PER_PIXEL; + u32 src_pos = (y * subtex->width + x) * BYTES_PER_PIXEL; + std::memcpy(&(static_cast(tex->data))[dst_pos], &(static_cast(buf))[src_pos], BYTES_PER_PIXEL); + } + } + + C3D_TexFlush(tex); + tex->border = RenderD7::Color::Hex("#000000", 0); + C3D_TexSetWrap(tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER); + if (tex && subtex) { + texture->tex = tex; + texture->subtex = subtex; + return true; + } + return false; +} + void RenderD7::Image::FromSheet(RenderD7::Sheet sheet, size_t index) {