From 2e652a3c241881b4463c1d98a9c3f4c290349cd3 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Sat, 28 Mar 2026 13:52:36 +0100 Subject: [PATCH] More stuff done at Project Ultra - Added renderspace - Revision System (to not always update pRenderspace - added some test scene --- CMakeLists.txt | 2 ++ include/pd/ultra/canvas.hpp | 22 ++++++--------- include/pd/ultra/elems/element.hpp | 20 ++++++++++++-- include/pd/ultra/elems/image.hpp | 43 ++++++++++++++++++++++++++++++ include/pd/ultra/elems/rect.hpp | 20 +++++++++----- include/pd/ultra/elems/text.hpp | 3 --- include/pd/ultra/flags.hpp | 18 +++++++++++++ source/ultra/canvas.cpp | 40 +++++++++++++++++++++++++-- source/ultra/elems/element.cpp | 26 ++++++++++++++++++ source/ultra/elems/image.cpp | 11 ++++++++ source/ultra/elems/rect.cpp | 4 ++- source/ultra/elems/text.cpp | 2 +- source/ultra/layout.cpp | 1 + tests/gfx/source/main.cpp | 13 ++++++--- 14 files changed, 192 insertions(+), 33 deletions(-) create mode 100644 include/pd/ultra/elems/image.hpp create mode 100644 include/pd/ultra/flags.hpp create mode 100644 source/ultra/elems/element.cpp create mode 100644 source/ultra/elems/image.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b30c1ea..49c4879 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,10 @@ set(PD_SOURCES # Ultra source/ultra/canvas.cpp source/ultra/layout.cpp + source/ultra/elems/element.cpp source/ultra/elems/rect.cpp source/ultra/elems/text.cpp + source/ultra/elems/image.cpp ) if(${PD_BUILD_SHARED}) diff --git a/include/pd/ultra/canvas.hpp b/include/pd/ultra/canvas.hpp index 30a2780..2dce1b4 100644 --- a/include/pd/ultra/canvas.hpp +++ b/include/pd/ultra/canvas.hpp @@ -2,19 +2,7 @@ #include #include - -using UltraAlignment = PD::u32; -enum UltraAlignment_ { - UltraAlignment_None = 0, - UltraAlignment_Top = 1 << 0, - UltraAlignment_Bot = 1 << 1, - UltraAlignment_Left = 1 << 2, - UltraAlignment_Right = 1 << 3, - UltraAlignment_TopLeft = UltraAlignment_Top | UltraAlignment_Left, - UltraAlignment_TopRight = UltraAlignment_Top | UltraAlignment_Right, - UltraAlignment_BotLeft = UltraAlignment_Bot | UltraAlignment_Left, - UltraAlignment_BotRight = UltraAlignment_Bot | UltraAlignment_Right, -}; +#include namespace PD { namespace Ultra { @@ -31,18 +19,24 @@ class PD_API Canvas { PD::fvec2 VTranslateAlignPos(const PD::fvec2& pos, const PD::fvec2& size, UltraAlignment align) const; PD::Li::Rect VTranslateObject(const PD::fvec2& pos, const PD::fvec2& size, - UltraAlignment align, bool size_modified = false) const; + UltraAlignment align, + bool size_modified = false) const; + PD::Li::Rect TranslateObject(const PD::fvec2& pos, const PD::fvec2& size, + UltraAlignment align, + bool size_modified = false) const; PD::fvec2 TranslatePos(const PD::fvec2& p) const; PD::fvec2 VTranslateSize(const PD::fvec2& s) const; PD::fvec2 TranslateSize(const PD::fvec2& s) const; float VTranslateFontscale(float f) const; float TranslateFontscale(float f) const; + const PD::u32& GetRevision() const; private: PD::fvec2 pViewport; PD::fvec2 pVirtualViewPort; float pVfactor = 0.f; PD::fvec2 pVoff; + PD::u32 pRev; // revision }; } // namespace Ultra } // namespace PD \ No newline at end of file diff --git a/include/pd/ultra/elems/element.hpp b/include/pd/ultra/elems/element.hpp index 29243bb..95cd155 100644 --- a/include/pd/ultra/elems/element.hpp +++ b/include/pd/ultra/elems/element.hpp @@ -1,10 +1,12 @@ #pragma once #include +#include namespace PD { namespace Ultra { -class ElementBase { +class Canvas; +class PD_API ElementBase { public: ElementBase() {} ~ElementBase() {} @@ -14,8 +16,22 @@ class ElementBase { * Reset Function (for PD::Pool::FastReset) */ virtual void Reset() {} + void Update(Canvas& c); - private: + void SetAlignment(UltraAlignment a) { pAlignment = a; } + void SetPosition(const PD::fvec2& pos) { pPos = pos; } + void SetPosition(float x, float y) { pPos = PD::fvec2(x, y); } + void SetSize(const PD::fvec2& size) { pSize = size; } + void SetSize(float w, float h) { pSize = PD::fvec2(w, h); } + + protected: + bool RevisionUpdate(PD::u32 req); + PD::u32 pCanvasRev = 0; + PD::Li::Rect pRenderspace; + UltraAlignment pAlignment = 0; + PD::fvec2 pPos; + PD::fvec2 pSize; + ElementBase* pParent = nullptr; }; } // namespace Ultra } // namespace PD \ No newline at end of file diff --git a/include/pd/ultra/elems/image.hpp b/include/pd/ultra/elems/image.hpp new file mode 100644 index 0000000..cf94500 --- /dev/null +++ b/include/pd/ultra/elems/image.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +namespace PD { +namespace Ultra { +class PD_API Image : public ElementBase { + public: + Image() {} + Image(PD::Li::Texture* tex, const PD::fvec2& pos, float rounding = 0.f, + UltraAlignment align = 0) + : pColor(0xffffffff), pRounding(rounding) { + this->pAlignment = align; + this->pPos = pos; + SetTexture(tex); + } + Image(PD::Li::Texture* tex, float x, float y, float rounding = 0.f, + UltraAlignment align = 0) + : pColor(0xffffffff), pRounding(rounding) { + this->pAlignment = align; + this->pPos = PD::fvec2(x, y); + SetTexture(tex); + } + ~Image() {} + + void Draw(PD::Li::Drawlist& l) override; + + void SetColor(const PD::Color& color) { pColor = color; } + void SetRounding(float r) { pRounding = r; } + void SetTexture(PD::Li::Texture* tex) { + pTex = tex; + SetSize(pTex->GetSize()); + } + + private: + PD::Color pColor = 0xffffffff; + PD::Li::Texture* pTex = nullptr; + float pRounding = 0.f; +}; +} // namespace Ultra + +} // namespace PD \ No newline at end of file diff --git a/include/pd/ultra/elems/rect.hpp b/include/pd/ultra/elems/rect.hpp index dc94238..d2c7ebc 100644 --- a/include/pd/ultra/elems/rect.hpp +++ b/include/pd/ultra/elems/rect.hpp @@ -8,20 +8,28 @@ namespace Ultra { class PD_API Rect : public ElementBase { public: Rect() {} + Rect(const PD::fvec2& pos, const PD::fvec2& size, const PD::Color& color, + float rounding = 0.f, UltraAlignment align = 0) + : pColor(color), pRounding(rounding) { + this->pAlignment = align; + this->pPos = pos; + this->pSize = size; + } + Rect(float x, float y, float w, float h, const PD::Color& color, + float rounding = 0.f, UltraAlignment align = 0) + : pColor(color), pRounding(rounding) { + this->pAlignment = align; + this->pPos = PD::fvec2(x, y); + this->pSize = PD::fvec2(w, h); + } ~Rect() {} void Draw(PD::Li::Drawlist& l) override; - void SetPosition(const PD::fvec2& pos) { pPos = pos; } - void SetPosition(float x, float y) { pPos = PD::fvec2(x, y); } - void SetSize(const PD::fvec2& size) { pSize = size; } - void SetSize(float w, float h) { pSize = PD::fvec2(w, h); } void SetColor(const PD::Color& color) { pColor = color; } void SetRounding(float r) { pRounding = r; } private: - PD::fvec2 pPos; - PD::fvec2 pSize; PD::Color pColor; float pRounding = 0.f; }; diff --git a/include/pd/ultra/elems/text.hpp b/include/pd/ultra/elems/text.hpp index abc1e30..0eff46e 100644 --- a/include/pd/ultra/elems/text.hpp +++ b/include/pd/ultra/elems/text.hpp @@ -12,13 +12,10 @@ class PD_API Text : public ElementBase { void Draw(PD::Li::Drawlist& l) override; - void SetPosition(const PD::fvec2& pos) { pPos = pos; } - void SetPosition(float x, float y) { pPos = PD::fvec2(x, y); } void SetColor(const PD::Color& color) { pColor = color; } void SetText(const std::string& text) { pText = text; } private: - PD::fvec2 pPos; PD::Color pColor; std::string pText; }; diff --git a/include/pd/ultra/flags.hpp b/include/pd/ultra/flags.hpp new file mode 100644 index 0000000..7014e41 --- /dev/null +++ b/include/pd/ultra/flags.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +using UltraAlignment = PD::u32; +enum UltraAlignment_ { + UltraAlignment_None = 0, + UltraAlignment_Top = 1 << 0, + UltraAlignment_Bot = 1 << 1, + UltraAlignment_Left = 1 << 2, + UltraAlignment_Right = 1 << 3, + UltraAlignment_CenterVertical = 1 << 4, + UltraAlignment_CenterHorizontal = 1 << 5, + UltraAlignment_TopLeft = UltraAlignment_Top | UltraAlignment_Left, + UltraAlignment_TopRight = UltraAlignment_Top | UltraAlignment_Right, + UltraAlignment_BotLeft = UltraAlignment_Bot | UltraAlignment_Left, + UltraAlignment_BotRight = UltraAlignment_Bot | UltraAlignment_Right, +}; \ No newline at end of file diff --git a/source/ultra/canvas.cpp b/source/ultra/canvas.cpp index aea5813..83ccb54 100644 --- a/source/ultra/canvas.cpp +++ b/source/ultra/canvas.cpp @@ -2,9 +2,9 @@ namespace PD { namespace Ultra { -PD_API Canvas::Canvas() {} +PD_API Canvas::Canvas() { pRev = 0; } -PD_API Canvas::Canvas(const PD::fvec2& size) : pViewport(size) {} +PD_API Canvas::Canvas(const PD::fvec2& size) : pViewport(size) { pRev = 0; } PD_API Canvas::~Canvas() {} @@ -15,9 +15,11 @@ PD_API void Canvas::SetVirtualViewport(const PD::fvec2& size) { pViewport.y / pVirtualViewPort.y); pVoff = (pViewport - (pVirtualViewPort * pVfactor)) * 0.5f; } + pRev++; } PD_API void Canvas::SetViewport(const PD::fvec2& size) { + if (pViewport == size) return; pViewport = size; SetVirtualViewport(pVirtualViewPort); // recalculate Vfactor } @@ -55,6 +57,8 @@ PD_API PD::Li::Rect Canvas::VTranslateObject(const PD::fvec2& pos, final.x = pos.x * pVfactor; } else if (align & UltraAlignment_Right) { final.x = pViewport.x - (pos.x * pVfactor) - nsize.x; + } else if (align & UltraAlignment_CenterHorizontal) { + final.x = pViewport.x * 0.5 - nsize.x * 0.5 + pos.x * pVfactor; } else { final.x = pVoff.x + (pos.x * pVfactor) - nsize.x * 0.5; } @@ -62,17 +66,49 @@ PD_API PD::Li::Rect Canvas::VTranslateObject(const PD::fvec2& pos, final.y = pos.y * pVfactor; } else if (align & UltraAlignment_Bot) { final.y = pViewport.y - (pos.y * pVfactor) - nsize.y; + } else if (align & UltraAlignment_CenterVertical) { + final.y = pViewport.y * 0.5 - nsize.y * 0.5 + pos.y * pVfactor; } else { final.y = pVoff.y + (pos.y * pVfactor) - nsize.y * 0.5; } return PD::fvec4(final, final + nsize); } +PD_API PD::Li::Rect Canvas::TranslateObject(const PD::fvec2& pos, + const PD::fvec2& size, + UltraAlignment align, + bool size_modified) const { + PD::fvec2 nsize = size; + if (!size_modified) nsize *= pViewport; + PD::fvec2 final; + if (align & UltraAlignment_Left) { + final.x = pos.x * pViewport.x; + } else if (align & UltraAlignment_Right) { + final.x = pViewport.x - (pos.x * pViewport.x) - nsize.x; + } else if (align & UltraAlignment_CenterHorizontal) { + final.x = pViewport.x * 0.5 - nsize.x * 0.5 + pos.x * pViewport.x; + } else { + final.x = pVoff.x + (pos.x * pViewport.x) - nsize.x * 0.5; + } + if (align & UltraAlignment_Top) { + final.y = pos.y * pViewport.y; + } else if (align & UltraAlignment_Bot) { + final.y = pViewport.y - (pos.y * pViewport.y) - nsize.y; + } else if (align & UltraAlignment_CenterVertical) { + final.y = pViewport.y * 0.5 - nsize.y * 0.5 + pos.y * pViewport.y; + } else { + final.y = pVoff.y + (pos.y * pViewport.y) - nsize.y * 0.5; + } + return PD::fvec4(final, final + nsize); +} + PD_API PD::fvec2 Canvas::VTranslateAlignPos(const PD::fvec2& pos, const PD::fvec2& size, UltraAlignment align) const { return VTranslateObject(pos, size, align).TopLeft(); } +PD_API const PD::u32& Canvas::GetRevision() const { return pRev; } + } // namespace Ultra } // namespace PD \ No newline at end of file diff --git a/source/ultra/elems/element.cpp b/source/ultra/elems/element.cpp new file mode 100644 index 0000000..77992ed --- /dev/null +++ b/source/ultra/elems/element.cpp @@ -0,0 +1,26 @@ +#include +#include + +namespace PD { +namespace Ultra { +PD_API bool ElementBase::RevisionUpdate(PD::u32 req) { + if (req != pCanvasRev) { + pCanvasRev = req; + return true; + } else { + return false; + } +} +PD_API void ElementBase::Update(Canvas& c) { + if (RevisionUpdate(c.GetRevision())) { + if (pParent) { + pRenderspace = c.VTranslateObject( + pParent->pRenderspace.TopLeft(), + pRenderspace.BotRight() - pRenderspace.TopLeft(), pAlignment, true); + } else { + pRenderspace = c.VTranslateObject(pPos, pSize, pAlignment); + } + } +} +} // namespace Ultra +} // namespace PD \ No newline at end of file diff --git a/source/ultra/elems/image.cpp b/source/ultra/elems/image.cpp new file mode 100644 index 0000000..b04e182 --- /dev/null +++ b/source/ultra/elems/image.cpp @@ -0,0 +1,11 @@ +#include + +namespace PD { +namespace Ultra { +PD_API void Image::Draw(PD::Li::Drawlist& l) { + l.BindTexture(*pTex); + l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), pRounding); + l.PathFill(pColor); +} +} // namespace Ultra +} // namespace PD \ No newline at end of file diff --git a/source/ultra/elems/rect.cpp b/source/ultra/elems/rect.cpp index 12a7321..88d5f55 100644 --- a/source/ultra/elems/rect.cpp +++ b/source/ultra/elems/rect.cpp @@ -3,8 +3,10 @@ namespace PD { namespace Ultra { PD_API void Rect::Draw(PD::Li::Drawlist& l) { - l.PathRect(pPos, pPos + pSize, pRounding); + l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), pRounding); l.PathFill(pColor); + l.DrawText(pRenderspace.BotRight(), std::to_string(pCanvasRev).c_str(), + 0xff0000ff); } } // namespace Ultra } // namespace PD \ No newline at end of file diff --git a/source/ultra/elems/text.cpp b/source/ultra/elems/text.cpp index 0b284af..5680967 100644 --- a/source/ultra/elems/text.cpp +++ b/source/ultra/elems/text.cpp @@ -3,7 +3,7 @@ namespace PD { namespace Ultra { PD_API void Text::Draw(PD::Li::Drawlist& l) { - l.DrawText(pPos, pText.c_str(), pColor); + l.DrawText(pRenderspace.TopLeft(), pText.c_str(), pColor); } } // namespace Ultra } // namespace PD \ No newline at end of file diff --git a/source/ultra/layout.cpp b/source/ultra/layout.cpp index c05894d..8e56c30 100644 --- a/source/ultra/layout.cpp +++ b/source/ultra/layout.cpp @@ -9,6 +9,7 @@ PD_API void Layout::SetFont(PD::Li::Font& font) { pList.SetFont(&font); } PD_API void Layout::Render() { pList.Clear(); for (auto& it : pElements) { + it->Update(pCanvas); it->Draw(pList); } } diff --git a/tests/gfx/source/main.cpp b/tests/gfx/source/main.cpp index f540640..d8b2fc3 100644 --- a/tests/gfx/source/main.cpp +++ b/tests/gfx/source/main.cpp @@ -4,9 +4,11 @@ #include //// +#include #include #include #include + //// PD::OsCtx* pOs = nullptr; @@ -65,15 +67,18 @@ int main(int argc, char** argv) { PD::Ultra::Layout lyt; lyt.GetCanvas().SetVirtualViewport(PD::fvec2(400, 240)); lyt.SetFont(font); - PD::Ultra::Rect rr; + PD::Ultra::Rect rr( + 0, 0, 90, 60, PD::Color("#ffff00"), 12.f, + UltraAlignment_CenterHorizontal | UltraAlignment_CenterVertical); PD::Ultra::Text txt; - rr.SetPosition(12, 20); - rr.SetSize(90, 60); - rr.SetColor(PD::Color("#ff00ff")); + PD::Ultra::Image _img(&pTex, 10, 10, 5.f, UltraAlignment_BotRight); + _img.SetSize(90); // Override size lyt.Add(rr); + lyt.Add(_img); txt.SetPosition(0); txt.SetColor(0xffffffff); txt.SetText("const std::string &text"); + txt.SetAlignment(UltraAlignment_TopLeft); lyt.Add(txt); while (pOs->Mainloop()) { lyt.GetCanvas().SetViewport(pOs->GetViewport());