Unfiy all sub projects back into 1 libpalladium

This commit is contained in:
2026-01-25 20:44:52 +01:00
parent d2806b2061
commit 337c016824
49 changed files with 3263 additions and 3321 deletions

38
source/core/bit_util.cpp Executable file
View File

@@ -0,0 +1,38 @@
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/bit_util.hpp>
namespace PD::BitUtil {
PD_CORE_API bool IsSingleBit(u32 v) { return v && !(v & (v - 1)); }
PD_CORE_API u32 GetPow2(u32 v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return (v >= 64 ? v : 64);
}
} // namespace PD::BitUtil

40
source/core/color.cpp Executable file
View File

@@ -0,0 +1,40 @@
/*
MIT License
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/color.hpp>
namespace PD {
PD_CORE_API std::string Color::Hex(bool rgba) const {
/** Need to int cast (so it is used as num and not char...) */
std::stringstream s;
s << "#";
s << std::hex << std::setw(2) << std::setfill('0') << (int)r;
s << std::hex << std::setw(2) << std::setfill('0') << (int)g;
s << std::hex << std::setw(2) << std::setfill('0') << (int)b;
if (rgba || a != 255) { // QoL change btw
s << std::hex << std::setw(2) << std::setfill('0') << (int)a;
}
return s.str();
}
} // namespace PD

97
source/core/io.cpp Executable file
View File

@@ -0,0 +1,97 @@
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/io.hpp>
namespace PD {
namespace IO {
PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path) {
std::ifstream iff(path, std::ios::binary);
if (!iff) {
return std::vector<u8>();
}
iff.seekg(0, std::ios::end);
size_t szs = iff.tellg();
iff.seekg(0, std::ios::beg);
std::vector<u8> res(szs, 0);
iff.read(reinterpret_cast<char*>(res.data()), res.size());
iff.close();
return res;
}
PD_CORE_API std::string LoadFile2Str(const std::string& path) {
std::ifstream iff(path, std::ios::binary);
if (!iff) {
return "";
}
std::string ret;
std::string line;
while (std::getline(iff, line)) {
ret += line;
}
iff.close();
return ret;
}
PD_CORE_API u32 HashMemory(const std::vector<u8>& data) {
u32 hash = 4477;
for (auto& it : data) {
hash = (hash * 33) + it;
}
return hash;
}
PD_CORE_API void DecompressRLE(std::vector<u8>& data) {
if ((data.size() % 2) != 0) {
return;
}
std::vector<u8> cpy = data;
data.clear();
for (size_t i = 0; i < cpy.size(); i += 2) {
data.insert(data.end(), cpy[i + 1], cpy[i]);
}
}
PD_CORE_API void CompressRLE(std::vector<u8>& data) {
if (data.empty()) {
/** No exceptions enabled :( */
return;
}
std::vector<u8> cpy = data;
data.clear();
/** 8-Bit RLE */
data.push_back(0);
size_t i = 0;
while (i < cpy.size()) {
u8 v = cpy[i];
u8 c = 1;
while (i + c < cpy.size() && cpy[i + c] == v && c < 255) {
c++; // c++ ...
}
data.push_back(v);
data.push_back(c);
i += c;
}
}
} // namespace IO
} // namespace PD

122
source/core/mat.cpp Executable file
View File

@@ -0,0 +1,122 @@
/*
MIT License
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/mat.hpp>
namespace PD {
PD_CORE_API Mat4 Mat4::RotateX(float a) {
float c = std::cos(a);
float s = std::sin(a);
Mat4 ret = Identity();
ret(1, 1) = c;
ret(1, 2) = -s;
ret(2, 1) = s;
ret(2, 2) = c;
return ret;
}
PD_CORE_API Mat4 Mat4::RotateY(float a) {
float c = std::cos(a);
float s = std::sin(a);
Mat4 ret = Identity();
ret(0, 0) = c;
ret(0, 2) = s;
ret(2, 0) = -s;
ret(2, 2) = c;
return ret;
}
PD_CORE_API Mat4 Mat4::RotateZ(float a) {
float c = std::cos(a);
float s = std::sin(a);
Mat4 ret = Identity();
ret(0, 0) = c;
ret(0, 1) = -s;
ret(1, 0) = s;
ret(1, 1) = c;
return ret;
}
PD_CORE_API Mat4 Mat4::Rotate(fvec3 axis, float a) {
float s = std::sin(a);
float c = std::cos(a);
float t = 1.f - c;
axis = axis.Normalize();
float x = axis.x;
float y = axis.y;
float z = axis.z;
Mat4 ret = Identity();
ret(0, 0) = t * x * x + c;
ret(0, 1) = t * x * y - z * s;
ret(0, 2) = t * x * z + y * s;
ret(1, 0) = t * x * y + z * s;
ret(1, 1) = t * y * y + c;
ret(1, 2) = t * y * z - x * s;
ret(2, 0) = t * x * z - y * s;
ret(2, 1) = t * y * z + x * s;
ret(2, 2) = t * z * z + c;
return ret;
}
PD_CORE_API Mat4 Mat4::Perspective(float fov, float aspect, float n, float f) {
float _fov = std::tan(fov / 2.f);
Mat4 ret;
ret(0, 0) = 1.f / (aspect * _fov);
ret(1, 1) = 1.f / _fov;
#ifdef __3DS__
ret(2, 3) = f * n / (n - f);
ret(2, 2) = -(-1.f) * n / (n - f);
#else
ret(2, 2) = -(f + n) / (f - n);
ret(2, 3) = -(2.f * f * n) / (f - n);
#endif
ret(3, 2) = -1.f;
ret(3, 3) = 0.0f;
return ret;
}
PD_CORE_API Mat4 Mat4::LookAt(const fvec3& pos, const fvec3& center,
const fvec3& up) {
auto f = fvec3(center - pos).Normalize();
auto s = f.Cross(up).Normalize();
auto u = s.Cross(f);
Mat4 ret = Identity();
ret(0, 0) = s.x;
ret(0, 1) = s.y;
ret(0, 2) = s.z;
ret(1, 0) = u.x;
ret(1, 1) = u.y;
ret(1, 2) = u.z;
ret(2, 0) = -f.x;
ret(2, 1) = -f.y;
ret(2, 2) = -f.z;
ret(0, 3) = -s.Dot(pos);
ret(1, 3) = -u.Dot(pos);
ret(2, 3) = f.Dot(pos);
return ret;
}
} // namespace PD

166
source/core/strings.cpp Executable file
View File

@@ -0,0 +1,166 @@
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/strings.hpp>
namespace PD::Strings {
PD_CORE_API bool StringEndsWith(const std::string& str,
const std::vector<std::string>& exts) {
// Changed order to not do an substr on empty string
if (str.empty()) {
return false;
} else if (str.substr(0, 2) == "._") {
return false;
}
// Use a more modern way here now
// to avoid strcasecmp
if (exts.size() != 0) {
for (const auto& ext : exts) {
if (str.substr(str.length() - ext.length()) == ext) {
return true;
}
}
}
return false;
}
PD_CORE_API std::wstring MakeWstring(const std::string& s) {
// Manually convert to wstring as they removed wstring_convert :(
std::wstring result;
size_t i = 0;
while (i < s.size()) {
uint8_t ch = static_cast<uint8_t>(s[i]);
if (ch < 0x80) { // 1-byte chsr
result += static_cast<wchar_t>(ch);
i++;
} else if ((ch >> 5) == 0b110) { // 2-byte char
if (i + 1 >= s.size()) {
return L""; // return empty if error
}
wchar_t wc = ((ch & 0x1F) << 6) | (s[i + 1] & 0x3F);
result += wc;
i += 2;
} else if ((ch >> 4) == 0b1110) { // 3-byte char
if (i + 2 >= s.size()) {
return L""; // return empty if error
}
wchar_t wc =
((ch & 0x0F) << 12) | ((s[i + 1] & 0x3F) << 6) | (s[i + 2] & 0x3F);
result += wc;
i += 3;
} else if ((ch >> 3) == 0b11110) { // 4-byte char
if (i + 3 >= s.size()) {
return L""; // return empty if error
}
uint32_t codepoint = ((ch & 0x07) << 18) | ((s[i + 1] & 0x3F) << 12) |
((s[i + 2] & 0x3F) << 6) | (s[i + 3] & 0x3F);
codepoint -= 0x10000;
result += static_cast<wchar_t>(0xD800 + (codepoint >> 10));
result += static_cast<wchar_t>(0xDC00 + (codepoint & 0x3FF));
i += 4;
} else {
return L"";
}
}
return result;
}
PD_CORE_API const std::string FormatNanos(unsigned long long nanos) {
// Based on some code of my minecraft plugins
if (nanos < 1000) {
return std::format("{}ns", nanos);
} else if (nanos < 1000000) {
unsigned long long micros = nanos / 1000;
return std::format("{}us {}ns", micros, nanos % 1000);
} else if (nanos < 1000000000) {
unsigned long long millis = nanos / 1000000;
return std::format("{}ms {}us", millis, (nanos % 1000000) / 1000);
} else if (nanos < 60000000000ULL) {
unsigned long long seconds = nanos / 1000000000;
return std::format("{}s {}ms", seconds, (nanos % 1000000000) / 1000000);
} else {
unsigned long long minutes = nanos / 60000000000ULL;
unsigned long long seconds = (nanos % 60000000000ULL) / 1000000000;
return std::format("{}m {}s", minutes, seconds);
}
return "";
}
PD_CORE_API const std::string FormatMillis(unsigned long long millis) {
// Original Code can be found in some of my mv plugins
if (millis < 1000) {
return std::format("{}ms", millis);
} else if (millis < 60000) {
unsigned long long seconds = millis / 1000;
return std::format("{}s {}ms", seconds, (millis % 1000));
} else {
unsigned long long minutes = millis / 60000;
unsigned long long seconds = (millis % 60000) / 1000;
return std::format("{}m {}s {}ms", minutes, seconds, (millis % 1000));
}
return "";
}
PD_CORE_API const std::string FormatBytes(unsigned long long bytes) {
static const std::vector<std::string> endings = {
"B", "KB", "MB", "GB", "TB", "Unk",
};
int i = 0;
double b = bytes;
while (b > 1024.0) {
i++;
b /= 1024;
}
if (i >= (int)endings.size()) {
i = (int)endings.size() - 1;
}
return std::format("{:.1f} {}", b, endings[i]);
}
PD_CORE_API const std::string GetFileName(const std::string& path,
const std::string& saperators) {
auto pos = path.find_last_of(saperators);
if (pos != path.npos) {
return path.substr(pos + 1);
}
// If No saperator was found return the entire path
return path;
}
PD_CORE_API const std::string PathRemoveExtension(const std::string& path) {
auto pos = path.find_last_of('.');
if (pos != path.npos) {
return path.substr(0, pos);
}
// If No saperator was found return the entire path
return path;
}
PD_CORE_API u32 FastHash(const std::string& s) {
u32 hash = 5381;
for (auto& it : s) {
hash = (hash * 33) + static_cast<u8>(it);
}
return hash;
}
} // namespace PD::Strings

50
source/core/timer.cpp Executable file
View File

@@ -0,0 +1,50 @@
/*
MIT License
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/timer.hpp>
#include <pd/drivers/drivers.hpp>
namespace PD {
PD_CORE_API Timer::Timer(bool autostart) {
pIsRunning = autostart;
Reset();
}
PD_CORE_API void Timer::Reset() {
pStart = OS::GetTime();
pNow = pStart;
}
PD_CORE_API void Timer::Update() {
if (pIsRunning) {
pNow = OS::GetTime();
}
}
PD_CORE_API void Timer::Pause() { pIsRunning = false; }
PD_CORE_API void Timer::Rseume() { pIsRunning = true; }
PD_CORE_API bool Timer::IsRunning() const { return pIsRunning; }
PD_CORE_API u64 Timer::Get() { return pNow - pStart; }
PD_CORE_API double Timer::GetSeconds() { return (double)Get() / 1000.0; }
} // namespace PD

37
source/core/timetrace.cpp Executable file
View File

@@ -0,0 +1,37 @@
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/timetrace.hpp>
#include <pd/drivers/drivers.hpp>
namespace PD::TT {
PD_CORE_API void Beg(const std::string& id) {
auto trace = OS::GetTraceRef(id);
trace->SetStart(PD::OS::GetNanoTime());
}
PD_CORE_API void End(const std::string& id) {
auto trace = OS::GetTraceRef(id);
trace->SetEnd(PD::OS::GetNanoTime());
}
} // namespace PD::TT