text changes

This commit is contained in:
Jorijn van der Graaf 2025-11-26 03:35:58 +01:00
commit d5ee272290
6 changed files with 135 additions and 54 deletions

View file

@ -30,11 +30,11 @@ import std;
using namespace Crafter; using namespace Crafter;
ImageElement::ImageElement() : RenderingElement() { ImageElement::ImageElement() : RenderingElementScaling() {
} }
ImageElement::ImageElement(const std::string_view imagePath, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElement(false, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { ImageElement::ImageElement(const std::string_view imagePath, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElementScaling(false, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
RenderImage(imagePath); RenderImage(imagePath);
} }

View file

@ -36,17 +36,63 @@ RenderingElement::RenderingElement(bool opaque, std::int_fast32_t anchorX, std::
} }
RenderingElement::RenderingElement(bool opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), opaque(opaque) { RenderingElementPreScaled::RenderingElementPreScaled(bool opaque) : RenderingElement(opaque) {
} }
void RenderingElement::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) { RenderingElementPreScaled::RenderingElementPreScaled(bool opaque, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElement(opaque, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
this->bufferWidth = width;
this->bufferHeight = height;
buffer.resize(width * height);
} }
void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const { RenderingElementScaling::RenderingElementScaling(bool opaque) : RenderingElement(opaque) {
}
RenderingElementScaling::RenderingElementScaling(bool opaque, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElement(opaque, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
}
RenderingElementScaling::RenderingElementScaling(bool opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), RenderingElement(opaque, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
}
void RenderingElementPreScaled::UpdatePosition(Window& window) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this);
if(oldWidth != scaled.width || oldHeight && scaled.height) {
bufferScaled.resize(scaled.width * scaled.height);
}
for(Transform* child : children) {
child->UpdatePosition(window, *this);
}
}
void RenderingElementPreScaled::UpdatePosition(Window& window, Transform& parent) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this, parent);
if(oldWidth != scaled.width || oldHeight && scaled.height) {
bufferScaled.resize(scaled.width * scaled.height);
}
for(Transform* child : children) {
child->UpdatePosition(window, *this);
}
}
void RenderingElementPreScaled::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const {
for (std::uint_fast32_t y = 0; y < dstHeight; y++) {
std::uint_fast32_t srcY = y * scaled.height / dstHeight;
for (std::uint_fast32_t x = 0; x < dstWidth; x++) {
std::uint_fast32_t srcX = x * scaled.width / dstWidth;
dst[y * dstWidth + x] = bufferScaled[srcY * scaled.width + srcX];
}
}
}
void RenderingElementScaling::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const {
for (std::uint_fast32_t y = 0; y < dstHeight; y++) { for (std::uint_fast32_t y = 0; y < dstHeight; y++) {
std::uint_fast32_t srcY = y * bufferHeight / dstHeight; std::uint_fast32_t srcY = y * bufferHeight / dstHeight;
for (std::uint_fast32_t x = 0; x < dstWidth; x++) { for (std::uint_fast32_t x = 0; x < dstWidth; x++) {
@ -56,7 +102,7 @@ void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uin
} }
} }
void RenderingElement::UpdatePosition(Window& window) { void RenderingElementScaling::UpdatePosition(Window& window) {
std::uint_fast32_t oldWidth = scaled.width; std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height; std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this); window.ScaleElement(*this);
@ -69,7 +115,7 @@ void RenderingElement::UpdatePosition(Window& window) {
} }
} }
void RenderingElement::UpdatePosition(Window& window, Transform& parent) { void RenderingElementScaling::UpdatePosition(Window& window, Transform& parent) {
std::uint_fast32_t oldWidth = scaled.width; std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height; std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this, parent); window.ScaleElement(*this, parent);
@ -77,9 +123,13 @@ void RenderingElement::UpdatePosition(Window& window, Transform& parent) {
bufferScaled.resize(scaled.width * scaled.height); bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height); CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
} }
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
for(Transform* child : children) { for(Transform* child : children) {
child->UpdatePosition(window, *this); child->UpdatePosition(window, *this);
} }
} }
void RenderingElementScaling::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) {
this->bufferWidth = width;
this->bufferHeight = height;
buffer.resize(width * height);
}

View file

@ -30,31 +30,35 @@ import std;
using namespace Crafter; using namespace Crafter;
TextElement::TextElement() : RenderingElement() { TextElement::TextElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElementPreScaled(false, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
} }
TextElement::TextElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElement(false, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
RenderText(text, size, pixel, font);
}
void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font) { void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font) {
buffer.clear(); // Calculate the actual size needed for the text
float scale = stbtt_ScaleForPixelHeight(&font.font, size); float scale = stbtt_ScaleForPixelHeight(&font.font, size);
int baseline = (int)(font.ascent * scale); int baseline = (int)(font.ascent * scale);
std::uint_fast32_t bufferWidth = 0; std::uint_fast32_t textWidth = 0;
for (const char c : text) { for (const char c : text) {
int advance, lsb; int advance, lsb;
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb); stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
bufferWidth += (int)(advance * scale); textWidth += (int)(advance * scale);
} }
ResizeBuffer(bufferWidth, (font.ascent - font.descent) * scale); // Clear the scaled buffer
for (auto& pixel : bufferScaled) {
pixel = {0, 0, 0, 0};
}
int x = 0; // Only render text if we have space
if (textWidth <= scaled.width && (font.ascent - font.descent) * scale <= scaled.height) {
// Calculate starting position to center text horizontally and vertically
int startX = (scaled.width - textWidth) / 2;
int startY = ( scaled.height - (font.ascent - font.descent) * scale) / 2;
startY += baseline; // Adjust for baseline
int x = startX;
for (std::uint_fast32_t i = 0; i < text.size(); i++) { for (std::uint_fast32_t i = 0; i < text.size(); i++) {
int codepoint = text[i]; int codepoint = text[i];
@ -71,9 +75,16 @@ void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_
std::vector<unsigned char> bitmap(w * h); std::vector<unsigned char> bitmap(w * h);
stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint); stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint);
// Only render characters that fit within the scaled bounds
for (int j = 0; j < h; j++) { for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) { for (int i = 0; i < w; i++) {
buffer[(baseline + j + c_y1) * bufferWidth + (x + i + c_x1)] = {color.r, color.g, color.b, bitmap[j * w + i]}; int bufferX = x + i + c_x1;
int bufferY = startY + j + c_y1;
// Only draw pixels that are within our scaled buffer bounds
if (bufferX >= 0 && bufferX < (int)scaled.width && bufferY >= 0 && bufferY < (int) scaled.height) {
bufferScaled[bufferY * scaled.width + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
}
} }
} }
@ -84,3 +95,4 @@ void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_
} }
} }
} }
}

View file

@ -23,7 +23,7 @@ import :RenderingElement;
import :Types; import :Types;
export namespace Crafter { export namespace Crafter {
class ImageElement : public RenderingElement { class ImageElement : public RenderingElementScaling {
public: public:
ImageElement(); ImageElement();
ImageElement(const std::string_view imagePath, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); ImageElement(const std::string_view imagePath, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);

View file

@ -24,20 +24,41 @@ import :Types;
export namespace Crafter { export namespace Crafter {
class Window; class Window;
class Font;
class RenderingElement : public Transform { class RenderingElement : public Transform {
public: public:
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled; std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled;
std::uint_fast32_t bufferWidth;
std::uint_fast32_t bufferHeight;
bool opaque = false; bool opaque = false;
RenderingElement(bool opague = false); RenderingElement(bool opague = false);
RenderingElement(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling); RenderingElement(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElement(bool opague, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
RenderingElement(RenderingElement&) = delete; RenderingElement(RenderingElement&) = delete;
RenderingElement& operator=(RenderingElement&) = delete; RenderingElement& operator=(RenderingElement&) = delete;
};
class RenderingElementPreScaled : public RenderingElement {
public:
RenderingElementPreScaled(bool opague = false);
RenderingElementPreScaled(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElementPreScaled(RenderingElementPreScaled&) = delete;
RenderingElementPreScaled& operator=(RenderingElementPreScaled&) = delete;
void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const;
void UpdatePosition(Window& window) override;
void UpdatePosition(Window& window, Transform& parent) override;
};
class RenderingElementScaling: public RenderingElement {
public:
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::uint_fast32_t bufferWidth;
std::uint_fast32_t bufferHeight;
bool opaque = false;
RenderingElementScaling(bool opague = false);
RenderingElementScaling(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElementScaling(bool opague, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
RenderingElementScaling(RenderingElementScaling&) = delete;
RenderingElementScaling& operator=(RenderingElementScaling&) = delete;
void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height); void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height);
void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const; void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const;

View file

@ -24,11 +24,9 @@ import :Types;
import :Font; import :Font;
export namespace Crafter { export namespace Crafter {
class TextElement : public RenderingElement { class TextElement : public RenderingElementPreScaled {
public: public:
TextElement(); TextElement(std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
TextElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
void RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font); void RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font);
}; };
} }