WIP Backend System Redesign Step 1

- Created 1 Context for Backend Management and Sharing
- Made every class that used a static Backend require the Context or specific Backend
- Bring Back 3ds support
This commit is contained in:
2026-01-26 20:46:27 +01:00
parent 892f8ce0c4
commit e8072a064c
47 changed files with 350 additions and 242 deletions

View File

@@ -78,7 +78,7 @@ class PD_API Color {
*/
constexpr Color& Hex(const std::string_view& hex) {
if (!(hex.length() == 7 || hex.length() == 9)) {
throw "[PD] Color: hex string is not rgb or rgba!";
PD::Throw("[PD] Color: hex string is not rgb or rgba!");
}
r = PD::Strings::HexChar2Int(hex[1]) * 16 +
PD::Strings::HexChar2Int(hex[2]);

View File

@@ -74,7 +74,12 @@ SOFTWARE.
namespace PD {
[[noreturn]] inline void Throw(const std::string& str) {
throw std::runtime_error("[PD] " + str);
#ifdef _EXCEPTIONS
throw std::runtime_error("PD Error " + str);
#else
std::cout << "PD Error " << str << std::endl;
std::abort();
#endif
}
/** Types */
using u8 = unsigned char;

View File

@@ -26,6 +26,7 @@ SOFTWARE.
#include <pd/core/common.hpp>
namespace PD {
class OsDriver;
/**
* Timer class
*/
@@ -35,7 +36,7 @@ class PD_API Timer {
* Constructor
* @param auto_start [default true] sets if timer should start after creation
*/
Timer(bool auto_start = true);
Timer(OsDriver& os, bool auto_start = true);
/**
* Unused Deconstructor
*/
@@ -81,5 +82,7 @@ class PD_API Timer {
u64 pNow;
/** Is Running */
bool pIsRunning = false;
/** Os Driver reference */
OsDriver& pOs;
};
} // namespace PD

View File

@@ -26,6 +26,7 @@ SOFTWARE.
#include <pd/core/common.hpp>
namespace PD {
class OsDriver;
/**
* Class to calculate Maximum/Minimum and Average Timings
*/
@@ -218,12 +219,12 @@ class Res {
* Begin a Trace
* @param id Name of the Trace
*/
PD_API void Beg(const std::string& id);
PD_API void Beg(OsDriver& os, const std::string& id);
/**
* End a Trace
* @param id Name of the Trace
*/
PD_API void End(const std::string& id);
PD_API void End(OsDriver& os, const std::string& id);
/**
* Collect Start end end of the trace by tracking
* when the Scope object goes out of scope
@@ -245,18 +246,20 @@ class Scope {
* Constructor requiring a Name for the Trace
* @param id Name of the Trace
*/
Scope(const std::string& id) {
Scope(OsDriver& os, const std::string& id) : pOs(os) {
this->ID = id;
Beg(id);
Beg(pOs, id);
}
/**
* Deconstructor getting the end time when going out of scope
*/
~Scope() { End(ID); }
~Scope() { End(pOs, ID); }
private:
/** Trace Name/ID */
std::string ID;
/** Os Driver Reference */
OsDriver& pOs;
};
} // namespace TT
} // namespace PD

View File

@@ -0,0 +1,79 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 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/core.hpp>
#include <pd/drivers/gfx.hpp>
#include <pd/drivers/hid.hpp>
#include <pd/drivers/os.hpp>
namespace PD {
class PD_API Context {
public:
Context()
: pOs(OsDriver::New()), pGfx(GfxDriver::New()), pHid(HidDriver::New()) {}
~Context() {}
PD_RAW(Context);
static Context::Ref Create();
template <typename Driver>
void UseGfxDriver(PDDriverData data) {
static_assert(std::is_base_of<GfxDriver, Driver>::value,
"Driver must extend GfxDriver");
pGfx.reset();
pGfx = Driver::New(data);
}
template <typename Driver>
void UseHidDriver(PDDriverData data) {
static_assert(std::is_base_of<HidDriver, Driver>::value,
"Driver must extend HidDriver");
pHid.reset();
pHid = Driver::New(data);
}
template <typename Driver>
void UseOsDriver(PDDriverData data) {
static_assert(std::is_base_of<OsDriver, Driver>::value,
"Driver must extend OsDriver");
pOs.reset();
pOs = Driver::New(data);
}
PD::Li::Texture::Ref GetSolidTex();
OsDriver::Ref Os() { return pOs; }
GfxDriver::Ref Gfx() { return pGfx; }
HidDriver::Ref Hid() { return pHid; }
private:
OsDriver::Ref pOs = nullptr;
GfxDriver::Ref pGfx = nullptr;
HidDriver::Ref pHid = nullptr;
PD::Li::Texture::Ref pSolidTex = nullptr;
};
} // namespace PD

View File

@@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/drivers/context.hpp>
#include <pd/drivers/gfx.hpp>
#include <pd/drivers/hid.hpp>
#include <pd/drivers/os.hpp>

View File

@@ -24,6 +24,7 @@ SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/drivers/types.hpp>
#include <pd/lithium/command.hpp>
#include <pd/lithium/texture.hpp>
@@ -61,7 +62,8 @@ class GfxDriver2 {
};
class GfxDriver {
public:
GfxDriver(const std::string& name = "NullGfx") : pName(name) {};
GfxDriver(const std::string& name = "NullGfx") : pName(name) {}
GfxDriver(PDDriverData data) : pName("NullGfx") {}
~GfxDriver() = default;
PD_SHARED(GfxDriver);
@@ -77,6 +79,7 @@ class GfxDriver {
virtual void RenderDrawData(const Li::CmdPool& Commands) {}
void SetViewPort(const ivec2& vp) { ViewPort = vp; }
void SetViewPort(int w, int h) { ViewPort = PD::ivec2(w, h); }
virtual Li::Texture::Ref LoadTex(
const std::vector<u8>& pixels, int w, int h,
@@ -90,6 +93,8 @@ class GfxDriver {
Li::Texture::Ref GetSolidTex() { return pSolid; }
const std::string& GetName() const { return pName; }
const std::string pName = "NullGfx";
LiBackendFlags Flags = 0;
ivec2 ViewPort;
@@ -107,38 +112,4 @@ class GfxDriver {
// Optional Frame Counter
u64 FrameCounter;
};
/** Static Gfx Controller */
class Gfx {
public:
Gfx() = default;
~Gfx() = default;
static void Init(GfxDriver::Ref d);
static void Deinit() { pGfx->Deinit(); }
static void NewFrame() { pGfx->NewFrame(); }
static void BindTex(Li::TexAddress addr) { pGfx->BindTex(addr); }
static void SetViewPort(const ivec2& vp) { pGfx->SetViewPort(vp); }
static void SetViewPort(int w, int h) { pGfx->SetViewPort(PD::ivec2(w, h)); }
static void RenderDrawData(const Li::CmdPool& Commands) {
pGfx->RenderDrawData(Commands);
}
static LiBackendFlags Flags() { return pGfx->Flags; }
static Li::Texture::Ref LoadTex(
const std::vector<u8>& pixels, int w, int h,
Li::Texture::Type type = Li::Texture::Type::RGBA32,
Li::Texture::Filter filter = Li::Texture::Filter::LINEAR) {
return pGfx->LoadTex(pixels, w, h, type, filter);
}
static void DestroyTex(Li::Texture::Ref tex) { pGfx->DestroyTex(tex); }
static Li::Texture::Ref GetSolidTex() { return pGfx->GetSolidTex(); }
static GfxDriver::Ref pGfx;
};
} // namespace PD

View File

@@ -24,6 +24,7 @@ SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/drivers/types.hpp>
namespace PD {
/** Did not found a better solution yet sadly */
@@ -134,7 +135,8 @@ class HidDriver {
Event_Up, ///< Key released
};
HidDriver(const std::string& name = "NullHid") : pName(name) {};
HidDriver(const std::string& name = "NullHid") : pName(name) {}
HidDriver(PDDriverData data) : pName("NullHid") {}
virtual ~HidDriver() = default;
PD_SHARED(HidDriver);
@@ -233,6 +235,8 @@ class HidDriver {
*/
virtual void GetInputStr(std::string& str) {}
const std::string& GetName() const { return pName; }
/** Data Section */
/** Backend Identification Name */
@@ -255,42 +259,8 @@ class HidDriver {
/** Keyboard Key Event Table Setup */
std::unordered_map<Event, u128> KbKeyEvents[2];
};
/** Static Hid Controller */
class Hid {
public:
Hid() = default;
~Hid() = default;
/** Referenec to Drivers enums */
using Key = HidDriver::Key;
using KbKey = HidKb::KbKey;
using Event = HidDriver::Event;
static void Init(HidDriver::Ref v = nullptr) {
if (v) {
pHid = v;
} else {
pHid = HidDriver::New();
}
}
static bool IsEvent(Event e, Key keys) { return pHid->IsEvent(e, keys); }
static bool IsEvent(Event e, KbKey key) { return pHid->IsEvent(e, key); }
static bool IsDown(Key keys) { return pHid->IsDown(keys); }
static bool IsUp(Key keys) { return pHid->IsUp(keys); }
static bool IsHeld(Key keys) { return pHid->IsHeld(keys); }
static fvec2 MousePos() { return pHid->MousePos(); }
static fvec2 MousePosLast() { return pHid->MousePosLast(); }
static void Clear() { pHid->Clear(); }
static void Lock() { pHid->Lock(); }
static void Lock(bool v) { pHid->Lock(v); }
static void Unlock() { pHid->Unlock(); }
static bool Locked() { return pHid->Locked(); }
static void Update() { pHid->Update(); }
static u32 GetFlags() { return pHid->Flags; }
static void GetStrInput(std::string& str) { pHid->GetInputStr(str); }
static HidDriver::Ref pHid;
};
namespace Hid {
using Event = HidDriver::Event;
using Key = HidDriver::Key;
} // namespace Hid
} // namespace PD

View File

@@ -25,13 +25,15 @@ SOFTWARE.
#include <pd/core/common.hpp>
#include <pd/core/timetrace.hpp>
#include <pd/drivers/types.hpp>
namespace PD {
using TraceMap = std::map<std::string, TT::Res::Ref>;
class OsDriver {
public:
OsDriver() = default;
OsDriver(const std::string& name = "StdPd") : pName(name) {}
OsDriver(PDDriverData data) : pName("StdPd") {}
virtual ~OsDriver() = default;
PD_SHARED(OsDriver);
@@ -40,32 +42,11 @@ class OsDriver {
TraceMap& GetTraceMap();
TT::Res::Ref& GetTraceRef(const std::string& id);
bool TraceExist(const std::string& id);
const std::string& GetName() const { return pName; }
TraceMap pTraces;
};
/** Static Os Controller */
class OS {
public:
OS() = default;
~OS() = default;
static void Init(OsDriver::Ref v = nullptr) {
if (v) {
pOs = v;
} else {
pOs = OsDriver::New();
}
}
static u64 GetTime() { return pOs->GetTime(); }
static u64 GetNanoTime() { return pOs->GetNanoTime(); }
static TraceMap& GetTraceMap() { return pOs->GetTraceMap(); }
static TT::Res::Ref& GetTraceRef(const std::string& id) {
return pOs->GetTraceRef(id);
}
static bool TraceExist(const std::string& id) { return pOs->TraceExist(id); }
static OsDriver::Ref pOs;
private:
const std::string pName = "StdPd";
};
} // namespace PD

View File

@@ -0,0 +1,3 @@
#pragma once
using PDDriverData = void*;

View File

@@ -47,7 +47,7 @@ namespace PD {
namespace Li {
class PD_API DrawList {
public:
DrawList(int initial_size = 64);
DrawList(Context& ctx, int initial_size = 64);
~DrawList();
/** Require Copy and Move Constructors */
@@ -218,6 +218,7 @@ class PD_API DrawList {
std::vector<fvec2> pPath;
u32 pNumIndices = 0;
u32 pNumVertices = 0;
Context* pCtx = nullptr;
};
} // namespace Li
} // namespace PD

View File

@@ -42,6 +42,7 @@ enum LiTextFlags_ {
};
namespace PD {
class Context;
namespace Li {
class PD_API Font {
public:
@@ -56,7 +57,7 @@ class PD_API Font {
};
/** Constructore doesnt need Backand anymore */
Font() = default;
Font(Context& ctx) : pCtx(ctx) {}
~Font() = default;
PD_SHARED(Font);
@@ -128,6 +129,7 @@ class PD_API Font {
u64 TimeStamp;
};
std::unordered_map<u32, TMELEM> pTMS;
Context& pCtx;
};
} // namespace Li
} // namespace PD

View File

@@ -32,7 +32,9 @@ namespace PD {
namespace UI7 {
class InputHandler {
public:
InputHandler() { DragTime = Timer::New(false); }
InputHandler(PD::Context& ctx) : pCtx(ctx) {
DragTime = Timer::New(*pCtx.Os().get(), false);
}
~InputHandler() = default;
PD_SHARED(InputHandler);
@@ -55,10 +57,10 @@ class InputHandler {
}
}
// Get a Short define for touch pos
fvec2 p = Hid::MousePos();
fvec2 p = pCtx.Hid()->MousePos();
// Check if Drag starts in the area position
if ((Hid::IsDown(Hid::Key::Touch) ||
Hid::IsEvent(PD::Hid::Event::Event_Down, HidKb::Kb_MouseLeft)) &&
if ((pCtx.Hid()->IsDown(pCtx.Hid()->Key::Touch) ||
pCtx.Hid()->IsEvent(PD::HidDriver::Event_Down, HidKb::Kb_MouseLeft)) &&
Li::Renderer::InBox(p, area)) {
// Set ID and iniatial Positions
DraggedObject = id;
@@ -70,15 +72,16 @@ class InputHandler {
DragTime->Reset();
DragTime->Rseume();
return false; // To make sure the Object is "Dragged"
} else if ((Hid::IsHeld(Hid::Key::Touch) ||
Hid::IsEvent(PD::Hid::Event::Event_Held,
HidKb::Kb_MouseLeft)) &&
} else if ((pCtx.Hid()->IsHeld(pCtx.Hid()->Key::Touch) ||
pCtx.Hid()->IsEvent(PD::HidDriver::Event_Held,
HidKb::Kb_MouseLeft)) &&
IsObjectDragged()) {
// Update DragLast and DragPoisition
DragLastPosition = DragPosition;
DragPosition = p;
} else if ((Hid::IsUp(Hid::Key::Touch) ||
Hid::IsEvent(PD::Hid::Event::Event_Up, HidKb::Kb_MouseLeft)) &&
} else if ((pCtx.Hid()->IsUp(pCtx.Hid()->Key::Touch) ||
pCtx.Hid()->IsEvent(PD::HidDriver::Event_Up,
HidKb::Kb_MouseLeft)) &&
IsObjectDragged()) {
// Released... Everything gets reset
DraggedObject = 0;
@@ -88,9 +91,9 @@ class InputHandler {
DragDestination = fvec4(0);
// Set Drag released to true (only one frame)
// and Only if still in Box
DragReleased = Li::Renderer::InBox(Hid::MousePosLast(), area);
DragReleased = Li::Renderer::InBox(pCtx.Hid()->MousePosLast(), area);
DragReleasedAW = true; // Advanced
u64 d_rel = PD::OS::GetTime();
u64 d_rel = pCtx.Os()->GetTime();
if (d_rel - DragLastReleased < DoubleClickTime) {
DragDoubleRelease = true;
DragLastReleased = 0; // Set 0 to prevent double exec
@@ -129,6 +132,7 @@ class InputHandler {
bool DragReleased = false; ///< Drag Releaded in Box
bool DragReleasedAW = false; ///< Drag Released Anywhere
bool DragDoubleRelease = false; ///< Double Click
PD::Context& pCtx;
/** Check if an object is Dragged already */
bool IsObjectDragged() const { return DraggedObject != 0; }
};

View File

@@ -30,20 +30,21 @@ SOFTWARE.
#include <pd/ui7/viewport.hpp>
namespace PD {
class Context;
namespace UI7 {
class PD_API IO {
public:
IO() {
Time = Timer::New();
InputHandler = InputHandler::New();
IO(PD::Context& ctx) : pCtx(ctx) {
Time = Timer::New(*pCtx.Os().get());
InputHandler = InputHandler::New(pCtx);
Theme = UI7::Theme::New();
Back = Li::DrawList::New();
Front = Li::DrawList::New();
FDL = Li::DrawList::New();
Back = Li::DrawList::New(pCtx);
Front = Li::DrawList::New(pCtx);
FDL = Li::DrawList::New(pCtx);
DeltaStats = TimeStats::New(60);
/** Probably not the best solution i guess */
CurrentViewPort.z = PD::Gfx::pGfx->ViewPort.x;
CurrentViewPort.w = PD::Gfx::pGfx->ViewPort.y;
CurrentViewPort.z = pCtx.Gfx()->ViewPort.x;
CurrentViewPort.w = pCtx.Gfx()->ViewPort.y;
}
~IO() {}
@@ -90,6 +91,8 @@ class PD_API IO {
u32 NumIndices = 0; ///< Debug Indices Num
std::vector<u32> MenuOrder;
PD::Context& pCtx; // Palladium base context
// DrawListApi
void RegisterDrawList(const UI7::ID& id, Li::DrawList::Ref v) {
DrawListRegestry.push_back(std::make_pair(id, v));

View File

@@ -38,7 +38,7 @@ class PD_API Layout {
public:
Layout(const ID& id, IO::Ref io) : ID(id) {
this->IO = io;
DrawList = Li::DrawList::New();
DrawList = Li::DrawList::New(io->pCtx);
DrawList->SetFont(IO->Font);
DrawList->SetFontScale(io->FontScale);
Scrolling[0] = false;

View File

@@ -49,7 +49,7 @@ PD_API std::string GetVersion(bool show_build = false);
/** Base Context for UI7 */
class PD_API Context {
public:
Context() { pIO = IO::New(); }
Context(PD::Context& ctx) { pIO = IO::New(ctx); }
~Context() = default;
PD_SHARED(Context);