Added support for SVG images inUI + added a texture flag to flip on load

This commit is contained in:
antopilo
2024-09-16 08:57:52 -04:00
parent 24be72b10a
commit ae426f7f17
3 changed files with 74 additions and 33 deletions

View File

@@ -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()
{
}
}

View File

@@ -89,5 +89,8 @@ namespace Nuake
json Serialize() override;
bool Deserialize(const json& j) override;
private:
void FlipOnLoad();
};
}

View File

@@ -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 <nanosvg.h>
#include <nanosvgrast.h>
#include <src/UI/Font/FontManager.h>
@@ -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<Texture>(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<Texture>(flags, Vector2{ w, h }, img);
ComputedStyle.BackgroundImage = texture;
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
}
else
{