Move CmdPool code to a source file

This commit is contained in:
2026-01-22 19:25:01 +01:00
parent 0db4953125
commit 931e02aefb
3 changed files with 96 additions and 70 deletions

View File

@@ -24,6 +24,7 @@ SOFTWARE.
*/ */
#include <pd/core/core.hpp> #include <pd/core/core.hpp>
#include <pd/lithium/pd_p_api.hpp>
#include <pd/lithium/texture.hpp> #include <pd/lithium/texture.hpp>
#include <pd/lithium/vertex.hpp> #include <pd/lithium/vertex.hpp>
@@ -71,82 +72,26 @@ class Command {
Texture::Ref Tex; Texture::Ref Tex;
}; };
class CmdPool { class PD_LITHIUM_API CmdPool {
public: public:
CmdPool() {} CmdPool() {}
~CmdPool() {} ~CmdPool() {}
Command::Ref NewCmd() { Command::Ref NewCmd();
if (pPoolIdx >= pPool.size()) { void Init(size_t initial_size);
Resize(pPool.size() + 128); void Deinit();
} void Resize(size_t nulen);
Command::Ref nu = pPool[pPoolIdx++]; void Reset();
nu->Layer = Layer; Command::Ref GetCmd(size_t idx) const;
nu->Index = pPoolIdx - 1; Command::Ref GetCmd(size_t idx);
return nu; size_t Size() const;
} size_t Cap() const;
void Merge(CmdPool& p);
void Init(size_t initial_size) { Resize(initial_size); } void Copy(CmdPool& p);
void Sort();
void Deinit() {
for (auto it : pPool) {
Command::Delete(it);
}
pPool.clear();
}
void Resize(size_t nulen) {
if (nulen <= pPool.size()) {
return; // no idea yet
}
size_t oldlen = pPool.size();
pPool.resize(nulen);
for (size_t i = oldlen; i < pPool.size(); i++) {
pPool[i] = Command::New();
}
}
void Reset() {
for (u32 i = 0; i < pPoolIdx; i++) {
pPool[i]->Clear();
}
pPoolIdx = 0;
}
Command::Ref GetCmd(size_t idx) const { return pPool[idx]; }
Command::Ref GetCmd(size_t idx) { return pPool[idx]; }
size_t Size() const { return pPoolIdx; }
size_t Cap() const { return pPool.size(); }
void Merge(CmdPool& p) {
Copy(p);
p.Reset();
}
void Copy(CmdPool& p) {
if (pPoolIdx + p.Size() > pPool.size()) {
Resize(pPoolIdx + p.Size());
}
for (size_t i = 0; i < p.Size(); i++) {
size_t idx = pPoolIdx++;
*pPool[idx] = *p.GetCmd(i);
pPool[idx]->Index = idx;
pPool[idx]->Layer += Layer;
}
}
void Sort() {
std::sort(begin(), end(),
[](const Command::Ref& a, const Command::Ref& b) -> bool {
if (a->Layer == b->Layer) {
return a->Tex < b->Tex;
}
return a->Layer < b->Layer;
});
}
private: private:
static bool pTheOrder(const Command::Ref& a, const Command::Ref& b);
friend class DrawList; friend class DrawList;
Command::Ref* begin() { return &pPool[0]; } Command::Ref* begin() { return &pPool[0]; }
Command::Ref* end() { return &pPool[pPoolIdx]; } Command::Ref* end() { return &pPool[pPoolIdx]; }

View File

@@ -5,6 +5,7 @@ project(pd-lithium LANGUAGES CXX VERSION 0.6.0)
option(PD_LI_INCLUDE_FONTS "Include Fonts" OFF) option(PD_LI_INCLUDE_FONTS "Include Fonts" OFF)
set(SRC set(SRC
source/command.cpp
source/drawlist.cpp source/drawlist.cpp
source/font.cpp source/font.cpp
source/fonts.cpp source/fonts.cpp

View File

@@ -0,0 +1,80 @@
#include <pd/lithium/command.hpp>
PD_LITHIUM_API PD::Li::Command::Ref PD::Li::CmdPool::NewCmd() {
if (pPoolIdx >= pPool.size()) {
Resize(pPool.size() + 128);
}
Command::Ref nu = pPool[pPoolIdx++];
nu->Layer = Layer;
nu->Index = pPoolIdx - 1;
return nu;
}
PD_LITHIUM_API void PD::Li::CmdPool::Init(size_t initial_size) {
Resize(initial_size);
}
PD_LITHIUM_API void PD::Li::CmdPool::Deinit() {
for (auto it : pPool) {
Command::Delete(it);
}
pPool.clear();
}
PD_LITHIUM_API void PD::Li::CmdPool::Resize(size_t nulen) {
if (nulen <= pPool.size()) {
return; // no idea yet
}
size_t oldlen = pPool.size();
pPool.resize(nulen);
for (size_t i = oldlen; i < pPool.size(); i++) {
pPool[i] = Command::New();
}
}
PD_LITHIUM_API void PD::Li::CmdPool::Reset() {
for (u32 i = 0; i < pPoolIdx; i++) {
pPool[i]->Clear();
}
pPoolIdx = 0;
}
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) const {
return pPool[idx];
}
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) { return pPool[idx]; }
size_t PD::Li::CmdPool::Size() const { return pPoolIdx; }
size_t PD::Li::CmdPool::Cap() const { return pPool.size(); }
PD_LITHIUM_API void PD::Li::CmdPool::Merge(CmdPool& p) {
Copy(p);
p.Reset();
}
PD_LITHIUM_API void PD::Li::CmdPool::Copy(CmdPool& p) {
if (pPoolIdx + p.Size() > pPool.size()) {
Resize(pPoolIdx + p.Size());
}
for (size_t i = 0; i < p.Size(); i++) {
size_t idx = pPoolIdx++;
*pPool[idx] = *p.GetCmd(i);
pPool[idx]->Index = idx;
pPool[idx]->Layer += Layer;
}
}
PD_LITHIUM_API void PD::Li::CmdPool::Sort() {
std::sort(begin(), end(), pTheOrder);
}
PD_LITHIUM_API bool PD::Li::CmdPool::pTheOrder(const Command::Ref& a,
const Command::Ref& b) {
if (a->Layer == b->Layer) {
if (a->Tex == b->Tex) {
return a->Index < b->Index;
}
return a->Tex < b->Tex;
}
return a->Layer < b->Layer;
}