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

@@ -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*;