From ae426f7f17c9855aa12cad149dfd54f7c8c0239e Mon Sep 17 00:00:00 2001 From: antopilo Date: Mon, 16 Sep 2024 08:57:52 -0400 Subject: [PATCH] Added support for SVG images inUI + added a texture flag to flip on load --- Nuake/src/Rendering/Textures/Texture.cpp | 32 ++++++++++- Nuake/src/Rendering/Textures/Texture.h | 3 + Nuake/src/UI/Nodes/Node.cpp | 72 +++++++++++++----------- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/Nuake/src/Rendering/Textures/Texture.cpp b/Nuake/src/Rendering/Textures/Texture.cpp index 15c5468d..ee60db60 100644 --- a/Nuake/src/Rendering/Textures/Texture.cpp +++ b/Nuake/src/Rendering/Textures/Texture.cpp @@ -48,9 +48,34 @@ namespace Nuake m_Width = size.x; m_Height = size.y; + void* tempData = data; + if (flags.flipVertical) + { + int row; + size_t bytes_per_row = (size_t)m_Width * 4; + stbi_uc temp[2048]; + stbi_uc* bytes = (stbi_uc*)tempData; + + for (row = 0; row < ((int)m_Width >> 1); row++) { + stbi_uc* row0 = bytes + row * bytes_per_row; + stbi_uc* row1 = bytes + ((int)m_Width - row - 1) * bytes_per_row; + // swap row0 with row1 + size_t bytes_left = bytes_per_row; + while (bytes_left) { + size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); + memcpy(temp, row0, bytes_copy); + memcpy(row0, row1, bytes_copy); + memcpy(row1, temp, bytes_copy); + row0 += bytes_copy; + row1 += bytes_copy; + bytes_left -= bytes_copy; + } + } + } + glGenTextures(1, &m_RendererId); glBindTexture(GL_TEXTURE_2D, m_RendererId); - glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)flags.pixelFormat, size.x, size.y, 0, (GLenum)flags.pixelFormat, (GLenum)flags.pixelDataType, data); + glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)flags.pixelFormat, size.x, size.y, 0, (GLenum)flags.pixelFormat, (GLenum)flags.pixelDataType, tempData); SetMagnificationFilter(flags.magFilter); SetMinificationFilter(flags.minFilter); @@ -188,4 +213,9 @@ namespace Nuake return true; } + + void Texture::FlipOnLoad() + { + + } } diff --git a/Nuake/src/Rendering/Textures/Texture.h b/Nuake/src/Rendering/Textures/Texture.h index e69ae2f7..a840a225 100644 --- a/Nuake/src/Rendering/Textures/Texture.h +++ b/Nuake/src/Rendering/Textures/Texture.h @@ -89,5 +89,8 @@ namespace Nuake json Serialize() override; bool Deserialize(const json& j) override; + + private: + void FlipOnLoad(); }; } diff --git a/Nuake/src/UI/Nodes/Node.cpp b/Nuake/src/UI/Nodes/Node.cpp index cb32fde4..3164d8e9 100644 --- a/Nuake/src/UI/Nodes/Node.cpp +++ b/Nuake/src/UI/Nodes/Node.cpp @@ -1,4 +1,5 @@ #include "Node.h" +#include "src/Core/String.h" #include "../Renderer.h" #include "src/FileSystem/FileSystem.h" @@ -6,6 +7,8 @@ #include "../StringHelper.h" #include "src/UI/Nodes/Canvas.h" +#define NANOSVGRAST_IMPLEMENTATION +#define NANOSVG_IMPLEMENTATION #include #include #include @@ -477,47 +480,52 @@ namespace NuakeUI ComputedStyle.ZIndex = value.value.Number; break; case StyleProperties::Font: - if (FileSystem::FileExists(value.string, true)) + { + bool fileExists = FileSystem::FileExists(Nuake::String::ReplaceSlash(value.string), true); + if (fileExists) { ComputedStyle.FontFamily = FontManager::Get().GetFont(value.string); } - break; + } + break; case StyleProperties::BackgroundImage: { if (ComputedStyle.BackgroundImage == nullptr) { if (StringHelper::EndsWith(value.string, ".svg")) { - //NSVGimage* image = NULL; - //NSVGrasterizer* rast = NULL; - //unsigned char* img = NULL; - // - //int w, h; - //image = nsvgParseFromFile(value.string.c_str(), "px", 96.0f); - //if (image == NULL) { - // printf("Could not open SVG image.\n"); - // - //} - //w = (int)image->width; - //h = (int)image->height; - // - //rast = nsvgCreateRasterizer(); - //if (rast == NULL) { - // printf("Could not init rasterizer.\n"); - //} - // - //img = (unsigned char*)malloc(w * h * 4); - //if (img == NULL) { - // printf("Could not alloc image buffer.\n"); - //} - // - //nsvgRasterize(rast, image, 0, 0, 1, img, w, h, w * 4); - // - //auto texture = std::make_shared(img, w * h * 4); - //ComputedStyle.BackgroundImage = texture; - // - //nsvgDeleteRasterizer(rast); - //nsvgDelete(image); + NSVGimage* image = NULL; + NSVGrasterizer* rast = NULL; + unsigned char* img = NULL; + + int w, h; + image = nsvgParseFromFile(value.string.c_str(), "px", 96.0f); + if (image == NULL) { + printf("Could not open SVG image.\n"); + + } + w = (int)image->width; + h = (int)image->height; + + rast = nsvgCreateRasterizer(); + if (rast == NULL) { + printf("Could not init rasterizer.\n"); + } + + img = (unsigned char*)malloc(w * h * 4); + if (img == NULL) { + printf("Could not alloc image buffer.\n"); + } + + nsvgRasterize(rast, image, 0, 0, 1, img, w, h, w * 4); + + TextureFlags flags; + flags.flipVertical = true; + auto texture = std::make_shared(flags, Vector2{ w, h }, img); + ComputedStyle.BackgroundImage = texture; + + nsvgDeleteRasterizer(rast); + nsvgDelete(image); } else {