# Rewrite Stage 1

- Switch to CMake build system
- delete everything for a new structure
- Add SmartCtor class and saperate New func
- New Faster and Open Lithium Command API
- Rewritten Text Renderer to ghet rid of all that janky code
- New TimeTrace System and use of NanoTime using GetTimeNano
- Overall going to a more Object oriented way
- Updated vec api to support vec2 input on vec3
## Todo
- Support vec2 and vec3 in vec4 as inputs
- Continue UI7
- Fix SystemFont on 3ds freezing the system
- Fix TTF Font UV Mapping
## Warning
Creating Apps for the 3ds is not possible yet as the 3ds is Freezing and this is only stage 1 of ?   Emulator works perfect
This commit is contained in:
2025-01-09 20:22:49 +01:00
parent d5c01b2988
commit d815bb5674
131 changed files with 21457 additions and 30844 deletions

68
include/pd/common/app.hpp Normal file
View File

@ -0,0 +1,68 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
#include <pd/common/timetrace.hpp>
#include <pd/graphics/lithium.hpp>
namespace PD {
/// @brief Template Class for User Application
class App : public SmartCtor<App> {
public:
App() = default;
~App() = default;
/// @brief Templete function where the user can Init his stuff
virtual void Init() {}
/// @brief Templeta funciton to deinit stuff
/// (most of that is done automatically)
virtual void Deinit() {}
/// @brief App Mainloop
/// @param delta Deltatime
/// @param time App RunTime
/// @return false to exit the app
virtual bool MainLoop(u64 delta, float time) { return false; }
/// @brief Function to run the App
/// (int main() {
/// auto app = PD::New<UserApp>();
/// app->Run();
/// return 0;
/// })
void Run();
LI::Renderer::Ref Renderer() { return renderer; }
float GetFps() const { return fps; }
private:
void PreInit();
void PostDeinit();
LI::Renderer::Ref renderer;
u64 last_time;
float app_time;
float fps;
};
} // namespace PD

View File

@ -0,0 +1,71 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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 <chrono>
#include <cinttypes>
#include <cmath>
#include <filesystem> // Requires C++ 17 or later
#include <format> // Requires C++ 20 or later
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
namespace PD {
// New Version of Smart CTOR
// Using as Template class
template <typename T>
class SmartCtor {
public:
/// @brief Type Reference
using Ref = std::shared_ptr<T>;
/// @brief Creates a New Shared Pointer
/// @param ...args Arguments to forward
/// @return Shared Pointer (Reference)
template <typename... Args>
static Ref New(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
};
/// @brief Wrapper for SmartCtor<Type>::New
/// @tparam T class type
/// @param ...args Arguments
/// @return SmartCtor<T>::Ref type reference
/// @note Not sure if this is solving the problem or
/// if I schould switch back to the macro
template <typename T, typename... Args>
SmartCtor<T>::Ref New(Args&&... args) {
return SmartCtor<T>::New(std::forward<Args>(args)...);
}
// Defines
using u64 = unsigned long long;
using u32 = unsigned int;
using u16 = unsigned short;
using u8 = unsigned char;
} // namespace PD

View File

@ -0,0 +1,31 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
namespace PD {
void Error(const std::string& error);
void Assert(bool v, const std::string& msg);
} // namespace PD

View File

@ -0,0 +1,57 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
namespace PD {
/// @brief Lang System
///< Translations are saved into json files
///< Path should point to a directory the fils
///< for example the files ar named [en.json, de.json, fr.json]
class Lang : public SmartCtor<Lang> {
public:
Lang() = default;
~Lang() = default;
void SetBasePath(const std::string &path) { langs_path = path; }
void Load(const std::string &lang_key) {
LoadFile(langs_path + "/" + lang_key + ".json");
}
void LoadFile(const std::string &path);
const std::string &Get(const std::string &k);
const std::string &GetName() { return lang_name; }
const std::string &GetID() { return lang_id; }
const std::string &GetAuthor() { return lang_author; }
const std::string &GetPath() { return langs_path; }
private:
const int ver = 0;
std::string langs_path = "romfs:/lang";
std::string lang_name;
std::string lang_id;
std::string lang_author;
std::map<std::string, std::string> ltable;
};
} // namespace PD

View File

@ -0,0 +1,60 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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 <3ds.h>
#include <pd/common/common.hpp>
#include <pd/common/error.hpp>
namespace PD {
template <typename T>
class LinearAllocator : public std::allocator<T> {
public:
using size_type = size_t;
using pointer = T*;
using const_pointer = const T*;
template <typename T1>
struct rebind {
using other = LinearAllocator<T1>;
};
pointer allocate(size_type n, const void* hint = nullptr) {
if (n > this->max_size()) {
Error("Linear Alloc failed (no space left)");
return nullptr;
}
return (pointer)linearAlloc(n * sizeof(T));
}
void deallocate(pointer p, size_type) { linearFree((void*)p); }
size_type max_size() { return linearSpaceFree(); }
LinearAllocator() noexcept {}
LinearAllocator(const LinearAllocator<T>& a) noexcept
: std::allocator<T>(a) {}
~LinearAllocator() noexcept {}
};
} // namespace PD

View File

@ -0,0 +1,42 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
namespace PD {
namespace Strings {
bool StringEndsWith(const std::string& str,
const std::vector<std::string>& exts);
std::wstring MakeWstring(const std::string& s);
const std::string FormatNanos(unsigned long long nanos);
const std::string FormatMillis(unsigned long long millis);
const std::string FormatBytes(unsigned long long bytes);
const std::string GetFileName(const std::string& path,
const std::string& saperators = "/\\");
const std::string PathRemoveExtension(const std::string& path);
template <typename T>
const std::string ToHex(const T& v);
} // namespace Strings
} // namespace PD

37
include/pd/common/sys.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
#include <pd/common/timetrace.hpp>
namespace PD {
namespace Sys {
using TraceMap = std::map<std::string, TT::Res::Ref>;
u64 GetTime();
u64 GetNanoTime();
TT::Res::Ref& GetTraceRef(const std::string& id);
TraceMap& GetTraceMap();
} // namespace Sys
} // namespace PD

View File

@ -0,0 +1,44 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
#include <pd/common/sys.hpp>
namespace PD {
class Timer {
public:
Timer() {}
~Timer() {}
void Start() {}
void Pause() {}
void Update() {}
void Reset() { start_ = Sys::GetTime(); }
u64 Get() {}
private:
u64 start_;
u64 now_;
};
} // namespace PD

View File

@ -0,0 +1,102 @@
#pragma once
/*
MIT License
Copyright (c) 2024 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/common/common.hpp>
namespace PD {
class TimeStats : public SmartCtor<TimeStats> {
public:
TimeStats(int l) : len(l), val(l, 0) {}
~TimeStats() = default;
void Add(unsigned long long v) {
val[idx] = v;
idx = next(idx);
num_val = std::min(num_val + 1, len);
}
unsigned long long GetAverage() {
if (!num_val) return 0.f;
unsigned long long res = 0;
for (int i = 0; i < num_val; i++) {
res += val[smart_idx(i)];
}
return res / num_val;
}
const std::vector<unsigned long long> &GetData() { return val; }
const unsigned long long &operator[](int i) { return val[smart_idx(i)]; }
const size_t GetLen() { return len; }
const size_t GetNumValues() { return num_val; }
private:
size_t next(size_t c) const { return (c + 1) % len; }
size_t smart_idx(size_t v) const { return (idx + len - num_val + v) % len; }
int len = 0;
std::vector<unsigned long long> val;
int idx = 0;
int num_val = 0;
};
namespace TT {
class Res : public SmartCtor<Res> {
public:
Res() { protocol = TimeStats::New(60); }
~Res() = default;
void SetID(const std::string &v) { id = v; }
const std::string GetID() { return id; }
void SetStart(unsigned long long v) { start = v; }
unsigned long long GetStart() { return start; }
void SetEnd(unsigned long long v) {
end = v;
protocol->Add(GetLastDiff());
}
unsigned long long GetEnd() { return end; }
unsigned long long GetLastDiff() { return end - start; }
TimeStats::Ref GetProtocol() { return protocol; }
private:
std::string id;
unsigned long long start;
unsigned long long end;
TimeStats::Ref protocol;
};
void Beg(const std::string &id);
void End(const std::string &id);
class Scope {
public:
Scope(const std::string &id) {
this->id = id;
Beg(id);
}
~Scope() { End(id); }
private:
std::string id;
};
} // namespace TT
} // namespace PD