From f75f7067ffe44ffafc852691b1bb56867842bb6f Mon Sep 17 00:00:00 2001 From: tobid7 Date: Fri, 2 May 2025 15:09:21 +0200 Subject: [PATCH] # Fixes - Fix LinearAlloc bug not using *sizeof(T) - Add WaitForRead to net backend - Add a Get func to Tween - Skip \r in Text Rendering - Add Citro3D Max Texsize check --- backends/3ds/include/li_backend_c3d.hpp | 3 +- backends/3ds/include/pd_net_3ds.hpp | 14 ++++++ backends/3ds/source/li_backend_c3d.cpp | 6 ++- backends/desktop/include/pd_net_desktop.hpp | 27 ++++++++--- include/pd/core/color.hpp | 51 +++++++++++++++------ include/pd/core/tween.hpp | 12 +++-- include/pd/net/backend.hpp | 1 + include/pd/net/socket.hpp | 1 + source/lithium/font.cpp | 2 +- source/net/socket.cpp | 4 ++ 10 files changed, 91 insertions(+), 30 deletions(-) diff --git a/backends/3ds/include/li_backend_c3d.hpp b/backends/3ds/include/li_backend_c3d.hpp index 268cbb3..2ad4248 100644 --- a/backends/3ds/include/li_backend_c3d.hpp +++ b/backends/3ds/include/li_backend_c3d.hpp @@ -39,7 +39,8 @@ class LinearAlloc : public Allocator { LinearAlloc() = default; ~LinearAlloc() = default; - T* Allocate(size_t n) override { return (T*)linearAlloc(n); } + /** Never forget the sizeof(T) again (most painful bug i created) */ + T* Allocate(size_t n) override { return (T*)linearAlloc(n * sizeof(T)); } void Deallocate(T* ptr) { linearFree(ptr); } }; namespace LI { diff --git a/backends/3ds/include/pd_net_3ds.hpp b/backends/3ds/include/pd_net_3ds.hpp index fbb1630..a2a60ce 100644 --- a/backends/3ds/include/pd_net_3ds.hpp +++ b/backends/3ds/include/pd_net_3ds.hpp @@ -66,6 +66,20 @@ class NetBackend3DS : public Net::Backend { bool Listen(int sock_id, int backlog = 5) { return listen(sock_id, backlog) != -1; } + + bool WaitForRead(int sock_id, int timeout_ms) override { + fd_set set; + FD_ZERO(&set); + FD_SET(sock_id, &set); + + timeval timeout{}; + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = (timeout_ms % 1000) * 1000; + + int result = select(sock_id + 1, &set, nullptr, nullptr, &timeout); + return (result > 0 && FD_ISSET(sock_id, &set)); + } + bool Accept(int sock_id, Net::Socket::Ref client) { int client_soc = accept(sock_id, nullptr, nullptr); if (client_soc == -1) { diff --git a/backends/3ds/source/li_backend_c3d.cpp b/backends/3ds/source/li_backend_c3d.cpp index adc6b74..5bf8e8a 100644 --- a/backends/3ds/source/li_backend_c3d.cpp +++ b/backends/3ds/source/li_backend_c3d.cpp @@ -44,7 +44,6 @@ unsigned char li_shader[] = { }; // clang-format on size_t li_shader_size = 0x124; - namespace PD { namespace LI { GPU_TEXCOLOR GetTexFmt(Texture::Type type) { @@ -170,6 +169,9 @@ PD::LI::Texture::Ref Backend_C3D::LoadTexture(const std::vector& pixels, PD::LI::Texture::Type type, PD::LI::Texture::Filter filter) { std::cout << "Loading Tex " << std::format("[{}, {}]", w, h) << std::endl; + if (w > 1024 || h > 1024) { + return nullptr; + } // Don't check here as check done before PD::LI::Texture::Ref res = PD::LI::Texture::New(); int bpp = GetBPP(type); @@ -226,4 +228,4 @@ PD::LI::Texture::Ref Backend_C3D::LoadTexture(const std::vector& pixels, return res; } } // namespace LI -} // namespace PD \ No newline at end of file +} // namespace PD diff --git a/backends/desktop/include/pd_net_desktop.hpp b/backends/desktop/include/pd_net_desktop.hpp index feccb3f..3383daf 100644 --- a/backends/desktop/include/pd_net_desktop.hpp +++ b/backends/desktop/include/pd_net_desktop.hpp @@ -33,6 +33,7 @@ SOFTWARE. #include #include #include +#include #endif #include @@ -76,17 +77,31 @@ class NetBackendDesktop : public PD::Net::Backend { #endif } - bool Bind(int sock_id, u16 port) { + bool Bind(int sock_id, u16 port) override { sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; return bind(sock_id, (sockaddr*)&addr, sizeof(addr)) != -1; } - bool Listen(int sock_id, int backlog = 5) { + bool Listen(int sock_id, int backlog = 5) override { return listen(sock_id, backlog) != -1; } - bool Accept(int sock_id, Net::Socket::Ref client) { + + bool WaitForRead(int sock_id, int timeout_ms) override { + fd_set set; + FD_ZERO(&set); + FD_SET(sock_id, &set); + + timeval timeout{}; + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = (timeout_ms % 1000) * 1000; + + int result = select(sock_id + 1, &set, nullptr, nullptr, &timeout); + return (result > 0 && FD_ISSET(sock_id, &set)); + } + + bool Accept(int sock_id, Net::Socket::Ref client) override { int client_soc = accept(sock_id, nullptr, nullptr); if (client_soc == GetInvalidRef()) { return false; @@ -94,17 +109,17 @@ class NetBackendDesktop : public PD::Net::Backend { client->pSocket = client_soc; return true; } - bool Connect(int sock_id, const std::string& ip, u16 port) { + bool Connect(int sock_id, const std::string& ip, u16 port) override { sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons(port); inet_pton(AF_INET, ip.c_str(), &addr.sin_addr); return connect(sock_id, (sockaddr*)&addr, sizeof(addr)) != -1; } - int Send(int sock_id, const std::string& data) { + int Send(int sock_id, const std::string& data) override { return send(sock_id, data.c_str(), static_cast(data.size()), 0); } - int Receive(int sock_id, std::string& data, int size = 1024) { + int Receive(int sock_id, std::string& data, int size = 1024) override { char* tmp = new char[size]; int res = recv(sock_id, tmp, size, 0); if (res > 0) { diff --git a/include/pd/core/color.hpp b/include/pd/core/color.hpp index c3d778c..2d1c3c7 100644 --- a/include/pd/core/color.hpp +++ b/include/pd/core/color.hpp @@ -37,16 +37,28 @@ namespace PD { * @note Safetey checks are disabled for maximum performance */ class PD_CORE_API Color { + private: + /** Red Value */ + u8 m_r; + /** Green Value */ + u8 m_g; + /** Blue Value */ + u8 m_b; + /** Alpha Value */ + u8 m_a; + public: /** * Default Constructor (all variables are set to 0) */ - Color() : m_r(0), m_g(0), m_b(0), m_a(0) {} + // Color() : m_r(0), m_g(0), m_b(0), m_a(0) {} + constexpr Color() : m_r(0), m_g(0), m_b(0), m_a(0) {} + constexpr ~Color() {} /** * Constructor for 32Bit Color Input * @param color 32Bit Color value */ - Color(u32 color) { + constexpr Color(u32 color) { m_a = (color >> 24) & 0xff; m_b = (color >> 16) & 0xff; m_g = (color >> 8) & 0xff; @@ -59,7 +71,7 @@ class PD_CORE_API Color { * @param b Blue Value * @param a Optional Alpha Value (Defaults to 255) */ - Color(u8 r, u8 g, u8 b, u8 a = 255) { + constexpr Color(int r, int g, int b, int a = 255) { m_r = r; m_g = g; m_b = b; @@ -73,7 +85,7 @@ class PD_CORE_API Color { * @param a Optional Alpha Value (Defaults to 1.0f) * @note There is no Check if the number is between 0.0 and 1.0 */ - Color(float r, float g, float b, float a = 1.f) { + constexpr Color(float r, float g, float b, float a = 1.f) { m_r = static_cast(255.f * r); m_g = static_cast(255.f * g); m_b = static_cast(255.f * b); @@ -87,7 +99,7 @@ class PD_CORE_API Color { /** * Unused Deconstructor */ - ~Color() {} + // ~Color() {} /** * Create Color Object by Hex String @@ -196,15 +208,24 @@ class PD_CORE_API Color { * @return 32Bit Color Value */ operator u32() const { return Get(); } - - private: - /** Red Value */ - u8 m_r; - /** Green Value */ - u8 m_g; - /** Blue Value */ - u8 m_b; - /** Alpha Value */ - u8 m_a; }; +namespace Colors { +constexpr Color White = Color(1.f, 1.f, 1.f, 1.f); +constexpr Color Black = Color(0.f, 0.f, 0.f, 1.f); +constexpr Color Red = Color(1.f, 0.f, 0.f, 1.f); +constexpr Color Green = Color(0.f, 1.f, 0.f, 1.f); +constexpr Color Blue = Color(0.f, 0.f, 1.f, 1.f); +constexpr Color Yellow = Color(1.f, 1.f, 0.f, 1.f); +constexpr Color Cyan = Color(0.f, 1.f, 1.f, 1.f); +constexpr Color Magenta = Color(1.f, 0.f, 1.f, 1.f); +constexpr Color Gray = Color(0.5f, 0.5f, 0.5f, 1.f); +constexpr Color LightGray = Color(0.75f, 0.75f, 0.75f, 1.f); +constexpr Color DarkGray = Color(0.25f, 0.25f, 0.25f, 1.f); +constexpr Color Orange = Color(1.f, 0.65f, 0.f, 1.f); +constexpr Color Pink = Color(1.f, 0.75f, 0.8f, 1.f); +constexpr Color Brown = Color(0.6f, 0.4f, 0.2f, 1.f); +constexpr Color Purple = Color(0.5f, 0.f, 0.5f, 1.f); +constexpr Color Teal = Color(0.f, 0.5f, 0.5f, 1.f); +constexpr Color Transparent = Color(0.f, 0.f, 0.f, 0.f); +} // namespace Colors } // namespace PD \ No newline at end of file diff --git a/include/pd/core/tween.hpp b/include/pd/core/tween.hpp index afb9d5d..0dc9ff3 100644 --- a/include/pd/core/tween.hpp +++ b/include/pd/core/tween.hpp @@ -150,11 +150,7 @@ class Tween { return *this; } - /** - * Operator that returns the current value calculated - * by time and effect - */ - operator T() { + T Get() const { float t = 0.f; switch (effect) { case EaseInQuad: @@ -203,6 +199,12 @@ class Tween { } } + /** + * Operator that returns the current value calculated + * by time and effect + */ + operator T() const { return Get(); } + private: /** Animation Effect */ Effect effect; diff --git a/include/pd/net/backend.hpp b/include/pd/net/backend.hpp index b7b3c7d..1b39f28 100644 --- a/include/pd/net/backend.hpp +++ b/include/pd/net/backend.hpp @@ -40,6 +40,7 @@ class Backend { virtual int GetInvalidRef() const = 0; virtual bool Bind(int sock_id, u16 port) = 0; virtual bool Listen(int sock_id, int backlog = 5) = 0; + virtual bool WaitForRead(int sock_id, int timeout_ms) = 0; virtual bool Accept(int sock_id, Socket::Ref client) = 0; virtual bool Connect(int sock_id, const std::string& ip, u16 port) = 0; virtual int Send(int sock_id, const std::string& data) = 0; diff --git a/include/pd/net/socket.hpp b/include/pd/net/socket.hpp index ede6526..ef2cd07 100644 --- a/include/pd/net/socket.hpp +++ b/include/pd/net/socket.hpp @@ -46,6 +46,7 @@ class PD_NET_API Socket { bool Create(); bool Bind(u16 port); bool Listen(int backlog = 5); + bool WaitForRead(int timeout_ms); bool Accept(Socket::Ref client); bool Connect(const std::string& ip, u16 port); int Send(const std::string& data); diff --git a/source/lithium/font.cpp b/source/lithium/font.cpp index a367fee..e2311f2 100644 --- a/source/lithium/font.cpp +++ b/source/lithium/font.cpp @@ -239,7 +239,7 @@ PD_LITHIUM_API void Font::CmdTextEx(Vec &cmds, const fvec2 &pos, cmd->Tex = Tex; for (auto &jt : wline) { auto cp = GetCodepoint(jt); - if (cp.pInvalid && jt != '\n' && jt != '\t') { + if ((cp.pInvalid && jt != '\n' && jt != '\t') && jt != '\r') { continue; } if (Tex != cp.Tex) { diff --git a/source/net/socket.cpp b/source/net/socket.cpp index 3dd3c82..b1dfb07 100644 --- a/source/net/socket.cpp +++ b/source/net/socket.cpp @@ -38,6 +38,10 @@ PD_NET_API bool Socket::Listen(int backlog) { return backend->Listen(pSocket, backlog); } +PD_NET_API bool Socket::WaitForRead(int timeout_ms) { + return backend->WaitForRead(pSocket, timeout_ms); +} + PD_NET_API bool Socket::Accept(Socket::Ref client) { return backend->Accept(pSocket, client); }