Initial Commit

This commit is contained in:
2024-07-12 19:48:34 +02:00
parent 9fd5826e0e
commit ac9e58cce2
89 changed files with 50981 additions and 23 deletions

229
source/Color.cpp Normal file
View File

@ -0,0 +1,229 @@
#include <filesystem>
#include <fstream>
#include <map>
#include <pd/Color.hpp>
#include <pd/Message.hpp>
#include <pd/external/json.hpp>
#include <pd/internal_db.hpp>
void pdi_swap32(unsigned int& c) {
c = ((c & 0xFF) << 24) | ((c & 0xFF00) << 8) | ((c & 0xFF0000) >> 8) |
((c & 0xFF000000) >> 24);
}
std::string Palladium::Color::RGBA2Hex(unsigned int c32) {
pdi_swap32(c32);
std::stringstream ss;
ss << "#";
ss << std::hex << std::setw(8) << std::setfill('0') << c32;
return ss.str();
}
// Standart Color Converter
static const std::map<char, int> HEX_TO_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}};
unsigned int pdi_special_color_hex(const std::string& hex) {
if (hex.length() < 9 || std::find_if(hex.begin() + 1, hex.end(), [](char c) {
return !std::isxdigit(c);
}) != hex.end()) {
return pdi_special_color_hex("#00000000");
}
int r = HEX_TO_DEC.at(hex[1]) * 16 + HEX_TO_DEC.at(hex[2]);
int g = HEX_TO_DEC.at(hex[3]) * 16 + HEX_TO_DEC.at(hex[4]);
int b = HEX_TO_DEC.at(hex[5]) * 16 + HEX_TO_DEC.at(hex[6]);
int a = HEX_TO_DEC.at(hex[7]) * 16 + HEX_TO_DEC.at(hex[8]);
return RGBA8(r, g, b, a);
}
// Default Theme
const std::map<PDColor, unsigned int> pdi_default_theme = {
{PDColor_Text, RGBA8(0, 0, 0, 255)},
{PDColor_Text2, RGBA8(255, 255, 255, 255)}, // For Background change or so
{PDColor_TextDisabled, RGBA8(170, 170, 170, 255)},
{PDColor_Background, RGBA8(238, 238, 238, 255)},
{PDColor_Header, RGBA8(17, 17, 17, 255)},
{PDColor_Selector, RGBA8(34, 34, 34, 255)},
{PDColor_SelectorFade, RGBA8(90, 90, 90, 255)},
{PDColor_List0, RGBA8(204, 204, 204, 255)}, // List0 = % 2
{PDColor_List1, RGBA8(187, 187, 187, 255)},
{PDColor_MessageBackground, RGBA8(51, 51, 51, 255)},
{PDColor_Button, RGBA8(17, 17, 17, 255)},
{PDColor_ButtonHovered, RGBA8(34, 34, 34, 255)},
{PDColor_ButtonDisabled, RGBA8(8, 8, 8, 255)},
{PDColor_ButtonActive, RGBA8(42, 42, 42, 255)},
{PDColor_Checkmark, RGBA8(42, 42, 42, 255)},
{PDColor_FrameBg, RGBA8(85, 85, 85, 255)},
{PDColor_FrameBgHovered, RGBA8(119, 119, 119, 255)},
{PDColor_Progressbar, RGBA8(0, 255, 0, 255)},
};
void Palladium::Theme::Load(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
return;
}
nlohmann::json js;
file >> js;
// clang-format off
if(THEMEVER != js["version"]) {
file.close();
return;
}
this->clr_tab.clear();
this->clr_tab.resize(PDColor_Len);
this->clr_tab[PDColor_Text] = pdi_special_color_hex(js["PDColor_Text"].get<std::string>());
this->clr_tab[PDColor_Text2] = pdi_special_color_hex(js["PDColor_Text2"].get<std::string>());
this->clr_tab[PDColor_TextDisabled] = pdi_special_color_hex(js["PDColor_TextDisabled"].get<std::string>());
this->clr_tab[PDColor_Background] = pdi_special_color_hex(js["PDColor_Background"].get<std::string>());
this->clr_tab[PDColor_Header] = pdi_special_color_hex(js["PDColor_Header"].get<std::string>());
this->clr_tab[PDColor_Selector] = pdi_special_color_hex(js["PDColor_Selector"].get<std::string>());
this->clr_tab[PDColor_SelectorFade] = pdi_special_color_hex(js["PDColor_SelectorFade"].get<std::string>());
this->clr_tab[PDColor_List0] = pdi_special_color_hex(js["PDColor_List0"].get<std::string>());
this->clr_tab[PDColor_List1] = pdi_special_color_hex(js["PDColor_List1"].get<std::string>());
this->clr_tab[PDColor_MessageBackground] = pdi_special_color_hex(js["PDColor_MessageBackground"].get<std::string>());
this->clr_tab[PDColor_Button] = pdi_special_color_hex(js["PDColor_Button"].get<std::string>());
this->clr_tab[PDColor_ButtonHovered] = pdi_special_color_hex(js["PDColor_ButtonHovered"].get<std::string>());
this->clr_tab[PDColor_ButtonDisabled] = pdi_special_color_hex(js["PDColor_ButtonDisabled"].get<std::string>());
this->clr_tab[PDColor_ButtonActive] = pdi_special_color_hex(js["PDColor_ButtonActive"].get<std::string>());
this->clr_tab[PDColor_Checkmark] = pdi_special_color_hex(js["PDColor_Checkmark"].get<std::string>());
this->clr_tab[PDColor_FrameBg] = pdi_special_color_hex(js["PDColor_FrameBg"].get<std::string>());
this->clr_tab[PDColor_FrameBgHovered] = pdi_special_color_hex(js["PDColor_FrameBgHovered"].get<std::string>());
this->clr_tab[PDColor_Progressbar] = pdi_special_color_hex(js["PDColor_Progressbar"].get<std::string>());
// clang-format on
file.close();
}
void Palladium::Theme::Default() {
this->clr_tab.clear();
this->clr_tab.resize(PDColor_Len);
for (auto& it : pdi_default_theme) {
this->clr_tab[it.first] = it.second;
}
}
void Palladium::Theme::CopyOther(Theme::Ref theme) {
this->clr_tab.clear();
this->clr_tab.resize(PDColor_Len);
for (int i = 0; i < (int)PDColor_Len; i++) {
this->clr_tab[i] = theme->Get(i);
}
}
unsigned int Palladium::Theme::Get(PDColor clr) {
if (clr < 0 || clr >= PDColor_Len) return 0;
return this->clr_tab[clr];
}
void Palladium::Theme::Set(PDColor clr, unsigned int v) {
if (clr < 0 || clr >= PDColor_Len) return;
this->changes.push_back(change(clr, this->clr_tab[clr], v));
this->clr_tab[clr] = v;
}
void Palladium::Theme::Swap(PDColor a, PDColor b) {
if (a < 0 || a >= PDColor_Len || b < 0 || b >= PDColor_Len) return;
auto c = this->clr_tab[a];
this->clr_tab[a] = this->clr_tab[b];
this->clr_tab[b] = c;
this->changes.push_back(change(a, b, c, this->clr_tab[a]));
}
void Palladium::Theme::TextBy(PDColor bg) {
if (!Color::RGBA(bg).is_light()) Swap(PDColor_Text, PDColor_Text2);
}
PDColor Palladium::Theme::AutoText(PDColor bg) {
return Color::RGBA(bg).is_light() ? PDColor_Text : PDColor_Text2;
}
bool Palladium::Theme::Undo() {
if (!this->changes.size()) return false;
auto ch = this->changes[this->changes.size() - 1];
this->changes.pop_back();
if (ch.clr2) {
this->clr_tab[ch.clr2] = ch.to;
this->clr_tab[ch.clr] = ch.from;
} else {
this->clr_tab[ch.clr] = ch.from;
}
return true;
}
void Palladium::Theme::UndoAll() {
while (Undo()) {
// Just Run Undo Until all is undone
}
}
void Palladium::Theme::Save(const std::string& path) {
if (std::filesystem::path(path).filename().string() == "Palladium.theme") {
if (!pdi_amdt) {
Palladium::PushMessage("Theme", "Default Theme cannot\nbe overwritten!");
return;
}
}
std::ofstream file(path);
if (!file.is_open()) {
Palladium::PushMessage("Theme", "Unable to\ncreate file!");
return;
}
nlohmann::json js;
// clang-format off
js["version"] = THEMEVER;
js["PDColor_Text"] = Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Text]);
js["PDColor_Text2"] = Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Text2]);
js["PDColor_TextDisabled"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_TextDisabled]); js["PDColor_Background"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Background]); js["PDColor_Header"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Header]); js["PDColor_Selector"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Selector]); js["PDColor_SelectorFade"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_SelectorFade]); js["PDColor_List0"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_List0]); js["PDColor_List1"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_List1]); js["PDColor_MessageBackground"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_MessageBackground]); js["PDColor_Button"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Button]); js["PDColor_ButtonHovered"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_ButtonHovered]);
js["PDColor_ButtonDisabled"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_ButtonDisabled]);
js["PDColor_ButtonActive"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_ButtonActive]); js["PDColor_Checkmark"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Checkmark]); js["PDColor_FrameBg"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_FrameBg]); js["PDColor_FrameBgHovered"] =
Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_FrameBgHovered]); js["PDColor_Progressbar"]
= Palladium::Color::RGBA2Hex(this->clr_tab[PDColor_Progressbar]);
// clang-format on
file << js.dump(4);
file.close();
}
Palladium::Theme::Ref Palladium::ThemeActive() { return pdi_active_theme; }
void Palladium::ThemeSet(Palladium::Theme::Ref theme) {
pdi_active_theme = theme;
}
unsigned int Palladium::Color::Hex(const std::string& color, uint8_t a) {
if (color.length() < 7 ||
std::find_if(color.begin() + 1, color.end(),
[](char c) { return !std::isxdigit(c); }) != color.end()) {
return Palladium::Color::Hex("#000000", 0);
}
int r = HEX_TO_DEC.at(color[1]) * 16 + HEX_TO_DEC.at(color[2]);
int g = HEX_TO_DEC.at(color[3]) * 16 + HEX_TO_DEC.at(color[4]);
int b = HEX_TO_DEC.at(color[5]) * 16 + HEX_TO_DEC.at(color[6]);
return RGBA8(r, g, b, a);
}
std::string Palladium::Color::RGB2Hex(int r, int g, int b) {
std::stringstream ss;
ss << "#";
ss << std::hex << (r << 16 | g << 8 | b);
return ss.str();
}

58
source/Error.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <3ds.h>
#include <pd/Error.hpp>
#include <pd/UI7.hpp>
#include <pd/internal_db.hpp>
#include <pd/palladium.hpp>
void pdi_save_report(const std::string& msg) {
auto ts = Palladium::GetTimeStr();
std::ofstream f("sdmc:/Palladium/Reports/report_" + ts + ".txt");
f << "Palladium Error [" << pdi_app_name << ", " << ts << "]" << std::endl;
f << "Error Message: " << std::endl;
f << msg << std::endl;
f << "SysInfo: " << std::endl;
f << "- Citra -> " << (pdi_is_citra ? "true" : "false") << std::endl;
f.close();
}
namespace Palladium {
void Error(const std::string& msg) {
pdi_save_report(msg);
if (pdi_graphics_on) {
while (aptMainLoop()) {
hidScanInput();
if (hidKeysDown() & KEY_START) break;
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
Palladium::ClearTextBufs();
C2D_TargetClear(pd_top, 0x00000000);
C2D_TargetClear(pd_bottom, 0x00000000);
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium - Error Manager", NVec2(),
UI7MenuFlags_TitleMid)) {
UI7::Label(msg, PDTextFlags_Wrap);
UI7::Label("Press Start to Exit!");
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
UI7::Update();
Palladium::R2::Process();
C3D_FrameEnd(0);
}
exit(0);
} else {
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
printf("Palladium - ERROR MANAGER\n\n%s\n", msg.c_str());
printf("Report Saved in\nsdmc:/Palladium/Reports\n");
printf("Press Start to Exit\n");
while (aptMainLoop()) {
hidScanInput();
if (hidKeysDown() & KEY_START) break;
gfxSwapBuffers();
}
gfxExit();
exit(0);
}
}
} // namespace Palladium

70
source/FileSystem.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <3ds.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include <pd/FileSystem.hpp>
// Debugging
#include <algorithm>
#include <filesystem>
#include <pd/stringtool.hpp>
bool ___dir__predicate__(const Palladium::FileSystem::Entry &lhs,
const Palladium::FileSystem::Entry &rhs) {
if (!lhs.dir && rhs.dir) return false;
if (lhs.dir && !rhs.dir) return true;
std::string a = lhs.name;
std::string b = rhs.name;
std::transform(a.begin(), a.end(), a.begin(),
[](int i) { return std::tolower(i); });
std::transform(b.begin(), b.end(), b.begin(),
[](int i) { return std::tolower(i); });
return a.compare(b) < 0;
}
std::string Palladium::FileSystem::GetParentPath(std::string path,
std::string mount_point) {
std::string tcl = path;
if (path.substr(path.length() - 1, 1) != "/") {
tcl += "/";
}
std::string res =
std::filesystem::path(tcl).parent_path().parent_path().string();
if (res.length() > mount_point.length()) {
return res;
}
return mount_point;
}
std::vector<Palladium::FileSystem::Entry> Palladium::FileSystem::GetDirContent(
std::string path) {
std::vector<Palladium::FileSystem::Entry> res;
for (const auto &entry :
std::filesystem::directory_iterator(std::filesystem::path(path))) {
res.push_back({entry.path().string(), GetFileName(entry.path().string()),
entry.is_directory()});
}
return res;
}
std::vector<Palladium::FileSystem::Entry>
Palladium::FileSystem::GetDirContentsExt(
std::string &path, const std::vector<std::string> &extensions) {
std::vector<Palladium::FileSystem::Entry> res;
for (auto const &it :
std::filesystem::directory_iterator(std::filesystem::path(path))) {
Palladium::FileSystem::Entry temp;
std::string fn = it.path().string();
temp.name = GetFileName(fn);
temp.path = it.path().string().c_str();
temp.dir = it.is_directory();
if (NameIsEndingWith(GetFileName(it.path().string()), extensions) ||
it.is_directory()) {
res.push_back(temp);
}
}
std::sort(res.begin(), res.end(), ___dir__predicate__);
return res;
}

3
source/FunctionTrace.cpp Normal file
View File

@ -0,0 +1,3 @@
#include <pd/FunctionTrace.hpp>
std::map<std::string, Palladium::Ftrace::FTRes> Palladium::Ftrace::pd_traces;

47
source/Hardware.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <pd/Hardware.hpp>
#include <pd/internal_db.hpp>
// Os Specific includes
#include <3ds.h>
void Palladium::Hardware::Initialisize() {
mcuHwcInit();
atexit(mcuHwcExit);
ptmuInit();
atexit(ptmuExit);
}
bool Palladium::Hardware::IsHeadphones() {
if (pdi_is_ndsp) {
bool inserted;
DSP_GetHeadphoneStatus(&inserted);
return inserted;
} else
return false;
}
bool Palladium::Hardware::IsCharging() {
uint8_t var;
PTMU_GetBatteryChargeState(&var);
// Some Security Stuff
if (var < 0x00 && var > 0x01) {
return false;
}
return (var == 0x01 ? true : false);
}
int Palladium::Hardware::GetBatteryPercentage() {
uint8_t percentLevel = 0;
MCUHWC_GetBatteryLevel(&percentLevel);
return percentLevel;
}
float Palladium::Hardware::Get3dSliderLevel() { return osGet3DSliderState(); }
float Palladium::Hardware::GetSoundSliderLevel() {
uint8_t percentLevel;
MCUHWC_GetSoundSliderLevel(&percentLevel);
return (float)percentLevel / 100;
}
int Palladium::Hardware::GetWifiLevel() { return osGetWifiStrength(); }

120
source/Hid.cpp Normal file
View File

@ -0,0 +1,120 @@
#include <map>
#include <pd/Hid.hpp>
namespace Palladium {
class HidApi {
public:
HidApi() {}
~HidApi() {}
void setKdown(uint32_t &in) { actions[Hid::Down] = &in; }
void setKheld(uint32_t &in) { actions[Hid::Held] = &in; }
void setKup(uint32_t &in) { actions[Hid::Up] = &in; }
void setKrepeat(uint32_t &in) { actions[Hid::DownRepeat] = &in; }
void setTouchCoords(NVec2 &touch_coords) { touch_pos = &touch_coords; }
void setJS1Movement(NVec2 &mvmt) { js1_mv = &mvmt; }
void setJS2Movement(NVec2 &mvmt) { js2_mv = &mvmt; }
void bindKey(const std::string &event, uint32_t key) {
key_bindings[event] = key; // Overrides if existing
}
void lock(bool lock) { locked = lock; }
void clear() {
// Clears Functionality for 1 Frame
last_touch_pos = NVec2();
touch_pos[0] = NVec2();
dtp = NVec2();
backups[Hid::Down] = 0;
backups[Hid::Held] = 0;
backups[Hid::Up] = 0;
backups[Hid::DownRepeat] = 0;
}
bool isEvent(const std::string &event, Hid::Actions action) {
if (locked) return false;
if (key_bindings.find(event) == key_bindings.end())
return false; // Unknown Event
if (backups.find(action) == backups.end())
return false; // What? NOT Alowed acrion
if (backups[action] & key_bindings[event])
return true; // Action contains key as flag
return false; // Nothing to do
}
NVec2 getTouchPos() { return touch_pos[0]; }
NVec2 getLastTouchPos() { return last_touch_pos; }
NVec2 getTouchDownPos() { return dtp; }
void update() {
last_touch_pos = touch_pos[0];
if (isEvent("touch", Hid::Down)) {
dtp = touch_pos[0];
}
if (isEvent("touch", Hid::Up)) {
dtp = NVec2();
}
for (const auto &it : actions) {
backups[it.first] = it.second[0];
}
if (locked) {
actions[Hid::Down][0] = 0;
actions[Hid::Held][0] = 0;
actions[Hid::Up][0] = 0;
actions[Hid::DownRepeat][0] = 0;
}
}
private:
std::map<Hid::Actions, uint32_t *> actions;
std::map<Hid::Actions, uint32_t> backups;
NVec2 *touch_pos = nullptr;
NVec2 *js1_mv = nullptr;
NVec2 *js2_mv = nullptr;
NVec2 last_touch_pos;
NVec2 dtp;
std::map<std::string, uint32_t> key_bindings;
bool locked = false;
};
static HidApi hid_handler;
namespace Hid {
// Register Functions
// Register Current state values
void RegKeyDown(uint32_t &key_down) { hid_handler.setKdown(key_down); }
void RegKeyHeld(uint32_t &key_held) { hid_handler.setKheld(key_held); }
void RegKeyUp(uint32_t &key_up) { hid_handler.setKup(key_up); }
void RegKeyRepeat(uint32_t &repeat) { hid_handler.setKrepeat(repeat); }
void RegTouchCoords(NVec2 &touch_pos) {
hid_handler.setTouchCoords(touch_pos);
}
void RegAnalog1Movement(NVec2 &movement) {
hid_handler.setJS1Movement(movement);
}
void RegAnalog2Movement(NVec2 &movement) {
hid_handler.setJS2Movement(movement);
}
// Register Keys
void RegKeyEvent(const std::string &event, uint32_t key) {
hid_handler.bindKey(event, key);
}
bool IsEvent(const std::string &event, Actions action) {
return hid_handler.isEvent(event, action);
}
NVec2 GetTouchPosition() { return hid_handler.getTouchPos(); }
NVec2 GetLastTouchPosition() { return hid_handler.getLastTouchPos(); }
NVec2 GetTouchDownPosition() { return hid_handler.getTouchDownPos(); }
void Update() { hid_handler.update(); }
void Lock() { hid_handler.lock(true); }
void Unlock() { hid_handler.lock(false); }
void Clear() { hid_handler.clear(); }
} // namespace Hid
} // namespace Palladium

181
source/Image.cpp Normal file
View File

@ -0,0 +1,181 @@
#include <pd/external/stb_image.h>
#include <pd/Image.hpp>
#include <pd/internal_db.hpp>
#include <vector>
static u32 __pdi_gp2o__(u32 v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return (v >= 64 ? v : 64);
}
static void __pdi_r24r32(std::vector<uint8_t> &out,
const std::vector<uint8_t> &in, const int &w,
const int &h) {
// Converts RGB24 to RGBA32
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int src = (y * w + x) * 3;
int dst = (y * w + x) * 4;
out[dst + 0] = in[src + 0];
out[dst + 1] = in[src + 1];
out[dst + 2] = in[src + 2];
out[dst + 3] = 255;
}
}
}
static void __pdi_maketex__(C3D_Tex *tex, Tex3DS_SubTexture *sub,
std::vector<unsigned char> &buf, int w, int h) {
if (!tex || !sub) {
_pdi_logger()->Write("Invalid Inpit (objects have no adress!)");
return;
}
// RGBA -> Abgr
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int pos = (x + y * w) * 4;
auto r = buf[pos + 0];
auto g = buf[pos + 1];
auto b = buf[pos + 2];
auto a = buf[pos + 3];
buf[pos + 0] = a;
buf[pos + 1] = b;
buf[pos + 2] = g;
buf[pos + 3] = r;
}
}
// Pow2
int wp2 = __pdi_gp2o__((unsigned int)w);
int hp2 = __pdi_gp2o__((unsigned int)h);
sub->width = (u16)w;
sub->height = (u16)h;
sub->left = 0.0f;
sub->top = 1.0f;
sub->right = ((float)w / (float)wp2);
sub->bottom = 1.0 - ((float)h / (float)hp2);
// Texture Setup
C3D_TexInit(tex, (u16)wp2, (u16)hp2, GPU_RGBA8);
C3D_TexSetFilter(tex, GPU_NEAREST, GPU_NEAREST);
memset(tex->data, 0, tex->size);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int dst_pos = ((((y >> 3) * (wp2 >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) |
((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) *
4;
int src_pos = (y * w + x) * 4;
memcpy(&((unsigned char *)tex->data)[dst_pos], &buf[src_pos], 4);
}
}
C3D_TexFlush(tex);
tex->border = 0x00000000;
C3D_TexSetWrap(tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
}
namespace Palladium {
void Image::Load(const std::string &path) {
// Make sure to cleanup
Delete();
ext = false;
// Setup Func and Load Data
int w, h, c = 0;
unsigned char *image = stbi_load(path.c_str(), &w, &h, &c, 4);
if (image == nullptr) {
//_pdi_logger()->Write("Failed to Load Image: " + path);
return;
}
// Size/Fmt Check
if (w > 1024 || h > 1024) {
// Reason: Image to Large
//_pdi_logger()->Write("Image too Large!");
stbi_image_free(image);
return;
}
std::vector<unsigned char> wimg;
if (c == 3) {
//_pdi_logger()->Write("Convert Image to RGBA");
stbi_image_free(image);
image = stbi_load(path.c_str(), &w, &h, &c, 3);
wimg.resize(w * h * 4);
__pdi_r24r32(wimg, std::vector<unsigned char>(image, image + (w * h * 3)),
w, h);
} else {
wimg.assign(&image[0], &image[(w * h * 4) - 1]);
stbi_image_free(image);
}
// Create C2D_Image
C3D_Tex *tex = new C3D_Tex;
Tex3DS_SubTexture *subtex = new Tex3DS_SubTexture;
__pdi_maketex__(tex, subtex, wimg, w, h);
_pdi_logger()->Write(Palladium::FormatString("Created Texture (%d, %d)",
tex->width, tex->height));
img = {tex, subtex};
}
void Image::From_NIMG(const nimg &image) {
// Make sure to cleanup
Delete();
ext = false;
if (image.width > 1024 || image.height > 1024) return;
C3D_Tex *tex = new C3D_Tex;
Tex3DS_SubTexture *subtex = new Tex3DS_SubTexture;
std::vector<unsigned char> mdpb = image.pixel_buffer;
__pdi_maketex__(tex, subtex, mdpb, image.width, image.height);
img = {tex, subtex};
}
C2D_Image Image::Get() {
if (!Loadet()) {
_pdi_logger()->Write("Image not Loadet!");
}
return img;
}
C2D_Image &Image::GetRef() {
if (!Loadet()) {
_pdi_logger()->Write("Image not Loadet!");
}
return img;
}
void Image::Set(const C2D_Image &i) {
Delete();
ext = true;
img = i;
}
NVec2 Image::GetSize() {
if (!img.subtex) return NVec2(0, 0);
return NVec2(img.subtex->width, img.subtex->height);
}
void Image::Delete() {
if (ext) return;
if (img.subtex != nullptr) {
delete img.subtex;
img.subtex = nullptr;
}
if (img.tex != nullptr) {
C3D_TexDelete(img.tex);
delete img.tex;
img.tex = nullptr;
}
}
bool Image::Loadet() { return (img.subtex != nullptr && img.tex != nullptr); }
} // namespace Palladium

149
source/Installer.cpp Normal file
View File

@ -0,0 +1,149 @@
#include <3ds.h>
#include <pd/Installer.hpp>
#include <pd/internal_db.hpp>
namespace Palladium {
Result DeletePrevious(u64 id, FS_MediaType mt) {
if (!pdi_is_am_init) return -1;
u32 num_titles = 0;
Result ret = AM_GetTitleCount(mt, &num_titles);
if (R_FAILED(ret)) {
return ret;
}
u32 read_titles = 0;
auto ids = new u64[num_titles];
ret = AM_GetTitleList(&read_titles, mt, num_titles, ids);
if (R_FAILED(ret)) {
delete[] ids;
return ret;
}
for (u32 i = 0; i < read_titles; i++) {
if (ids[i] == id) {
ret = AM_DeleteAppTitle(mt, id);
break;
}
}
delete[] ids;
if (R_FAILED(ret)) {
return ret;
}
return 0;
}
FS_MediaType GetTitleDest(u64 id) {
u16 platform = (u16)((id >> 48) & 0xFFFF);
u16 category = (u16)((id >> 32) & 0xFFFF);
u8 variation = (u8)(id & 0xFF);
/* DSiWare 3DS DSiWare, System, DLP
* Application System Title */
return platform == 0x0003 || (platform == 0x0004 &&
((category & 0x8011) != 0 ||
(category == 0x0000 && variation == 0x02)))
? MEDIATYPE_NAND
: MEDIATYPE_SD;
}
InstallerInfo pdi_installer_info;
InstallerInfo InstallGetInfo() { return pdi_installer_info; }
void InstallSetBuffersSize(unsigned int bytes) {
if (pdi_installer_info.active) return;
if (bytes == 0) return;
if (bytes >= 0x200000) bytes = 0x200000;
pdi_installer_info.mem_size = bytes;
}
Result InstallCia(const std::string& path, bool self) {
if (!pdi_is_am_init) return -1;
if (pdi_installer_info.active) return -1;
u32 bytes_read = 0;
u32 bytes_written = 0;
pdi_installer_info.current = 0;
// Set 1 to avoid div0 error
pdi_installer_info.total = 1;
u64 size = 0;
Handle cia, file;
AM_TitleEntry info;
Result ret = 0;
FS_MediaType media = MEDIATYPE_SD;
std::string pth;
if (path[0] == '/') {
pth = path;
} else if (path.substr(0, 5) == "sdmc:") {
pth = path.substr(5);
} else {
return -1;
}
ret = FSUSER_OpenFileDirectly(&file, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""),
fsMakePath(PATH_ASCII, pth.c_str()),
FS_OPEN_READ, 0);
if (R_FAILED(ret)) {
return ret;
}
ret = AM_GetCiaFileInfo(media, &info, file);
if (R_FAILED(ret)) {
return ret;
}
media = GetTitleDest(info.titleID);
if (!self) {
ret = DeletePrevious(info.titleID, media);
if (R_FAILED(ret)) return ret;
}
ret = FSFILE_GetSize(file, &size);
if (R_FAILED(ret)) {
return ret;
}
ret = AM_StartCiaInstall(media, &cia);
if (R_FAILED(ret)) {
return ret;
}
std::vector<unsigned char> buf(pdi_installer_info.mem_size);
pdi_installer_info.total = size;
do {
FSFILE_Read(file, &bytes_read, pdi_installer_info.current, &buf[0],
buf.size());
FSFILE_Write(cia, &bytes_written, pdi_installer_info.current, &buf[0],
buf.size(), FS_WRITE_FLUSH);
pdi_installer_info.current += bytes_read;
} while (pdi_installer_info.current < pdi_installer_info.total);
ret = AM_FinishCiaInstall(cia);
if (R_FAILED(ret)) {
return ret;
}
ret = FSFILE_Close(file);
if (R_FAILED(ret)) {
return ret;
}
if (self) {
aptSetChainloaderToSelf();
}
return 0;
}
} // namespace Palladium

326
source/LI7.cpp Normal file
View File

@ -0,0 +1,326 @@
#include <pd/LI7.hpp>
#include <pd/Color.hpp>
#include <pd/palladium.hpp>
#include <pd/external/stb_truetype.h>
#include <algorithm>
#include <li7_shbin.h>
#define DISPLAY_TRANSFER_FLAGS \
(GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | \
GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | \
GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | \
GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO))
#ifndef __builtin_swap_32
#define __builtin_swap_32(x) \
((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | \
(((x) >> 24) & 0xff))
#endif
#define bs32(x) __builtin_swap_32(x)
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
namespace Palladium {
const float LI7::m_dffs = 8.f;
int LI7::m_uLoc_proj;
float LI7::m_scale = 1.0f;
int LI7::m_width, LI7::m_height;
int LI7::m_d_vertices = 0;
int LI7::m_d_drawcalls = 0;
int LI7::m_d_commands = 0;
const int LI7::m_char_height = 8; // Constant
float LI7::m_rot = 0.f;
// UI Stuff
std::vector<LI7::Cmd> LI7::m_top_draw_cmds;
std::vector<LI7::Cmd> LI7::m_bot_draw_cmds;
Texture::Ref LI7::m_current_texture;
std::vector<LI7::Vtx, LinearAllocator<LI7::Vtx>> LI7::m_vtx_list[2];
// LI7::Font *LI7::m_font;
std::vector<char> LI7::m_text_buffer;
// Matrix
C3D_Mtx LI7::m_icon_model_matrix;
// Ctx Stuff
bool LI7::m_bottom_active = false;
// Shader
DVLB_s *LI7::li7_dvlb;
shaderProgram_s LI7::li7_shader;
C3D_AttrInfo LI7::li7_attr;
void LIFont::LoadTFF(const std::string path, int px_size) {
// Supported Sizings (Tested [12, 16, 21, 24, 32, 48, 56, 63])
this->pixel_height = px_size;
if(!Palladium::FS::FileExist(path)) return;
int type = px_size*16;
// Load TTF
stbtt_fontinfo inf;
std::ifstream loader(path, std::ios::binary);
if (!loader.is_open()) return;
loader.seekg(0, std::ios::end);
size_t len = loader.tellg();
loader.seekg(0, std::ios::beg);
unsigned char* buffer = new unsigned char[len];
loader.read(reinterpret_cast<char*>(buffer), len);
loader.close();
stbtt_InitFont(&inf, buffer, 0);
std::vector<unsigned char> fmap(type*type*4);
float scale = stbtt_ScaleForPixelHeight(&inf, pixel_height);
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&inf, &ascent, &descent, &lineGap);
int baseline = static_cast<int>(ascent * scale);
for (int c = 0; c < 255; c++) {
if (stbtt_IsGlyphEmpty(&inf, c)) continue;
int width, height, xOffset, yOffset;
unsigned char* bitmap = stbtt_GetCodepointBitmap(
&inf, scale, scale, c, &width, &height, &xOffset, &yOffset);
int x0, y0, x1, y1;
stbtt_GetCodepointBitmapBox(&inf, c, scale, scale, &x0, &y0, &x1, &y1);
this->fontw[c] = x0 + x1;
int i = c % 16;
int j = c / 16;
int xoff = i * pixel_height;
int yoff = j * pixel_height + baseline + yOffset;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int map_pos = ((yoff + y) * type + (xoff + x)) * 4;
fmap[map_pos + 0] = 255;
fmap[map_pos + 1] = 255;
fmap[map_pos + 2] = 255;
fmap[map_pos + 3] = bitmap[x + y * width];
}
}
free(bitmap);
}
auto ftex = Texture::New();
ftex->LoadPixels(fmap, type, type);
this->tex.push_back(ftex);
}
void LIFont::LoadBitmapFont(const std::string& path) {
}
void LIFont::LoadSystemFont() {
}
int LIFont::GetPixelHeight() {
}
LIFont::CPI LIFont::GetCodepoint() {
}
void LI7::Init() {
m_text_buffer.resize(1024);
m_vtx_list[0].reserve(2 * 4096);
m_vtx_list[1].reserve(2 * 4096);
li7_dvlb = DVLB_ParseFile((u32*)li7_shbin, li7_shbin_size);
shaderProgramInit(&li7_shader);
shaderProgramSetVsh(&li7_shader, &li7_dvlb->DVLE[0]);
m_uLoc_proj =
shaderInstanceGetUniformLocation(li7_shader.vertexShader, "projection");
AttrInfo_Init(&li7_attr);
AttrInfo_AddLoader(&li7_attr, 0, GPU_FLOAT, 3);
AttrInfo_AddLoader(&li7_attr, 1, GPU_FLOAT, 2);
AttrInfo_AddLoader(&li7_attr, 2, GPU_UNSIGNED_BYTE, 4);
}
void LI7::Exit() {
shaderProgramFree(&li7_shader);
DVLB_Free(li7_dvlb);
}
void LI7::OnScreen(bool bottom) {
m_width = bottom ? 320 : 400;
m_height = 240;
m_bottom_active = bottom;
}
bool LI7::CompareCommands(const LI7::Cmd &a,
const LI7::Cmd &b) {
if (a.layer == b.layer)
return a.tex < b.tex;
return b.layer < a.layer;
}
void LI7::RenderFrame(bool bottom) {
m_rot += M_PI / 60.f;
C3D_Mtx proj;
Mtx_OrthoTilt(&proj, 0.f, (bottom ? 320 : 400), m_height, 0.f, 1.f, -1.f,
false);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, m_uLoc_proj, &proj);
C3D_DepthTest(false, GPU_GREATER, GPU_WRITE_ALL);
C3D_TexEnv *env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, (GPU_TEVSRC)0);
C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
auto& active_list = m_vtx_list[bottom];
int total_vertices = 0;
m_d_drawcalls = 0;
auto &draw_cmds = bottom ? m_bot_draw_cmds : m_top_draw_cmds;
std::sort(draw_cmds.begin(), draw_cmds.end(), CompareCommands);
m_d_commands = draw_cmds.size();
size_t vtx = 0;
while (draw_cmds.size() > 0) {
C3D_Tex *texture = draw_cmds[draw_cmds.size() - 1].tex->Get();
size_t start_vtx = vtx;
while (draw_cmds.size() > 0 &&
draw_cmds[draw_cmds.size() - 1].tex->Get() == texture) {
auto c = draw_cmds[draw_cmds.size() - 1];
if(c.cmd_type == 1) {
active_list[vtx++] = Vtx{{c.ppos.x, c.ppos.y, 0}, {c.uv.x, c.uv.y}, c.clr};
active_list[vtx++] = Vtx{{c.pszs.x, c.pszs.y, 0}, {c.uv.x, c.uv.w}, c.clr};
active_list[vtx++] = Vtx{{c.apos.x, c.apos.y, 0}, {c.uv.z, c.uv.w}, c.clr};
} else if (c.cmd_type == 2) {
// TRI1
active_list[vtx++] = Vtx{{c.ppos.x+c.pszs.x, c.ppos.y+c.pszs.y, 0}, {c.uv.z, c.uv.y}, c.clr};
active_list[vtx++] = Vtx{{c.ppos.x+c.pszs.x, c.ppos.y, 0}, {c.uv.z, c.uv.w}, c.clr};
active_list[vtx++] = Vtx{{c.ppos.x, c.ppos.y, 0}, {c.uv.x, c.uv.w}, c.clr};
// TRI2
active_list[vtx++] = Vtx{{c.ppos.x, c.ppos.y, 0}, {c.uv.x, c.uv.w}, c.clr};
active_list[vtx++] = Vtx{{c.ppos.x, c.ppos.y+c.pszs.y, 0}, {c.uv.x, c.uv.y}, c.clr};
active_list[vtx++] = Vtx{{c.ppos.x+c.pszs.x, c.ppos.y+c.pszs.y, 0}, {c.uv.z, c.uv.y}, c.clr};
}
draw_cmds.pop_back();
}
C3D_TexBind(0, texture);
C3D_BufInfo *bufinfo = C3D_GetBufInfo();
BufInfo_Init(bufinfo);
BufInfo_Add(bufinfo, active_list.data() + start_vtx, sizeof(Vtx), 3,
0x210);
C3D_DrawArrays(GPU_TRIANGLES, 0, vtx - start_vtx);
m_d_drawcalls++;
total_vertices += vtx - start_vtx;
}
C3D_DepthTest(true, GPU_GREATER, GPU_WRITE_ALL);
m_d_vertices = total_vertices;
m_current_texture = nullptr;
m_scale = 1.f;
}
void LI7::Render(C3D_RenderTarget* top, C3D_RenderTarget* bot) {
C3D_BindProgram(&li7_shader);
C3D_SetAttrInfo(&li7_attr);
C3D_FrameDrawOn(top);
RenderFrame(false);
int d_tmp_cmds1 = m_d_commands;
int d_tmp_dcls1 = m_d_drawcalls;
int d_tmp_vtxs1 = m_d_vertices;
C3D_FrameDrawOn(bot);
RenderFrame(true);
int d_tmp_cmds2 = m_d_commands;
int d_tmp_dcls2 = m_d_drawcalls;
int d_tmp_vtxs2 = m_d_vertices;
m_d_commands = d_tmp_cmds1 + d_tmp_cmds2;
m_d_drawcalls = d_tmp_dcls1 + d_tmp_dcls2;
m_d_vertices = d_tmp_vtxs1 + d_tmp_vtxs2;
}
void LI7::ColorRect(NVec2 pos, NVec2 szs, NVec4 uvs, unsigned int clr) {
Cmd c;
c.ppos = pos;
c.pszs = szs;
c.uv = uvs;
c.clr = bs32(clr);
c.tex = m_current_texture;
c.cmd_type = 2;
if(m_bottom_active) m_bot_draw_cmds.push_back(c);
else m_top_draw_cmds.push_back(c);
}
void LI7::ColorRect(NVec2 pos, NVec2 szs, unsigned int clr) {
ColorRect(pos, szs, NVec4(0, 0, 1, 1), clr);
}
void LI7::Rect(NVec2 pos, NVec2 szs, NVec4 uvs) {
ColorRect(pos, szs, uvs, 0xffffffff);
}
void LI7::Rect(NVec2 pos, NVec2 szs) {
ColorRect(pos, szs, NVec4(0, 0, 1, 1), 0xffffffff);
}
void LI7::TexCutRect(NVec2 pos, NVec2 szs, NVec2 cb, NVec2 ce) {
float u0 = (float)(cb.x / (float)m_current_texture->Get()->width);
float v1 = (float)(((float)m_current_texture->Get()->height - cb.y) /
(float)m_current_texture->Get()->height);
float u1 = (float)(ce.x / (float)m_current_texture->Get()->width);
float v0 = (float)(((float)m_current_texture->Get()->height - ce.y) /
(float)m_current_texture->Get()->height);
Rect(pos, szs, NVec4(u0, v0, u1, v1));
}
int LI7::DrawTextVargs(int x, int y, int z, unsigned int color, bool shadow,
int wrap, int* ySize, const char* fmt, va_list arg) {
// FONT
int len = vsnprintf(m_text_buffer.data(), m_text_buffer.size(), fmt, arg);
m_text_buffer[len] = '\0';
int offsetX = 0;
int offsetY = 0;
int maxWidth = 0;
float cpm = m_dffs /1;// m_font->char_size;
for (const auto &it : m_text_buffer) {
if (it == '\0') break;
bool implicitBreak = offsetX + 0/*m_font->fontWidth[(int)it]*/ >= wrap;
if (it == '\n' || implicitBreak) {
offsetY += /*m_font->char_size*/0 * cpm * m_scale;
maxWidth = std::max(maxWidth, offsetX);
offsetX = 0;
if (implicitBreak) continue;
} else if (it == '\t') {
offsetX = ((offsetX / m_dffs) / 4 + 1) * 4 * m_dffs;
} else {
if (it != ' ') {
int texX = (it % 16) * /*m_font->char_size*/0;
int texY = (/*m_font->texture.tex.height*/0 - /*m_font->char_size*/0) -
(it / 16) * /*m_font->char_size*/0;
ColorRect(NVec2(x + offsetX, y + offsetY), NVec2(m_dffs, m_dffs),
NVec4(static_cast<float>(texX) / /*m_font->texture.tex.width*/0,
static_cast<float>(texY) / /*m_font->texture.tex.height*/0,
(static_cast<float>(texX) + /*m_font->char_size*/0) /
/*m_font->texture.tex.width*/0,
(static_cast<float>(texY) + /*m_font->char_size*/0) /
/*m_font->texture.tex.height*/0),
color);
if (shadow)
ColorRect(NVec2(x + offsetX + 1, y + offsetY + 1), NVec2(m_dffs, m_dffs),
NVec4(static_cast<float>(texX) / /*m_font->texture.tex.width*/0,
static_cast<float>(texY) / /*m_font->texture.tex.height*/0,
(static_cast<float>(texX) + /*m_font->char_size*/0) /
/*m_font->texture.tex.width*/0,
(static_cast<float>(texY) + /*m_font->char_size*/0) /
/*m_font->texture.tex.height*/0),
RGBA8(10, 10, 10, 255));
}
offsetX += /*m_font->fontWidth[(int)it]*/0 * cpm * m_scale +
((/*m_font->char_size*/0 * 0.2) * cpm * m_scale);
}
}
maxWidth = std::max(maxWidth, offsetX);
if (ySize != nullptr) *ySize = offsetY + /*m_font->char_size*/0;
return maxWidth;
}
}

44
source/Logger.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <filesystem>
#include <fstream>
#include <memory>
#include <pd/Logger.hpp>
#include <pd/Time.hpp>
#include <pd/palladium.hpp>
namespace Palladium {
LoggerBase::~LoggerBase() {
if (_log.is_open()) _log.close();
}
void LoggerBase::Init(const std::string& name, bool fileless) {
if (!fileless) {
std::string path_base = Palladium::GetAppDirectory() + "/logs/";
if (!std::filesystem::is_directory(path_base)) {
std::filesystem::create_directories(path_base);
}
auto ts = Palladium::GetTimeStr();
std::string fn = name + ts + ".txt";
this->filename = name;
this->log_path = path_base + name;
if (std::filesystem::exists(this->log_path)) {
// Do nothing
} else {
_log.open(this->log_path, std::ios::out);
}
}
this->Write("Palladium Log\n\n");
}
void LoggerBase::Write(const std::string& debug_text, int lvl) {
std::string msg = "[" + Palladium::GetTimeStr() + "]: " + debug_text;
if (this->_log.is_open() && lvl <= writelvl) {
this->_log << msg << std::endl;
}
/*while (msg.find_first_of('\n') != 0) {
lines.push_back(msg.substr(0, msg.find_first_of('\n')));
msg = msg.substr(msg.find_first_of('\n'));
}*/
lines.push_back(msg);
}
const std::vector<std::string>& LoggerBase::Lines() { return this->lines; }
} // namespace Palladium

56
source/Memory.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <cstdlib>
#include <map>
#include <pd/Memory.hpp>
static Palladium::Memory::memory_metrics metrics;
bool pdi_enable_memtrack;
void *operator new(size_t size) {
void *ptr = malloc(size);
if (pdi_enable_memtrack) metrics.t_TotalAllocated += size;
return ptr;
}
void operator delete(void *memory, size_t size) {
if (pdi_enable_memtrack) metrics.t_TotalFreed += size;
free(memory);
}
int allocations = 0;
int total_size = 0;
std::map<void *, size_t> sizes;
void *operator new[](size_t size) {
void *ptr = malloc(size);
if (pdi_enable_memtrack) {
allocations++;
total_size += size;
sizes[ptr] = size;
metrics.t_TotalAllocated += size;
}
return ptr;
}
void operator delete[](void *ptr) {
if (pdi_enable_memtrack) {
allocations--;
total_size -= sizes[ptr];
metrics.t_TotalFreed += sizes[ptr];
sizes.erase(ptr);
}
free(ptr);
}
namespace Palladium {
namespace Memory {
size_t GetTotalAllocated() { return metrics.t_TotalAllocated; }
size_t GetTotalFreed() { return metrics.t_TotalFreed; }
size_t GetCurrent() { return metrics.t_CurrentlyAllocated(); }
} // namespace Memory
} // namespace Palladium

95
source/Message.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <algorithm>
#include <memory>
#include <pd/Color.hpp>
#include <pd/Message.hpp>
#include <pd/palladium.hpp>
#include <vector>
extern bool pdi_debugging;
static std::vector<std::shared_ptr<Palladium::Message>> msg_lst;
static int fade_outs = 200; // Start of fadeout
static int idles = 60; // start of Idle
static int anim_len = 300; // Full Length of Animation
NVec2 MakePos(int frame, int entry) {
float fol = anim_len - fade_outs;
if (frame > fade_outs)
return NVec2(5, 240 - ((entry + 1) * 55) - 5 +
(float)((frame - fade_outs) / fol) * -20);
if (frame > idles) return NVec2(5, 240 - ((entry + 1) * 55) - 5);
return NVec2(-150 + ((float)(frame / (float)idles) * 155),
240 - ((entry + 1) * 55) - 5);
}
namespace Palladium {
float GetDeltaTime(); // Extern from Palladium.cpp
void ProcessMessages() {
float tmp_txt = R2::GetTextSize();
R2::DefaultTextSize();
// Draw in ovl mode
R2::OnScreen(R2Screen_Top);
float fol = anim_len - fade_outs;
std::reverse(msg_lst.begin(), msg_lst.end());
for (size_t i = 0; i < msg_lst.size(); i++) {
NVec2 pos = MakePos(msg_lst[i]->animationframe, i);
if ((pos.y + 150) < 0) {
// Dont Render Out of Screen
// And as thay aren't relevant anymore
// Thay get deleted!
msg_lst.erase(msg_lst.begin() + i);
} else {
int new_alpha = 200;
if (msg_lst[i]->animationframe > fade_outs) {
new_alpha =
200 - (float(msg_lst[i]->animationframe - fade_outs) / fol) * 200;
}
// Wtf is this function lol
auto bgc = Palladium::Color::RGBA(PDColor_MessageBackground)
.changeA(new_alpha)
.toRGBA();
auto tc =
Palladium::Color::RGBA(PDColor_Text2).changeA(new_alpha).toRGBA();
R2::AddRect(pos, NVec2(150, 50), bgc);
R2::AddText(pos + NVec2(5, 1), msg_lst[i]->title, tc);
R2::AddText(pos + NVec2(5, 17), msg_lst[i]->message, tc);
if (pdi_debugging)
R2::AddText(pos + NVec2(155, 1),
std::to_string(msg_lst[i]->animationframe), tc);
// Why Frameadd? because Message uses int as frame and
// It seems that lower 0.5 will be rounded to 0
// Why not replace int with float ?
// cause of buggy positions (seen in Flappy Bird 3ds for example)
float frameadd = 60.f * Palladium::GetDeltaTime();
// 60fps animation * delta to not slowdown
// Oh and fix for Startup lol
// Todo: Only do this on AppStart
if (msg_lst[i]->animationframe == 0) {
msg_lst[i]->animationframe += 1;
} else {
msg_lst[i]->animationframe += (frameadd < 1.f ? 1.f : frameadd);
}
if (msg_lst[i]->animationframe > anim_len) {
msg_lst.erase(msg_lst.begin() + i);
}
}
}
// ReReverse ?? lol
// Cause otherwise the Toasts will swap
std::reverse(msg_lst.begin(), msg_lst.end());
R2::SetTextSize(tmp_txt);
}
void PushMessage(const Message &msg) {
msg_lst.push_back(std::make_shared<Palladium::Message>(msg));
}
void SetMessageIdleStartFrame(int frame) { idles = frame; }
void SetMessageTotalAnimationFrames(int total_frames) {
anim_len = total_frames;
}
void SetMessageFadeOutStartFrame(int frame) { fade_outs = frame; }
} // namespace Palladium

245
source/Net.cpp Normal file
View File

@ -0,0 +1,245 @@
// TODO: Make Download2File faster on large files
#include <3ds.h>
#include <curl/curl.h>
#include <malloc.h>
#include <unistd.h>
#include <filesystem>
#include <fstream>
#include <regex>
#include <pd/Net.hpp>
#include <pd/internal_db.hpp>
static Palladium::Net::Error pdi_check_wifi() {
// if (pdi_is_citra) return 0;
int s = osGetWifiStrength();
return (s == 0 ? Palladium::Net::Error_NoWifi : 0);
}
static size_t pdi_handle_data(char* ptr, size_t size, size_t nmemb,
void* userdata) {
size_t ms = size * nmemb;
((std::string*)userdata)->append(ptr, ms);
return ms;
}
static size_t pdi_handle_file(char* ptr, size_t size, size_t nmemb,
void* out) {
size_t ms = size * nmemb;
((std::ofstream*)out)->write(reinterpret_cast<const char*>(ptr), ms);
return ms;
}
struct pdi_net_dl {
unsigned long long current = 0;
unsigned long long total = 1;
void Reset() {
current = 0;
total = 1;
}
};
static pdi_net_dl pdi_net_dl_spec;
static int pdi_handle_curl_progress(CURL* hnd, curl_off_t dltotal,
curl_off_t dlnow, curl_off_t ultotal,
curl_off_t ulnow) {
pdi_net_dl_spec.total = dltotal;
pdi_net_dl_spec.current = dlnow;
return 0;
}
static void pdi_setup_curl_context(CURL* hnd, const std::string& url,
void* userptr, bool mem) {
std::string user_agent =
pdi_app_name + "/Palladium (Version: " + std::string(PDVSTRING) +
")";
if (!mem) {
curl_easy_setopt(hnd, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "gzip");
curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, pdi_handle_curl_progress);
}
curl_easy_setopt(hnd, CURLOPT_URL, url.c_str());
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, user_agent.c_str());
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, userptr);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION,
mem ? pdi_handle_data : pdi_handle_file);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_STDERR, stdout);
}
static bool pdi_curl_is_busy = false;
static bool pdi_apir_is_busy = false;
namespace Palladium {
namespace Net {
Error Download(const std::string& url, std::string& data) {
if (pdi_curl_is_busy) return Error_Busy;
Error ret = pdi_check_wifi();
if (ret != 0) {
return ret;
}
pdi_curl_is_busy = true;
pdi_net_dl_spec.Reset();
auto hnd = curl_easy_init();
pdi_setup_curl_context(hnd, url, &data, true);
CURLcode curl_res = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
if (curl_res != CURLE_OK) {
data.clear();
pdi_curl_is_busy = false;
return ((static_cast<Error>(curl_res) << 32) |
static_cast<Error>(Error_Curl));
}
pdi_curl_is_busy = false;
return 0;
}
Error Download2File(const std::string& url, const std::string& path) {
if (pdi_curl_is_busy) return Error_Busy;
Error ret = pdi_check_wifi();
if (ret != 0) {
return ret;
}
pdi_curl_is_busy = true;
pdi_net_dl_spec.Reset();
// std::filesystem::create_directories(
// std::filesystem::path(path).remove_filename());
std::ofstream file(path, std::ios::binary);
if (!file.is_open()) {
pdi_curl_is_busy = false;
return Error_Write;
}
auto hnd = curl_easy_init();
pdi_setup_curl_context(hnd, url, &file, false);
CURLcode curl_res = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
file.close();
if (curl_res != CURLE_OK) {
if (Palladium::FS::FileExist(path)) {
std::filesystem::remove(path);
}
pdi_curl_is_busy = false;
return ((static_cast<Error>(curl_res) << 32) |
static_cast<Error>(Error_Curl));
}
pdi_curl_is_busy = false;
return 0;
}
Error GitDownloadRelease(const std::string& url, const std::string& asset_name,
const std::string& path, bool prerelease) {
if (pdi_apir_is_busy) return Error_Busy;
Error ret = pdi_check_wifi();
if (ret != 0) {
return ret;
}
pdi_apir_is_busy = true;
std::regex parse("github\\.com\\/(.+)\\/(.+)");
std::smatch res;
std::regex_search(url, res, parse);
std::string user = res[1].str();
std::string repo = res[2].str();
std::stringstream req;
req << "https://api.github.com/repos/" << user << "/" << repo
<< (prerelease ? "/releases" : "/releases/latest");
std::string buf;
ret = Download(req.str(), buf);
if (ret) {
pdi_apir_is_busy = false;
return ret;
}
int pret = 0;
std::string freq;
if (nlohmann::json::accept(buf)) {
nlohmann::json api = nlohmann::json::parse(buf);
if (!api.size()) pret = -1;
if (pret != -1) {
if (prerelease) api = api[0];
if (api["assets"].is_array()) {
for (const auto& asset : api["assets"]) {
if (asset.is_object() && asset["name"].is_string() &&
asset["browser_download_url"].is_string()) {
if (std::regex_match(std::string(asset["name"]),
std::regex(asset_name))) {
freq = asset["browser_download_url"];
break;
}
}
}
}
}
} else {
pret = -1;
}
if (pret != 0 || freq.empty()) {
pdi_apir_is_busy = false;
return Error_Git;
}
ret = Download2File(freq, path);
pdi_apir_is_busy = false;
return ret;
}
Error JsonApiRequest(const std::string& api_url, nlohmann::json& res) {
if (pdi_apir_is_busy) return Error_Busy;
Error ret = pdi_check_wifi();
if (ret != 0) {
return ret;
}
pdi_apir_is_busy = true;
std::string buf;
ret = Download(api_url, buf);
if (ret) {
pdi_apir_is_busy = false;
return ret;
}
if (nlohmann::json::accept(buf)) {
res = nlohmann::json::parse(buf);
if (!res.size()) {
pdi_apir_is_busy = false;
return Error_Invalid;
}
} else {
pdi_apir_is_busy = false;
return Error_Invalid;
}
pdi_apir_is_busy = false;
return 0;
}
unsigned long long GetProgressCurrent() { return pdi_net_dl_spec.current; }
unsigned long long GetProgressTotal() {
// As curl sets total to 0 we need
// to return a 1 as devide by zeroi will crash
if (pdi_net_dl_spec.total <= 0) return 1;
return pdi_net_dl_spec.total;
}
} // namespace Net
} // namespace Palladium

497
source/Overlays.cpp Normal file
View File

@ -0,0 +1,497 @@
#include <pd/FunctionTrace.hpp>
#include <pd/Hid.hpp>
#include <pd/Overlays.hpp>
#include <pd/internal_db.hpp>
#include <pd/palladium.hpp>
///////////////////////////////
struct Key {
std::string disp;
NVec2 pos;
NVec2 size;
// 0 = default key
// 1 = Shift
// 2 = Backsp
// 3 = Enter
// 4 = Cancel
// 5 = Confirm
// 6 = Tab
// 7 = Caps
// 8 = Space
int action = 0;
};
std::vector<Key> keyboard_layout_num{
// 1st row
{"7", NVec2(5, 135), NVec2(36, 24), 0},
{"8", NVec2(43, 135), NVec2(36, 24), 0},
{"9", NVec2(81, 135), NVec2(36, 24), 0},
// 2nd row
{"4", NVec2(5, 161), NVec2(36, 24), 0},
{"5", NVec2(43, 161), NVec2(36, 24), 0},
{"6", NVec2(81, 161), NVec2(36, 24), 0},
// 3rd row
{"1", NVec2(5, 187), NVec2(36, 24), 0},
{"2", NVec2(43, 187), NVec2(36, 24), 0},
{"3", NVec2(81, 187), NVec2(36, 24), 0},
// 4th row
{"0", NVec2(5, 213), NVec2(74, 24), 0},
{".", NVec2(81, 213), NVec2(36, 24), 0},
// additional actions
{"<---", NVec2(119, 135), NVec2(74, 24), 2},
//{"", NVec2(119, 161), NVec2(74, 24), 0},
{"Confirm", NVec2(119, 187), NVec2(74, 24), 5},
{"Cancel", NVec2(119, 213), NVec2(74, 24), 4},
};
std::vector<Key> keyboard_layout = {
// 1st row
{"`", NVec2(5, 137), NVec2(18, 18), 0},
{"1", NVec2(25, 137), NVec2(18, 18), 0},
{"2", NVec2(45, 137), NVec2(18, 18), 0},
{"3", NVec2(65, 137), NVec2(18, 18), 0},
{"4", NVec2(85, 137), NVec2(18, 18), 0},
{"5", NVec2(105, 137), NVec2(18, 18), 0},
{"6", NVec2(125, 137), NVec2(18, 18), 0},
{"7", NVec2(145, 137), NVec2(18, 18), 0},
{"8", NVec2(165, 137), NVec2(18, 18), 0},
{"9", NVec2(185, 137), NVec2(18, 18), 0},
{"0", NVec2(205, 137), NVec2(18, 18), 0},
{"-", NVec2(225, 137), NVec2(18, 18), 0},
{"=", NVec2(245, 137), NVec2(18, 18), 0},
{"<---", NVec2(265, 137), NVec2(50, 18), 2},
// 2nd row
{"Tab", NVec2(5, 157), NVec2(40, 18), 6},
{"q", NVec2(47, 157), NVec2(18, 18), 0},
{"w", NVec2(67, 157), NVec2(18, 18), 0},
{"e", NVec2(87, 157), NVec2(18, 18), 0},
{"r", NVec2(107, 157), NVec2(18, 18), 0},
{"t", NVec2(127, 157), NVec2(18, 18), 0},
{"y", NVec2(147, 157), NVec2(18, 18), 0},
{"u", NVec2(167, 157), NVec2(18, 18), 0},
{"i", NVec2(187, 157), NVec2(18, 18), 0},
{"o", NVec2(207, 157), NVec2(18, 18), 0},
{"p", NVec2(227, 157), NVec2(18, 18), 0},
{"[", NVec2(247, 157), NVec2(18, 18), 0},
{"]", NVec2(267, 157), NVec2(18, 18), 0},
{"\\", NVec2(287, 157), NVec2(28, 18), 0},
// 3rd row
{"Caps", NVec2(5, 177), NVec2(50, 18), 7},
{"a", NVec2(57, 177), NVec2(18, 18), 0},
{"s", NVec2(77, 177), NVec2(18, 18), 0},
{"d", NVec2(97, 177), NVec2(18, 18), 0},
{"f", NVec2(117, 177), NVec2(18, 18), 0},
{"g", NVec2(137, 177), NVec2(18, 18), 0},
{"h", NVec2(157, 177), NVec2(18, 18), 0},
{"j", NVec2(177, 177), NVec2(18, 18), 0},
{"k", NVec2(197, 177), NVec2(18, 18), 0},
{"l", NVec2(217, 177), NVec2(18, 18), 0},
{";", NVec2(237, 177), NVec2(18, 18), 0},
{"'", NVec2(257, 177), NVec2(18, 18), 0},
{"Enter", NVec2(277, 177), NVec2(38, 18), 3},
// 4th row
{"Shift", NVec2(5, 197), NVec2(60, 18), 1},
{"z", NVec2(67, 197), NVec2(18, 18), 0},
{"x", NVec2(87, 197), NVec2(18, 18), 0},
{"c", NVec2(107, 197), NVec2(18, 18), 0},
{"v", NVec2(127, 197), NVec2(18, 18), 0},
{"b", NVec2(147, 197), NVec2(18, 18), 0},
{"n", NVec2(167, 197), NVec2(18, 18), 0},
{"m", NVec2(187, 197), NVec2(18, 18), 0},
{",", NVec2(207, 197), NVec2(18, 18), 0},
{".", NVec2(227, 197), NVec2(18, 18), 0},
{"/", NVec2(247, 197), NVec2(18, 18), 0},
{"Shift", NVec2(267, 197), NVec2(48, 18), 1},
// 5th row
{"Cancel", NVec2(5, 217), NVec2(70, 18), 4},
{"(X)", NVec2(77, 217), NVec2(23, 18), 10},
{"Space", NVec2(102, 217), NVec2(108, 18), 8},
{"(!)", NVec2(212, 217), NVec2(23, 18), 10},
{"Confirm", NVec2(237, 217), NVec2(78, 18), 5},
/*{"←", NVec2(237, 217), NVec2(18, 18)},
{"→", NVec2(257, 217), NVec2(18, 18)},
{"↓", NVec2(277, 217), NVec2(18, 18)},
{"↑", NVec2(297, 217), NVec2(18, 18)},*/
};
std::vector<Key> keyboard_layout_caps = {
// 1st row
{"`", NVec2(5, 137), NVec2(18, 18), 0},
{"1", NVec2(25, 137), NVec2(18, 18), 0},
{"2", NVec2(45, 137), NVec2(18, 18), 0},
{"3", NVec2(65, 137), NVec2(18, 18), 0},
{"4", NVec2(85, 137), NVec2(18, 18), 0},
{"5", NVec2(105, 137), NVec2(18, 18), 0},
{"6", NVec2(125, 137), NVec2(18, 18), 0},
{"7", NVec2(145, 137), NVec2(18, 18), 0},
{"8", NVec2(165, 137), NVec2(18, 18), 0},
{"9", NVec2(185, 137), NVec2(18, 18), 0},
{"0", NVec2(205, 137), NVec2(18, 18), 0},
{"-", NVec2(225, 137), NVec2(18, 18), 0},
{"=", NVec2(245, 137), NVec2(18, 18), 0},
{"<---", NVec2(265, 137), NVec2(50, 18), 2},
// 2nd row
{"Tab", NVec2(5, 157), NVec2(40, 18), 6},
{"Q", NVec2(47, 157), NVec2(18, 18), 0},
{"W", NVec2(67, 157), NVec2(18, 18), 0},
{"E", NVec2(87, 157), NVec2(18, 18), 0},
{"R", NVec2(107, 157), NVec2(18, 18), 0},
{"T", NVec2(127, 157), NVec2(18, 18), 0},
{"Y", NVec2(147, 157), NVec2(18, 18), 0},
{"U", NVec2(167, 157), NVec2(18, 18), 0},
{"I", NVec2(187, 157), NVec2(18, 18), 0},
{"O", NVec2(207, 157), NVec2(18, 18), 0},
{"P", NVec2(227, 157), NVec2(18, 18), 0},
{"[", NVec2(247, 157), NVec2(18, 18), 0},
{"]", NVec2(267, 157), NVec2(18, 18), 0},
{"\\", NVec2(287, 157), NVec2(28, 18), 0},
// 3rd row
{"Caps", NVec2(5, 177), NVec2(50, 18), 7},
{"A", NVec2(57, 177), NVec2(18, 18), 0},
{"S", NVec2(77, 177), NVec2(18, 18), 0},
{"D", NVec2(97, 177), NVec2(18, 18), 0},
{"F", NVec2(117, 177), NVec2(18, 18), 0},
{"G", NVec2(137, 177), NVec2(18, 18), 0},
{"H", NVec2(157, 177), NVec2(18, 18), 0},
{"J", NVec2(177, 177), NVec2(18, 18), 0},
{"K", NVec2(197, 177), NVec2(18, 18), 0},
{"L", NVec2(217, 177), NVec2(18, 18), 0},
{";", NVec2(237, 177), NVec2(18, 18), 0},
{"'", NVec2(257, 177), NVec2(18, 18), 0},
{"Enter", NVec2(277, 177), NVec2(38, 18), 3},
// 4th row
{"Shift", NVec2(5, 197), NVec2(60, 18), 1},
{"Z", NVec2(67, 197), NVec2(18, 18), 0},
{"X", NVec2(87, 197), NVec2(18, 18), 0},
{"C", NVec2(107, 197), NVec2(18, 18), 0},
{"V", NVec2(127, 197), NVec2(18, 18), 0},
{"B", NVec2(147, 197), NVec2(18, 18), 0},
{"N", NVec2(167, 197), NVec2(18, 18), 0},
{"M", NVec2(187, 197), NVec2(18, 18), 0},
{",", NVec2(207, 197), NVec2(18, 18), 0},
{".", NVec2(227, 197), NVec2(18, 18), 0},
{"/", NVec2(247, 197), NVec2(18, 18), 0},
{"Shift", NVec2(267, 197), NVec2(48, 18), 1},
// 5th row
{"Cancel", NVec2(5, 217), NVec2(70, 18), 4},
{"(X)", NVec2(77, 217), NVec2(23, 18), 10},
{"Space", NVec2(102, 217), NVec2(108, 18), 8},
{"(!)", NVec2(212, 217), NVec2(23, 18), 10},
{"Confirm", NVec2(237, 217), NVec2(78, 18), 5},
/*{"←", NVec2(237, 217), NVec2(18, 18)},
{"→", NVec2(257, 217), NVec2(18, 18)},
{"↑", NVec2(277, 217), NVec2(18, 18)},
{"↓", NVec2(297, 217), NVec2(18, 18)},*/
};
std::vector<Key> keyboard_layout_shift = {
// 1st row
{"~", NVec2(5, 137), NVec2(18, 18), 0},
{"!", NVec2(25, 137), NVec2(18, 18), 0},
{"@", NVec2(45, 137), NVec2(18, 18), 0},
{"#", NVec2(65, 137), NVec2(18, 18), 0},
{"$", NVec2(85, 137), NVec2(18, 18), 0},
{"%", NVec2(105, 137), NVec2(18, 18), 0},
{"^", NVec2(125, 137), NVec2(18, 18), 0},
{"&", NVec2(145, 137), NVec2(18, 18), 0},
{"*", NVec2(165, 137), NVec2(18, 18), 0},
{"(", NVec2(185, 137), NVec2(18, 18), 0},
{")", NVec2(205, 137), NVec2(18, 18), 0},
{"_", NVec2(225, 137), NVec2(18, 18), 0},
{"+", NVec2(245, 137), NVec2(18, 18), 0},
{"<---", NVec2(265, 137), NVec2(50, 18), 2},
// 2nd row
{"Tab", NVec2(5, 157), NVec2(40, 18), 6},
{"Q", NVec2(47, 157), NVec2(18, 18), 0},
{"W", NVec2(67, 157), NVec2(18, 18), 0},
{"E", NVec2(87, 157), NVec2(18, 18), 0},
{"R", NVec2(107, 157), NVec2(18, 18), 0},
{"T", NVec2(127, 157), NVec2(18, 18), 0},
{"Y", NVec2(147, 157), NVec2(18, 18), 0},
{"U", NVec2(167, 157), NVec2(18, 18), 0},
{"I", NVec2(187, 157), NVec2(18, 18), 0},
{"O", NVec2(207, 157), NVec2(18, 18), 0},
{"P", NVec2(227, 157), NVec2(18, 18), 0},
{"{", NVec2(247, 157), NVec2(18, 18), 0},
{"}", NVec2(267, 157), NVec2(18, 18), 0},
{"|", NVec2(287, 157), NVec2(28, 18), 0},
// 3rd row
{"Caps", NVec2(5, 177), NVec2(50, 18), 7},
{"A", NVec2(57, 177), NVec2(18, 18), 0},
{"S", NVec2(77, 177), NVec2(18, 18), 0},
{"D", NVec2(97, 177), NVec2(18, 18), 0},
{"F", NVec2(117, 177), NVec2(18, 18), 0},
{"G", NVec2(137, 177), NVec2(18, 18), 0},
{"H", NVec2(157, 177), NVec2(18, 18), 0},
{"J", NVec2(177, 177), NVec2(18, 18), 0},
{"K", NVec2(197, 177), NVec2(18, 18), 0},
{"L", NVec2(217, 177), NVec2(18, 18), 0},
{":", NVec2(237, 177), NVec2(18, 18), 0},
{"\"", NVec2(257, 177), NVec2(18, 18), 0},
{"Enter", NVec2(277, 177), NVec2(38, 18), 3},
// 4th row
{"Shift", NVec2(5, 197), NVec2(60, 18), 1},
{"Z", NVec2(67, 197), NVec2(18, 18), 0},
{"X", NVec2(87, 197), NVec2(18, 18), 0},
{"C", NVec2(107, 197), NVec2(18, 18), 0},
{"V", NVec2(127, 197), NVec2(18, 18), 0},
{"B", NVec2(147, 197), NVec2(18, 18), 0},
{"N", NVec2(167, 197), NVec2(18, 18), 0},
{"M", NVec2(187, 197), NVec2(18, 18), 0},
{"<", NVec2(207, 197), NVec2(18, 18), 0},
{">", NVec2(227, 197), NVec2(18, 18), 0},
{"?", NVec2(247, 197), NVec2(18, 18), 0},
{"Shift", NVec2(267, 197), NVec2(48, 18), 1},
// 5th row
{"Cancel", NVec2(5, 217), NVec2(70, 18), 4},
{"(X)", NVec2(77, 217), NVec2(23, 18), 10},
{"Space", NVec2(102, 217), NVec2(108, 18), 8},
{"(!)", NVec2(212, 217), NVec2(23, 18), 10},
{"Confirm", NVec2(237, 217), NVec2(78, 18), 5},
/*{"←", NVec2(237, 217), NVec2(18, 18)},
{"→", NVec2(257, 217), NVec2(18, 18)},
{"↑", NVec2(277, 217), NVec2(18, 18)},
{"↓", NVec2(297, 217), NVec2(18, 18)},*/
};
// From UI7
bool UI7_InBox(NVec2 inpos, NVec2 boxpos, NVec2 boxsize) {
if ((inpos.x > boxpos.x) && (inpos.y > boxpos.y) &&
(inpos.x < boxpos.x + boxsize.x) && (inpos.y < boxpos.y + boxsize.y))
return true;
return false;
}
namespace Palladium {
Ovl_Ftrace::Ovl_Ftrace(bool* is_enabled) { i_is_enabled = is_enabled; }
void Ovl_Ftrace::Draw(void) const {
float tmp_txt = R2::GetTextSize();
R2::DefaultTextSize();
R2::OnScreen(R2Screen_Top);
Palladium::Color::RGBA bg(PDColor_Background);
bg.changeA(150);
R2::AddRect(NVec2(0, 0), NVec2(400, 20), bg.toRGBA());
std::vector<Palladium::Ftrace::FTRes> dt;
for (auto const& it : Palladium::Ftrace::pd_traces)
if (it.second.is_ovl && dt.size() < 10) dt.push_back(it.second);
for (size_t i = 0; i < (dt.size() < 10 ? dt.size() : 10); i++) {
R2::AddText(NVec2(5, 30 + i * 15), dt[i].func_name, PDColor_Text);
R2::AddText(NVec2(295, 30 + i * 15), Palladium::MsTimeFmt(dt[i].time_of),
PDColor_Text);
}
R2::SetTextSize(tmp_txt);
}
void Ovl_Ftrace::Logic() {
if (!i_is_enabled[0]) this->Kill();
}
Ovl_Metrik::Ovl_Metrik(bool* is_enabled, bool* screen, uint32_t* mt_color,
uint32_t* txt_color, float* txt_size) {
i_is_enabled = is_enabled;
i_screen = screen;
i_mt_color = mt_color;
i_txt_color = txt_color;
i_txt_size = txt_size;
}
void Ovl_Metrik::Draw(void) const {
float tmp_txt = R2::GetTextSize();
R2::DefaultTextSize();
R2::OnScreen(i_screen[0] ? R2Screen_Bottom : R2Screen_Top);
std::string info =
"Palladium " + std::string(PDVSTRING) + " Debug Overlay";
float dim_y = R2::GetTextDimensions(info).y;
float infoy = 240 - dim_y;
mt_fps = "FPS: " + Palladium::GetFramerate();
if (pdi_idb_running) mt_fps += " IDB -> ON";
mt_cpu = "CPU: " +
std::to_string(C3D_GetProcessingTime() * (Palladium::GetFps() / 10))
.substr(0, 4) +
"%/" + std::to_string(C3D_GetProcessingTime()).substr(0, 4) + "ms";
mt_gpu = "GPU: " +
std::to_string(C3D_GetDrawingTime() * (Palladium::GetFps() / 10))
.substr(0, 4) +
"%/" + std::to_string(C3D_GetDrawingTime()).substr(0, 4) + "ms";
mt_cmd =
"CMD: " + std::to_string(C3D_GetCmdBufUsage() * 100.0f).substr(0, 4) +
"%";
mt_lfr = "Linear: " + Palladium::FormatBytes(linearSpaceFree());
mt_tbs =
"TextBuf: " + std::to_string(C2D_TextBufGetNumGlyphs(pdi_text_buffer)) +
"/4096";
if (pdi_enable_memtrack)
mt_mem = "Mem: " + Palladium::FormatBytes(Palladium::Memory::GetCurrent()) +
" | " +
Palladium::FormatBytes(Palladium::Memory::GetTotalAllocated()) +
" | " + Palladium::FormatBytes(Palladium::Memory::GetTotalFreed());
R2::AddRect(NVec2(0, 0), R2::GetTextDimensions(mt_fps),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, 50), R2::GetTextDimensions(mt_cpu),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, 50 + dim_y * 1), R2::GetTextDimensions(mt_gpu),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, 50 + dim_y * 2), R2::GetTextDimensions(mt_cmd),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, 50 + dim_y * 3), R2::GetTextDimensions(mt_lfr),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, 50 + dim_y * 4), R2::GetTextDimensions(mt_tbs),
(unsigned int)i_mt_color[0]);
if (pdi_enable_memtrack)
R2::AddRect(NVec2(0, 50 + dim_y * 5), R2::GetTextDimensions(mt_mem),
(unsigned int)i_mt_color[0]);
R2::AddRect(NVec2(0, infoy), R2::GetTextDimensions(info),
(unsigned int)i_mt_color[0]);
R2::AddText(NVec2(0, 0), mt_fps, (unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, 50), mt_cpu, (unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, 50 + dim_y * 1), mt_gpu,
(unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, 50 + dim_y * 2), mt_cmd,
(unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, 50 + dim_y * 3), mt_lfr,
(unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, 50 + dim_y * 4), mt_tbs,
(unsigned int)i_txt_color[0]);
if (pdi_enable_memtrack)
R2::AddText(NVec2(0, 50 + dim_y * 5), mt_mem,
(unsigned int)i_txt_color[0]);
R2::AddText(NVec2(0, infoy), info, (unsigned int)i_txt_color[0]);
// Force Bottom (Debug Touchpos)
R2::OnScreen(R2Screen_Bottom);
if (Hid::IsEvent("touch", Hid::Held)) {
R2::AddLine(NVec2(Hid::GetTouchPosition().x, 0),
NVec2(Hid::GetTouchPosition().x, 240),
Palladium::Color::Hex("#ff0000"));
R2::AddLine(NVec2(0, Hid::GetTouchPosition().y),
NVec2(320, Hid::GetTouchPosition().y),
Palladium::Color::Hex("#ff0000"));
}
R2::SetTextSize(tmp_txt);
}
void Ovl_Metrik::Logic() {
if (!i_is_enabled[0]) this->Kill();
}
Ovl_Keyboard::Ovl_Keyboard(std::string& ref, PDKeyboardState& state,
const std::string& hint, PDKeyboard type) {
// Blocks All Input outside of Keyboard
// Doesnt work for Hidkeys down etc
Palladium::Hid::Lock();
typed_text = &ref;
this->state = &state;
this->type = type;
*this->state = PDKeyboardState_None;
str_bak = ref;
ft3 = 0;
}
Ovl_Keyboard::~Ovl_Keyboard() {
// And Unlock when closing Keyboard lol
Palladium::Hid::Unlock();
}
void Ovl_Keyboard::Draw(void) const {
float tmp_txt = R2::GetTextSize();
R2::DefaultTextSize();
if (ft3 > 5) Palladium::Hid::Unlock();
auto key_table =
(type == PDKeyboard_Numpad) ? keyboard_layout_num : keyboard_layout;
if (mode == 1)
key_table = keyboard_layout_caps;
else if (mode == 2)
key_table = keyboard_layout_shift;
R2::OnScreen(R2Screen_Top);
R2::AddRect(NVec2(0, 0), NVec2(400, 240),
Palladium::Color::RGBA(PDColor_FrameBg).changeA(150).toRGBA());
R2::OnScreen(R2Screen_Bottom);
R2::AddRect(NVec2(0, 0), NVec2(320, 112),
Palladium::Color::RGBA(PDColor_FrameBg).changeA(150).toRGBA());
R2::AddRect(NVec2(0, 112), NVec2(320, 128), PDColor_FrameBg);
R2::AddRect(NVec2(0, 112), NVec2(320, 20), PDColor_Header);
R2::AddText(NVec2(5, 114), "> " + *typed_text,
Palladium::ThemeActive()->AutoText(PDColor_Header));
for (auto const& it : key_table) {
NVec2 szs = it.size;
NVec2 pos = it.pos;
NVec2 txtdim = R2::GetTextDimensions(it.disp);
PDColor btn = PDColor_Button;
if (Palladium::Hid::IsEvent("cancel", Palladium::Hid::Up)) {
Palladium::Hid::Clear();
shared_data[0x05] = 1;
}
if (Palladium::Hid::IsEvent("touch", Palladium::Hid::Up) &&
UI7_InBox(Palladium::Hid::GetLastTouchPosition(), pos, szs)) {
if (mode == 2) // Request Disable Shift
shared_data[0x02] = 1;
if (it.action == 0)
shared_data[0x01] = it.disp[0];
else if (it.action == 1)
shared_data[0x02] = 1;
else if (it.action == 2)
shared_data[0x03] = 1;
else if (it.action == 3)
shared_data[0x04] = 1;
else if (it.action == 4)
shared_data[0x05] = 1;
else if (it.action == 5)
shared_data[0x06] = 1;
else if (it.action == 6)
shared_data[0x07] = 1;
else if (it.action == 7)
shared_data[0x08] = 1;
else if (it.action == 8)
shared_data[0x09] = 1;
} else if (Palladium::Hid::IsEvent("touch", Palladium::Hid::Held) &&
UI7_InBox(Palladium::Hid::GetTouchPosition(), it.pos, it.size)) {
btn = PDColor_ButtonHovered;
pos -= NVec2(1, 1);
szs += NVec2(2, 2);
}
NVec2 txtpos = NVec2(pos.x + szs.x * 0.5 - txtdim.x * 0.5,
pos.y + szs.y * 0.5 - txtdim.y * 0.5);
R2::AddRect(pos, szs, btn);
R2::AddText(txtpos, it.disp, Palladium::ThemeActive()->AutoText(btn));
}
if (ft3 > 5) Palladium::Hid::Lock();
R2::SetTextSize(tmp_txt);
}
void Ovl_Keyboard::Logic() {
ft3++;
for (const auto& it : shared_data) {
if (it.first == 0x01) {
typed_text->push_back(it.second);
} else if (it.first == 0x02) {
// Shift
mode = (mode == 2) ? 0 : 2;
} else if (it.first == 0x03) {
if (typed_text->length() >= 1)
typed_text->erase(typed_text->begin() + typed_text->length() - 1);
} else if (it.first == 0x04) {
// Enter
} else if (it.first == 0x05) {
*typed_text = str_bak;
*state = PDKeyboardState_Cancel;
this->Kill();
} else if (it.first == 0x06) {
*state = PDKeyboardState_Confirm;
this->Kill();
} else if (it.first == 0x07) {
// this->typed_text += '\t'; // Tab
} else if (it.first == 0x08) {
// Caps
mode = (mode == 1) ? 0 : 1;
} else if (it.first == 0x09) {
typed_text->append(" "); // Space
}
}
shared_data.clear();
}
} // namespace Palladium

378
source/Render2.cpp Normal file
View File

@ -0,0 +1,378 @@
#include <citro2d.h>
#include <pd/Render2.hpp>
#include <pd/internal_db.hpp>
namespace Palladium {
const float R2::default_text_size = 0.5f;
float R2::text_size = 0.5;
Font::Ref R2::font;
std::map<std::string, float> R2::ts;
std::map<std::string, int> R2::mln;
bool R2::next_lined = false;
std::vector<R2::R2Cmd::Ref> R2::commands;
R2Screen R2::current_screen = R2Screen_Bottom;
void R2::Init() { R2::font = Font::New(); }
void R2::SetFont(Font::Ref fnt) {
if (!fnt) return;
R2::font = fnt;
}
Font::Ref R2::GetFont() { return R2::font; }
void R2::DefaultFont() { R2::font->Unload(); }
void R2::DrawNextLined() { R2::next_lined = true; }
void R2::OnScreen(R2Screen screen) {
if (screen < 0 || screen > R2Screen_Top) return;
R2::current_screen = screen;
}
void R2::SetTextSize(float szs) { text_size = szs; }
void R2::DefaultTextSize() { text_size = R2::default_text_size; }
float R2::GetTextSize() { return text_size; }
R2Screen R2::GetCurrentScreen() { return current_screen; }
NVec2 R2::GetTextDimensions(const std::string& text) {
C2D_TextBufClear(pdi_d2_dimbuf);
float w = 0, h = 0;
C2D_Text c2dtext;
C2D_TextFontParse(&c2dtext, font->Ptr(), pdi_d2_dimbuf, text.c_str());
C2D_TextGetDimensions(&c2dtext, R2::text_size, R2::text_size, &w, &h);
return NVec2(w, h);
}
std::string R2::WrapText(const std ::string& in, int maxlen) {
std::string out;
std::string line;
int line_x = 0;
std::istringstream istream(in);
std::string temp;
while (istream >> temp) {
NVec2 dim = R2::GetTextDimensions(line + temp);
if (line_x + dim.x <= maxlen) {
line += temp + ' ';
line_x += dim.x;
} else {
out += line + '\n';
line = temp + ' ';
line_x = dim.x;
}
}
out += line;
return out;
}
std::string R2::ShortText(const std::string& in, int maxlen) {
auto textdim = R2::GetTextDimensions(in);
if (textdim.x < (float)maxlen) return in;
std::string ft = "";
std::string worker = in;
if (in.find_last_of('.') != in.npos) {
ft = in.substr(in.find_last_of('.'));
worker = in.substr(0, in.find_last_of('.'));
}
maxlen -= R2::GetTextDimensions(ft).x - R2::GetTextDimensions("(...)").x;
float len_mod = (float)maxlen / textdim.x;
int pos = (in.length() * len_mod) / pd_draw2_tsm;
std::string out;
out = in.substr(0, pos);
for (size_t i = pos; i < worker.length(); i++) {
out += worker[i];
if (R2::GetTextDimensions(out + "(...)" + ft).x > (float)maxlen) {
out += "(...)";
out += ft;
return out;
}
}
return out; // Impossible to reach
}
NVec2 R2::GetCurrentScreenSize() {
return NVec2(R2::current_screen == R2Screen_Bottom ? 320 : 400, 240);
}
// Main Processing of Draw Calls
void R2::Process() {
for (auto& it : R2::commands) {
if (it->type <= 0 || it->type > 6) {
// Skip
continue;
}
C2D_SceneBegin(it->Screen ? pd_top : pd_bottom);
if (it->type == 1) {
// Rect
if (it->lined) {
C2D_DrawLine(it->pos.x, it->pos.y, it->clr, it->pos.x + it->pszs.x,
it->pos.y, it->clr, 1.f, 0.5f);
C2D_DrawLine(it->pos.x, it->pos.y, it->clr, it->pos.x,
it->pos.y + it->pszs.y, it->clr, 1.f, 0.5f);
C2D_DrawLine(it->pos.x + it->pszs.x, it->pos.y, it->clr,
it->pos.x + it->pszs.x, it->pos.y + it->pszs.y, it->clr,
1.f, 0.5f);
C2D_DrawLine(it->pos.x, it->pos.y + it->pszs.y, it->clr,
it->pos.x + it->pszs.x, it->pos.y + it->pszs.y, it->clr,
1.f, 0.5f);
} else {
C2D_DrawRectSolid(it->pos.x, it->pos.y, 0.5, it->pszs.x, it->pszs.y,
it->clr);
}
} else if (it->type == 2) {
// Triangle
if (it->lined) {
C2D_DrawLine(it->pos.x, it->pos.y, it->clr, it->pszs.x, it->pszs.y,
it->clr, 1, 0.5f);
C2D_DrawLine(it->pos.x, it->pos.y, it->clr, it->ap.x, it->ap.y, it->clr,
1, 0.5f);
C2D_DrawLine(it->pszs.x, it->pszs.y, it->clr, it->ap.x, it->ap.y,
it->clr, 1, 0.5f);
} else {
C2D_DrawTriangle(it->pos.x, it->pos.y, it->clr, it->pszs.x, it->pszs.y,
it->clr, it->ap.x, it->ap.y, it->clr, 0.5);
}
} else if (it->type == 3) {
// Text
// little patch for a freeze
if (it->text.length() < 1) continue;
if (it->pszs.x == 0.0f) {
it->pszs.x = it->Screen == R2Screen_Top ? 400 : 320;
}
if (it->pszs.y == 0.0f) {
it->pszs.y = 240;
}
std::string edit_text = it->text;
if (edit_text.substr(it->text.length() - 1) != "\n")
edit_text.append("\n"); // Add \n to end if not exist
int line = 0;
if (it->flags & PDTextFlags_Wrap) {
edit_text = WrapText(it->text, it->pszs.x - it->pos.x);
}
while (edit_text.find('\n') != edit_text.npos) {
std::string current_line = edit_text.substr(0, edit_text.find('\n'));
if (it->flags & PDTextFlags_Short)
current_line = R2::ShortText(current_line, it->pszs.x - it->pos.x);
NVec2 newpos = it->pos;
// Check Flags
NVec2 dim = R2::GetTextDimensions(current_line);
if (it->flags & PDTextFlags_AlignRight) newpos.x = newpos.x - dim.x;
if (it->flags & PDTextFlags_AlignMid) // Offset by inpos
newpos.x = (it->pszs.x * 0.5) - (dim.x * 0.5) + it->pos.x;
if (it->flags & PDTextFlags_Scroll) { // Scroll Text
// Look into Old Draw2 Code
// TODO: Create Code for this
}
if (pdi_debugging) {
R2::DrawNextLined();
R2::AddRect(newpos, dim, 0xff0000ff);
}
C2D_Text c2dtext;
C2D_TextFontParse(&c2dtext, font->Ptr(), pdi_text_buffer,
current_line.c_str());
C2D_TextOptimize(&c2dtext);
if (it->flags & PDTextFlags_Shaddow) // performance Killer xd
C2D_DrawText(&c2dtext, C2D_WithColor, newpos.x + 1 + (dim.y * line),
newpos.y + 1, 0.5, R2::text_size, R2::text_size,
Palladium::ThemeActive()->Get(PDColor_TextDisabled));
C2D_DrawText(&c2dtext, C2D_WithColor, newpos.x,
newpos.y + (dim.y * line), 0.5, R2::text_size,
R2::text_size, it->clr);
edit_text = edit_text.substr(edit_text.find('\n') + 1);
line++;
}
} else if (it->type == 4) {
if (it->img->Loadet()) {
C2D_DrawImageAt(it->img->Get(), it->pos.x, it->pos.y, 0.5f);
}
} else if (it->type == 5) {
// TODO: Move the Draw Func into this API
it->spr->Draw();
} else if (it->type == 6) {
C2D_DrawLine(it->pos.x, it->pos.y, it->clr, it->pszs.x, it->pszs.y,
it->clr, it->ap.x, 0.5f);
}
}
R2::commands.clear();
}
void R2::AddRect(NVec2 pos, NVec2 size, PDColor clr) {
auto cmd = R2Cmd::New();
cmd->pos = pos;
cmd->pszs = size;
cmd->clr = Palladium::ThemeActive()->Get(clr);
cmd->type = 1; // Rect
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddRect(NVec2 pos, NVec2 size, unsigned int clr) {
auto cmd = R2Cmd::New();
cmd->pos = pos;
cmd->pszs = size;
cmd->clr = clr;
cmd->type = 1; // Rect
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddLine(NVec2 pos_a, NVec2 pos_b, PDColor clr, int t) {
auto cmd = R2Cmd::New();
cmd->pos = pos_a;
cmd->pszs = pos_b;
cmd->ap.x = t;
cmd->clr = Palladium::ThemeActive()->Get(clr);
cmd->type = 6; // Line
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddLine(NVec2 pos_a, NVec2 pos_b, unsigned int clr, int t) {
auto cmd = R2Cmd::New();
cmd->pos = pos_a;
cmd->pszs = pos_b;
cmd->ap.x = t;
cmd->clr = clr;
cmd->type = 6; // Line
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddTriangle(NVec2 pos0, NVec2 pos1, NVec2 pos2, PDColor clr) {
auto cmd = R2Cmd::New();
cmd->pos = pos0;
cmd->pszs = pos1;
cmd->ap = pos2;
cmd->clr = Palladium::ThemeActive()->Get(clr);
cmd->type = 2; // Triangle
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddTriangle(NVec2 pos0, NVec2 pos1, NVec2 pos2, unsigned int clr) {
auto cmd = R2Cmd::New();
cmd->pos = pos0;
cmd->pszs = pos1;
cmd->ap = pos2;
cmd->clr = clr;
cmd->type = 2; // Triangle
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddText(NVec2 pos, const std::string& text, PDColor clr,
PDTextFlags flags, NVec2 tmb) {
auto cmd = R2Cmd::New();
cmd->pos = pos;
cmd->pszs = tmb;
cmd->clr = Palladium::ThemeActive()->Get(clr);
cmd->flags = flags;
cmd->text = text;
cmd->type = 3; // Text
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddText(NVec2 pos, const std::string& text, unsigned int clr,
PDTextFlags flags, NVec2 tmb) {
auto cmd = R2Cmd::New();
cmd->pos = pos;
cmd->pszs = tmb;
cmd->clr = clr;
cmd->flags = flags;
cmd->text = text;
cmd->type = 3; // Text
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddImage(NVec2 pos, Image::Ref img) {
auto cmd = R2Cmd::New();
cmd->pos = pos;
cmd->img = img;
cmd->type = 4; // Image
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
void R2::AddSprite(Sprite::Ref spr) {
auto cmd = R2Cmd::New();
cmd->spr = spr;
cmd->type = 5; // Sprite
// Just assign current screen as bottom is 0 (false)
// and Top and TopRight are !0 (true)
cmd->Screen = current_screen;
if (R2::next_lined) {
cmd->lined = true;
R2::next_lined = false;
}
R2::commands.push_back(cmd);
}
} // namespace Palladium

460
source/ResultDecoder.cpp Normal file
View File

@ -0,0 +1,460 @@
#include <filesystem>
#include <fstream>
#include <map>
#include <pd/ResultDecoder.hpp>
#include <pd/internal_db.hpp>
#include <sstream>
static std::map<int, std::string> modules = {
{0, "common"},
{1, "kernel"},
{2, "util"},
{3, "file server"},
{4, "loader server"},
{5, "tcb"},
{6, "os"},
{7, "dbg"},
{8, "dmnt"},
{9, "pdn"},
{10, "gsp"},
{11, "i2c"},
{12, "gpio"},
{13, "dd"},
{14, "codec"},
{15, "spi"},
{16, "pxi"},
{17, "fs"},
{18, "di"},
{19, "hid"},
{20, "cam"},
{21, "pi"},
{22, "pm"},
{23, "pm_low"},
{24, "fsi"},
{25, "srv"},
{26, "ndm"},
{27, "nwm"},
{28, "soc"},
{29, "ldr"},
{30, "acc"},
{31, "romfs"},
{32, "am"},
{33, "hio"},
{34, "updater"},
{35, "mic"},
{36, "fnd"},
{37, "mp"},
{38, "mpwl"},
{39, "ac"},
{40, "http"},
{41, "dsp"},
{42, "snd"},
{43, "dlp"},
{44, "hio_low"},
{45, "csnd"},
{46, "ssl"},
{47, "am_low"},
{48, "nex"},
{49, "friends"},
{50, "rdt"},
{51, "applet"},
{52, "nim"},
{53, "ptm"},
{54, "midi"},
{55, "mc"},
{56, "swc"},
{57, "fatfs"},
{58, "ngc"},
{59, "card"},
{60, "cardnor"},
{61, "sdmc"},
{62, "boss"},
{63, "dbm"},
{64, "config"},
{65, "ps"},
{66, "cec"},
{67, "ir"},
{68, "uds"},
{69, "pl"},
{70, "cup"},
{71, "gyroscope"},
{72, "mcu"},
{73, "ns"},
{74, "news"},
{75, "ro"},
{76, "gd"},
{77, "card spi"},
{78, "ec"},
{79, "web browser"},
{80, "test"},
{81, "enc"},
{82, "pia"},
{83, "act"},
{84, "vctl"},
{85, "olv"},
{86, "neia"},
{87, "npns"},
{90, "avd"},
{91, "l2b"},
{92, "mvd"},
{93, "nfc"},
{94, "uart"},
{95, "spm"},
{96, "qtm"},
{97, "nfp"},
{254, "application"},
};
static std::map<int, std::string> levels = {
{0, "Success"}, {1, "Info"}, {25, "Status"},
{26, "Temporary"}, {27, "Permanent"}, {28, "Usage"},
{29, "Reinitialize"}, {30, "Reset"}, {31, "Fatal"},
};
static std::map<int, std::string> summaries = {
{0, "Success"},
{1, "Nothing happened"},
{2, "Would block"},
{3, "Out of resource"},
{4, "Not found"},
{5, "Invalid state"},
{6, "Not supported"},
{7, "Invalid argument"},
{8, "Wrong argument"},
{9, "Canceled"},
{10, "Status changed"},
{11, "Internal"},
{63, "Invalid result value"},
};
static std::map<int, std::string> desccommon = {
{0, "Success"},
{1000, "Invalid selection"},
{1001, "Too large"},
{1002, "Not authorized"},
{1003, "Already done"},
{1004, "Invalid size"},
{1005, "Invalid enum value"},
{1006, "Invalid combination"},
{1007, "No data"},
{1008, "Busy"},
{1009, "Misaligned address"},
{1010, "Misaligned size"},
{1011, "Out of memory"},
{1012, "Not implemented"},
{1013, "Invalid address"},
{1014, "Invalid pointer"},
{1015, "Invalid handle"},
{1016, "Not initialized"},
{1017, "Already initialized"},
{1018, "Not found"},
{1019, "Cancel requested"},
{1020, "Already exists"},
{1021, "Out of range"},
{1022, "Timeout"},
{1023, "Invalid result value"},
};
static std::map<int, std::string> desckernel = {
{2, "Invalid memory permissions."},
};
static std::map<int, std::string> descos = {
{10, "Not enough memory."},
{26, "Session closed by remote."},
{47, "Invalid command header."},
};
// Need to Fix The Range based Values
static std::map<int, std::string> descfs = {
{101, "Archive not mounted or mount-point not found."},
{120, "Title or object not found."},
{141, "Gamecard not inserted."},
{230, "Invalid open flags or permissions."},
{391, "NCCH hash check failed."},
{302, "RSA or AES-MAC verification failed."},
{395, "RomFS or Savedata hash check failed."},
{630, "Command not allowed, or missing permissions."},
{702, "Invalid path."},
{761, "Incorrect ExeFS read size."},
{100, "[Media] not found."},
{180, "Exists already."},
{200, "Not enough space."},
{220, "Invalidated archive."},
{230, "Unacceptable or write protected."},
{340, "0x01"},
{360, "Bad format."},
{390, "Verification failure."},
{400, "0x01"},
{600, "Out of resources."},
{630, "Access denied."},
{661, "0x01"},
{700, "Invalid argument."},
{730, "Not initialized."},
{750, "Already initialized."},
{760, "Not supported."},
{780, "0x01"},
};
static std::map<int, std::string> descsrv = {
{5,
"Invalid string length (service name length is zero or longer than 8 "
"chars)."},
{6,
"Access to service denied (requested a service the application does "
"not have access to)."},
{7,
"String size does not match contents (service name contains unexpected "
"null byte)."},
};
static std::map<int, std::string> descnwm = {
{2,
"This error usually indicates the wifi chipset in the console is dying "
"or dead."},
};
static std::map<int, std::string> descam = {
{4, "Invalid ticket version."},
{32, "Empty CIA."},
{37, "Invalid NCCH."},
{39, "Invalid title version."},
{43, "Database doesn\"t exist, or it failed to open."},
{44, "Trying to uninstall system-app."},
{106,
"Invalid signature/CIA. Usually happens when developer UNITINFO is "
"enabled in Luma3DS."},
{393, "Invalid database."},
};
static std::map<int, std::string> deschttp = {
{105, "Request timed out."},
};
static std::map<int, std::string> descnim = {
{1,
"Invalid string IPC paramater (non null terminated at its indicated "
"length)."},
{12,
"Invalid country code returned by CFG module reading config save "
"0xB0000."},
{13,
"Zero string length console serial number or '000000000000000' "
"returned by CFG's SecureInfoGetSerialNo."},
{18,
"General data reading error of NIM's .dat files from its system save, "
"bad data or bad data lengths."},
{22,
"General invalid data or length of data returned from nintendo "
"servers. (Only applicable for some operations)"},
{25,
"IntegrityVerificationSeed is waiting on servers to be synced into "
"console. Can't processed with online services without sync being "
"completed first over IPC request."},
{26,
"Unavailable/unaccessable IntegrityVerificationSeed on Nintendo "
"servers. May happen if NIM is told to import "
"IntegrityVerificationSeed from servers at any time other than after "
"the successful System Transfer reboot."},
{27,
"Invalid country language code returned by CFG module reading config "
"save 0xA0002."},
{37,
"Service is in Standby Mode. (eShop ban? General service is down? "
"This caused by a server response flag on account information. "
"Account is not referring to NNID.)"},
{39, "HTTP Status non 200. (Only applicable for some operations)"},
{40, "General XML read/write error while processing Auto Delivery XMLs."},
{41,
"General XML read/write error while processing Auto Delivery XMLs. "
"(Stubbed virtual call was called)"},
{58,
"Invalid NPNS token returned by CFG module reading config save 0xF0006."},
{67, "HTTP Status 404 while trying to download a game's seed."},
{68, "HTTP Status 503 while trying to download a game's seed."},
};
static std::map<int, std::string> descmvd = {
{271, "Invalid configuration."},
};
static std::map<int, std::string> descqtm = {
{8, "Camera is already in use or busy."},
};
// Need to Fix The Range based Values
static std::map<int, std::string> descapplication = {
{0,
"The application raised an error. Please consult the application's "
"source code or ask the author for assistance with it."},
{1024, "0x01"},
};
namespace Palladium {
void ResultDecoder::Load(Result rescode) { this->m_rescode = rescode; }
void ResultDecoder::Load(std::string rescode) {
std::stringstream ss;
ss << rescode;
ss >> std::hex >> this->m_rescode;
}
std::string Palladium::ResultDecoder::GetCode() {
std::stringstream ss;
ss << std::hex << m_rescode;
std::string reshex(ss.str());
return reshex;
}
std::string ResultDecoder::GetLevel() {
std::string res = levels.at(this->GetLevelInt()) + " (" +
std::to_string(this->GetLevelInt()) + ")";
return res;
}
int ResultDecoder::GetLevelInt() { return R_LEVEL(m_rescode); }
std::string ResultDecoder::GetModule() {
std::string res = modules.at(this->GetModuleInt()) + " (" +
std::to_string(this->GetModuleInt()) + ")";
return res;
}
int ResultDecoder::GetModuleInt() { return R_MODULE(m_rescode); }
std::string ResultDecoder::GetDescription() {
std::string res = "Desc Not Implemented!";
switch (this->GetModuleInt()) {
case 0:
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
break;
case 1:
if ((desckernel.find(this->GetDescriptionInt()) == desckernel.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = desckernel.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 6:
if ((descos.find(this->GetDescriptionInt()) == descos.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descos.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 17:
if ((descfs.find(this->GetDescriptionInt()) == descfs.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descfs.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 25:
if ((descsrv.find(this->GetDescriptionInt()) == descsrv.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descsrv.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 27:
if ((descnwm.find(this->GetDescriptionInt()) == descnwm.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descnwm.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 32:
if ((descam.find(this->GetDescriptionInt()) == descam.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descam.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 40:
if ((deschttp.find(this->GetDescriptionInt()) == deschttp.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = deschttp.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 52:
if ((descnim.find(this->GetDescriptionInt()) == descnim.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descnim.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 92:
if ((descmvd.find(this->GetDescriptionInt()) == descmvd.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descmvd.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 96:
if ((descqtm.find(this->GetDescriptionInt()) == descqtm.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descqtm.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
case 254:
if ((descapplication.find(this->GetDescriptionInt()) ==
descapplication.end())) {
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
} else {
res = descapplication.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
}
break;
default:
res = desccommon.at(this->GetDescriptionInt()) + " (" +
std::to_string(this->GetDescriptionInt()) + ")";
break;
}
return res;
}
int ResultDecoder::GetDescriptionInt() { return R_DESCRIPTION(m_rescode); }
std::string ResultDecoder::GetSummary() {
std::string res = summaries.at(this->GetSummaryInt()) + " (" +
std::to_string(this->GetSummaryInt()) + ")";
return res;
}
void Palladium::ResultDecoder::WriteLog() {
std::string out_path = "sdmc:/Palladium/Apps/" + pdi_app_name + "/resdec";
std::filesystem::create_directories(out_path);
out_path += "/err_result_" + std::to_string(time(0)) + ".log";
std::ofstream out(out_path, std::ios::app);
out << "+-------------------\n";
out << "| Error: " << GetCode() << "\n";
out << "+-------------------\n";
out << "| Module: " << GetModule() << "\n";
out << "+-------------------\n";
out << "| Level: " << GetLevel() << "\n";
out << "+-------------------\n";
out << "| Summary: " << GetSummary() << "\n";
out << "+-------------------\n";
out << "| Description: " << GetDescription() << "\n";
out << "+-------------------\n";
out.close();
}
int ResultDecoder::GetSummaryInt() { return R_SUMMARY(m_rescode); }
} // namespace Palladium

22
source/Sheet.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <pd/Sheet.hpp>
#include <pd/internal_db.hpp>
Result Palladium::Sheet::Load(const std::string& path) {
if (this->spritesheet) Free();
this->spritesheet = C2D_SpriteSheetLoad(path.c_str());
if (!this->spritesheet) {
_pdi_logger()->Write("Failed to Load Spritesheet from: " + path, 0);
}
return 0;
}
void Palladium::Sheet::Free() {
if (!this->spritesheet) return;
C2D_SpriteSheetFree(this->spritesheet);
this->spritesheet = nullptr;
}
C2D_Image Palladium::Sheet::GetImage(int idx) {
if (!this->spritesheet) return {nullptr, nullptr};
return C2D_SpriteSheetGetImage(this->spritesheet, idx);
}

133
source/Sound.cpp Normal file
View File

@ -0,0 +1,133 @@
#include <cstring>
#include <fstream>
#include <pd/Sound.hpp>
#include <pd/internal_db.hpp>
#include <string>
using std::string;
// Reference: http://yannesposito.com/Scratch/en/blog/2010-10-14-Fun-with-wav/
typedef struct _WavHeader {
char magic[4]; // "RIFF"
u32 totallength; // Total file length, minus 8.
char wavefmt[8]; // Should be "WAVEfmt "
u32 format; // 16 for PCM format
u16 pcm; // 1 for PCM format
u16 channels; // Channels
u32 frequency; // Sampling frequency
u32 bytes_per_second;
u16 bytes_by_capture;
u16 bits_per_sample;
char data[4]; // "data"
u32 bytes_in_data;
} WavHeader;
static_assert(sizeof(WavHeader) == 44, "WavHeader size is not 44 bytes.");
using namespace Palladium;
Sound::Sound(const string &path, int channel, bool toloop) {
if (pdi_is_ndsp) {
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
ndspSetOutputCount(2); // Num of buffers
// Reading wav file
std::fstream fp(path, std::ios::in | std::ios::binary);
if (!fp.is_open()) {
_pdi_logger()->Write("Could not open WAV: " + path, 0);
return;
}
WavHeader wavHeader;
fp.read(reinterpret_cast<char *>(&wavHeader), sizeof(WavHeader));
size_t read = fp.tellg();
if (read != sizeof(wavHeader)) {
// Short read.
_pdi_logger()->Write("WAV Header is too short", 0);
fp.close();
return;
}
// Verify the header.
static const char RIFF_magic[4] = {'R', 'I', 'F', 'F'};
if (memcmp(wavHeader.magic, RIFF_magic, sizeof(wavHeader.magic)) != 0) {
// Incorrect magic number.
_pdi_logger()->Write("Wrong Fileformat", 0);
fp.close();
return;
}
if (wavHeader.totallength == 0 ||
(wavHeader.channels != 1 && wavHeader.channels != 2) ||
(wavHeader.bits_per_sample != 8 && wavHeader.bits_per_sample != 16)) {
// Unsupported WAV file.
_pdi_logger()->Write("File is invalid", 0);
fp.close();
return;
}
// Get the file size.
fp.seekg(0, std::ios::end);
dataSize = fp.tellg();
dataSize -= sizeof(WavHeader);
// Allocating and reading samples
data = static_cast<u8 *>(linearAlloc(dataSize));
fp.seekg(44, std::ios::beg);
fp.read(reinterpret_cast<char *>(data), dataSize);
fp.close();
dataSize /= 2; // FIXME: 16-bit or stereo?
// Find the right format
u16 ndspFormat;
if (wavHeader.bits_per_sample == 8) {
ndspFormat = (wavHeader.channels == 1) ? NDSP_FORMAT_MONO_PCM8
: NDSP_FORMAT_STEREO_PCM8;
} else {
ndspFormat = (wavHeader.channels == 1) ? NDSP_FORMAT_MONO_PCM16
: NDSP_FORMAT_STEREO_PCM16;
}
ndspChnReset(channel);
ndspChnSetInterp(channel, NDSP_INTERP_NONE);
ndspChnSetRate(channel, float(wavHeader.frequency));
ndspChnSetFormat(channel, ndspFormat);
// Create and play a wav buffer
memset(&waveBuf, 0, sizeof(waveBuf));
waveBuf.data_vaddr = reinterpret_cast<u32 *>(data);
waveBuf.nsamples = dataSize / (wavHeader.bits_per_sample >> 3);
waveBuf.looping = toloop;
waveBuf.status = NDSP_WBUF_FREE;
chnl = channel;
}
}
Sound::~Sound() {
if (pdi_is_ndsp) {
waveBuf.data_vaddr = 0;
waveBuf.nsamples = 0;
waveBuf.looping = false;
waveBuf.status = 0;
ndspChnWaveBufClear(chnl);
if (data) {
linearFree(data);
}
}
}
void Sound::Play() {
if (pdi_is_ndsp) {
if (!data) return;
DSP_FlushDataCache(data, dataSize);
ndspChnWaveBufAdd(chnl, &waveBuf);
}
}
void Sound::Stop() {
if (pdi_is_ndsp) {
if (!data) return;
ndspChnWaveBufClear(chnl);
}
}

53
source/Sprite.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <pd/Sprite.hpp>
void Palladium::Sprite::FromSheet(Palladium::Sheet::Ref sheet, size_t index) {
C2D_SpriteFromSheet(&this->sprite, sheet->Get(), index);
}
bool Palladium::Sprite::Draw() {
// Patch Depth before draw
sprite.params.depth = 0.5;
return C2D_DrawSprite(&this->sprite);
}
void Palladium::Sprite::SetCenter(float x, float y) {
C2D_SpriteSetCenter(&this->sprite, x, y);
}
void Palladium::Sprite::SetPos(float x, float y) {
C2D_SpriteSetPos(&this->sprite, x, y);
}
void Palladium::Sprite::SetRotation(float rotation) {
C2D_SpriteSetRotation(&this->sprite, rotation);
}
void Palladium::Sprite::Rotate(float speed) {
C2D_SpriteRotateDegrees(&this->sprite, speed);
}
float Palladium::Sprite::GetHeight() { return GetSize().x; }
float Palladium::Sprite::GetWidth() { return GetSize().y; }
float Palladium::Sprite::GetPosX() { return GetPos().x; }
float Palladium::Sprite::GetPosY() { return GetPos().y; }
NVec2 Palladium::Sprite::GetPos() {
return NVec2(this->sprite.params.pos.x, this->sprite.params.pos.y);
}
NVec2 Palladium::Sprite::GetSize() {
return NVec2(this->sprite.params.pos.w, this->sprite.params.pos.h);
}
void Palladium::Sprite::SetPos(NVec2 pos) {
C2D_SpriteSetPos(&this->sprite, pos.x, pos.y);
}
void Palladium::Sprite::SetScale(NVec2 scale) {
C2D_SpriteScale(&this->sprite, scale.x, scale.y);
}
void Palladium::Sprite::SetRotCenter(NVec2 percentage) {
C2D_SpriteSetCenter(&this->sprite, percentage.x, percentage.y);
}
void Palladium::Sprite::FromImage(Palladium::Image::Ref img) {
C2D_SpriteFromImage(&this->sprite, img->Get());
}
void Palladium::Sprite::SetScale(float x, float y) {
C2D_SpriteScale(&this->sprite, x, y);
}

View File

@ -0,0 +1,28 @@
#include <pd/SpriteAnimation.hpp>
void Palladium::SpriteSheetAnimation::Setup(Palladium::Sheet::Ref sheet,
size_t imagecount, size_t startimage,
float frame_begin,
float frame_finish) {
D_totaltime = frame_begin;
this->images = imagecount;
this->sheet = sheet;
this->time = frame_finish;
Palladium::SpriteSheetAnimation::FromSheet(this->sheet, startimage);
}
void Palladium::SpriteSheetAnimation::Play(float timespeed) {
D_totaltime += timespeed;
if (D_totaltime >= time) {
D_totaltime -= time;
imgs++;
if (imgs == images) {
imgs = 0;
}
}
Palladium::SpriteSheetAnimation::FromSheet(sheet, imgs);
// Palladium::SpriteSheetAnimation::Draw();
}

22
source/Tasks.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <memory>
#include <pd/Tasks.hpp>
#include <thread>
#include <vector>
static std::vector<std::shared_ptr<std::thread>> threads;
int Palladium::Tasks::Create(std::function<void()> fun) {
auto thrd = std::make_shared<std::thread>(fun);
threads.push_back(thrd);
return threads.size();
}
void Palladium::Tasks::DestroyAll() {
for (auto& it : threads) {
if (it && it->joinable()) {
it->join();
}
}
// Delete Pointers
threads.clear();
}

177
source/Texture.cpp Normal file
View File

@ -0,0 +1,177 @@
#include <pd/Texture.hpp>
#include <pd/external/stb_image.h>
#include <pd/internal_db.hpp>
#include <pd/external/stb_image_write.h>
namespace pdi {
static u32 GP2O(u32 v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return (v >= 64 ? v : 64);
}
static void R24R32(std::vector<uint8_t> &out,
const std::vector<uint8_t> &in, const int &w,
const int &h) {
// Converts RGB24 to RGBA32
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int src = (y * w + x) * 3;
int dst = (y * w + x) * 4;
out[dst + 0] = in[src + 0];
out[dst + 1] = in[src + 1];
out[dst + 2] = in[src + 2];
out[dst + 3] = 255;
}
}
}}
namespace Palladium {
void Texture::MakeTex(std::vector<unsigned char> &buf, int w, int h) {
if (!tex) {
_pdi_logger()->Write("Invalid Input (object has no adress!)");
return;
}
// RGBA -> Abgr
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int pos = (x + y * w) * 4;
auto r = buf[pos + 0];
auto g = buf[pos + 1];
auto b = buf[pos + 2];
auto a = buf[pos + 3];
buf[pos + 0] = a;
buf[pos + 1] = b;
buf[pos + 2] = g;
buf[pos + 3] = r;
}
}
// Pow2
this->tex_size.x = pdi::GP2O((unsigned int)w);
this->tex_size.y = pdi::GP2O((unsigned int)h);
this->img_size.x = (u16)w;
this->img_size.y = (u16)h;
this->uvs.x = 0.0f;
this->uvs.y = 1.0f;
this->uvs.z = ((float)w / (float)this->tex_size.x);
this->uvs.w = 1.0 - ((float)h / (float)this->tex_size.y);
// Texture Setup
C3D_TexInit(tex, (u16)this->tex_size.x, (u16)this->tex_size.y, GPU_RGBA8);
C3D_TexSetFilter(tex, GPU_NEAREST, GPU_NEAREST);
memset(tex->data, 0, tex->size);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int dst_pos = ((((y >> 3) * ((int)this->tex_size.x >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) |
((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) *
4;
int src_pos = (y * w + x) * 4;
memcpy(&((unsigned char *)tex->data)[dst_pos], &buf[src_pos], 4);
}
}
C3D_TexFlush(tex);
tex->border = 0x00000000;
C3D_TexSetWrap(tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
}
void Texture::LoadFile(const std::string& path) {
int w, h, c = 0;
unsigned char *image = stbi_load(path.c_str(), &w, &h, &c, 4);
if (image == nullptr) {
//_pdi_logger()->Write("Failed to Load Image: " + path);
return;
}
// Size/Fmt Check
if (w > 1024 || h > 1024) {
// Reason: Image to Large
//_pdi_logger()->Write("Image too Large!");
stbi_image_free(image);
return;
}
std::vector<unsigned char> wimg;
if (c == 3) {
//_pdi_logger()->Write("Convert Image to RGBA");
stbi_image_free(image);
image = stbi_load(path.c_str(), &w, &h, &c, 3);
wimg.resize(w * h * 4);
pdi::R24R32(wimg, std::vector<unsigned char>(image, image + (w * h * 3)),
w, h);
} else {
wimg.assign(&image[0], &image[(w * h * 4) - 1]);
stbi_image_free(image);
}
// Create C2D_Image
tex = new C3D_Tex;
MakeTex(wimg, w, h);
}
void Texture::LoadFromMemory(const std::vector<unsigned char>& data) {
int w, h, c = 0;
unsigned char *image = stbi_load_from_memory(data.data(), data.size(), &w, &h, &c, 4);
if (image == nullptr) {
//_pdi_logger()->Write("Failed to Load Image: " + path);
return;
}
// Size/Fmt Check
if (w > 1024 || h > 1024) {
// Reason: Image to Large
//_pdi_logger()->Write("Image too Large!");
stbi_image_free(image);
return;
}
std::vector<unsigned char> wimg;
if (c == 3) {
//_pdi_logger()->Write("Convert Image to RGBA");
stbi_image_free(image);
image = stbi_load_from_memory(data.data(), data.size(), &w, &h, &c, 3);
wimg.resize(w * h * 4);
pdi::R24R32(wimg, std::vector<unsigned char>(image, image + (w * h * 3)),
w, h);
} else {
wimg.assign(&image[0], &image[(w * h * 4) - 1]);
stbi_image_free(image);
}
// Create C2D_Image
tex = new C3D_Tex;
MakeTex(wimg, w, h);
}
void Texture::LoadPixels(const std::vector<unsigned char>& data, int w, int h) {
Delete();
if(w*h*4 != (int)data.size()) {
return;
}
if (w > 1024 || h > 1024) {
// Reason: Image to Large
//_pdi_logger()->Write("Image too Large!");
return;
}
tex = new C3D_Tex;
std::vector<unsigned char> wimg(data);
MakeTex(wimg, w, h);
}
void Texture::Delete() {
if(tex) {
delete tex;
tex = nullptr;
img_size = NVec2();
tex_size = NVec2();
uvs = NVec4();
}
}
}

138
source/ThemeEditor.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <filesystem>
#include <pd/Hid.hpp>
#include <pd/Message.hpp>
#include <pd/ThemeEditor.hpp>
#include <pd/UI7.hpp>
std::map<PDColor, std::string> color_names = {
{PDColor_Background, "Background"},
{PDColor_Button, "Button"},
{PDColor_ButtonActive, "ButtonActive"},
{PDColor_ButtonDisabled, "ButtonDisabled"},
{PDColor_ButtonHovered, "ButtonHovered"},
{PDColor_Checkmark, "Checkmark"},
{PDColor_FrameBg, "FrameBg"},
{PDColor_FrameBgHovered, "FrameBgHovered"},
{PDColor_Header, "Header"},
{PDColor_List0, "List0"},
{PDColor_List1, "List1"},
{PDColor_MessageBackground, "Message Background"},
{PDColor_Progressbar, "Progressbar"},
{PDColor_Selector, "Selector"},
{PDColor_SelectorFade, "SelectorFade"},
{PDColor_Text2, "Text Light"},
{PDColor_Text, "Text Dark"},
{PDColor_TextDisabled, "Text Disabled"},
};
Palladium::ThemeEditor::ThemeEditor() {
// Backup active Theme and create New one to edit
temp_theme = Palladium::ThemeActive();
edit_theme = Palladium::Theme::New();
edit_theme->CopyOther(temp_theme);
Palladium::ThemeSet(edit_theme);
}
Palladium::ThemeEditor::~ThemeEditor() {
// Set Back to Acrive Theme
Palladium::ThemeSet(temp_theme);
}
void Palladium::ThemeEditor::Draw() const {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> Theme Editor")) {
UI7::Label("Sample Text");
UI7::Checkbox("Checkbox", cm);
UI7::InputText("Input Text", inpt, "Input Text");
UI7::Button("Button");
UI7::Progressbar(0.5f);
UI7::ColorSelector("Color Selector",
edit_theme->GetTableRef()[PDColor_Progressbar]);
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Theme", NVec2(), UI7MenuFlags_Scrolling)) {
if (menu == 0) {
if (UI7::Button("Create New")) {
menu = 1;
edit_theme->Default();
} else if (UI7::Button("Edit Current")) {
menu = 1;
} else if (UI7::Button("Select Theme")) {
menu = 2;
theme_list.clear();
for (const auto& it : std::filesystem::directory_iterator(
Palladium::GetAppDirectory() + "/themes")) {
theme_list.push_back(it.path().filename().string());
}
}
} else if (menu == 1) {
if (UI7::Button("Go back")) {
edit_theme->CopyOther(temp_theme);
menu = 0;
} else if (UI7::Button("Save")) {
Palladium::AddOvl(std::make_unique<Ovl_Keyboard>(kbd_text, kbd_state,
"<name>.theme"));
}
for (auto& it : color_names) {
UI7::ColorSelector(it.second, edit_theme->GetTableRef()[it.first]);
}
} else if (menu == 2) {
if (UI7::Button("Go back")) {
menu = 0;
}
for (auto& it : theme_list) {
if (UI7::Button(it)) {
edit_theme->Load(Palladium::GetAppDirectory() + "/themes/" + it);
menu = 1;
}
UI7::SameLine();
if (UI7::Button("Make Current")) {
edit_theme->Load(Palladium::GetAppDirectory() + "/themes/" + it);
temp_theme->CopyOther(edit_theme);
menu = 0;
}
UI7::SameLine();
if (UI7::Button("Delete")) {
if (std::string(it) != "Palladium.theme") {
std::filesystem::remove(Palladium::GetAppDirectory() + "/themes/" +
it);
theme_list.clear();
for (const auto& it : std::filesystem::directory_iterator(
Palladium::GetAppDirectory() + "/themes")) {
theme_list.push_back(it.path().filename().string());
}
} else {
Palladium::PushMessage("ThemeEditor",
"Cannot Delete\nPalladium.theme!");
}
}
}
}
UI7::EndMenu();
}
}
void Palladium::ThemeEditor::Logic() {
if (kbd_state) {
if (kbd_state == PDKeyboardState_Confirm) {
auto path =
Palladium::GetAppDirectory() + "/themes/" + kbd_text + ".theme";
kbd_text = "";
if (std::filesystem::exists(path)) {
// Prompt Override
return;
}
edit_theme->Save(path);
}
kbd_state = PDKeyboardState_None;
}
if (Hid::IsEvent("cancel", Hid::Down)) {
if (menu == 0) {
Palladium::Scene::Back();
} else {
if (menu == 1) edit_theme->CopyOther(temp_theme);
menu = 0;
}
}
}

30
source/Time.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <memory>
#include <pd/Time.hpp>
#include <string>
std::string Palladium::FormatString(std::string fmt_str, ...) {
va_list ap;
char *fp = NULL;
va_start(ap, fmt_str);
vasprintf(&fp, fmt_str.c_str(), ap);
va_end(ap);
std::unique_ptr<char, decltype(free) *> formatted(fp, free);
return std::string(formatted.get());
}
std::string Palladium::GetTimeStr(void) {
time_t unixTime;
struct tm timeStruct;
time(&unixTime);
localtime_r(&unixTime, &timeStruct);
return FormatString("%04i-%02i-%02i_%02i-%02i-%02i",
timeStruct.tm_year + 1900, timeStruct.tm_mon + 1,
timeStruct.tm_mday, timeStruct.tm_hour, timeStruct.tm_min,
timeStruct.tm_sec);
}

31
source/Timer.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <pd/Timer.hpp>
// Ticks per MSEC
#define TPMS 268111.856
namespace Palladium {
Timer::Timer(bool autostart) {
if (autostart) is_running = true;
last = svcGetSystemTick();
current = last;
}
void Timer::Reset() {
last = svcGetSystemTick();
current = last;
}
void Timer::Tick() {
if (is_running) current = svcGetSystemTick();
}
void Timer::Pause() { is_running = false; }
void Timer::Resume() { is_running = true; }
bool Timer::Running() { return is_running; }
float Timer::Get() { return (float)((current - last) / TPMS); }
float Timer::GetLive() { return (float)((svcGetSystemTick() - last) / TPMS); }
} // namespace Palladium

1254
source/UI7.cpp Normal file

File diff suppressed because it is too large Load Diff

291
source/internal_db.cpp Normal file
View File

@ -0,0 +1,291 @@
#include <arpa/inet.h>
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pd/Error.hpp>
#include <pd/FileSystem.hpp>
#include <pd/external/json.hpp>
#include <pd/internal_db.hpp>
#include <pd/palladium.hpp>
/// Base ///
PDFlags pd_flags = PDFlags_Default;
static Palladium::Thread pdi_idb_server; // Protected
std::string pdi_app_name;
std::string pdi_config_path;
nlohmann::json pdi_config;
u8 pdi_console_model = 0;
u8 pdi_system_region = CFG_REGION_USA;
bool pdi_is_citra = false;
bool pdi_settings = false;
NVec2 pdi_hid_touch_pos;
C2D_TextBuf pdi_text_buffer;
C2D_Font pdi_base_font;
C2D_TextBuf pdi_d2_dimbuf;
bool pdi_is_ndsp = false;
bool pdi_running = false;
std::stack<std::unique_ptr<Palladium::Scene>> Palladium::Scene::scenes;
std::unique_ptr<Palladium::Scene> pdi_fade_scene;
std::vector<std::unique_ptr<Palladium::Ovl>> pdi_overlays;
unsigned int pdi_frames = 0;
u64 pdi_last_time = 0;
float pdi_framerate = 0.0f;
u32 pdi_mt_color = 0xaa000000;
u32 pdi_mt_txtcolor = 0xbbffffff;
bool pdi_mt_screen;
float pdi_mt_txtSize;
bool pdi_metrikd = false;
bool pdi_ftraced = false;
u64 pdi_delta_time;
u64 pdi_last_tm;
float pdi_dtm;
float pdi_time;
bool pdi_fadeout = false, pdi_fadein = false, pdi_fadeout2 = false,
pdi_fadein2 = false;
int pdi_fadealpha = 0;
int pdi_fadecolor = 0;
bool pdi_wait_fade = false;
bool pdi_fade_exit = false;
bool pdi_fade_scene_wait = false;
bool pdi_idb_running = false;
bool pdi_graphics_on = false;
float pd_draw2_tsm = 1.2f;
bool pdi_amdt = false;
void *pdi_soc_buf = nullptr;
bool pdi_is_am_init = false;
Palladium::Theme::Ref pdi_active_theme;
Palladium::LoggerBase::Ref pdi_logger;
bool pdi_lggrf = false;
Palladium::LoggerBase::Ref _pdi_logger() {
if (!pdi_logger) {
Palladium::Error(
"You're trying to use a Palladium Func without Init Palladium!");
}
return pdi_logger;
}
/// Global ///
// Outdated HidApi (HidV2Patched)
u32 d7_hDown;
u32 d7_hHeld;
u32 d7_hUp;
u32 d7_hRepeat; // Inofficial lol
touchPosition d7_touch;
// Modern Global Api
int pd_max_objects = C2D_DEFAULT_MAX_OBJECTS;
bool pdi_do_splash = false;
bool pdi_enable_scene_system = true;
bool pdi_debugging = false;
C3D_RenderTarget *pd_top;
C3D_RenderTarget *pd_top_right;
C3D_RenderTarget *pd_bottom;
Palladium::Net::Error pdi_soc_init() {
if (pdi_soc_buf != nullptr) {
return 0;
}
pdi_soc_buf = memalign(0x1000, 0x100000);
if (!pdi_soc_buf) {
return Palladium::Net::Error_Memory;
}
Result ret = socInit((u32 *)pdi_soc_buf, 0x100000);
if (R_FAILED(ret)) {
free(pdi_soc_buf);
return ((static_cast<Palladium::Net::Error>(ret) << 32) |
static_cast<Palladium::Net::Error>(Palladium::Net::Error_CtrStatus));
}
return 0;
}
void pdi_soc_deinit() {
if (pdi_soc_buf) {
socExit();
free(pdi_soc_buf);
}
pdi_soc_buf = nullptr;
}
class Logger {
public:
Logger() = default;
virtual ~Logger() = default;
static void log(const std::string &prefix, const std::string &message) {
std::cout << "[" << prefix << "]: " << message << std::endl;
}
};
#define pd_err(x) Logger::log("ERROR", x)
#define pd_wrn(x) Logger::log("WARNING", x)
class tcp_server {
public:
tcp_server(const std::string &ip, int port) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
pd_err("Unable to create socket!");
return;
}
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr = inet_addr(ip.c_str());
e = bind(sockfd, (struct sockaddr *)&server, sizeof(server));
if (e == -1) {
pd_err("Unable to bind!");
return;
}
e = listen(sockfd, 10);
if (e == 0) {
printf("[+]Listening...\n");
} else {
pd_err("Error in Binding");
return;
}
addr_size = sizeof(new_addr);
new_sock = accept(sockfd, (struct sockaddr *)&new_addr, &addr_size);
std::cout << "Connected" << std::endl;
}
~tcp_server() {
// Nothing here
}
bool reconnect() {
close(new_sock);
new_sock = accept(sockfd, (struct sockaddr *)&new_addr, &addr_size);
return true;
}
size_t snd(void *ptr, size_t size) { return send(new_sock, ptr, size, 0); }
size_t rec(void *ptr, size_t size) { return recv(new_sock, ptr, size, 0); }
private:
int e, sockfd, new_sock;
struct sockaddr_in server, new_addr;
socklen_t addr_size;
};
#define stupid(x) &x, sizeof(x)
#define pdi_reacttion(x) \
{ \
int code = x; \
server.snd(stupid(code)); \
}
struct pak32 {
pak32() {}
pak32(const std::string &n0, float n1, unsigned char n2, unsigned char n3,
bool n4, bool n5, bool n6, float n7, float n8, float n9, float n10,
unsigned int n11, unsigned int n12, unsigned int n13,
unsigned int n14) {
magic = 0x44772277;
for (int i = 0; i < 64; i++) app_name[i] = (char)0;
int l = n0.length();
if (l > 64) l = 64;
for (int i = 0; i < l; i++) app_name[i] = n0[i];
framerate = n1;
console_model = n2;
system_region = n3;
is_citra = n4;
ndsp_support = n5;
in_settings = n6;
deltatime = n7;
rtime = n8;
cpu = n9;
gpu = n10;
mem_alloc = n11;
mem_dalloc = n12;
mem_ialloc = n13;
tbs = n14;
}
uint32_t magic;
char app_name[64];
float framerate;
unsigned char console_model;
unsigned char system_region;
bool is_citra;
bool ndsp_support;
bool in_settings;
float deltatime;
float rtime;
float cpu;
float gpu;
unsigned int mem_alloc;
unsigned int mem_dalloc;
unsigned int mem_ialloc;
unsigned int tbs;
};
static bool pdi_idb_fp = false;
void KillIdbServer() {
pdi_idb_fp = true;
pdi_idb_server.join(100);
}
void ServerThread(Palladium::Parameter param) {
if (pdi_soc_buf == nullptr) return;
pdi_idb_running = true;
pdi_idb_fp = false;
atexit(KillIdbServer);
tcp_server server("0.0.0.0", 4727);
int cmd = 0;
while (!pdi_idb_fp) {
size_t r = server.rec(&cmd, sizeof(int));
if (r == 0) {
server.reconnect();
}
if (cmd == 1) {
pdi_reacttion(1);
auto pak = pak32(
pdi_app_name, pdi_framerate, pdi_console_model, pdi_system_region,
pdi_is_citra, pdi_is_ndsp, pdi_settings, pdi_dtm, pdi_time,
C3D_GetProcessingTime(), C3D_GetDrawingTime(),
Palladium::Memory::GetTotalAllocated(),
Palladium::Memory::GetTotalFreed(), Palladium::Memory::GetCurrent(),
C2D_TextBufGetNumGlyphs(pdi_text_buffer));
server.snd(stupid(pak));
} else if (cmd == 2) {
pdi_reacttion(2);
std::vector<Palladium::FileSystem::Entry> el =
Palladium::FileSystem::GetDirContent("sdmc:/");
size_t buf = el.size() * sizeof(Palladium::FileSystem::Entry);
server.snd(stupid(buf));
server.snd(el.data(), buf);
} else if (cmd == 3) {
pdi_reacttion(3);
Palladium::ExitApp();
pdi_idb_fp = true;
} else {
pdi_reacttion(1234);
}
}
pdi_idb_running = false;
}
namespace Palladium {
namespace IDB {
void Start() {
if (pdi_idb_running) return;
pdi_idb_server.initialize(ServerThread);
pdi_idb_server.start(true);
}
void Stop() {
if (!pdi_idb_running) return;
pdi_idb_fp = true;
KillIdbServer();
}
void Restart() {
Stop();
Start();
}
} // namespace IDB
} // namespace Palladium

118
source/lang.cpp Normal file
View File

@ -0,0 +1,118 @@
#include <3ds.h>
#include <filesystem>
#include <fstream>
#include <pd/lang.hpp>
static nlohmann::json appJson;
std::string Palladium::Lang::GetSys() {
u8 language = 1;
CFGU_GetSystemLanguage(&language);
switch (language) {
case 0:
return "jp"; // Japanese
break;
case 1:
return "en"; // English
break;
case 2:
return "fr"; // French
break;
case 3:
return "de"; // German
break;
case 4:
return "it"; // Italian
break;
case 5:
return "es"; // Spanish
break;
case 6:
return "zh-CN"; // Chinese (Simplified)
break;
case 7:
return "ko"; // Korean
break;
case 8:
return "nl"; // Dutch
break;
case 9:
return "pt"; // Portuguese
break;
case 10:
return "ru"; // Russian
break;
case 11:
return "zh-TW"; // Chinese (Traditional)
break;
default:
return "en"; // Fall back to English if missing
break;
}
}
std::string Palladium::Lang::Get(const std::string &key) {
if (!appJson.contains("keys")) return "ERR-01";
nlohmann::json js = appJson["keys"];
if (!js.contains(key)) return key;
return js.at(key).get<std::string>();
}
void Palladium::Lang::Load(const std::string &lang) {
std::fstream values;
if (std::filesystem::exists("romfs:/lang/" + lang + "/app.json")) {
values.open("romfs:/lang/" + lang + "/app.json", std::ios::in);
if (values.is_open()) {
appJson = nlohmann::json::parse(values);
}
values.close();
if (appJson.is_discarded()) {
appJson = {};
}
return;
} else {
values.open("romfs:/lang/en/app.json", std::ios::in);
if (values.is_open()) {
appJson = nlohmann::json::parse(values);
}
values.close();
if (appJson.is_discarded()) {
appJson = {};
}
return;
}
}
std::string Palladium::Lang::GetName() {
if (!appJson.contains("info")) return "";
nlohmann::json js = appJson["info"];
if (!js.contains("name")) return "Unknown";
return js.at("name").get<std::string>();
}
std::string Palladium::Lang::GetAuthor() {
if (!appJson.contains("info")) return "";
nlohmann::json js = appJson["info"];
if (!js.contains("author")) return "Unknown";
return js.at("author").get<std::string>();
}
std::string Palladium::Lang::GetShortcut() {
if (!appJson.contains("info")) return "";
nlohmann::json js = appJson["info"];
if (!js.contains("shortcut")) return "Unknown";
return js.at("shortcut").get<std::string>();
}

34
source/li7.v.pica Normal file
View File

@ -0,0 +1,34 @@
; LI7 Shader
; Constants
.constf myconst(0.0, 1.0, 0.00392156862745, 0.0)
.alias ones myconst.yyyy ; Vector full of ones
; Uniforms
.fvec projection[4]
; Outputs
.out out_position position
.out out_color color
.out out_uv texcoord0
; Inputs
.alias in_xyz v0
.alias in_uvc v1
.alias in_col v2
.entry vmain
.proc vmain
mov r0.xyz, in_xyz.xyz
mov r0.w, ones
dp4 out_position.x, projection[0], r0
dp4 out_position.y, projection[1], r0
dp4 out_position.z, projection[2], r0
dp4 out_position.w, projection[3], r0
mov out_uv, in_uvc.xy
mul r1, myconst.zzzz, in_col
mov out_color, r1
end
.end

117
source/nimg.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <fstream>
#include <iostream>
#include <pd/nimg.hpp>
// Use an Npi simplifier cause I am lazy
#define reca_cc(x) reinterpret_cast<const char*>(x)
#define reca_c(x) reinterpret_cast<char*>(x)
#define pak32(q, w, e, r) \
((((q) & 0xff) << 0) | (((w) & 0xff) << 8) | (((e) & 0xff) << 16) | \
(((r) & 0xff) << 24))
// Stupid RLE Algorithm
void npi_compress(std::vector<unsigned char>& ret,
const std::vector<unsigned char>& in) {
unsigned char counter = 1;
unsigned char tmp = in[0];
for (size_t i = 1; i < in.size(); ++i) {
if (tmp == in[i]) {
counter++;
} else {
ret.push_back(counter);
ret.push_back(tmp);
counter = 1;
tmp = in[i];
}
if (counter == 255) {
ret.push_back(counter);
ret.push_back(tmp);
counter = 0;
}
}
if (counter > 0) {
ret.push_back(counter);
ret.push_back(tmp);
}
}
void npi_decompress(std::vector<unsigned char>& ret,
const std::vector<unsigned char>& in) {
// Size is sus
if ((in.size() % 2) != 0) return;
for (size_t i = 0; i < in.size(); i += 2) {
int count = in[i];
int value = in[i + 1];
for (int c = 0; c < count; c++) {
ret.push_back(value);
}
}
}
namespace Palladium {
nimg NIMG_Load(std::string path) {
nimg res;
std::ifstream fin(path, std::ios::in | std::ios::binary);
// Check magic
fin.read(reca_c(&res.magic), sizeof(uint32_t));
if (res.magic != NPI_NIMG_) {
std::cout << path << " is invalid!" << std::endl;
return res;
}
// Read Information
fin.read(reca_c(&res.width), sizeof(int));
fin.read(reca_c(&res.height), sizeof(int));
fin.read(reca_c(&res.format), sizeof(int));
fin.read(reca_c(&res.compression), sizeof(int));
// Read Pixeldata
if (res.compression == 1) {
std::vector<unsigned char> tb;
int pb_size = 0;
fin.read(reca_c(&pb_size), sizeof(int));
tb.resize(pb_size);
fin.read(reca_c(tb.data()), pb_size);
npi_decompress(res.pixel_buffer, tb);
} else {
int pb_size = 0;
fin.read(reca_c(&pb_size), sizeof(int));
res.pixel_buffer.resize(pb_size);
fin.read(reca_c(res.pixel_buffer.data()), pb_size);
}
// Close stream
fin.close();
// Return the loadet imaeg
return res;
}
nimg NIMG_LoadFromMem(unsigned char* buffer, size_t bf_size) { return nimg(); }
void NIMG_Save(nimg image, std::string path) {
std::ofstream fout(path);
// Write Magic
fout.write(reca_cc(&image.magic), sizeof(uint32_t));
// Write Information
fout.write(reca_cc(&image.width), sizeof(int));
fout.write(reca_cc(&image.height), sizeof(int));
fout.write(reca_cc(&image.format), sizeof(int));
fout.write(reca_cc(&image.compression), sizeof(int));
std::vector<unsigned char> test;
// Write pixels
if (image.compression == 1) {
std::vector<unsigned char> tb;
npi_compress(tb, image.pixel_buffer);
int buf_szs = tb.size();
fout.write(reca_cc(&buf_szs), sizeof(int)); // buf_size
fout.write(reca_cc(tb.data()),
tb.size()); // buffer
} else {
int buf_szs = image.pixel_buffer.size();
fout.write(reca_cc(&buf_szs), sizeof(int)); // buf_size
fout.write(reca_cc(image.pixel_buffer.data()),
image.pixel_buffer.size()); // buffer
}
// Close stream
fout.close();
}
} // namespace Palladium

901
source/palladium.cpp Normal file
View File

@ -0,0 +1,901 @@
#include <pd/Hid.hpp> // Integate HidApi
#include <pd/Message.hpp>
#include <pd/Overlays.hpp>
#include <pd/ThemeEditor.hpp>
#include <pd/UI7.hpp>
#include <pd/palladium.hpp>
#include <pd/LI7.hpp>
// Config 2
#include <pd/external/json.hpp>
#include <pd/internal_db.hpp>
// C++ includes
#include <filesystem>
#include <random>
Palladium::LoggerBase::Ref pdi_glogger;
extern Palladium::LoggerBase::Ref pdi_logger;
static void pdi_ExitHook() {
C2D_TextBufDelete(pdi_text_buffer);
C2D_TextBufDelete(pdi_d2_dimbuf);
romfsExit();
}
std::vector<std::string> string_to_lines(std::string input_str) {
std::vector<std::string> lines;
std::stringstream ss(input_str);
std::string line;
while (std::getline(ss, line)) {
lines.push_back(line);
}
return lines;
}
void Npifade() {
if (pdi_fadein) {
if (pdi_fadealpha < 255) {
if ((int)pdi_fadealpha + 3 > 255) {
pdi_fadealpha = 255;
} else {
pdi_fadealpha += 3;
}
} else {
pdi_fadein = false;
}
} else if (pdi_fadeout) {
if (pdi_fadealpha > 0) {
if ((int)pdi_fadealpha - 3 < 0) {
pdi_fadealpha = 0;
} else {
pdi_fadealpha -= 3;
}
} else {
pdi_fadeout = false;
}
} else {
if (pdi_wait_fade) pdi_wait_fade = false;
if (pdi_fade_exit) pdi_running = false;
if (pdi_fade_scene_wait) {
Palladium::Scene::scenes.push(std::move(pdi_fade_scene));
pdi_fade_scene_wait = false;
Palladium::FadeIn();
}
// No fade
}
/*if (pdi_fadein || pdi_fadeout) {
Palladium::R2::OnScreen(Palladium::R2Screen_Top);
Palladium::R2::AddRect(NVec2(0, 0), NVec2(400, 240),
((pdi_fadealpha << 24) | 0x00000000));
Palladium::R2::OnScreen(Palladium::R2Screen_Bottom);
Palladium::R2::AddRect(NVec2(0, 0), NVec2(320, 240),
((pdi_fadealpha << 24) | 0x00000000));
}*/
}
void pdi_init_input() {
Palladium::Hid::RegTouchCoords(pdi_hid_touch_pos);
Palladium::Hid::RegKeyDown(d7_hDown);
Palladium::Hid::RegKeyHeld(d7_hHeld);
Palladium::Hid::RegKeyUp(d7_hUp);
Palladium::Hid::RegKeyRepeat(d7_hRepeat);
Palladium::Hid::RegKeyEvent("touch", KEY_TOUCH);
// Default Buttons
Palladium::Hid::RegKeyEvent("confirm", KEY_A);
Palladium::Hid::RegKeyEvent("cancel", KEY_B);
Palladium::Hid::RegKeyEvent("spec2", KEY_X);
Palladium::Hid::RegKeyEvent("spec3", KEY_Y);
Palladium::Hid::RegKeyEvent("options", KEY_SELECT);
Palladium::Hid::RegKeyEvent("spec1", KEY_START);
// Dpad only
Palladium::Hid::RegKeyEvent("dright", KEY_DRIGHT);
Palladium::Hid::RegKeyEvent("dleft", KEY_DLEFT);
Palladium::Hid::RegKeyEvent("dup", KEY_DUP);
Palladium::Hid::RegKeyEvent("ddown", KEY_DDOWN);
// D + Cpad
Palladium::Hid::RegKeyEvent("right", KEY_RIGHT);
Palladium::Hid::RegKeyEvent("left", KEY_LEFT);
Palladium::Hid::RegKeyEvent("up", KEY_UP);
Palladium::Hid::RegKeyEvent("down", KEY_DOWN);
// Back keys
Palladium::Hid::RegKeyEvent("rt1", KEY_R);
Palladium::Hid::RegKeyEvent("lt1", KEY_L);
Palladium::Hid::RegKeyEvent("rt2", KEY_ZL);
Palladium::Hid::RegKeyEvent("lt2", KEY_ZR);
// Key by their names
Palladium::Hid::RegKeyEvent("A", KEY_A);
Palladium::Hid::RegKeyEvent("B", KEY_B);
Palladium::Hid::RegKeyEvent("X", KEY_X);
Palladium::Hid::RegKeyEvent("Y", KEY_Y);
Palladium::Hid::RegKeyEvent("L", KEY_L);
Palladium::Hid::RegKeyEvent("R", KEY_R);
Palladium::Hid::RegKeyEvent("ZR", KEY_ZR);
Palladium::Hid::RegKeyEvent("ZL", KEY_ZL);
Palladium::Hid::RegKeyEvent("START", KEY_START);
Palladium::Hid::RegKeyEvent("SELECT", KEY_SELECT);
Palladium::Hid::RegKeyEvent("DUP", KEY_DUP);
Palladium::Hid::RegKeyEvent("DDOWN", KEY_DDOWN);
Palladium::Hid::RegKeyEvent("DLEFT", KEY_DRIGHT);
Palladium::Hid::RegKeyEvent("DRIGHT", KEY_DLEFT);
}
void pdi_init_config() {
pdi_config_path = "sdmc:/Palladium/Apps/";
pdi_config_path += pdi_app_name;
std::filesystem::create_directories(pdi_config_path.c_str());
std::filesystem::create_directories("sdmc:/Palladium/Reports");
bool renew = false;
if (Palladium::FS::FileExist(pdi_config_path + "/config.rc7")) {
std::fstream cfg_ldr(pdi_config_path + "/config.rc7", std::ios::in);
cfg_ldr >> pdi_config;
cfg_ldr.close();
std::string version = pdi_config["info"]["version"].get<std::string>();
if (version != CFGVER) renew = true;
}
if (!Palladium::FS::FileExist(pdi_config_path + "/config.rc7") || renew) {
if (renew) {
std::fstream cfg_ldr(pdi_config_path + "/config.rc7", std::ios::in);
cfg_ldr >> pdi_config;
cfg_ldr.close();
}
pdi_config.clear();
pdi_config["info"]["version"] = CFGVER;
pdi_config["info"]["Palladiumver"] = PDVSTRING;
pdi_config["metrik-settings"]["show"] = false;
pdi_config["metrik-settings"]["Screen"] = true;
pdi_config["metrik-settings"]["Text"] = "#ffffffff";
pdi_config["metrik-settings"]["Bg"] = "#aa000000";
pdi_config["metrik-settings"]["Size"] = 0.7f;
pdi_config["internal_logger"]["nowritetxt"] = true;
std::fstream cfg_wrt(pdi_config_path + "/config.rc7", std::ios::out);
cfg_wrt << pdi_config.dump(4);
cfg_wrt.close();
}
std::fstream cfg_ldr(pdi_config_path + "/config.rc7", std::ios::in);
cfg_ldr >> pdi_config;
cfg_ldr.close();
pdi_metrikd = pdi_config["metrik-settings"]["show"].get<bool>();
pdi_mt_txtSize = pdi_config["metrik-settings"]["Size"].get<float>();
pdi_mt_screen = pdi_config["metrik-settings"]["Screen"].get<bool>();
pdi_lggrf = pdi_config["internal_logger"]["nowritetxt"].get<bool>();
if (pdi_metrikd)
Palladium::AddOvl(std::make_unique<Palladium::Ovl_Metrik>(
&pdi_metrikd, &pdi_mt_screen, &pdi_mt_color, &pdi_mt_txtcolor,
&pdi_mt_txtSize));
}
void pdi_init_theme() {
if (pdi_config_path == "") {
pdi_config_path = "sdmc:/Palladium/Apps/";
pdi_config_path += pdi_app_name;
}
std::string path = pdi_config_path + "/themes";
std::filesystem::create_directories(path.c_str());
bool renew = false;
if (Palladium::FS::FileExist(path + "/Palladium.theme")) {
std::fstream cfg_ldr(path + "/Palladium.theme", std::ios::in);
nlohmann::json js;
cfg_ldr >> js;
cfg_ldr.close();
std::string version = js["version"].get<std::string>();
if (version != THEMEVER) renew = true;
}
if (!Palladium::FS::FileExist(path + "/Palladium.theme") || renew) {
pdi_amdt = true;
Palladium::ThemeActive()->Save(path + "/Palladium.theme");
pdi_amdt = false;
}
}
Palladium::LoggerBase::Ref Palladium::Logger() {
if (!pdi_glogger) {
Palladium::Error("Logger Was Called before being Init!");
// return schould not be reached then
}
return pdi_glogger;
}
float Palladium::GetDeltaTime() { return (float)pdi_dtm; }
void Palladium::Init::NdspFirm() {
if (access("sdmc:/3ds/dspfirm.cdc", F_OK) != -1) {
ndspInit();
atexit(ndspExit);
pdi_is_ndsp = true;
} else {
Palladium::PushMessage(Palladium::Message(
"Palladium", "dspfirm.cdc not found!\nUnable to play sounds!"));
}
}
void Palladium::Scene::doDraw() {
Ftrace::ScopedTrace st("pd-core", f2s(Scene::doDraw));
if (!Palladium::Scene::scenes.empty()) Palladium::Scene::scenes.top()->Draw();
}
void Palladium::Scene::doLogic() {
Ftrace::ScopedTrace st("pd-core", f2s(Scene::doLogic));
if (!Palladium::Scene::scenes.empty()) Palladium::Scene::scenes.top()->Logic();
}
void Palladium::Scene::Load(std::unique_ptr<Scene> scene, bool fade) {
if (fade) {
Palladium::FadeOut();
pdi_fade_scene = std::move(scene);
pdi_fade_scene_wait = true;
} else
Scene::scenes.push(std::move(scene));
}
void Palladium::Scene::Back() {
if (Palladium::Scene::scenes.size() > 0) Palladium::Scene::scenes.pop();
}
void frameloop() {
pdi_frames++;
pdi_delta_time = osGetTime() - pdi_last_time;
if (pdi_delta_time >= 1000) {
pdi_framerate = pdi_frames / (pdi_delta_time / 1000.0f) + 1;
pdi_frames = 0;
pdi_last_time = osGetTime();
}
}
float getframerate() { return pdi_framerate; }
std::string Palladium::GetFramerate() {
return (std::to_string((int)pdi_framerate).substr(0, 2));
}
bool Palladium::MainLoop() {
Palladium::Ftrace::ScopedTrace st("pd-core", f2s(MainLoop));
if (!aptMainLoop()) return false;
// Deltatime
uint64_t currentTime = svcGetSystemTick();
pdi_dtm = ((float)(currentTime / (float)TICKS_PER_MSEC) -
(float)(pdi_last_tm / (float)TICKS_PER_MSEC)) /
1000.f;
pdi_time += pdi_dtm;
pdi_last_tm = currentTime;
hidScanInput();
d7_hDown = hidKeysDown();
d7_hUp = hidKeysUp();
d7_hHeld = hidKeysHeld();
// Inofficial
d7_hRepeat = hidKeysDownRepeat();
hidTouchRead(&d7_touch);
Hid::Update();
pdi_hid_touch_pos = NVec2(d7_touch.px, d7_touch.py);
Palladium::ClearTextBufs();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(pd_top, C2D_Color32(0, 0, 0, 0));
C2D_TargetClear(pd_bottom, C2D_Color32(0, 0, 0, 0));
frameloop();
if (pdi_enable_scene_system) {
Palladium::Scene::doDraw();
Palladium::Scene::doLogic();
}
return pdi_running;
}
void Palladium::ClearTextBufs(void) { C2D_TextBufClear(pdi_text_buffer); }
void Palladium::Init::Graphics() {
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init((size_t)pd_max_objects);
C2D_Prepare();
pd_top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
pd_top_right = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT);
pd_bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
pdi_text_buffer = C2D_TextBufNew(4096);
pdi_d2_dimbuf = C2D_TextBufNew(4096);
pdi_base_font = C2D_FontLoadSystem(CFG_REGION_USA);
R2::Init();
}
Result Palladium::Init::Main(std::string app_name) {
Palladium::Ftrace::ScopedTrace st("pd-core", f2s(Init::Main));
pdi_app_name = app_name;
pdi_logger = LoggerBase::New();
pdi_glogger = LoggerBase::New();
pdi_do_splash = (pd_flags & PDFlags_ShowSplash);
pdi_enable_scene_system = (pd_flags & PDFlags_SceneSystem);
pdi_enable_memtrack = (pd_flags & PDFlags_MemTrack);
gfxInitDefault();
atexit(gfxExit);
// Speedup
osSetSpeedupEnable(true);
// consoleInit(GFX_TOP, NULL);
cfguInit();
atexit(cfguExit);
CFGU_SecureInfoGetRegion(&pdi_system_region);
CFGU_GetSystemModel(&pdi_console_model);
aptInit();
atexit(aptExit);
romfsInit();
pdi_init_config();
_pdi_logger()->Init("Palladium", pdi_lggrf);
pdi_active_theme = Theme::New();
pdi_active_theme->Default();
auto ret = pdi_soc_init();
if (ret) {
pdi_logger->Write("Failed to Init Soc!");
Palladium::PushMessage("Palladium", "Failed to\nInit Soc!");
} else {
atexit(pdi_soc_deinit);
}
if (R_SUCCEEDED(amInit())) {
atexit(amExit);
pdi_is_am_init = true;
}
Hardware::Initialisize();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
atexit(C3D_Fini);
C2D_Init((size_t)pd_max_objects);
atexit(C2D_Fini);
atexit(pdi_ExitHook);
C2D_Prepare();
pd_top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
pd_top_right = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT);
pd_bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
pdi_text_buffer = C2D_TextBufNew(4096);
pdi_d2_dimbuf = C2D_TextBufNew(4096);
pdi_base_font = C2D_FontLoadSystem(CFG_REGION_USA);
R2::Init();
pdi_graphics_on = true;
pdi_last_tm = svcGetSystemTick();
pdi_init_input();
pdi_init_theme();
UI7::Init();
atexit(UI7::Deinit);
pdi_running = true;
return 0;
}
Result Palladium::Init::Minimal(std::string app_name) {
Palladium::Ftrace::ScopedTrace st("pd-core", f2s(Init::Minimal));
pdi_app_name = app_name;
pdi_logger = LoggerBase::New();
pdi_glogger = LoggerBase::New();
pdi_do_splash = (pd_flags & PDFlags_ShowSplash);
pdi_enable_scene_system = (pd_flags & PDFlags_SceneSystem);
pdi_enable_memtrack = (pd_flags & PDFlags_MemTrack);
gfxInitDefault();
atexit(gfxExit);
romfsInit();
pdi_init_config();
_pdi_logger()->Init("Palladium", pdi_lggrf);
pdi_active_theme = Theme::New();
pdi_active_theme->Default();
auto ret = pdi_soc_init();
if (ret) {
pdi_logger->Write("Failed to Init Soc!");
Palladium::PushMessage("Palladium", "Failed to\nInit Soc!");
} else {
atexit(pdi_soc_deinit);
}
if (R_SUCCEEDED(amInit())) {
atexit(amExit);
pdi_is_am_init = true;
}
Hardware::Initialisize();
osSetSpeedupEnable(true);
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
atexit(C3D_Fini);
C2D_Init((size_t)pd_max_objects);
atexit(C2D_Fini);
atexit(pdi_ExitHook);
C2D_Prepare();
pd_top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
pd_top_right = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT);
pd_bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
pdi_text_buffer = C2D_TextBufNew(4096);
pdi_d2_dimbuf = C2D_TextBufNew(4096);
pdi_base_font = C2D_FontLoadSystem(CFG_REGION_USA);
R2::Init();
pdi_graphics_on = true;
// Check if citra
s64 citracheck = 0;
svcGetSystemInfo(&citracheck, 0x20000, 0);
pdi_is_citra = citracheck ? true : false;
pdi_init_input();
pdi_init_theme();
UI7::Init();
atexit(UI7::Deinit);
pdi_running = true;
return 0;
}
Result Palladium::Init::Reload() {
pdi_graphics_on = false;
C2D_TextBufDelete(pdi_text_buffer);
C2D_Fini();
C3D_Fini();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init((size_t)pd_max_objects);
C2D_Prepare();
pd_top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
pd_top_right = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT);
pd_bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
pdi_text_buffer = C2D_TextBufNew(4096);
pdi_base_font = C2D_FontLoadSystem(CFG_REGION_USA);
R2::Init();
pdi_graphics_on = true;
return 0;
}
void Palladium::ExitApp() {
if (pdi_wait_fade) {
pdi_fade_exit = true;
} else
pdi_running = false;
}
int Palladium::GetRandomInt(int b, int e) {
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(b, e);
int r = distribution(generator);
return r;
}
bool Palladium::FS::FileExist(const std::string &path) {
return std::filesystem::exists(path) &&
std::filesystem::is_regular_file(path);
}
int Palladium::GetFps() { return (int)pdi_framerate; }
bool Palladium::IsNdspInit() { return pdi_is_ndsp; }
void OvlHandler() {
Palladium::Ftrace::ScopedTrace st("pd-core", f2s(OvlHandler));
for (size_t i = 0; i < pdi_overlays.size(); i++) {
pdi_overlays[i]->Draw();
pdi_overlays[i]->Logic();
if (pdi_overlays[i]->IsKilled())
pdi_overlays.erase(pdi_overlays.begin() + i);
}
}
void Palladium::FrameEnd() {
Ftrace::ScopedTrace st("pd-core", f2s(FrameEnd));
C3D_FrameBegin(2);
if (!pdi_enable_scene_system && pdi_settings) {
Palladium::Scene::doDraw();
Palladium::Scene::doLogic();
}
UI7::Update();
UI7::Debug();
Palladium::ProcessMessages();
OvlHandler();
Npifade();
R2::Process();
C3D_FrameEnd(0);
}
Palladium::RSettings::RSettings() {
// Palladium Settings is designed for
// System Font
R2::DefaultFont();
tmp_txt = R2::GetTextSize();
R2::DefaultTextSize();
Palladium::FadeIn();
std::fstream cfg_ldr(pdi_config_path + "/config.rc7", std::ios::in);
cfg_ldr >> pdi_config;
cfg_ldr.close();
pdi_settings = true;
statemtold = pdi_metrikd;
stateftold = pdi_ftraced;
}
Palladium::RSettings::~RSettings() { R2::SetTextSize(tmp_txt); }
std::vector<std::string> StrHelper(std::string input) {
std::string ss(input);
std::istringstream in(ss);
std::vector<std::string> test1;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(), std::back_inserter(test1));
return test1;
}
void Palladium::RSettings::Draw(void) const {
if (m_state == RSETTINGS) {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> Settings")) {
UI7::SetCursorPos(NVec2(395, 2));
UI7::Label(PDVSTRING, PDTextFlags_AlignRight);
UI7::RestoreCursor();
UI7::Label("Config Version: " + std::string(CFGVER));
UI7::Label("App: " + pdi_app_name);
UI7::Label("Palladium: " + std::string(PDVSTRING));
UI7::Label("Citra: " + std::string(pdi_is_citra ? "true" : "false"));
UI7::Label("Current: " + std::to_string(Palladium::Memory::GetCurrent()) +
"b");
UI7::Label("Delta: " + std::to_string(Palladium::GetDeltaTime()));
UI7::Label("Kbd test: " + kbd_test);
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!")) {
if (UI7::Button("FTrace")) {
shared_request[0x00000001] = RFTRACE;
}
if (UI7::Button("UI7")) {
shared_request[0x00000001] = RUI7;
}
if (UI7::Button("Overlays")) {
shared_request[0x00000001] = ROVERLAYS;
}
if (UI7::Button("IDB")) {
shared_request[0x00000001] = RIDB;
}
if (UI7::Button("ThemeEditor")) {
Palladium::LoadThemeEditor();
}
if (UI7::Button("Logs")) {
shared_request[0x00000001] = RLOGS;
}
UI7::SameLine();
UI7::Checkbox("No File", pdi_lggrf);
if (UI7::Button("Back")) {
shared_request[0x00000002] = 1U;
}
if (UI7::Button("Keyboard")) {
shared_request[0x00000003] = 1U;
}
UI7::EndMenu();
}
} else if (m_state == RIDB) {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> Debugger")) {
UI7::SetCursorPos(NVec2(395, 2));
UI7::Label(PDVSTRING, PDTextFlags_AlignRight);
UI7::RestoreCursor();
UI7::Label("Server Running: " +
std::string(pdi_idb_running ? "true" : "false"));
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!")) {
if (UI7::Button("Start Server")) {
Palladium::IDB::Start();
}
UI7::SameLine();
if (UI7::Button("Stop Server")) {
Palladium::IDB::Stop();
}
UI7::SameLine();
if (UI7::Button("Restart Server")) {
Palladium::IDB::Restart();
}
UI7::EndMenu();
}
} else if (m_state == RFTRACE) {
Palladium::R2::OnScreen(R2Screen_Top);
// Draw Top Screen Into Background DrawList
UI7::GetBackgroundList()->AddRectangle(NVec2(0, 0), NVec2(400, 240),
PDColor_Background);
UI7::GetBackgroundList()->AddRectangle(NVec2(0, 0), NVec2(400, 20),
PDColor_Header);
UI7::GetBackgroundList()->AddText(
NVec2(5, 2), "Palladium -> FTrace",
Palladium::ThemeActive()->AutoText(PDColor_Header));
UI7::GetBackgroundList()->AddText(
NVec2(395, 2), PDVSTRING,
Palladium::ThemeActive()->AutoText(PDColor_Header),
PDTextFlags_AlignRight);
UI7::GetBackgroundList()->AddRectangle(
NVec2(0, 220), NVec2(400, 20),
Palladium::ThemeActive()->Get(PDColor_Header));
UI7::GetBackgroundList()->AddText(
NVec2(5, 222),
"Traces: " + std::to_string(ftrace_index + 1) + "/" +
std::to_string(Palladium::Ftrace::pd_traces.size()),
Palladium::ThemeActive()->AutoText(PDColor_Header));
UI7::GetBackgroundList()->AddRectangle(NVec2(0, 20), NVec2(400, 20),
PDColor_TextDisabled);
UI7::GetBackgroundList()->AddText(
NVec2(5, 22),
"Function:", Palladium::ThemeActive()->AutoText(PDColor_TextDisabled));
UI7::GetBackgroundList()->AddText(
NVec2(395, 22),
"Time (ms):", Palladium::ThemeActive()->AutoText(PDColor_TextDisabled),
PDTextFlags_AlignRight);
// List Bg
for (int i = 0; i < 12; i++) {
if ((i % 2 == 0))
UI7::GetBackgroundList()->AddRectangle(NVec2(0, 40 + (i) * 15),
NVec2(400, 15), PDColor_List0);
else
UI7::GetBackgroundList()->AddRectangle(NVec2(0, 40 + (i) * 15),
NVec2(400, 15), PDColor_List1);
}
Palladium::Ftrace::Beg("PDft", "display_traces");
int start_index = ftrace_index < 11 ? 0 : ftrace_index - 11;
auto it = Palladium::Ftrace::pd_traces.begin();
std::advance(it, start_index);
int ix = start_index;
std::string _fkey__ = "0";
while (ix < (int)Palladium::Ftrace::pd_traces.size() &&
ix < start_index + 10 && it != Palladium::Ftrace::pd_traces.end()) {
if (ix == ftrace_index) {
_fkey__ = it->first;
UI7::GetBackgroundList()->AddRectangle(
NVec2(0, 40 + (ix - start_index) * 15), NVec2(400, 15),
PDColor_Selector);
}
auto clr = ix == ftrace_index
? PDColor_Selector
: (ix % 2 == 0 ? PDColor_List0 : PDColor_List1);
UI7::GetBackgroundList()->AddText(NVec2(5, 40 + (ix - start_index) * 15),
it->second.func_name,
Palladium::ThemeActive()->AutoText(clr));
UI7::GetBackgroundList()->AddText(
NVec2(395, 40 + (ix - start_index) * 15),
Palladium::MsTimeFmt(it->second.time_of),
Palladium::ThemeActive()->AutoText(clr), PDTextFlags_AlignRight);
++it;
++ix;
}
Palladium::Ftrace::End("PDft", "display_traces");
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!")) {
auto jt = Palladium::Ftrace::pd_traces.begin();
std::advance(jt, ftrace_index);
UI7::Label("Group: " + jt->second.group);
UI7::Label("Function: " + jt->second.func_name);
UI7::Checkbox("In Overlay", jt->second.is_ovl);
UI7::Label("Time: " + Palladium::MsTimeFmt(jt->second.time_of));
UI7::Label("Max: " + Palladium::MsTimeFmt(jt->second.time_ofm));
UI7::Label("TS: " + std::to_string(jt->second.time_start));
UI7::Label("TE: " + std::to_string(jt->second.time_end));
UI7::Label("SVC_Stk: " + std::to_string(svcGetSystemTick()));
UI7::EndMenu();
}
} else if (m_state == RUI7) {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> UI7")) {
UI7::SetCursorPos(NVec2(395, 2));
UI7::Label(PDVSTRING, PDTextFlags_AlignRight);
UI7::RestoreCursor();
UI7::Label("Time: " + std::to_string(UI7::GetTime()));
UI7::Label("Delta: " + std::to_string(UI7::GetDeltaTime() * 1000.f));
UI7::Label("Hid Down Touch: " +
std::to_string(Hid::IsEvent("touch", Hid::Down)));
UI7::Label("Hid Held Touch: " +
std::to_string(Hid::IsEvent("touch", Hid::Held)));
UI7::Label("Hid Up Touch: " +
std::to_string(Hid::IsEvent("touch", Hid::Up)));
UI7::Label("Touch Pos: " + std::to_string(Hid::GetTouchPosition().x) +
", " + std::to_string(Hid::GetTouchPosition().y));
UI7::Label(
"Touch Last Pos: " + std::to_string(Hid::GetLastTouchPosition().x) +
", " + std::to_string(Hid::GetLastTouchPosition().y));
UI7::Label(
"Touch Down Pos: " + std::to_string(Hid::GetTouchDownPosition().x) +
", " + std::to_string(Hid::GetTouchDownPosition().y));
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!", NVec2(),
UI7MenuFlags_Scrolling)) {
if (UI7::Button("Go back")) {
/// Request a state switch to state RSETTINGS
shared_request[0x00000001] = RSETTINGS;
}
UI7::Checkbox("Debug", UI7::IsDebugging());
UI7::Checkbox("ShowMenuInfo", UI7::DebugMenu());
UI7::EndMenu();
}
} else if (m_state == ROVERLAYS) {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> Overlays")) {
UI7::SetCursorPos(NVec2(395, 2));
UI7::Label(PDVSTRING, PDTextFlags_AlignRight);
UI7::RestoreCursor();
UI7::Label("Metrik Overlay: " + mtovlstate);
UI7::Label("Metrik Screen: " + mtscreenstate);
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!")) {
UI7::Label("Metrik:");
UI7::Checkbox("Enable Overlay", pdi_metrikd);
UI7::Checkbox("Bottom Screen", pdi_mt_screen);
UI7::Label("FTrace:");
UI7::Checkbox("Enable Overlay", pdi_ftraced);
UI7::SetCursorPos(NVec2(5, 215));
if (UI7::Button("Go back")) {
/// Request a state switch to state RSETTINGS
shared_request[0x00000001] = RSETTINGS;
}
UI7::EndMenu();
}
} else if (m_state == RLOGS) {
Palladium::R2::OnScreen(R2Screen_Top);
if (UI7::BeginMenu("Palladium -> Logs")) {
UI7::SetCursorPos(NVec2(395, 2));
UI7::Label(PDVSTRING, PDTextFlags_AlignRight);
UI7::RestoreCursor();
UI7::EndMenu();
}
Palladium::R2::OnScreen(R2Screen_Bottom);
if (UI7::BeginMenu("Press \uE001 to go back!", NVec2(),
UI7MenuFlags_Scrolling)) {
for (auto &it : pdi_logger->Lines()) UI7::Label(it, PDTextFlags_Wrap);
UI7::EndMenu();
}
}
}
void Palladium::RSettings::Logic() {
/// Requests
for (const auto &it : shared_request) {
if (it.first == 0x00000001) {
m_state = (RState)it.second;
} else if (it.first == 0x00000002) {
if (it.second) {
std::fstream cfg_wrt(pdi_config_path + "/config.rc7", std::ios::out);
pdi_config["metrik-settings"]["enableoverlay"] = pdi_metrikd;
pdi_config["metrik-settings"]["Screen"] = pdi_mt_screen;
pdi_config["internal_logger"]["nowritetxt"] = pdi_lggrf;
cfg_wrt << pdi_config.dump(4);
cfg_wrt.close();
pdi_settings = false;
Hid::Clear();
Palladium::Scene::Back();
// Instant break logic or it will crash
return;
}
} else if (it.first == 0x00000003) {
if (it.second)
Palladium::AddOvl(std::make_unique<Ovl_Keyboard>(kbd_test, kbd_state));
}
}
/// Clear if handled
shared_request.clear();
if (statemtold != pdi_metrikd && pdi_metrikd == true)
Palladium::AddOvl(std::make_unique<Ovl_Metrik>(
&pdi_metrikd, &pdi_mt_screen, &pdi_mt_color, &pdi_mt_txtcolor,
&pdi_mt_txtSize));
statemtold = pdi_metrikd;
if (stateftold != pdi_ftraced && pdi_ftraced == true)
Palladium::AddOvl(std::make_unique<Ovl_Ftrace>(&pdi_ftraced));
stateftold = pdi_ftraced;
if (m_state == RSETTINGS) {
if (d7_hDown & KEY_B) {
std::fstream cfg_wrt(pdi_config_path + "/config.rc7", std::ios::out);
pdi_config["metrik-settings"]["enableoverlay"] = pdi_metrikd;
pdi_config["metrik-settings"]["Screen"] = pdi_mt_screen;
pdi_config["internal_logger"]["nowritetxt"] = pdi_lggrf;
cfg_wrt << pdi_config.dump(4);
cfg_wrt.close();
pdi_settings = false;
Hid::Clear();
Palladium::Scene::Back();
}
}
if (m_state == RUI7) {
if (d7_hDown & KEY_B) {
m_state = RSETTINGS;
}
}
if (m_state == ROVERLAYS) {
mtovlstate = pdi_metrikd ? "true" : "false";
mtscreenstate = pdi_mt_screen ? "Bottom" : "Top";
if (d7_hDown & KEY_B) {
m_state = RSETTINGS;
}
}
if (m_state == RIDB || m_state == RLOGS) {
if (d7_hDown & KEY_B) {
m_state = RSETTINGS;
}
}
if (m_state == RFTRACE) {
if (d7_hDown & KEY_DOWN) {
if (ftrace_index < (int)Palladium::Ftrace::pd_traces.size() - 1)
ftrace_index++;
}
if (d7_hDown & KEY_UP) {
if (ftrace_index > 0) ftrace_index--;
}
if (d7_hDown & KEY_B) {
m_state = RSETTINGS;
}
}
}
void Palladium::LoadSettings() {
if (!pdi_settings)
Palladium::Scene::Load(std::make_unique<Palladium::RSettings>());
}
void Palladium::LoadThemeEditor() {
Palladium::Scene::Load(std::make_unique<Palladium::ThemeEditor>());
}
void Palladium::AddOvl(std::unique_ptr<Palladium::Ovl> overlay) {
pdi_overlays.push_back(std::move(overlay));
}
void Palladium::FadeOut() {
if (!pdi_wait_fade) {
pdi_fadein = true;
pdi_fadealpha = 0;
pdi_wait_fade = true;
}
}
void Palladium::FadeIn() {
if (!pdi_wait_fade) {
pdi_fadeout = true;
pdi_fadealpha = 255;
pdi_wait_fade = true;
}
}
void Palladium::FadeDisplay() { Npifade(); }
float Palladium::GetTime() { return pdi_time; }
std::string Palladium::GetAppDirectory() {
std::string dir = "sdmc:/Palladium/Apps/" + pdi_app_name;
if (!std::filesystem::is_directory(dir))
std::filesystem::create_directories(dir);
return dir;
}
std::string Palladium::GetDataDirectory() {
std::string dir = GetAppDirectory() + "/data";
if (!std::filesystem::is_directory(dir))
std::filesystem::create_directories(dir);
return dir;
}

6
source/stb.cpp Normal file
View File

@ -0,0 +1,6 @@
#define STB_IMAGE_IMPLEMENTATION
#include <pd/external/stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <pd/external/stb_image_write.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <pd/external/stb_truetype.h>

94
source/swr.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <pd/external/stb_image.h>
#include <pd/Color.hpp>
#include <pd/swr.hpp>
namespace Palladium {
swr::swr(int w, int h) { image = Palladium::nimg(w, h); }
swr::swr() { image = Palladium::nimg(1, 1); }
swr::~swr() {
// Do nothing
}
void swr::load_file(const std::string& path) {
int w, h, c;
uint8_t* dat = stbi_load(path.c_str(), &w, &h, &c, 4);
image = nimg(w, h);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int pos = (y * w + x) * 4;
image.pixel_buffer[pos + 0] = dat[pos + 0];
image.pixel_buffer[pos + 1] = dat[pos + 1];
image.pixel_buffer[pos + 2] = dat[pos + 2];
image.pixel_buffer[pos + 3] = dat[pos + 3];
}
}
stbi_image_free(dat);
}
void swr::load_nimg(const std::string& path) {
image = Palladium::NIMG_Load(path);
}
void swr::draw_pixel(int x, int y, unsigned int color) {
if (x > image.width || x < 0 || y > image.height || y < 0) return;
Palladium::Color::RGBA splitter(color);
image.pixel_buffer[((y * image.width + x) * 4) + 0] = splitter.m_r;
image.pixel_buffer[((y * image.width + x) * 4) + 1] = splitter.m_g;
image.pixel_buffer[((y * image.width + x) * 4) + 2] = splitter.m_b;
image.pixel_buffer[((y * image.width + x) * 4) + 3] = splitter.m_a;
}
void swr::draw_rect(int x, int y, int w, int h, unsigned int color, int t) {
draw_line(x, y, x + w, y, color, t);
draw_line(x, y, x, y + h, color, t);
draw_line(x, y + h, x + w, y + h, color, t);
draw_line(x + w, y + h, x + w, y + h, color, t);
}
void swr::draw_rect_solid(int x, int y, int w, int h, unsigned int color) {
for (int ix = x; ix < x + w; ix++) {
for (int iy = y; iy < y + h; iy++) {
draw_pixel(ix, iy, color);
}
}
}
void swr::draw_line(int x1, int y1, int x2, int y2, unsigned int color, int t) {
for (int ix = x1; ix < x2 * t; ix++) {
for (int iy = y1; iy < y2 * t; iy++) {
draw_pixel(ix, iy, color);
}
}
}
void swr::flip(bool h, bool v) {
const nimg _bak = image;
if (h) {
for (int x = 0; x < image.width; x++) {
for (int y = 0; y < image.height; y++) {
int src = (y * image.width + x) * 4;
int dst = ((image.height - 1 - y) * image.width + x) * 4;
image.pixel_buffer[src + 0] = _bak.pixel_buffer[dst + 3];
image.pixel_buffer[src + 1] = _bak.pixel_buffer[dst + 2];
image.pixel_buffer[src + 2] = _bak.pixel_buffer[dst + 1];
image.pixel_buffer[src + 3] = _bak.pixel_buffer[dst + 0];
}
}
}
if (v) {
for (int x = 0; x < image.width; x++) {
for (int y = 0; y < image.height; y++) {
int src = (y * image.width + x) * 4;
int dst = (y * image.width + (image.width - 1 - x)) * 4;
image.pixel_buffer[src + 0] = _bak.pixel_buffer[dst + 3];
image.pixel_buffer[src + 1] = _bak.pixel_buffer[dst + 2];
image.pixel_buffer[src + 2] = _bak.pixel_buffer[dst + 1];
image.pixel_buffer[src + 3] = _bak.pixel_buffer[dst + 0];
}
}
}
}
} // namespace Palladium

77
source/thread.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <pd/thread.hpp>
namespace Palladium {
Thread::Thread() : m_started(false), m_running(false) { /* do nothing */
}
Thread::Thread(std::function<void(Palladium::Parameter)> t_function,
Palladium::Parameter t_parameter, bool t_autostart,
bool t_detached, unsigned long long int t_stackSize)
: m_started(false), m_running(false) {
initialize(t_function, t_parameter, t_autostart, t_detached, t_stackSize);
}
Thread::~Thread() {
join();
if (m_started) threadFree(m_thread);
}
void Thread::initialize(std::function<void(Palladium::Parameter)> t_function,
Palladium::Parameter t_parameter, bool t_autostart,
bool t_detached, unsigned long long int t_stackSize) {
m_stackSize = t_stackSize;
m_data.m_parameter = t_parameter;
m_data.m_function = t_function;
m_data.m_running = &m_running;
if (t_autostart) {
start(t_detached);
}
}
void Thread::setStackSize(unsigned long long int t_stackSize) {
m_stackSize = t_stackSize;
}
void Thread::start(bool t_detached) {
if (!m_running) {
m_started = true;
m_running = true;
s32 prio;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
m_thread = threadCreate(threadFunction, &m_data, m_stackSize, prio + 1, -2,
t_detached);
}
}
void Thread::kill() {
threadDetach(m_thread);
m_running = false;
m_started = false;
}
void Thread::join(long long unsigned int t_timeout) {
if (m_running) {
threadJoin(m_thread, t_timeout);
threadFree(m_thread);
m_running = false;
m_started = false;
}
}
bool Thread::isRunning() { return m_running; }
void Thread::sleep() { svcSleepThread(0); }
void Thread::sleep(int t_milliseconds) {
svcSleepThread(1000000 * t_milliseconds);
}
// private methods
void Thread::threadFunction(void *arg) {
Palladium::Thread::ThreadData data =
*static_cast<Palladium::Thread::ThreadData *>(arg);
data.m_function(data.m_parameter);
*data.m_running = false;
}
} // namespace Palladium