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

Binary file not shown.

View File

@@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED true)
option(AMY_GOD_DEV "Turn this on if you think you are god" OFF) option(AMY_GOD_DEV "Turn this on if you think you are god" OFF)
# THis option should be disabled if you use STB_IMAGE in you main project # THis option should be disabled if you use STB_IMAGE in you main project
set(AMY_BUILD_STB_IMAGE "Include STB Image code" CACHE BOOL 1) set(AMY_BUILD_STB_IMAGE "Include STB Image code" CACHE BOOL 1)
set(AMY_WITH_MPG123 "Include MP3 Support" CACHE BOOL 1)
#add_subdirectory(vendor/libpicasso) #add_subdirectory(vendor/libpicasso)
@@ -15,6 +16,7 @@ add_library(${PROJECT_NAME} STATIC
source/app.cpp source/app.cpp
source/assets.cpp source/assets.cpp
source/amethyst.cpp source/amethyst.cpp
source/color.cpp
source/image.cpp source/image.cpp
source/renderer.cpp source/renderer.cpp
source/texture.cpp source/texture.cpp
@@ -24,12 +26,23 @@ add_library(${PROJECT_NAME} STATIC
source/iron/drawlist.cpp source/iron/drawlist.cpp
source/maths/mat.cpp source/maths/mat.cpp
) )
target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${DEVKITPRO}/portlibs/3ds/include
)
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/source/internal
)
#target_link_libraries(${PROJECT_NAME} PUBLIC pica::pica) #target_link_libraries(${PROJECT_NAME} PUBLIC pica::pica)
target_link_libraries(${PROJECT_NAME} PUBLIC m z ctru citro3d) target_link_libraries(${PROJECT_NAME} PUBLIC m z ctru citro3d mpg123)
target_compile_definitions(${PROJECT_NAME} PUBLIC AMY_3DS AMY_STB_IMAGE=${AMY_BUILD_STB_IMAGE}) target_compile_definitions(${PROJECT_NAME} PUBLIC
AMY_3DS
AMY_STB_IMAGE=${AMY_BUILD_STB_IMAGE}
AMY_WITH_MPG123=${AMY_WITH_MPG123}
)
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi) target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi)
add_subdirectory(example) add_subdirectory(example)
install(TARGETS ${PROJECT_NAME}) install(TARGETS ${PROJECT_NAME})
install(DIRECTORY include DESTINATION .)

View File

@@ -1,3 +1,29 @@
# amethyst # amethyst
2d Renderer Library made for ReCraft3DS based on palladium. 2d Renderer Library made for ReCraft3DS based on palladium.
## Usage
### Submodule:
Add amethyst as submodule to your repository
```bash
git submodule add https://github.com/tobid7/amethyst vendor/amethyst
```
Add it as submodule to your `CMakeLists.txt`
```cmake
add_subdirectory(vendor/amethyst)
target_link_libraries(${PROJECT_NAME} PUBLIC amethyst)
```
### Installed
Coming Soon...
## Credits
- [tobid7](https://github.com/tobid7) Lead Developer, Stealing a lot of code of [Palladium](https://github.com/tobid7/palladium)
- [nothings](https://github.com/nothings) [stb_image](https://github.com/nothings/stb)

View File

@@ -41,10 +41,11 @@ class Example : public Amy::App {
Top->Clear(); Top->Clear();
Top->Use(); Top->Use();
dl->DrawTex(Mgr->Get<Amy::Texture>("icon")); dl->DrawTex(Mgr->Get<Amy::Texture>("icon"));
dl->DrawRectFilled(Amy::fvec2(50, 0), 48, 0x99999999); dl->DrawRectFilled(Amy::fvec2(50, 0), 48, Amy::Color(255, 255, 255, 160));
dl->DrawCircleFilled(Amy::fvec2(200, 120), 50, 0xffffffff, 40); // Color only at runtime...yet (Palladium 0.6.0 dev is targeting this)
dl->DrawCircleFilled(Amy::fvec2(200, 120), 50, Amy::Color("#ffffff"), 40);
dl->DrawSolid(); dl->DrawSolid();
dl->DrawRectFilled(0, 50, 0x4400ff00); dl->DrawRectFilled(0, 50, Amy::Color(0.f, 1.f, 0.f, 1.f));
Iron::NewFrame(); Iron::NewFrame();
Iron::DrawOn(Top); Iron::DrawOn(Top);

View File

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

View File

@@ -4,8 +4,8 @@
#include <amethyst/maths/vec.hpp> #include <amethyst/maths/vec.hpp>
#include <chrono> #include <chrono>
#include <cinttypes> #include <cinttypes>
#include <functional>
#include <format> #include <format>
#include <functional>
#include <map> #include <map>
#include <memory> #include <memory>
#include <numbers> #include <numbers>
@@ -14,6 +14,20 @@
#include <string> #include <string>
#include <vector> #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 { namespace Amy {
using uc = unsigned char; using uc = unsigned char;
using us = unsigned short; using us = unsigned short;

50
source/color.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include <amethyst/color.hpp>
namespace Amy {
// The Solution of the biggest performance issue
// A Simple Lookup table
static const std::map<char, int> HEX_DEC = {
{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11},
{'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}, {'A', 10}, {'B', 11},
{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
Color& Color::Hex(const std::string& hex) {
// zu dumm nen safetey check zu schreiben wadafuk
/**#ifndef AMY_GOD_DEV
/// Safetey check (not required if you just program well xd)
if (hex.length() != 7 || hex.length() != 9 || hex.length() != 6 ||
hex.length() != 8 || std::find_if(hex.begin(), hex.end(), [](char c) {
return !std::isxdigit(c);
}) != hex.end()) {
return *this;
}
#endif*/
int offset = ((hex.length() == 7 || hex.length() == 9) ? 1 : 0);
r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
offset += 2;
g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
offset += 2;
b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
offset += 2;
if (hex.length() == 9) {
a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
} else {
a = 255;
}
return *this;
}
std::string Color::Hex(bool rgba) const {
/** Need to int cast (so it is used as num and not char...) */
std::stringstream s;
s << "#";
s << std::hex << std::setw(2) << std::setfill('0') << (int)r;
s << std::hex << std::setw(2) << std::setfill('0') << (int)g;
s << std::hex << std::setw(2) << std::setfill('0') << (int)b;
if (rgba) {
s << std::hex << std::setw(2) << std::setfill('0') << (int)a;
}
return s.str();
}
} // namespace Amy

0
include/stb_image.h → source/internal/stb_image.h Executable file → Normal file
View File

View File

@@ -33,8 +33,8 @@ void Iron::Drawlist::Clear() {
pLayer = 0; pLayer = 0;
} }
Iron::Command::ref Iron::Drawlist::NewCommand() { Iron::Command::Ref Iron::Drawlist::NewCommand() {
auto ret = std::make_unique<Command>(); auto ret = Command::New();
ret->Layer = pLayer; ret->Layer = pLayer;
ret->Index = pData.size(); ret->Index = pData.size();
ret->Tex = pTex; ret->Tex = pTex;
@@ -48,7 +48,7 @@ void Iron::Drawlist::clipCmd(Command* ptr) {
} }
} }
void Iron::Drawlist::Push(Command::ref cmd) { pData.push_back(std::move(cmd)); } void Iron::Drawlist::Push(Command::Ref cmd) { pData.push_back(std::move(cmd)); }
void Iron::Drawlist::DrawSolid() { pTex = Iron::WhiteTex(); } void Iron::Drawlist::DrawSolid() { pTex = Iron::WhiteTex(); }

24
source/iron/font.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <amethyst/iron.hpp>
namespace Amy {
void Iron::Font::LoadBMF(ksr path) {}
void Iron::Font::LoadTTF(ksr path, int size) {}
void Iron::Font::pMakeAtlas(bool final, vec<uc>& font_tex, int texszs,
Texture* tex) {
tex->Load(font_tex, texszs, texszs);
Textures.push_back(tex);
}
Iron::Font::Codepoint& Iron::Font::GetCodepoint(ui cp) {
// Check if codepoijt exist or return a static invalid one
auto res = pCodeMap.find(cp);
if (res == pCodeMap.end()) {
static Codepoint invalid;
invalid.Valid = false;
return invalid;
}
return res->second;
}
} // namespace Amy

View File

@@ -83,7 +83,7 @@ void Iron::DrawOn(C3D::Screen* screen) {
m_shader->SetMat4(uLocProj, m_mtx); m_shader->SetMat4(uLocProj, m_mtx);
} }
void Iron::Draw(const std::vector<Iron::Command::ref>& data) { void Iron::Draw(const std::vector<Iron::Command::Ref>& data) {
// disable depthtest cause we have no z buffer // disable depthtest cause we have no z buffer
C3D::DepthTest(false); C3D::DepthTest(false);
pFragConfig(); pFragConfig();