This commit is contained in:
skymz4 2022-04-23 14:54:10 +02:00 committed by GitHub
parent 05835a5c07
commit 6c22b58487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -950,6 +950,72 @@ void RenderD7::Image::LoadPFromBuffer(const std::vector<u8> &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<u16>(width);
subtex->height = static_cast<u16>(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<float>(w_pow2));
subtex->bottom = (1.0 - (subtex->height / static_cast<float>(h_pow2)));
C3D_TexInit(tex, static_cast<u16>(w_pow2), static_cast<u16>(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<u8 *>(tex->data))[dst_pos], &(static_cast<u8 *>(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)
{