# Changes
- Add Debugstuff to main - Add IsType and AutoLoad to AssetManager - Add ID class for at compile time hashing - Add A8 to image (but not supported yet) - Fix error in Vertex constructor - Add PathClear to PathFill func - Add pCheckSize to check for overflows - Make Tex in Texture a pointer ref - Add default uv to texture - Add own c++ exception - Add FNV32 Hash func (compile and runtime) - Fix Power2 check in texture loader - Load Shader manualy in iron (cause it seems not working correctly with files)
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
namespace Amy {
|
||||
void App::Run() {
|
||||
pLast = Utils::GetTimeNano();
|
||||
consoleInit(GFX_BOTTOM, NULL);
|
||||
while (aptMainLoop()) {
|
||||
ull c = Utils::GetTimeNano();
|
||||
pDelta = static_cast<double>(static_cast<double>(c) -
|
||||
|
||||
21
source/assets.cpp
Normal file
21
source/assets.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <amethyst/assets.hpp>
|
||||
#include <amethyst/c3d.hpp>
|
||||
#include <amethyst/texture.hpp>
|
||||
|
||||
namespace Amy {
|
||||
void AssetMgr::AutoLoad(const ID& id, ksr path) {
|
||||
if (path.ends_with(".png") || path.ends_with(".jpg") ||
|
||||
path.ends_with(".bmp")) {
|
||||
Texture* tex = new Texture();
|
||||
tex->Load(path);
|
||||
Add(id, tex);
|
||||
} else if (path.ends_with(".shbin")) {
|
||||
C3D::Shader* shader = new C3D::Shader();
|
||||
shader->Load(path);
|
||||
Add(id, shader);
|
||||
} else {
|
||||
throw std::runtime_error("[amy]: assets: " + id.GetName() + " (" + path +
|
||||
") is unsupported for AssetMgr::AutoLoad!");
|
||||
}
|
||||
}
|
||||
} // namespace Amy
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <amethyst/c3d.hpp>
|
||||
#include <amethyst/utils.hpp>
|
||||
#include <pica.hpp>
|
||||
// #include <pica.hpp>
|
||||
|
||||
namespace Amy {
|
||||
|
||||
@@ -46,7 +46,7 @@ void C3D::Shader::Load(const std::string& path) {
|
||||
auto code = Utils::LoadFile2Mem(path);
|
||||
if (!code.size()) {
|
||||
throw std::runtime_error(
|
||||
std::format("[amy] unsable to load shader ({})", path));
|
||||
std::format("[amy] unable to load shader ({})", path));
|
||||
}
|
||||
Load(code);
|
||||
}
|
||||
@@ -60,8 +60,9 @@ void C3D::Shader::Load(const std::vector<uc>& data) {
|
||||
}
|
||||
|
||||
void C3D::Shader::Compile(const std::string& code) {
|
||||
auto ret = Pica::AssembleCode(code.c_str());
|
||||
Load(ret);
|
||||
throw std::runtime_error("[amy]: unable to compile shader (not allowed)");
|
||||
/*auto ret = Pica::AssembleCode(code.c_str());
|
||||
Load(ret);*/
|
||||
}
|
||||
|
||||
void C3D::Shader::Use() {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <amethyst/utils.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
// #define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
namespace Amy {
|
||||
@@ -75,6 +75,9 @@ int Image::GetBppOfFmt(const Image::Format& fmt) {
|
||||
case RGB565:
|
||||
return 2;
|
||||
break;
|
||||
case A8:
|
||||
return 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0; // Unknown
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <amethyst/iron.hpp>
|
||||
#include <amethyst/utils.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
@@ -103,6 +104,12 @@ void Iron::Draw(const std::vector<Iron::Command::ref>& data) {
|
||||
scissor == data[i]->ScissorRect && tex == data[i]->Tex) {
|
||||
auto c = data[i].get();
|
||||
|
||||
if (!pCheckSize(c->IndexBuf.size(), c->VertexBuf.size())) {
|
||||
throw Error("iron: too much draw data!!!" +
|
||||
std::format("\nIdx: {}\nVtx: {}", c->IndexBuf.size(),
|
||||
c->VertexBuf.size()));
|
||||
}
|
||||
|
||||
for (int j = 0; j < c->IndexBuf.size(); j++) {
|
||||
m_ibuf[m_idx++] = m_vtx + c->IndexBuf[j];
|
||||
}
|
||||
@@ -118,15 +125,34 @@ void Iron::Draw(const std::vector<Iron::Command::ref>& data) {
|
||||
C3D::DrawElements(i - start, m_ibuf.data() + start);
|
||||
}
|
||||
C3D::DepthTest(true);
|
||||
/*std::ofstream off("hello.txt");
|
||||
for (int i = 0; i < m_idx; i++) {
|
||||
auto& v = m_vbuf[m_ibuf[i]];
|
||||
off << std::format("{} -> [{}] [{}] #{:08X}\n", m_ibuf[i], v.pos, v.uv,
|
||||
v.color);
|
||||
}
|
||||
off.close();
|
||||
throw std::runtime_error("halt");*/
|
||||
}
|
||||
|
||||
bool Iron::pCheckSize(size_t idx, size_t vtx) {
|
||||
return idx < m_ibuf.size() && vtx < m_vbuf.size();
|
||||
}
|
||||
|
||||
void Iron::pSetupShader() {
|
||||
m_shader = new C3D::Shader();
|
||||
m_shader->Load("romfs:/shaders/lithium.shbin");
|
||||
m_shader->pCode = DVLB_ParseFile((u32*)li_shader, li_shader_size);
|
||||
shaderProgramInit(&m_shader->pProgram);
|
||||
shaderProgramSetVsh(&m_shader->pProgram, &m_shader->pCode->DVLE[0]);
|
||||
AttrInfo_Init(&m_shader->pInfo);
|
||||
AttrInfo_AddLoader(&m_shader->pInfo, 0, GPU_FLOAT, 2);
|
||||
AttrInfo_AddLoader(&m_shader->pInfo, 1, GPU_FLOAT, 2);
|
||||
AttrInfo_AddLoader(&m_shader->pInfo, 2, GPU_UNSIGNED_BYTE, 4);
|
||||
// m_shader->Load("romfs:/shaders/lithium.shbin");
|
||||
// m_shader->Compile(__ironshader__);
|
||||
m_shader->Input(GPU_FLOAT, 2); // pos
|
||||
m_shader->Input(GPU_FLOAT, 2); // uv
|
||||
m_shader->Input(GPU_UNSIGNED_BYTE, 4); // color
|
||||
// m_shader->Input(GPU_FLOAT, 2); // pos
|
||||
// m_shader->Input(GPU_FLOAT, 2); // uv
|
||||
// m_shader->Input(GPU_UNSIGNED_BYTE, 4); // color
|
||||
uLocProj = m_shader->loc("projection");
|
||||
}
|
||||
|
||||
@@ -141,6 +167,9 @@ void Iron::pInitSolidTex() {
|
||||
std::vector<uc> pixels(16 * 16 * 4, 0xff);
|
||||
m_solid = new Texture();
|
||||
m_solid->Load(pixels, 16, 16);
|
||||
if (!m_solid->Ptr()) {
|
||||
throw Error("white tex failed to load!");
|
||||
}
|
||||
}
|
||||
|
||||
bool Iron::InBox(const fvec2& pos, const fvec2& size, const fvec4& area) {
|
||||
|
||||
@@ -24,9 +24,11 @@ GPU_TEXCOLOR image2TexFmt(const Image::Format& fmt) {
|
||||
case Image::RGB565:
|
||||
return GPU_RGB565;
|
||||
break;
|
||||
case Image::A8:
|
||||
return GPU_A8;
|
||||
break;
|
||||
default:
|
||||
// Dummy
|
||||
return GPU_A4;
|
||||
throw std::runtime_error(
|
||||
"[amy] texture: Unsupported texture Format used!");
|
||||
break;
|
||||
@@ -38,55 +40,55 @@ Texture::~Texture() { Unload(); }
|
||||
|
||||
void Texture::Unload() {
|
||||
if (pLoaded) {
|
||||
C3D_TexDelete(&pTex);
|
||||
C3D_TexDelete(pTex);
|
||||
delete pTex;
|
||||
pTex = nullptr;
|
||||
pLoaded = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::Load(ksr path) {
|
||||
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());
|
||||
}
|
||||
|
||||
void Texture::Load(kvr<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!");
|
||||
throw std::runtime_error("[amy] texture: Max Texture Size is 1024x1024!");
|
||||
}
|
||||
Unload();
|
||||
|
||||
pSize.x = w;
|
||||
if (Utils::IsSingleBitNum(pSize.x)) {
|
||||
if (!Utils::IsSingleBitNum(pSize.x)) {
|
||||
pSize.x = Utils::NextPow2(pSize.x);
|
||||
}
|
||||
pSize.y = h;
|
||||
if (Utils::IsSingleBitNum(pSize.y)) {
|
||||
if (!Utils::IsSingleBitNum(pSize.y)) {
|
||||
pSize.y = Utils::NextPow2(pSize.y);
|
||||
}
|
||||
auto filter = GPU_NEAREST;
|
||||
auto Format = image2TexFmt(fmt);
|
||||
C3D_TexInit(&pTex, (u16)pSize.x, (u16)pSize.y, Format);
|
||||
C3D_TexSetFilter(&pTex, filter, filter);
|
||||
pTex = new C3D_Tex;
|
||||
C3D_TexInit(pTex, (u16)pSize.x, (u16)pSize.y, Format);
|
||||
C3D_TexSetFilter(pTex, 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*)pTex.data, pTex.size, 0);
|
||||
std::fill_n((uc*)pTex->data, pTex->size, 0);
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int dst_pos = tile3dsTex(x, y, pSize.x) * bpp;
|
||||
int src_pos = (y * w + x) * bpp;
|
||||
/// Best idea i had
|
||||
for (int i = 0; i < bpp; i++) {
|
||||
((u8*)pTex.data)[dst_pos + bpp - 1 - i] = pixels[src_pos + i];
|
||||
((u8*)pTex->data)[dst_pos + bpp - 1 - i] = pixels[src_pos + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
C3D_TexFlush(&pTex);
|
||||
pTex.border = 0x00000000;
|
||||
C3D_TexSetWrap(&pTex, GPU_REPEAT, GPU_REPEAT);
|
||||
C3D_TexFlush(pTex);
|
||||
pTex->border = 0x00000000;
|
||||
C3D_TexSetWrap(pTex, GPU_REPEAT, GPU_REPEAT);
|
||||
pLoaded = true;
|
||||
}
|
||||
|
||||
void Texture::Bind(int reg) { C3D_TexBind(reg, &pTex); }
|
||||
void Texture::Bind(int reg) { C3D_TexBind(reg, pTex); }
|
||||
} // namespace Amy
|
||||
@@ -3,14 +3,6 @@
|
||||
|
||||
namespace Amy {
|
||||
namespace Utils {
|
||||
ui fastHash(ksr s) {
|
||||
ui hash = 5381;
|
||||
for (auto& it : s) {
|
||||
hash = (hash * 33) + static_cast<uc>(it);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
vec<uc> LoadFile2Mem(ksr path) {
|
||||
std::ifstream iff(path, std::ios::binary);
|
||||
if (!iff) {
|
||||
|
||||
Reference in New Issue
Block a user