# Rewrite 5
- Move Libraries Source into pd directory and give them all their own CMakeLists.txt - Partial rewrite core (color, autogenerated vec), lithium (now uses UNIQUE PTR for Commands), UI7 - Use MenuV2 as new standart in UI7 - Implementz ViewPort Pre alpha to UI7 - Add Line Drawing to DrawList (not Working) - Implement a Complete new drievrs API (static Drivers) - NO SUPPORT FOR SHARED LIBRARY BUILDS IN VERSION 5 YET - Add Tools to Autogenerate Headers and Stuff
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
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
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
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 {
|
||||
// The Solution of the biggest performance issue
|
||||
// A Simple Lookup table
|
||||
static const std::map<char, int> HEX_DEC = {
|
||||
{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
|
||||
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11},
|
||||
{'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}, {'A', 10}, {'B', 11},
|
||||
{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
|
||||
|
||||
PD_CORE_API Color& Color::Hex(const std::string& hex) {
|
||||
#ifdef PD_NO_SAFE_CODE
|
||||
/// Safetey check (not required if you programm well xd)
|
||||
if (hex.length() != 7 || hex.length() != 9 || hex.length() != 6 ||
|
||||
hex.length() != 8 || std::find_if(hex.begin(), hex.end(), [](char c) {
|
||||
return !std::isxdigit(c);
|
||||
}) != hex.end()) {
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
int offset = ((hex.length() == 7 || hex.length() == 9) ? 1 : 0);
|
||||
m_r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
if (hex.length() == 9) {
|
||||
m_a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
} else {
|
||||
m_a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
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)m_r;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_g;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_b;
|
||||
if (rgba) {
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_a;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
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/common.hpp>
|
||||
#include <pd/core/strings.hpp>
|
||||
|
||||
#ifndef PALLADIUM_VERSION
|
||||
#define PALLADIUM_VERSION "unknown"
|
||||
#endif
|
||||
#ifndef PALLADIUM_GIT_COMMIT
|
||||
#define PALLADIUM_GIT_COMMIT "unknown"
|
||||
#endif
|
||||
#ifndef PALLADIUM_GIT_BRANCH
|
||||
#define PALLADIUM_GIT_BRANCH "unknown"
|
||||
#endif
|
||||
|
||||
PD_CORE_API const std::string PD::LibInfo::CompiledWith() {
|
||||
return Strings::GetCompilerVersion();
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::CxxVersion() {
|
||||
return "CPP: " + std::to_string(__cplusplus);
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::BuildTime() {
|
||||
return __DATE__ " - " __TIME__;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Version() {
|
||||
return PALLADIUM_VERSION;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Commit() {
|
||||
return PALLADIUM_GIT_COMMIT;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Branch() {
|
||||
return PALLADIUM_GIT_BRANCH;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
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/hid_driver.hpp>
|
||||
|
||||
/// Reform of the RenderD7 095 Hid Api
|
||||
/// Using Custom Keybindings for future
|
||||
/// Porting of the library
|
||||
|
||||
namespace PD {
|
||||
PD_CORE_API bool Hid::IsEvent(Event e, Key keys) {
|
||||
return key_events[0][e] & keys;
|
||||
}
|
||||
|
||||
PD_CORE_API void Hid::SwappyTable() {
|
||||
auto tkd = key_events[1][Event_Down];
|
||||
auto tkh = key_events[1][Event_Held];
|
||||
auto tku = key_events[1][Event_Up];
|
||||
key_events[1][Event_Down] = key_events[0][Event_Down];
|
||||
key_events[1][Event_Held] = key_events[0][Event_Held];
|
||||
key_events[1][Event_Up] = key_events[0][Event_Up];
|
||||
key_events[0][Event_Down] = tkd;
|
||||
key_events[0][Event_Held] = tkh;
|
||||
key_events[0][Event_Up] = tku;
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
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>
|
||||
|
||||
PD_CORE_API void DecompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; i < cpy.size(); i += 3) {
|
||||
for (size_t j = 0; j < cpy[i + 2]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; (i + 4) < cpy.size(); i += 5) {
|
||||
for (size_t j = 0; j < cpy[i + 4]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
data.push_back(cpy[i + 2]);
|
||||
data.push_back(cpy[i + 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 1) < cpy.size()) {
|
||||
PD::u16 v = PD::u16(cpy[i]) | (PD::u16(cpy[i + 1]) << 8);
|
||||
size_t c = 1;
|
||||
while ((i + c * 2 + 1) < cpy.size() &&
|
||||
(PD::u16(cpy[i + c * 2]) | (PD::u16(cpy[i + c * 2 + 1])) << 8) ==
|
||||
v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 3) < cpy.size()) {
|
||||
PD::u32 v = PD::u32(cpy[i]) | (PD::u32(cpy[i + 1]) << 8) |
|
||||
(PD::u32(cpy[i + 2]) << 16) | (PD::u32(cpy[i + 3]) << 24);
|
||||
size_t c = 1;
|
||||
while ((i + c * 4 + 3) < cpy.size() &&
|
||||
(PD::u32(cpy[i + c * 4]) | (PD::u32(cpy[i + c * 4 + 1]) << 8) |
|
||||
(PD::u32(cpy[i + c * 4 + 2]) << 16) |
|
||||
(PD::u32(cpy[i + c * 4 + 3]) << 24)) == v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 16) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 24) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 4;
|
||||
}
|
||||
}
|
||||
|
||||
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 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 DecompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (!data.size()) {
|
||||
return;
|
||||
}
|
||||
u8 fmt = data[0];
|
||||
data.erase(data.begin());
|
||||
if (fmt == 0) {
|
||||
DecompressRLE(data);
|
||||
} else if (fmt == 1) {
|
||||
DecompressRLE16(data);
|
||||
} else if (fmt == 2) {
|
||||
DecompressRLE32(data);
|
||||
}
|
||||
/** unknown returns input data */
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (data.empty()) {
|
||||
/** No exceptions enabled :( */
|
||||
return;
|
||||
}
|
||||
std::vector<u8> _8 = data;
|
||||
std::vector<u8> _16 = data;
|
||||
std::vector<u8> _32 = data;
|
||||
CompressRLE(_8);
|
||||
CompressRLE16(_16);
|
||||
CompressRLE32(_32);
|
||||
if (_16.size() < _8.size() && _16.size() < _32.size()) {
|
||||
_16.insert(_16.begin(), 1);
|
||||
data = _16;
|
||||
return;
|
||||
} else if (_32.size() < _8.size()) {
|
||||
_32.insert(_32.begin(), 2);
|
||||
data = _32;
|
||||
return;
|
||||
}
|
||||
_8.insert(_8.begin(), 0);
|
||||
data = _8;
|
||||
}
|
||||
} // namespace IO
|
||||
} // namespace PD
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
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 void Mat4::Zeros() {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
m[i] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void Mat4::Ortho(float left, float right, float bottom, float top,
|
||||
float near, float far) {
|
||||
m[0] = 2.0f / (right - left);
|
||||
m[1] = 0.0f;
|
||||
m[2] = 0.0f;
|
||||
m[3] = -(right + left) / (right - left);
|
||||
|
||||
m[4] = 0.0f;
|
||||
m[5] = 2.0f / (top - bottom);
|
||||
m[6] = 0.0f;
|
||||
m[7] = -(top + bottom) / (top - bottom);
|
||||
|
||||
m[8] = 0.0f;
|
||||
m[9] = 0.0f;
|
||||
m[10] = -2.0f / (far - near);
|
||||
m[11] = -(far + near) / (far - near);
|
||||
|
||||
m[12] = 0.0f;
|
||||
m[13] = 0.0f;
|
||||
m[14] = 0.0f;
|
||||
m[15] = 1.0f;
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,166 +0,0 @@
|
||||
/*
|
||||
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
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
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/sys.hpp>
|
||||
|
||||
namespace PD::Sys {
|
||||
TraceMap pd_sys_tm;
|
||||
PD_CORE_API u64 GetTime() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
PD_CORE_API u64 GetNanoTime() {
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
PD_CORE_API TT::Res::Ref& GetTraceRef(const std::string& id) {
|
||||
// Auto Generate a New if doesnt exist
|
||||
if (pd_sys_tm.find(id) == pd_sys_tm.end()) {
|
||||
pd_sys_tm[id] = TT::Res::New();
|
||||
pd_sys_tm[id]->SetID(id);
|
||||
}
|
||||
return pd_sys_tm[id];
|
||||
}
|
||||
PD_CORE_API bool TraceExist(const std::string& id) {
|
||||
return pd_sys_tm.find(id) != pd_sys_tm.end();
|
||||
}
|
||||
PD_CORE_API TraceMap& GetTraceMap() { return pd_sys_tm; }
|
||||
} // namespace PD::Sys
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
PD_CORE_API Timer::Timer(bool autostart) {
|
||||
is_running = autostart;
|
||||
Reset();
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Reset() {
|
||||
start = Sys::GetTime();
|
||||
now = start;
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Update() {
|
||||
if (is_running) {
|
||||
now = Sys::GetTime();
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Pause() { is_running = false; }
|
||||
PD_CORE_API void Timer::Rseume() { is_running = true; }
|
||||
PD_CORE_API bool Timer::IsRunning() const { return is_running; }
|
||||
PD_CORE_API u64 Timer::Get() { return now - start; }
|
||||
PD_CORE_API double Timer::GetSeconds() { return double(Get()) / 1000.0; }
|
||||
} // namespace PD
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
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/sys.hpp>
|
||||
#include <pd/core/timetrace.hpp>
|
||||
|
||||
namespace PD::TT {
|
||||
PD_CORE_API void Beg(const std::string& id) {
|
||||
auto trace = Sys::GetTraceRef(id);
|
||||
trace->SetStart(PD::Sys::GetNanoTime());
|
||||
}
|
||||
|
||||
PD_CORE_API void End(const std::string& id) {
|
||||
auto trace = Sys::GetTraceRef(id);
|
||||
trace->SetEnd(PD::Sys::GetNanoTime());
|
||||
}
|
||||
} // namespace PD::TT
|
||||
Reference in New Issue
Block a user