# 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:
2025-11-27 13:32:44 +01:00
parent 2a2a670e1a
commit febf506a5e
17 changed files with 265 additions and 58 deletions

View File

@@ -2,6 +2,7 @@
#include <amethyst/asset.hpp>
#include <amethyst/types.hpp>
#include <amethyst/utils.hpp>
namespace Amy {
class App {

View File

@@ -1,34 +1,55 @@
#pragma once
#include <amethyst/id.hpp>
#include <amethyst/texture.hpp>
#include <amethyst/types.hpp>
namespace Amy {
class Assets {
class AssetMgr {
public:
Assets() = default;
~Assets() = default;
AssetMgr() = default;
~AssetMgr() = default;
void add(ksr id, Asset* v) { pAssets[id] = v; }
void remove(ksr id) {
void AutoLoad(const ID& name, ksr path);
void Add(const ID& id, Asset* v) {
if (pAssets.count(id)) {
throw std::runtime_error("[amy]: assets: " + id.GetName() +
" already exists!");
}
pAssets[id] = v;
}
void Remove(const ID& id) {
if (pAssets.count(id)) {
pAssets.erase(id);
}
}
template <typename T>
T* get(ksr id) {
T* Get(const ID& id) {
auto r = pAssets.find(id);
if (r == pAssets.end()) {
throw std::runtime_error("[amy] assets: unable to find " + id);
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
}
if (auto v = dynamic_cast<T*>(r->second)) {
return v;
} else {
throw std::runtime_error(id + " is not of type " + typeid(T).name());
throw std::runtime_error(id.GetName() + " is not of type " +
typeid(T).name());
}
}
template <typename T>
bool IsType(const ID& id) {
auto r = pAssets.find(id);
if (r == pAssets.end()) {
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
}
return dynamic_cast<T*>(r->second) != nullptr;
}
private:
std::map<str, Asset*> pAssets;
std::map<ID, Asset*> pAssets;
};
} // namespace Amy

View File

@@ -53,7 +53,7 @@ class C3D {
void SetMat4(int loc, const mat4& m);
int loc(ksr name);
private:
// private:
C3D_AttrInfo pInfo;
shaderProgram_s pProgram;
DVLB_s* pCode;

42
include/amethyst/id.hpp Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
#include <amethyst/types.hpp>
#include <amethyst/utils.hpp>
namespace Amy {
class ID {
public:
constexpr ID(const ui& id) { pID = id; }
constexpr ID(std::string name) {
pID = Utils::FNV32(name);
#ifdef AMY_KEEP_STR_ID
pName = name;
#endif
}
constexpr ID(const char* name) {
pID = Utils::FNV32(name);
#ifdef AMY_KEEP_STR_ID
pName = name;
#endif
}
~ID() {}
ui GetID() { return pID; }
std::string GetName() const {
#ifdef AMY_KEEP_STR_ID
return pName;
#else
return std::format("hash({:#08x})", pID);
#endif
}
operator ui() const { return pID; }
operator std::string() const { return GetName(); }
private:
ui pID;
#ifdef AMY_KEEP_STR_ID
str pName;
#endif
};
} // namespace Amy

View File

@@ -12,6 +12,7 @@ class Image {
BGR, // bpp == 3
ABGR, // bpp == 4
BGRA, // bpp == 4
A8, // bpp == 1 (not supported in laoding)
};
Image() = default;
Image(ksr path) { this->Load(path); }

View File

@@ -15,8 +15,8 @@ class Iron {
Vertex(float x, float y, float u, float v, ui clr) {
pos.x = x;
pos.y = y;
uv.x = x;
uv.y = y;
uv.x = u;
uv.y = v;
color = clr;
}
Vertex(const fvec2& pos, const fvec2& uv, ui clr) {
@@ -33,6 +33,7 @@ class Iron {
class Command {
public:
Command() = default;
~Command() = default;
using ref = up<Command>;
Command& Add(const u16& idx) {
IndexBuf.push_back(VertexBuf.size() + idx);
@@ -100,7 +101,10 @@ class Iron {
DrawPolyLine(pPath, color, flags, thickness);
PathClear();
}
void PathFill(ui color) { DrawConvexPolyFilled(pPath, color); }
void PathFill(ui color) {
DrawConvexPolyFilled(pPath, color);
PathClear();
}
void PathArcToN(const fvec2& c, float radius, float a_min, float a_max,
int segments);
void PathFastArcToN(const fvec2& c, float radius, float a_min, float a_max,
@@ -152,6 +156,7 @@ class Iron {
static void pSetupShader();
static void pFragConfig();
static void pInitSolidTex();
static bool pCheckSize(size_t idx, size_t vtx);
static std::vector<Vertex, linearAllocator<Vertex>> m_vbuf;
static std::vector<u16, linearAllocator<u16>> m_ibuf;

View File

@@ -26,14 +26,14 @@ class Texture : public Asset {
ivec2& Size() { return pSize; }
Rect& Uv() { return pUv; }
C3D_Tex* Ptr() { return pLoaded ? &pTex : nullptr; }
C3D_Tex* Ptr() { return pLoaded ? pTex : nullptr; }
void Bind(int reg = 0);
private:
C3D_Tex pTex;
C3D_Tex* pTex = nullptr;
ivec2 pSize;
Rect pUv;
Rect pUv = fvec4(0, 1, 1, 0);
bool pLoaded = false;
};
} // namespace Amy

View File

@@ -3,8 +3,11 @@
#include <amethyst/types.hpp>
namespace Amy {
class Error : public std::runtime_error {
public:
Error(ksr str) : std::runtime_error("[amy] " + str) {}
};
namespace Utils {
ui FastHash(ksr s);
vec<uc> LoadFile2Mem(ksr path);
ui HashMemory(kvr<uc> data);
ui NextPow2(ui v);
@@ -12,6 +15,20 @@ bool IsSingleBitNum(ui v);
ull GetTimeNano();
ull GetTimeMicro();
ull GetTimeMilli();
/**
* FNV Hash functiom (32 Bit)
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
* Able to run at compile time btw
*/
constexpr ui FNV32(std::string_view str) {
ui ret = 0x811C9DC5;
for (char it : str) {
ret ^= static_cast<ui>(it);
ret *= 16777619;
}
return ret;
}
namespace Image {
void ReverseBuf(vec<uc>& buf, int w, int h, int c);
void RemoveAlphaChannel(vec<uc>& buf, int w, int h);