Add color class

Add Palladium Unique and Shared defs
Make stb_image an internal dependency
Start some work on font loader
This commit is contained in:
2025-12-07 18:08:36 +01:00
parent c18ef2161a
commit 28d2e291b3
13 changed files with 267 additions and 20 deletions

View File

@@ -3,6 +3,7 @@
#include <amethyst/app.hpp>
#include <amethyst/assets.hpp>
#include <amethyst/c3d.hpp>
#include <amethyst/color.hpp>
#include <amethyst/image.hpp>
#include <amethyst/iron.hpp>
#include <amethyst/renderer.hpp>

110
include/amethyst/color.hpp Normal file
View File

@@ -0,0 +1,110 @@
#pragma once
#include <amethyst/types.hpp>
// We all know where the code is from ... RenderD7 -> Palladium/Amy
namespace Amy {
class Color {
public:
/**
* Default Constructor (all variables are set to 0)
*/
constexpr Color() : r(0), g(0), b(0), a(0) {}
constexpr ~Color() {}
/**
* Constructor for 32Bit Color Input
* @param color 32Bit Color value
*/
constexpr Color(ui clr) {
a = (clr >> 24) & 0xff;
b = (clr >> 16) & 0xff;
g = (clr >> 8) & 0xff;
r = clr & 0xff;
}
/**
* Constructor for 8Bit Input
* @param r Red Value
* @param g Green Value
* @param b Blue Value
* @param a Optional Alpha Value (Defaults to 255)
*/
constexpr Color(int r, int g, int b, int a = 255) : r(r), g(g), b(b), a(a) {}
/**
* Constructor for float Input
* @param r Red Value
* @param g Green Value
* @param b Blue Value
* @param a Optional Alpha Value (Defaults to 1.0f)
* @note There is no Check if the number is between 0.0 and 1.0
*/
constexpr Color(float r, float g, float b, float a = 1.f)
: r(static_cast<uc>(255.f * r)),
g(static_cast<uc>(255.f * g)),
b(static_cast<uc>(255.f * b)),
a(static_cast<uc>(255.f * a)) {}
/**
* Constructor for Hex Input
* @param hex Hex String in `#ffffff` or `#ffffffff` format
*/
Color(const std::string& hex) { Hex(hex); }
/**
* Create Color Object by Hex String
* @param hex Hex String in `#ffffff` or `#ffffffff` format
* @return Color class itself
*/
Color& Hex(const std::string& hex);
/**
* Convert this Color Object to Hex string
* @param rgba [default false] sets if 8 or 6 digit color should be returned
* @return Color Hex String
*/
std::string Hex(bool rgba = false) const;
/**
* Fade from Current to another Color
* @param color Color to fade to
* @param p Amount (supports -1.0 to 1.0 for use of sine)
* @return Class Reference
*/
constexpr Color& Fade(const Color& color, float p) {
a = static_cast<uc>((color.a - a) * ((p + 1.f) / 2));
b = static_cast<uc>((color.b - b) * ((p + 1.f) / 2));
g = static_cast<uc>((color.g - g) * ((p + 1.f) / 2));
r = static_cast<uc>((color.r - r) * ((p + 1.f) / 2));
return *this;
}
/**
* Get 32Bit Color Value
* @return 32Bit Color Value (ABGR iirc)
*/
constexpr ui Get() const { return (a << 24) | (b << 16) | (g << 8) | r; }
/**
* Get The Luminance of the Color
* @return luminance (from 0.0 to 1.0)
*/
constexpr float Luminance() const {
// For Reference https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
return (0.3 * (r / 255.f) + 0.59 * (g / 255.f) + 0.11 * (b / 255.f));
}
/**
* Check if the Color is Light or Dark
* @return true if light
*/
constexpr bool IsLight() const { return (Luminance() >= 0.5); }
/**
* Operator to cast Color to 32Bit Value
* @return 32Bit Color Value
*/
constexpr operator ui() const { return Get(); }
/** Public Access Data section */
uc r;
uc g;
uc b;
uc a;
};
} // namespace Amy

View File

@@ -34,7 +34,8 @@ class Iron {
public:
Command() = default;
~Command() = default;
using ref = up<Command>;
AMY_UNIQUE(Command)
Command& Add(const u16& idx) {
IndexBuf.push_back(VertexBuf.size() + idx);
return *this;
@@ -66,20 +67,26 @@ class Iron {
Font() = default;
~Font() = default;
AMY_SHARED(Font)
void LoadTTF(ksr path, int pxh = 32);
void LoadTTF(kvr<uc> data, int pxh = 32);
/**
* Bitmap Font BTW
*/
void LoadBMF(ksr path);
Codepoint& GetCodepoint(ui c);
fvec2 GetTextBounds(ksr text, float scale);
void CmdTextEx(vec<Command::ref>& cmds, const fvec2& pos, ui color,
void CmdTextEx(vec<Command::Ref>& cmds, const fvec2& pos, ui color,
float scale, ksr text, ui flags = 0, const fvec2& box = 0);
void pMakeAtlas(bool final, vec<uc>& pixels, int size, Texture* tex);
int PxHeight;
int PxFactor = 24;
vec<Texture*> Textures;
std::map<u32, Codepoint> pCodeMap;
};
class Drawlist {
@@ -93,11 +100,11 @@ class Iron {
Drawlist(Drawlist&&) noexcept = default;
Drawlist& operator=(Drawlist&&) noexcept = default;
std::vector<Command::ref>& Data() { return pData; }
std::vector<Command::Ref>& Data() { return pData; }
void Merge(Drawlist* list);
Command::ref NewCommand();
void Push(Command ::ref cmd);
Command::Ref NewCommand();
void Push(Command ::Ref cmd);
void Clear();
void DrawSolid();
@@ -147,14 +154,15 @@ class Iron {
if (!ClipRects.empty()) ClipRects.pop();
}
operator std::vector<Command::ref>&() { return pData; }
operator std::vector<Command::Ref>&() { return pData; }
private:
void clipCmd(Command* ptr);
std::vector<Command::ref> pData;
std::vector<Command::Ref> pData;
std::vector<fvec2> pPath;
Texture* pTex = nullptr;
std::stack<fvec4> ClipRects;
Font* pCurrentFont;
int pLayer = 0;
};
Iron() = default;
@@ -164,7 +172,7 @@ class Iron {
static void Exit();
static void NewFrame();
static void DrawOn(C3D::Screen* screen);
static void Draw(const std::vector<Command::ref>& data);
static void Draw(const std::vector<Command::Ref>& data);
static Texture* WhiteTex() { return m_solid; }
/** Static renderer utility funcs */

View File

@@ -4,8 +4,8 @@
#include <amethyst/maths/vec.hpp>
#include <chrono>
#include <cinttypes>
#include <functional>
#include <format>
#include <functional>
#include <map>
#include <memory>
#include <numbers>
@@ -14,6 +14,20 @@
#include <string>
#include <vector>
#define AMY_SHARED(x) \
using Ref = std::shared_ptr<x>; \
template <typename... Args> \
static Ref New(Args&&... args) { \
return std::make_shared<x>(std::forward<Args>(args)...); \
}
#define AMY_UNIQUE(x) \
using Ref = std::unique_ptr<x>; \
template <typename... Args> \
static Ref New(Args&&... args) { \
return std::make_unique<x>(std::forward<Args>(args)...); \
}
namespace Amy {
using uc = unsigned char;
using us = unsigned short;

File diff suppressed because it is too large Load Diff