Add rendering functions (crashes)

This commit is contained in:
2025-11-24 22:11:26 +01:00
parent a9eed546b9
commit 692611e162
9 changed files with 597 additions and 18 deletions

View File

@@ -44,35 +44,42 @@ void texture::unload() {
}
void texture::load(cstr& path) {
unload();
image img(path);
if (img.width() > 1024 || img.height() > 1024) {
throw std::runtime_error("Max Texture Size is 1024x1024!");
}
load(img.getBuffer(), img.width(), img.height(), img.bpp(), img.fmt());
}
int bpp = img.bpp();
m_size.x = img.width();
void texture::load(const std::vector<uc>& pixels, int w, int h, int bpp,
image::format fmt) {
if (w > 1024 || h > 1024) {
throw std::runtime_error("Max Texture Size is 1024x1024!");
}
unload();
m_size.x = w;
if (utils::isSingleBitNum(m_size.x)) {
m_size.x = utils::nextPow2(m_size.x);
}
m_size.y = img.height();
m_size.y = h;
if (utils::isSingleBitNum(m_size.y)) {
m_size.y = utils::nextPow2(m_size.y);
}
auto filter = GPU_NEAREST;
auto format = image2TexFmt(img.fmt());
auto format = image2TexFmt(fmt);
C3D_TexInit(&m_tex, (u16)m_size.x, (u16)m_size.y, format);
C3D_TexSetFilter(&m_tex, filter, filter);
// Using std::fill_n instead cause i hate this error lines
// under the memset func in my editor
std::fill_n((unsigned char*)m_tex.data, m_tex.size, 0);
for (int x = 0; x < img.width(); x++) {
for (int y = 0; y < img.height(); y++) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int dst_pos = tile3dsTex(x, y, m_size.x) * bpp;
int src_pos = (y * img.width() + x) * bpp;
int src_pos = (y * w + x) * bpp;
/// Best idea i had
for (int i = 0; i < bpp; i++) {
((u8*)m_tex.data)[dst_pos + bpp - 1 - i] = img[src_pos + i];
((u8*)m_tex.data)[dst_pos + bpp - 1 - i] = pixels[src_pos + i];
}
}
}