Files
amethyst/source/texture.cpp

94 lines
2.4 KiB
C++
Raw Normal View History

2025-11-19 22:20:27 +01:00
#include <3ds.h>
#include <algorithm>
#include <amethyst/image.hpp>
#include <amethyst/texture.hpp>
#include <amethyst/utils.hpp>
#include <stdexcept>
namespace Amy {
2025-11-19 22:20:27 +01:00
ui tile3dsTex(int x, int y, int w) {
return ((((y >> 3) * ((int)w >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3)));
}
GPU_TEXCOLOR image2TexFmt(const Image::Format& fmt) {
2025-11-19 22:20:27 +01:00
switch (fmt) {
case Image::RGB:
2025-11-19 22:20:27 +01:00
return GPU_RGB8;
break;
case Image::RGBA:
2025-11-19 22:20:27 +01:00
return GPU_RGBA8;
break;
case Image::RGB565:
2025-11-19 22:20:27 +01:00
return GPU_RGB565;
break;
case Image::A8:
return GPU_A8;
break;
2025-11-19 22:20:27 +01:00
default:
// Dummy
throw std::runtime_error(
"[amy] texture: Unsupported texture Format used!");
2025-11-19 22:20:27 +01:00
break;
}
}
Texture::Texture(ksr path) { Load(path); }
2025-11-19 22:20:27 +01:00
Texture::~Texture() { Unload(); }
2025-11-19 22:20:27 +01:00
void Texture::Unload() {
if (pLoaded) {
C3D_TexDelete(pTex);
delete pTex;
pTex = nullptr;
pLoaded = false;
2025-11-19 22:20:27 +01:00
}
}
void Texture::Load(ksr path) {
Image img(path);
Load(img.GetBuffer(), img.Width(), img.Height(), img.Bpp(), img.Fmt());
2025-11-24 22:11:26 +01:00
}
void Texture::Load(kvr<uc> pixels, int w, int h, int bpp, Image::Format fmt) {
2025-11-24 22:11:26 +01:00
if (w > 1024 || h > 1024) {
throw std::runtime_error("[amy] texture: Max Texture Size is 1024x1024!");
2025-11-24 22:11:26 +01:00
}
Unload();
2025-11-19 22:20:27 +01:00
pSize.x = w;
if (!Utils::IsSingleBitNum(pSize.x)) {
pSize.x = Utils::NextPow2(pSize.x);
2025-11-19 22:20:27 +01:00
}
pSize.y = h;
if (!Utils::IsSingleBitNum(pSize.y)) {
pSize.y = Utils::NextPow2(pSize.y);
2025-11-19 22:20:27 +01:00
}
auto filter = GPU_NEAREST;
auto Format = image2TexFmt(fmt);
pTex = new C3D_Tex;
C3D_TexInit(pTex, (u16)pSize.x, (u16)pSize.y, Format);
C3D_TexSetFilter(pTex, filter, filter);
2025-11-19 22:20:27 +01:00
// Using std::fill_n instead cause i hate this error lines
// under the memset func in my editor
std::fill_n((uc*)pTex->data, pTex->size, 0);
2025-11-24 22:11:26 +01:00
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int dst_pos = tile3dsTex(x, y, pSize.x) * bpp;
2025-11-24 22:11:26 +01:00
int src_pos = (y * w + x) * bpp;
2025-11-19 22:20:27 +01:00
/// Best idea i had
for (int i = 0; i < bpp; i++) {
((u8*)pTex->data)[dst_pos + bpp - 1 - i] = pixels[src_pos + i];
2025-11-19 22:20:27 +01:00
}
}
}
C3D_TexFlush(pTex);
pTex->border = 0x00000000;
C3D_TexSetWrap(pTex, GPU_REPEAT, GPU_REPEAT);
pLoaded = true;
2025-11-19 22:20:27 +01:00
}
void Texture::Bind(int reg) { C3D_TexBind(reg, pTex); }
} // namespace Amy