Initial Cross Platform Work
This commit is contained in:
@ -36,12 +36,12 @@ namespace BitUtil {
|
||||
* @param v 32 bit unsigned int
|
||||
* @return true if its a single bit number
|
||||
*/
|
||||
bool IsSingleBit(u32 v);
|
||||
PD_CORE_API bool IsSingleBit(u32 v);
|
||||
/**
|
||||
* Get the Next Power of two Number
|
||||
* @param v Current Number
|
||||
* @return Next Number thats a Pow of 2
|
||||
*/
|
||||
u32 GetPow2(u32 v);
|
||||
PD_CORE_API u32 GetPow2(u32 v);
|
||||
} // namespace BitUtil
|
||||
} // namespace PD
|
@ -36,7 +36,7 @@ namespace PD {
|
||||
* - Supports 32Bit input color
|
||||
* @note Safetey checks are disabled for maximum performance
|
||||
*/
|
||||
class Color {
|
||||
class PD_CORE_API Color {
|
||||
public:
|
||||
/**
|
||||
* Default Constructor (all variables are set to 0)
|
||||
|
@ -38,6 +38,17 @@ SOFTWARE.
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Platform API
|
||||
#include <pd/core/pd_p_api.hpp>
|
||||
|
||||
// Legacy Smart Pointer
|
||||
#define PD_SMART_CTOR(x) \
|
||||
using Ref = std::shared_ptr<x>; \
|
||||
template <typename... args> \
|
||||
static Ref New(args&&... cargs) { \
|
||||
return std::make_shared<x>(std::forward<args>(cargs)...); \
|
||||
}
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* SmartCtor (std::shared_ptr) Template class for Smart Pointers
|
||||
@ -90,31 +101,31 @@ namespace LibInfo {
|
||||
* Get the Compiler Name and Version the lib got Compiled with
|
||||
* @return Compiler Name / Version
|
||||
*/
|
||||
const std::string CompiledWith();
|
||||
PD_CORE_API const std::string CompiledWith();
|
||||
/**
|
||||
* Get the C++ Version used to compile the lib
|
||||
* @return C++ Version (__cplusplus)
|
||||
*/
|
||||
const std::string CxxVersion();
|
||||
PD_CORE_API const std::string CxxVersion();
|
||||
/**
|
||||
* Get the Buildtime of the Library
|
||||
* @return Build Time
|
||||
*/
|
||||
const std::string BuildTime();
|
||||
PD_CORE_API const std::string BuildTime();
|
||||
/**
|
||||
* Get the Library Version
|
||||
* @return Library Version String
|
||||
*/
|
||||
const std::string Version();
|
||||
PD_CORE_API const std::string Version();
|
||||
/**
|
||||
* Get the Git Commit the Lib got compiled in
|
||||
* @return Git Commit 7digit short hash
|
||||
*/
|
||||
const std::string Commit();
|
||||
PD_CORE_API const std::string Commit();
|
||||
/**
|
||||
* Get the Git Branch which was active when compiling the lib
|
||||
* @return Git Branch
|
||||
*/
|
||||
const std::string Branch();
|
||||
PD_CORE_API const std::string Branch();
|
||||
} // namespace LibInfo
|
||||
} // namespace PD
|
39
include/pd/core/core.hpp
Normal file
39
include/pd/core/core.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/bit_util.hpp>
|
||||
#include <pd/core/color.hpp>
|
||||
#include <pd/core/common.hpp>
|
||||
#include <pd/core/hid_driver.hpp>
|
||||
#include <pd/core/io.hpp>
|
||||
#include <pd/core/mat.hpp>
|
||||
#include <pd/core/sl/sl.hpp>
|
||||
#include <pd/core/strings.hpp>
|
||||
#include <pd/core/sys.hpp>
|
||||
#include <pd/core/timer.hpp>
|
||||
#include <pd/core/timetrace.hpp>
|
||||
#include <pd/core/tween.hpp>
|
||||
#include <pd/core/vec.hpp>
|
175
include/pd/core/hid_driver.hpp
Normal file
175
include/pd/core/hid_driver.hpp
Normal file
@ -0,0 +1,175 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
#include <pd/core/vec.hpp>
|
||||
|
||||
namespace PD {
|
||||
/** Input Driver Template class */
|
||||
class PD_CORE_API Hid : public SmartCtor<Hid> {
|
||||
public:
|
||||
/** Key [Controller] */
|
||||
enum Key : u32 {
|
||||
No = 0, ///< No Key
|
||||
A = 1 << 0, ///< A
|
||||
B = 1 << 1, ///< B
|
||||
X = 1 << 2, ///< X
|
||||
Y = 1 << 3, ///< Y
|
||||
Start = 1 << 4, ///< Start
|
||||
Select = 1 << 5, ///< Select
|
||||
L = 1 << 6, ///< L
|
||||
R = 1 << 7, ///< R
|
||||
DUp = 1 << 8, ///< Dpad Up
|
||||
DDown = 1 << 9, ///< Dpad down
|
||||
DLeft = 1 << 10, ///< Dpad left
|
||||
DRight = 1 << 11, ///< Dpad right
|
||||
CPUp = 1 << 12, ///< Cpad up
|
||||
CPDown = 1 << 13, ///< cpad down
|
||||
CPLeft = 1 << 14, ///< cpad left
|
||||
CPRight = 1 << 15, ///< Cpad right
|
||||
CSUp = 1 << 16, ///< Cstick up
|
||||
CSDown = 1 << 17, ///< cstick down
|
||||
CSLeft = 1 << 18, ///< cstick left
|
||||
CSRight = 1 << 19, ///< cstick right
|
||||
ZL = 1 << 20, ///< ZL
|
||||
ZR = 1 << 21, ///< ZR
|
||||
Touch = 1 << 22, ///< Touch
|
||||
Up = DUp | CPUp, ///< DPad or CPad Up
|
||||
Down = DDown | CPDown, ///< DPad or CPad Down
|
||||
Left = DLeft | CPLeft, ///< DPad or CPad Left
|
||||
Right = DRight | CPRight, ///< DPad or CPad Right
|
||||
};
|
||||
/** Event */
|
||||
enum Event {
|
||||
Event_Down, ///< Key Pressed
|
||||
Event_Held, ///< Key Held
|
||||
Event_Up, ///< Key released
|
||||
};
|
||||
Hid(const std::string& name = "NullBackend") : pName(name) {}
|
||||
~Hid() = default;
|
||||
|
||||
/**
|
||||
* Get TOuch Position
|
||||
* @return touch pos
|
||||
*/
|
||||
fvec2 TouchPos() const { return touch[0]; }
|
||||
/**
|
||||
* Get Last Touch Position (from last frame)
|
||||
* @return touch pos
|
||||
*/
|
||||
fvec2 TouchPosLast() const { return touch[1]; }
|
||||
|
||||
/**
|
||||
* Check for a Button Event
|
||||
* @param e Event Type
|
||||
* @param keys Keys to check for
|
||||
* @return if key(s) doing the requiested event
|
||||
*/
|
||||
bool IsEvent(Event e, Key keys);
|
||||
/**
|
||||
* Check for Key Press Event
|
||||
* @param keys set of keys
|
||||
* @return true if key is pressed
|
||||
*/
|
||||
bool IsDown(Key keys) const { return key_events[0].at(Event_Down) & keys; }
|
||||
/**
|
||||
* Check for Key Held Event
|
||||
* @param keys set of keys
|
||||
* @return true if key is held
|
||||
*/
|
||||
bool IsHeld(Key keys) const { return key_events[0].at(Event_Held) & keys; }
|
||||
/**
|
||||
* Check for Key Release Event
|
||||
* @param keys set of keys
|
||||
* @return true if key is released
|
||||
*/
|
||||
bool IsUp(Key keys) const { return key_events[0].at(Event_Up) & keys; }
|
||||
|
||||
/**
|
||||
* Sett all keyevents to 0
|
||||
*/
|
||||
void Clear() {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
key_events[i][Event_Down] = 0;
|
||||
key_events[i][Event_Up] = 0;
|
||||
key_events[i][Event_Held] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock input driver
|
||||
* @param v lock or not lock
|
||||
*/
|
||||
void Lock(bool v) {
|
||||
if (v != locked) {
|
||||
SwappyTable();
|
||||
}
|
||||
locked = v;
|
||||
}
|
||||
/**
|
||||
* Check if Driver is locked
|
||||
* @return true if locked
|
||||
*/
|
||||
bool Locked() const { return locked; }
|
||||
/**
|
||||
* Lock Input Driver
|
||||
*/
|
||||
void Lock() {
|
||||
if (!locked) {
|
||||
SwappyTable();
|
||||
}
|
||||
locked = true;
|
||||
}
|
||||
/**
|
||||
* Unlock Input Driver
|
||||
*/
|
||||
void Unlock() {
|
||||
if (locked) {
|
||||
SwappyTable();
|
||||
}
|
||||
locked = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template Update Function for a device specific driver
|
||||
*/
|
||||
virtual void Update() {}
|
||||
|
||||
/** Backend Identification Name */
|
||||
const std::string pName;
|
||||
|
||||
protected:
|
||||
/** Key binds map */
|
||||
std::unordered_map<u32, u32> binds;
|
||||
/** Function to swap around the Table */
|
||||
void SwappyTable();
|
||||
/** Using 2 Touch positions for current and last frame */
|
||||
fvec2 touch[2];
|
||||
/** locked state */
|
||||
bool locked = false;
|
||||
/** Key event tables */
|
||||
std::unordered_map<Event, u32> key_events[2];
|
||||
};
|
||||
} // namespace PD
|
@ -35,12 +35,12 @@ namespace IO {
|
||||
* @param path Path to the File
|
||||
* @return 8Bit FileBuffer
|
||||
*/
|
||||
std::vector<u8> LoadFile2Mem(const std::string& path);
|
||||
PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path);
|
||||
/**
|
||||
* Hash a 8Bit Memory Buffer
|
||||
* @param data 8Bit input Buffer
|
||||
* @return 32Bit Hash
|
||||
*/
|
||||
u32 HashMemory(const std::vector<u8>& data);
|
||||
PD_CORE_API u32 HashMemory(const std::vector<u8>& data);
|
||||
} // namespace IO
|
||||
} // namespace PD
|
@ -1,103 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
class Markdown {
|
||||
public:
|
||||
Markdown() = default;
|
||||
~Markdown() = default;
|
||||
|
||||
void Header(const std::string& hdr, int lvl = 2) {
|
||||
if (task != 0 || lvl < 1 || lvl > 10) {
|
||||
return;
|
||||
}
|
||||
/// Directly create the string with its amount of #
|
||||
std::string str(lvl, '#');
|
||||
str += " ";
|
||||
str += hdr;
|
||||
s << str << std::endl << std::endl;
|
||||
}
|
||||
|
||||
void BeginTable(const std::vector<std::string>& head) {
|
||||
if (task != 0) {
|
||||
return;
|
||||
}
|
||||
ctc = head.size();
|
||||
for (auto& it : head) {
|
||||
s << "| " << it << " ";
|
||||
}
|
||||
s << "|\n";
|
||||
for (int i = 0; i < ctc; i++) {
|
||||
s << "|---";
|
||||
}
|
||||
s << "|\n";
|
||||
task = 1;
|
||||
}
|
||||
|
||||
Markdown& TableAddEntry(const std::string& e) {
|
||||
if (task != 1) {
|
||||
return *this;
|
||||
}
|
||||
ctci++;
|
||||
s << "| " << e << " ";
|
||||
if (ctci == ctc) {
|
||||
s << "|\n";
|
||||
ctci = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void EndTable() {
|
||||
if (task != 1) {
|
||||
return;
|
||||
}
|
||||
s << std::endl;
|
||||
task = 0;
|
||||
}
|
||||
|
||||
void Write(const std::string& path) {
|
||||
std::ofstream f(path);
|
||||
if (!f) {
|
||||
return;
|
||||
}
|
||||
f << s.str();
|
||||
f.close();
|
||||
}
|
||||
|
||||
private:
|
||||
/// @brief Tasks
|
||||
/// 0 = free
|
||||
/// 1 = table
|
||||
/// 2 = text
|
||||
int task = 0;
|
||||
/// @brief Current Table Columns
|
||||
int ctc = 0;
|
||||
/// @brief Current Table Column Index
|
||||
int ctci = 0;
|
||||
std::stringstream s;
|
||||
};
|
||||
} // namespace PD
|
41
include/pd/core/mat.hpp
Normal file
41
include/pd/core/mat.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
class PD_CORE_API Mat4 {
|
||||
public:
|
||||
Mat4() { Zeros(); }
|
||||
~Mat4() = default;
|
||||
|
||||
void Zeros();
|
||||
void Ortho(float left, float right, float bottom, float top, float near,
|
||||
float far);
|
||||
|
||||
float m[16];
|
||||
};
|
||||
} // namespace PD
|
50
include/pd/core/pd_p_api.hpp
Normal file
50
include/pd/core/pd_p_api.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32 // Windows (MSVC Tested)
|
||||
#ifdef PD_CORE_BUILD_SHARED
|
||||
#define PD_CORE_API __declspec(dllexport)
|
||||
#else
|
||||
#define PD_CORE_API __declspec(dllimport)
|
||||
#endif
|
||||
#elif defined(__APPLE__) // macOS (untested yet)
|
||||
#ifdef PD_CORE_BUILD_SHARED
|
||||
#define PD_CORE_API __attribute__((visibility("default")))
|
||||
#else
|
||||
#define PD_CORE_API
|
||||
#endif
|
||||
#elif defined(__linux__) // Linux (untested yet)
|
||||
#ifdef PD_CORE_BUILD_SHARED
|
||||
#define PD_CORE_API __attribute__((visibility("default")))
|
||||
#else
|
||||
#define PD_CORE_API
|
||||
#endif
|
||||
#elif defined(__3DS__) // 3ds Specific
|
||||
// Only Static supported
|
||||
#define PD_CORE_API
|
||||
#else
|
||||
#define PD_CORE_API
|
||||
#endif
|
51
include/pd/core/sl/allocator.hpp
Normal file
51
include/pd/core/sl/allocator.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Custom Allocator for Custom Vec and probably other stuff in future
|
||||
*/
|
||||
template <typename T>
|
||||
class Allocator {
|
||||
public:
|
||||
Allocator() = default;
|
||||
~Allocator() = default;
|
||||
|
||||
virtual T* Allocate(size_t n) { return new T[n]; }
|
||||
virtual T* AllocateRaw(size_t n) {
|
||||
return reinterpret_cast<T*>(::operator new(sizeof(T) * n));
|
||||
}
|
||||
virtual void DeallocateRaw(T* ptr) { operator delete(ptr); }
|
||||
virtual void Deallocate(T* ptr) { delete[] ptr; }
|
||||
template <typename... Args>
|
||||
void Construct(T* ptr, Args&&... args) {
|
||||
new (ptr) T(std::forward<Args>(args)...);
|
||||
}
|
||||
void Destroy(T* ptr) { ptr->~T(); }
|
||||
};
|
||||
} // namespace PD
|
71
include/pd/core/sl/hashmap.hpp
Normal file
71
include/pd/core/sl/hashmap.hpp
Normal file
@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/sl/list.hpp>
|
||||
#include <pd/core/sl/pair.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename K, typename V, size_t bucket_count = 10>
|
||||
class HashMap {
|
||||
public:
|
||||
HashMap() {}
|
||||
~HashMap() {}
|
||||
|
||||
void Insert(const K& k, const V& v) {
|
||||
size_t idx = Hash(k);
|
||||
auto& bukket = pBuckets[idx];
|
||||
for (auto& it : bukket) {
|
||||
if (it.First == k) {
|
||||
it.Second = v;
|
||||
return;
|
||||
}
|
||||
}
|
||||
bukket.PushBack(Pair(k, v));
|
||||
}
|
||||
|
||||
bool Contains(const K& k) const {
|
||||
size_t idx = Hash(k);
|
||||
auto& bukket = pBuckets[idx];
|
||||
|
||||
for (auto& it : bukket) {
|
||||
if (it.First == k) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
for (size_t i = 0; i < bucket_count; i++) {
|
||||
pBuckets[i].Clear();
|
||||
}
|
||||
}
|
||||
|
||||
size_t Hash(const K& k) const { return std::hash<K>{}(k) % bucket_count; }
|
||||
PD::List<PD::Pair<K, V>> pBuckets[bucket_count];
|
||||
};
|
||||
} // namespace PD
|
210
include/pd/core/sl/list.hpp
Normal file
210
include/pd/core/sl/list.hpp
Normal file
@ -0,0 +1,210 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
List() {}
|
||||
~List() {}
|
||||
|
||||
struct Node {
|
||||
Node(const T& v) : Data(v) {}
|
||||
T Data;
|
||||
Node* Prev = nullptr;
|
||||
Node* Next = nullptr;
|
||||
};
|
||||
|
||||
class Iterator {
|
||||
public:
|
||||
Iterator(Node* n) : pNode(n) {}
|
||||
T& operator*() { return pNode->Data; }
|
||||
Iterator& operator++() {
|
||||
pNode = pNode->Next;
|
||||
return *this;
|
||||
}
|
||||
bool operator!=(const Iterator& o) const { return pNode != o.pNode; }
|
||||
|
||||
Node* pNode = nullptr;
|
||||
};
|
||||
|
||||
void PushFront(const T& val) {
|
||||
Node* node = new Node(val);
|
||||
// node->Data = val;
|
||||
node->Prev = nullptr;
|
||||
node->Next = pHead;
|
||||
if (pHead) {
|
||||
pHead->Prev = node;
|
||||
}
|
||||
pHead = node;
|
||||
if (!pTail) {
|
||||
pTail = node;
|
||||
}
|
||||
pSize++;
|
||||
}
|
||||
|
||||
void PushBack(const T& val) {
|
||||
Node* node = new Node(val);
|
||||
// node->Data = val;
|
||||
node->Prev = pTail;
|
||||
node->Next = nullptr;
|
||||
if (pTail) {
|
||||
pTail->Next = node;
|
||||
}
|
||||
pTail = node;
|
||||
if (!pHead) {
|
||||
pHead = node;
|
||||
}
|
||||
pSize++;
|
||||
}
|
||||
|
||||
void PopFront() {
|
||||
if (!pHead) {
|
||||
return;
|
||||
}
|
||||
Node* t = pHead;
|
||||
pHead = pHead->Next;
|
||||
if (pHead) {
|
||||
pHead->Prev = nullptr;
|
||||
} else {
|
||||
pTail = nullptr;
|
||||
}
|
||||
delete t;
|
||||
pSize--;
|
||||
}
|
||||
|
||||
void PopBack() {
|
||||
if (!pTail) {
|
||||
return;
|
||||
}
|
||||
Node* t = pTail;
|
||||
pTail = pTail->Prev;
|
||||
if (pTail) {
|
||||
pTail->Next = nullptr;
|
||||
} else {
|
||||
pHead = nullptr;
|
||||
}
|
||||
delete t;
|
||||
pSize--;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
while (pHead) {
|
||||
PopFront();
|
||||
}
|
||||
}
|
||||
|
||||
void Remove(const T& v) {
|
||||
Node* s = pHead;
|
||||
while (s) {
|
||||
if (s->Data == v) {
|
||||
if (s->Prev) {
|
||||
s->Prev->Next = s->Next;
|
||||
} else {
|
||||
pHead = s->Next;
|
||||
}
|
||||
if (s->Next) {
|
||||
s->Next->Prev = s->Prev;
|
||||
} else {
|
||||
pTail = s->Prev;
|
||||
}
|
||||
delete s;
|
||||
pSize--;
|
||||
return;
|
||||
}
|
||||
s = s->Next;
|
||||
}
|
||||
}
|
||||
|
||||
void Reverse() {
|
||||
Node* cur = pHead;
|
||||
while (cur) {
|
||||
Node* temp = cur->Prev;
|
||||
cur->Prev = cur->Next;
|
||||
cur->Next = temp;
|
||||
cur = cur->Prev;
|
||||
}
|
||||
Node* temp = pHead;
|
||||
pHead = pTail;
|
||||
pTail = temp;
|
||||
}
|
||||
|
||||
T& Front() {
|
||||
if (pHead) {
|
||||
return pHead->Data;
|
||||
}
|
||||
// Need a List Empty Error Here (exceptions are disabled on 3ds)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const T& Front() const {
|
||||
if (pHead) {
|
||||
return pHead->Data;
|
||||
}
|
||||
// Need a List Empty Error Here (exceptions are disabled on 3ds)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
T& Back() {
|
||||
if (pTail) {
|
||||
return pTail->Data;
|
||||
}
|
||||
// Need a List Empty Error Here (exceptions are disabled on 3ds)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const T& Back() const {
|
||||
if (pTail) {
|
||||
return pTail->Data;
|
||||
}
|
||||
// Need a List Empty Error Here (exceptions are disabled on 3ds)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
size_t Size() const { return pSize; }
|
||||
|
||||
Iterator begin() { return Iterator(pHead); }
|
||||
Iterator end() { return Iterator(nullptr); }
|
||||
|
||||
private:
|
||||
Node* Find(const T& v) const {
|
||||
Node* t = pHead;
|
||||
while (t) {
|
||||
if (t->Data == v) {
|
||||
return t;
|
||||
}
|
||||
t = t->Next;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Node* pHead = nullptr;
|
||||
Node* pTail = nullptr;
|
||||
size_t pSize = 0;
|
||||
};
|
||||
} // namespace PD
|
41
include/pd/core/sl/pair.hpp
Normal file
41
include/pd/core/sl/pair.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
template <typename T1, typename T2>
|
||||
class Pair {
|
||||
public:
|
||||
Pair() = default;
|
||||
Pair(const T1& f, const T2& s) : First(f), Second(s) {}
|
||||
Pair(T1&& f, T2&& s) : First(std::move(f)), Second(std::move(s)) {}
|
||||
~Pair() = default;
|
||||
|
||||
T1 First;
|
||||
T2 Second;
|
||||
};
|
||||
} // namespace PD
|
33
include/pd/core/sl/sl.hpp
Normal file
33
include/pd/core/sl/sl.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/sl/allocator.hpp>
|
||||
#include <pd/core/sl/hashmap.hpp>
|
||||
#include <pd/core/sl/list.hpp>
|
||||
#include <pd/core/sl/pair.hpp>
|
||||
#include <pd/core/sl/stack.hpp>
|
||||
#include <pd/core/sl/tools.hpp>
|
||||
#include <pd/core/sl/vector.hpp>
|
70
include/pd/core/sl/stack.hpp
Normal file
70
include/pd/core/sl/stack.hpp
Normal file
@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/sl/vector.hpp>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Custom Stack class (caus std::stack ofsten lead to memory coruption)
|
||||
*/
|
||||
template <typename T, typename Alloc = Allocator<T>>
|
||||
class Stack {
|
||||
public:
|
||||
Stack() = default;
|
||||
explicit Stack(size_t cap) : pVec(cap) {}
|
||||
|
||||
void Push(const T& val) { pVec.Add(val); }
|
||||
|
||||
void Pop() {
|
||||
if (pVec.Size() == 0) {
|
||||
exit(1);
|
||||
}
|
||||
pVec.PopBack();
|
||||
}
|
||||
|
||||
T& Top() {
|
||||
if (pVec.Size() == 0) {
|
||||
exit(1);
|
||||
}
|
||||
return pVec[pVec.Size() - 1];
|
||||
}
|
||||
|
||||
const T& Top() const {
|
||||
if (pVec.Size() == 0) {
|
||||
exit(1);
|
||||
}
|
||||
return pVec[pVec.Size() - 1];
|
||||
}
|
||||
|
||||
bool IsEmpty() const { return pVec.Size() == 0; }
|
||||
size_t Size() const { return pVec.Size(); }
|
||||
void Clear() { pVec.Clear(); }
|
||||
|
||||
private:
|
||||
Vec<T, Alloc> pVec;
|
||||
};
|
||||
} // namespace PD
|
43
include/pd/core/sl/tools.hpp
Normal file
43
include/pd/core/sl/tools.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Function to get Arraysize for any type using modern c++ to
|
||||
* get the size at compiletime instead of runtime
|
||||
* @note this function only works for Arrays declared as
|
||||
* type arr[size] and not for pointer references.
|
||||
* This function will precalculate the size at compile time
|
||||
* while keeping the code clean to not hardcode arraysizes
|
||||
* into functions like std::fill_n
|
||||
*/
|
||||
template <typename T, size_t N>
|
||||
constexpr size_t ArraySize(T (&)[N]) noexcept {
|
||||
return N;
|
||||
}
|
||||
} // namespace PD
|
165
include/pd/core/sl/vector.hpp
Normal file
165
include/pd/core/sl/vector.hpp
Normal file
@ -0,0 +1,165 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
#include <pd/core/sl/allocator.hpp>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Open Access Vector class (Alternative to std::vector)
|
||||
*/
|
||||
template <typename T, typename Alloc = Allocator<T>>
|
||||
class Vec {
|
||||
public:
|
||||
Vec() {
|
||||
pData = pAllocator.Allocate(2);
|
||||
pCap = 2;
|
||||
Clear();
|
||||
pPos = 0;
|
||||
};
|
||||
Vec(const size_t& Size) {
|
||||
pData = pAllocator.Allocate(Size + 2);
|
||||
pCap = Size + 2;
|
||||
Clear();
|
||||
pPos = Size;
|
||||
}
|
||||
Vec(const size_t& Size, const T& v) {
|
||||
pData = pAllocator.Allocate(Size + 2);
|
||||
pCap = Size + 2;
|
||||
Clear();
|
||||
pPos = Size;
|
||||
std::fill_n(pData, Size, v);
|
||||
}
|
||||
Vec(const T* s, const T* e) {
|
||||
pData = pAllocator.Allocate(2);
|
||||
pCap = 2;
|
||||
Clear();
|
||||
pPos = 0;
|
||||
Resize(e - s);
|
||||
std::copy_n(s, e - s, pData);
|
||||
}
|
||||
Vec(const Vec& v) {
|
||||
pCap = v.pCap;
|
||||
pPos = v.pPos;
|
||||
pData = pAllocator.Allocate(pCap);
|
||||
for (size_t i = 0; i < pPos; i++) {
|
||||
pData[i] = v.pData[i];
|
||||
}
|
||||
}
|
||||
~Vec() { pAllocator.Deallocate(pData); }
|
||||
|
||||
void Add(const T& v) {
|
||||
if (pPos >= pCap) {
|
||||
Reserve(pCap * 2);
|
||||
}
|
||||
pData[pPos++] = v;
|
||||
}
|
||||
|
||||
void PopBack() {
|
||||
if (pPos == 0) return;
|
||||
pPos--;
|
||||
}
|
||||
|
||||
T Pop() {
|
||||
if (pPos == 0) {
|
||||
// Todo: LOG
|
||||
exit(1);
|
||||
}
|
||||
return pData[pPos--];
|
||||
}
|
||||
|
||||
T& At(const size_t& Idx) {
|
||||
if (Idx >= pPos) {
|
||||
// Log
|
||||
exit(1);
|
||||
}
|
||||
return pData[Idx];
|
||||
}
|
||||
|
||||
const T& At(const size_t& Idx) const {
|
||||
if (Idx >= pPos) {
|
||||
// Log
|
||||
exit(1);
|
||||
}
|
||||
return pData[Idx];
|
||||
}
|
||||
|
||||
T& operator[](const size_t& Idx) { return At(Idx); }
|
||||
const T& operator[](const size_t& Idx) const { return At(Idx); }
|
||||
void operator+=(T v) { Add(v); }
|
||||
|
||||
T* Begin() { return pData; }
|
||||
const T* Begin() const { return pData; }
|
||||
T* End() { return pData + pPos; }
|
||||
const T* End() const { return pData + pPos; }
|
||||
|
||||
// Support: `for(auto& it : Vec)` //
|
||||
T* begin() { return pData; }
|
||||
const T* begin() const { return pData; }
|
||||
T* end() { return pData + pPos; }
|
||||
const T* end() const { return pData + pPos; }
|
||||
|
||||
T* Data() { return pData; }
|
||||
T* Data() const { return pData; }
|
||||
size_t Size() const { return pPos; }
|
||||
size_t Capacity() const { return pCap; }
|
||||
void Clear() {
|
||||
// Avoid memset to support std::string for now
|
||||
// probably revert this decision based if it lacks performance
|
||||
// or make it a setting
|
||||
std::fill(pData, pData + pCap, T());
|
||||
pPos = 0;
|
||||
}
|
||||
|
||||
void Reserve(const size_t& Size) {
|
||||
if (Size <= pCap) return;
|
||||
T* tmp = pAllocator.Allocate(Size);
|
||||
std::fill(tmp, tmp + Size, T());
|
||||
std::copy(pData, pData + pCap, tmp);
|
||||
pAllocator.Deallocate(pData);
|
||||
pData = tmp;
|
||||
pCap = Size;
|
||||
}
|
||||
|
||||
void Resize(size_t Size) {
|
||||
if (Size < pPos) {
|
||||
pPos = Size;
|
||||
} else if (Size > pCap) {
|
||||
Reserve(Size);
|
||||
}
|
||||
std::fill(pData + pPos, pData + Size, T());
|
||||
pPos = Size;
|
||||
}
|
||||
|
||||
// Allocator
|
||||
Alloc pAllocator;
|
||||
// Data Reference Pointer
|
||||
T* pData;
|
||||
// Capacity
|
||||
size_t pCap;
|
||||
// Current Position (Size)
|
||||
size_t pPos;
|
||||
};
|
||||
} // namespace PD
|
@ -36,48 +36,47 @@ namespace Strings {
|
||||
* @param exts List of Extensions to check for
|
||||
* @return true if one of the extensions is found in the String
|
||||
*/
|
||||
bool StringEndsWith(const std::string& str,
|
||||
const std::vector<std::string>& exts);
|
||||
PD_CORE_API bool StringEndsWith(const std::string& str,
|
||||
const std::vector<std::string>& exts);
|
||||
/**
|
||||
* Function to Create a wstring of a string
|
||||
* @param s Input String to Convert
|
||||
* @return Result wstring
|
||||
* @note Currently using std::filesystem::path for this as wstring_convert
|
||||
* got removed in c++ 20
|
||||
* @note Returns Empty if it has an error
|
||||
*/
|
||||
std::wstring MakeWstring(const std::string& s);
|
||||
PD_CORE_API std::wstring MakeWstring(const std::string& s);
|
||||
/**
|
||||
* Generate a Formatted String by an Nanoseconds Input
|
||||
* @param nanos Nanoseconds Input
|
||||
* @return Result String
|
||||
*/
|
||||
const std::string FormatNanos(unsigned long long nanos);
|
||||
PD_CORE_API const std::string FormatNanos(unsigned long long nanos);
|
||||
/**
|
||||
* Generate a Formatted String by an Milliseconds Input
|
||||
* @param millis Milliseconds Input
|
||||
* @return Result String
|
||||
*/
|
||||
const std::string FormatMillis(unsigned long long millis);
|
||||
PD_CORE_API const std::string FormatMillis(unsigned long long millis);
|
||||
/**
|
||||
* Create a formatted String by an input bytes value
|
||||
* @param bytes value in bytes
|
||||
* @result Formatted String for example `2.5MB`
|
||||
*/
|
||||
const std::string FormatBytes(unsigned long long bytes);
|
||||
PD_CORE_API const std::string FormatBytes(unsigned long long bytes);
|
||||
/**
|
||||
* Extract the Filename out of a Path
|
||||
* @param path Path to extract from
|
||||
* @param saperators Path Split Chars
|
||||
* @return extracted filename
|
||||
*/
|
||||
const std::string GetFileName(const std::string& path,
|
||||
const std::string& saperators = "/\\");
|
||||
PD_CORE_API const std::string GetFileName(
|
||||
const std::string& path, const std::string& saperators = "/\\");
|
||||
/**
|
||||
* Remove Extension from a Path / Filename
|
||||
* @param path Input Path
|
||||
* @return Path without Extension
|
||||
*/
|
||||
const std::string PathRemoveExtension(const std::string& path);
|
||||
PD_CORE_API const std::string PathRemoveExtension(const std::string& path);
|
||||
/**
|
||||
* Function to Convert a Type to a hex value
|
||||
* @tparam T Type
|
||||
@ -95,7 +94,7 @@ inline const std::string ToHex(const T& v) {
|
||||
* @param s String to hash
|
||||
* @return 32Bit Hash
|
||||
*/
|
||||
u32 FastHash(const std::string& s);
|
||||
PD_CORE_API u32 FastHash(const std::string& s);
|
||||
/**
|
||||
* Function to Generate a Compiler Name and Version String
|
||||
* Based on their Macros
|
||||
@ -105,14 +104,14 @@ inline const std::string GetCompilerVersion() {
|
||||
/// As the function looks like this Project is meant to
|
||||
/// Be ported to other systems as well
|
||||
std::stringstream res;
|
||||
#ifdef __GNUC__
|
||||
res << "GCC: " << __GNUC__;
|
||||
res << "." << __GNUC_MINOR__ << ".";
|
||||
res << __GNUC_PATCHLEVEL__;
|
||||
#elif __clang__
|
||||
#ifdef __clang__ // Check clang first
|
||||
res << "Clang: " << __clang_major__ << ".";
|
||||
res << __clang_minor__ << ".";
|
||||
res << __clang_patchlevel__;
|
||||
#elif __GNUC__
|
||||
res << "GCC: " << __GNUC__;
|
||||
res << "." << __GNUC_MINOR__ << ".";
|
||||
res << __GNUC_PATCHLEVEL__;
|
||||
#elif _MSC_VER
|
||||
res << "MSVC: " << _MSC_VER;
|
||||
#else
|
||||
|
@ -39,28 +39,28 @@ using TraceMap = std::map<std::string, TT::Res::Ref>;
|
||||
* Get Current Time in Milliseconds
|
||||
* @return 64Bit value of millis
|
||||
*/
|
||||
u64 GetTime();
|
||||
PD_CORE_API u64 GetTime();
|
||||
/**
|
||||
* Get Current Time in Nanoseconds
|
||||
* @return 64Bit value of nanos
|
||||
*/
|
||||
u64 GetNanoTime();
|
||||
PD_CORE_API u64 GetNanoTime();
|
||||
/**
|
||||
* Get a TimeTrace Reference by its string ID
|
||||
* @param id trace name
|
||||
* @return Trace reference or nullptr if not found
|
||||
*/
|
||||
TT::Res::Ref& GetTraceRef(const std::string& id);
|
||||
PD_CORE_API TT::Res::Ref& GetTraceRef(const std::string& id);
|
||||
/**
|
||||
* Check if a Trace with the name exists
|
||||
* @param id tracename to search
|
||||
* @return true if exist
|
||||
*/
|
||||
bool TraceExist(const std::string& id);
|
||||
PD_CORE_API bool TraceExist(const std::string& id);
|
||||
/**
|
||||
* Get TraceMap Reference
|
||||
* @return edidable Reference to the TraceMap
|
||||
*/
|
||||
TraceMap& GetTraceMap();
|
||||
PD_CORE_API TraceMap& GetTraceMap();
|
||||
} // namespace Sys
|
||||
} // namespace PD
|
||||
|
@ -30,7 +30,7 @@ namespace PD {
|
||||
/**
|
||||
* Timer class
|
||||
*/
|
||||
class Timer : public SmartCtor<Timer> {
|
||||
class PD_CORE_API Timer : public SmartCtor<Timer> {
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
|
@ -211,12 +211,12 @@ class Res : public SmartCtor<Res> {
|
||||
* Begin a Trace
|
||||
* @param id Name of the Trace
|
||||
*/
|
||||
void Beg(const std::string &id);
|
||||
PD_CORE_API void Beg(const std::string &id);
|
||||
/**
|
||||
* End a Trace
|
||||
* @param id Name of the Trace
|
||||
*/
|
||||
void End(const std::string &id);
|
||||
PD_CORE_API void End(const std::string &id);
|
||||
/**
|
||||
* Collect Start end end of the trace by tracking
|
||||
* when the Scope object goes out of scope
|
||||
|
@ -23,7 +23,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/vec.hpp>
|
||||
#include <pd/core/common.hpp>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
|
@ -1,424 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// Why Creating this:
|
||||
// Cause using makes coding much better structured
|
||||
// and easy to use like in glsl or glm
|
||||
|
||||
#include <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
struct vec2 {
|
||||
// Init Funcs
|
||||
vec2() {
|
||||
v[0] = 0;
|
||||
v[1] = 0;
|
||||
}
|
||||
vec2(float x, float y) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
}
|
||||
vec2(const vec2 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
}
|
||||
vec2(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
}
|
||||
|
||||
// Operations
|
||||
// Add
|
||||
vec2 &operator+=(const vec2 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator+=(const float &i) {
|
||||
v[0] += i;
|
||||
v[1] += i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator+(const vec2 &i) const { return vec2(v[0] + i[0], v[1] + i[1]); }
|
||||
vec2 operator+(const float &i) const { return vec2(v[0] + i, v[1] + i); }
|
||||
|
||||
// Sub
|
||||
vec2 &operator-=(const vec2 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator-=(const float &i) {
|
||||
v[0] -= i;
|
||||
v[1] -= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator-(const vec2 &i) const { return vec2(v[0] - i[0], v[1] - i[1]); }
|
||||
vec2 operator-(const float &i) const { return vec2(v[0] - i, v[1] - i); }
|
||||
|
||||
// Mul
|
||||
vec2 &operator*=(const vec2 &i) {
|
||||
v[0] *= i[0];
|
||||
v[1] *= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator*=(const float &i) {
|
||||
v[0] *= i;
|
||||
v[1] *= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator*(const vec2 &i) const { return vec2(v[0] * i[0], v[1] * i[1]); }
|
||||
vec2 operator*(const float &i) const { return vec2(v[0] * i, v[1] * i); }
|
||||
|
||||
// Div
|
||||
vec2 &operator/=(const vec2 &i) {
|
||||
v[0] /= i[0];
|
||||
v[1] /= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator/=(const float &i) {
|
||||
v[0] /= i;
|
||||
v[1] /= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator/(const vec2 &i) const { return vec2(v[0] / i[0], v[1] / i[1]); }
|
||||
vec2 operator/(const float &i) const { return vec2(v[0] / i, v[1] / i); }
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec2 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1];
|
||||
}
|
||||
|
||||
bool operator!=(const vec2 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
vec2 operator-() const { return vec2(-v[0], -v[1]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const { return v[0] * v[0] + v[1] * v[1]; }
|
||||
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
// Internal Values
|
||||
float v[2];
|
||||
};
|
||||
|
||||
struct vec3 {
|
||||
// Init Funcs
|
||||
vec3() {
|
||||
v[0] = 0.f;
|
||||
v[1] = 0.f;
|
||||
v[2] = 0.f;
|
||||
}
|
||||
vec3(float x, float y, float z) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
v[2] = z;
|
||||
}
|
||||
vec3(const vec3 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
v[2] = i[2];
|
||||
}
|
||||
vec3(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
v[2] = i;
|
||||
}
|
||||
//// PD REWRITE ADDITIONAL CONTENT ////
|
||||
vec3(const vec2 &xy, float z) {
|
||||
v[0] = xy[0];
|
||||
v[1] = xy[1];
|
||||
v[2] = z;
|
||||
}
|
||||
vec3(float x, const vec2 &yz) {
|
||||
v[0] = x;
|
||||
v[1] = yz[0];
|
||||
v[2] = yz[1];
|
||||
}
|
||||
|
||||
// Operations
|
||||
// Add
|
||||
vec3 &operator+=(const vec3 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
v[2] += i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator+=(const float &i) {
|
||||
v[0] += i;
|
||||
v[1] += i;
|
||||
v[2] += i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator+(const vec3 &i) const {
|
||||
return vec3(v[0] + i[0], v[1] + i[1], v[2] + i[2]);
|
||||
}
|
||||
vec3 operator+(const float &i) const {
|
||||
return vec3(v[0] + i, v[1] + i, v[2] + i);
|
||||
}
|
||||
|
||||
// Sub
|
||||
vec3 &operator-=(const vec3 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
v[2] -= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator-=(const float &i) {
|
||||
v[0] -= i;
|
||||
v[1] -= i;
|
||||
v[2] -= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator-(const vec3 &i) const {
|
||||
return vec3(v[0] - i[0], v[1] - i[1], v[2] - i[2]);
|
||||
}
|
||||
vec3 operator-(const float &i) const {
|
||||
return vec3(v[0] - i, v[1] - i, v[2] - i);
|
||||
}
|
||||
|
||||
// Mul
|
||||
vec3 &operator*=(const vec3 &i) {
|
||||
v[0] *= i[0];
|
||||
v[1] *= i[1];
|
||||
v[2] *= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator*=(const float &i) {
|
||||
v[0] *= i;
|
||||
v[1] *= i;
|
||||
v[2] *= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator*(const vec3 &i) const {
|
||||
return vec3(v[0] * i[0], v[1] * i[1], v[2] * i[2]);
|
||||
}
|
||||
vec3 operator*(const float &i) const {
|
||||
return vec3(v[0] * i, v[1] * i, v[2] * i);
|
||||
}
|
||||
|
||||
// Div
|
||||
vec3 &operator/=(const vec3 &i) {
|
||||
v[0] /= i[0];
|
||||
v[1] /= i[1];
|
||||
v[2] /= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator/=(const float &i) {
|
||||
v[0] /= i;
|
||||
v[1] /= i;
|
||||
v[2] /= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator/(const vec3 &i) const {
|
||||
return vec3(v[0] / i[0], v[1] / i[1], v[2] / i[2]);
|
||||
}
|
||||
vec3 operator/(const float &i) const {
|
||||
return vec3(v[0] / i, v[1] / i, v[2] / i);
|
||||
}
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec3 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1] && v[2] == v[2];
|
||||
}
|
||||
|
||||
bool operator!=(const vec3 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
// Base
|
||||
vec3 operator-() const { return vec3(-v[0], -v[1], -v[2]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const { return v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; }
|
||||
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
float z() const { return v[2]; }
|
||||
float &z() { return v[2]; }
|
||||
|
||||
//// PALLADIUM REWRITE ADDITIONAL CONTENT ////
|
||||
vec2 xy() const { return vec2(v[0], v[1]); }
|
||||
vec2 yz() const { return vec2(v[1], v[2]); }
|
||||
|
||||
float v[3];
|
||||
};
|
||||
|
||||
struct vec4 {
|
||||
// Init Funcs
|
||||
vec4() {
|
||||
v[0] = 0.f;
|
||||
v[1] = 0.f;
|
||||
v[2] = 0.f;
|
||||
v[3] = 0.f;
|
||||
}
|
||||
vec4(float x, float y, float z, float w) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
v[2] = z;
|
||||
v[3] = w;
|
||||
}
|
||||
vec4(const vec4 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
v[2] = i[2];
|
||||
v[3] = i[3];
|
||||
}
|
||||
|
||||
vec4(const vec2 &i0, const vec2 &i1) {
|
||||
v[0] = i0[0];
|
||||
v[1] = i0[1];
|
||||
v[2] = i1[0];
|
||||
v[3] = i1[1];
|
||||
}
|
||||
vec4(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
v[2] = i;
|
||||
v[3] = i;
|
||||
}
|
||||
|
||||
vec4(const vec3 &xyz, float w) {
|
||||
v[0] = xyz[0];
|
||||
v[1] = xyz[1];
|
||||
v[2] = xyz[2];
|
||||
v[3] = w;
|
||||
}
|
||||
|
||||
vec4(float x, const vec3 &yzw) {
|
||||
v[0] = x;
|
||||
v[1] = yzw[1];
|
||||
v[2] = yzw[2];
|
||||
v[3] = yzw[3];
|
||||
}
|
||||
|
||||
// Operators
|
||||
// Add
|
||||
vec4 &operator+=(const vec4 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
v[2] += i[2];
|
||||
v[3] += i[3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec4 operator+(const vec4 &i) const {
|
||||
return vec4(v[0] + i[0], v[1] + i[1], v[2] + i[2], v[3] + i[3]);
|
||||
}
|
||||
|
||||
// Sub
|
||||
vec4 &operator-=(const vec4 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
v[2] -= i[2];
|
||||
v[3] -= i[3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec4 operator-(const vec4 &i) const {
|
||||
return vec4(v[0] - i[0], v[1] - i[1], v[2] - i[2], v[3] - i[3]);
|
||||
}
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec4 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1] && v[2] == in[2] && v[3] == in[3];
|
||||
}
|
||||
|
||||
bool operator!=(const vec4 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
// Base
|
||||
vec4 operator-() const { return vec4(-v[0], -v[1], -v[2], -v[3]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const {
|
||||
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
|
||||
}
|
||||
|
||||
// Vec Acess
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
float z() const { return v[2]; }
|
||||
float &z() { return v[2]; }
|
||||
float w() const { return v[3]; }
|
||||
float &w() { return v[3]; }
|
||||
vec2 xy() const { return vec2(v[0], v[1]); }
|
||||
vec2 zw() const { return vec2(v[2], v[3]); }
|
||||
vec3 xyz() const { return vec3(v[0], v[1], v[2]); }
|
||||
vec3 yzw() const { return vec3(v[1], v[2], v[3]); }
|
||||
// Quaternion Acess
|
||||
float r() const { return v[0]; }
|
||||
float &r() { return v[0]; }
|
||||
float k() const { return v[1]; }
|
||||
float &k() { return v[1]; }
|
||||
float j() const { return v[2]; }
|
||||
float &j() { return v[2]; }
|
||||
float i() const { return v[3]; }
|
||||
float &i() { return v[3]; }
|
||||
vec2 rk() const { return vec2(v[0], v[1]); }
|
||||
vec2 ji() const { return vec2(v[2], v[3]); }
|
||||
vec3 rkj() const { return vec3(v[0], v[1], v[2]); }
|
||||
vec3 kji() const { return vec3(v[1], v[2], v[3]); }
|
||||
// Internal Values
|
||||
float v[4];
|
||||
};
|
||||
} // namespace PD
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/vec2.hpp>
|
||||
#include <pd/core/vec3.hpp>
|
||||
#include <pd/core/vec4.hpp>
|
135
include/pd/core/vec2.hpp
Normal file
135
include/pd/core/vec2.hpp
Normal file
@ -0,0 +1,135 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename T>
|
||||
class vec2 {
|
||||
public:
|
||||
// SECTION: Constructors //
|
||||
|
||||
vec2() = default;
|
||||
vec2(T v) {
|
||||
x = v;
|
||||
y = v;
|
||||
}
|
||||
vec2(T x, T y) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
vec2(const vec2<T> &v) {
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
}
|
||||
|
||||
// SECTION: Operators //
|
||||
|
||||
// ADD //
|
||||
|
||||
vec2 &operator+=(T v) {
|
||||
x += v;
|
||||
y += v;
|
||||
return *this;
|
||||
}
|
||||
vec2 &operator+=(const vec2 &v) {
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
return *this;
|
||||
}
|
||||
vec2 operator+(T v) const { return vec2(x + v, y + v); }
|
||||
vec2 operator+(vec2 v) const { return vec2(x + v.x, y + v.y); }
|
||||
|
||||
// SUB //
|
||||
|
||||
vec2 &operator-=(T v) {
|
||||
x -= v;
|
||||
y -= v;
|
||||
return *this;
|
||||
}
|
||||
vec2 &operator-=(const vec2 &v) {
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
return *this;
|
||||
}
|
||||
vec2 operator-(T v) const { return vec2(x - v, y - v); }
|
||||
vec2 operator-(vec2 v) const { return vec2(x - v.x, y - v.y); }
|
||||
|
||||
// MUL //
|
||||
|
||||
vec2 &operator*=(T v) {
|
||||
x *= v;
|
||||
y *= v;
|
||||
return *this;
|
||||
}
|
||||
vec2 &operator*=(const vec2 &v) {
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
return *this;
|
||||
}
|
||||
vec2 operator*(T v) const { return vec2(x * v, y * v); }
|
||||
vec2 operator*(vec2 v) const { return vec2(x * v.x, y * v.y); }
|
||||
|
||||
// DIV //
|
||||
|
||||
vec2 &operator/=(T v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
return *this;
|
||||
}
|
||||
vec2 &operator/=(const vec2 &v) {
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
return *this;
|
||||
}
|
||||
vec2 operator/(T v) const { return vec2(x / v, y / v); }
|
||||
vec2 operator/(vec2 v) const { return vec2(x / v.x, y / v.y); }
|
||||
|
||||
// Make Negative //
|
||||
vec2 operator-() const { return vec2(-x, -y); }
|
||||
|
||||
bool operator==(const vec2 &v) const { return x == v.x && y == v.y; }
|
||||
bool operator!=(const vec2 &v) const { return !(*this == v); }
|
||||
|
||||
// SECTION: Additional Functions //
|
||||
|
||||
float Len() const { return sqrt(SqLen()); }
|
||||
float SqLen() const { return x * x + y * y; }
|
||||
void Swap() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
|
||||
// SECTION: DATA //
|
||||
T x = 0;
|
||||
T y = 0;
|
||||
};
|
||||
|
||||
using dvec2 = vec2<double>;
|
||||
using fvec2 = vec2<float>;
|
||||
using ivec2 = vec2<int>;
|
||||
} // namespace PD
|
160
include/pd/core/vec3.hpp
Normal file
160
include/pd/core/vec3.hpp
Normal file
@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
#include <pd/core/vec2.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename T>
|
||||
class vec3 {
|
||||
public:
|
||||
// SECTION: Constructors //
|
||||
|
||||
vec3() = default;
|
||||
vec3(T v) {
|
||||
x = v;
|
||||
y = v;
|
||||
z = v;
|
||||
}
|
||||
vec3(T x, T y, T z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
vec3(const vec3 &v) {
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
}
|
||||
|
||||
// SECTION: Operators //
|
||||
|
||||
// ADD //
|
||||
|
||||
vec3 &operator+=(T v) {
|
||||
x += v;
|
||||
y += v;
|
||||
z += v;
|
||||
return *this;
|
||||
}
|
||||
vec3 &operator+=(const vec3 &v) {
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
return *this;
|
||||
}
|
||||
vec3 operator+(T v) const { return vec3(x + v, y + v, z + v); }
|
||||
vec3 operator+(vec3 v) const { return vec3(x + v.x, y + v.y, z + v.z); }
|
||||
|
||||
// SUB //
|
||||
|
||||
vec3 &operator-=(T v) {
|
||||
x -= v;
|
||||
y -= v;
|
||||
z -= v;
|
||||
return *this;
|
||||
}
|
||||
vec3 &operator-=(const vec3 &v) {
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
return *this;
|
||||
}
|
||||
vec3 operator-(T v) const { return vec3(x - v, y - v, z - v); }
|
||||
vec3 operator-(vec3 v) const { return vec3(x - v.x, y - v.y, z - v.z); }
|
||||
|
||||
// MUL //
|
||||
|
||||
vec3 &operator*=(T v) {
|
||||
x *= v;
|
||||
y *= v;
|
||||
z *= v;
|
||||
return *this;
|
||||
}
|
||||
vec3 &operator*=(const vec3 &v) {
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
return *this;
|
||||
}
|
||||
vec3 operator*(T v) const { return vec3(x * v, y * v, z * v); }
|
||||
vec3 operator*(vec3 v) const { return vec3(x * v.x, y * v.y, z * v.z); }
|
||||
|
||||
// DIV //
|
||||
|
||||
vec3 &operator/=(T v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
z /= v;
|
||||
return *this;
|
||||
}
|
||||
vec3 &operator/=(const vec3 &v) {
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
z /= v.z;
|
||||
return *this;
|
||||
}
|
||||
vec3 operator/(T v) const { return vec3(x / v, y / v, z / v); }
|
||||
vec3 operator/(vec3 v) const { return vec3(x / v.x, y / v.y, z / v.z); }
|
||||
|
||||
// Make Negative //
|
||||
vec3 operator-() const { return vec3(-x, -y, -z); }
|
||||
|
||||
bool operator==(const vec3 &v) const {
|
||||
return x == v.x && y == v.y && z == v.z;
|
||||
}
|
||||
bool operator!=(const vec3 &v) const { return !(*this == v); }
|
||||
|
||||
// SECTION: Additional Functions //
|
||||
|
||||
float Len() const { return sqrt(SqLen()); }
|
||||
float SqLen() const { return x * x + y * y + z * z; }
|
||||
void SwapXY() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapYZ() {
|
||||
T t = z;
|
||||
z = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapXZ() {
|
||||
T t = z;
|
||||
z = x;
|
||||
x = t;
|
||||
}
|
||||
|
||||
// SECTION: DATA //
|
||||
T x = 0;
|
||||
T y = 0;
|
||||
T z = 0;
|
||||
};
|
||||
|
||||
using dvec3 = vec3<double>;
|
||||
using fvec3 = vec3<float>;
|
||||
using ivec3 = vec3<int>;
|
||||
} // namespace PD
|
193
include/pd/core/vec4.hpp
Normal file
193
include/pd/core/vec4.hpp
Normal file
@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
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/common.hpp>
|
||||
#include <pd/core/vec3.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename T>
|
||||
class vec4 {
|
||||
public:
|
||||
// SECTION: Constructors //
|
||||
|
||||
vec4() = default;
|
||||
vec4(T v) {
|
||||
x = v;
|
||||
y = v;
|
||||
z = v;
|
||||
w = v;
|
||||
}
|
||||
vec4(T x, T y, T z, T w) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
vec4(const vec4 &v) {
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
w = v.w;
|
||||
}
|
||||
|
||||
vec4(const vec2<T> &xy, const vec2<T> &zw) {
|
||||
x = xy.x;
|
||||
y = xy.y;
|
||||
z = zw.x;
|
||||
w = zw.y;
|
||||
}
|
||||
|
||||
// SECTION: Operators //
|
||||
|
||||
// ADD //
|
||||
|
||||
vec4 &operator+=(T v) {
|
||||
x += v;
|
||||
y += v;
|
||||
z += v;
|
||||
w += v;
|
||||
return *this;
|
||||
}
|
||||
vec4 &operator+=(const vec4<T> &v) {
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
w += v.w;
|
||||
return *this;
|
||||
}
|
||||
vec4 operator+(T v) const { return vec4<T>(x + v, y + v, z + v, w + v); }
|
||||
vec4 operator+(vec4 v) const {
|
||||
return vec4(x + v.x, y + v.y, z + v.z, w + v.w);
|
||||
}
|
||||
|
||||
// SUB //
|
||||
|
||||
vec4 &operator-=(T v) {
|
||||
x -= v;
|
||||
y -= v;
|
||||
z -= v;
|
||||
w -= v;
|
||||
return *this;
|
||||
}
|
||||
vec4 &operator-=(const vec4 &v) {
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
w -= v.w;
|
||||
return *this;
|
||||
}
|
||||
vec4 operator-(T v) const { return vec4(x - v, y - v, z - v, w - v); }
|
||||
vec4 operator-(vec4 v) const {
|
||||
return vec4(x - v.x, y - v.y, z - v.z, w - v.w);
|
||||
}
|
||||
|
||||
// MUL //
|
||||
|
||||
vec4 &operator*=(T v) {
|
||||
x *= v;
|
||||
y *= v;
|
||||
z *= v;
|
||||
w *= v;
|
||||
return *this;
|
||||
}
|
||||
vec4 &operator*=(const vec4 &v) {
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
w *= v.w;
|
||||
return *this;
|
||||
}
|
||||
vec4 operator*(T v) const { return vec4(x * v, y * v, z * v, w * v); }
|
||||
vec4 operator*(vec4 v) const {
|
||||
return vec4(x * v.x, y * v.y, z * v.z, w * v.w);
|
||||
}
|
||||
|
||||
// DIV //
|
||||
|
||||
vec4 &operator/=(T v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
z /= v;
|
||||
w /= v;
|
||||
return *this;
|
||||
}
|
||||
vec4 &operator/=(const vec4 &v) {
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
z /= v.z;
|
||||
w /= v.w;
|
||||
return *this;
|
||||
}
|
||||
vec4 operator/(T v) const { return vec4(x / v, y / v, z / v, w / v); }
|
||||
vec4 operator/(vec4 v) const {
|
||||
return vec4(x / v.x, y / v.y, z / v.z, w / v.w);
|
||||
}
|
||||
|
||||
// Make Negative //
|
||||
vec4 operator-() const { return vec4(-x, -y, -z, -w); }
|
||||
|
||||
bool operator==(const vec4 &v) const {
|
||||
return x == v.x && y == v.y && z == v.z && w == v.w;
|
||||
}
|
||||
bool operator!=(const vec4 &v) const { return !(*this == v); }
|
||||
|
||||
// SECTION: Additional Functions //
|
||||
|
||||
float Len() const { return sqrt(SqLen()); }
|
||||
float SqLen() const { return x * x + y * y + z * z + w * w; }
|
||||
void SwapXY() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapYZ() {
|
||||
T t = z;
|
||||
z = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapXZ() {
|
||||
T t = z;
|
||||
z = x;
|
||||
x = t;
|
||||
}
|
||||
// Adding ZW (to lazy to add all of those yet)
|
||||
void SwapZW() {
|
||||
T t = w;
|
||||
w = z;
|
||||
z = t;
|
||||
}
|
||||
|
||||
// SECTION: DATA //
|
||||
T x = 0;
|
||||
T y = 0;
|
||||
T z = 0;
|
||||
T w = 0;
|
||||
};
|
||||
|
||||
using dvec4 = vec4<double>;
|
||||
using fvec4 = vec4<float>;
|
||||
using ivec4 = vec4<int>;
|
||||
} // namespace PD
|
Reference in New Issue
Block a user