Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion include/Util/Text.hpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I set the alignment to CENTER, it appears the same as when I set it to LEFT Here's how I tested it.

void GiraffeText::Update() {
    m_Text->SetWrapLength(1000);

    if (Util::Input::IsKeyDown(Util::Keycode::U)) {
        LOG_DEBUG("RIGHT");
        m_Text->SetAlignment(Util::Text::Alignment::RIGHT);
    }

    if (Util::Input::IsKeyDown(Util::Keycode::Y)) {
        LOG_DEBUG("CENTER");
        m_Text->SetAlignment(Util::Text::Alignment::CENTER);
    }

    if (Util::Input::IsKeyDown(Util::Keycode::T)) {
        LOG_DEBUG("LEFT");
        m_Text->SetAlignment(Util::Text::Alignment::LEFT);
    }

    m_Text->SetText("7414");
    m_Text->SetColor(Util::Color::FromName(Util::Colors::RED));
}

image

image

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like CENTER is working and LEFT is not working instead
LEFT
image

CENTER
image

will try to figure out what caused it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I extended the string to the next line, it magically started working.

image

image

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after some investigation, I think SDL_ttf does some hacks under the hood
since left alignment doesn't require any offset, the surface returned doesn't account for the wrap length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK if this would break stuff if people use Core::Drawable::GetSize() to align stuff because Util::Image::GetSize() uses the surface width returned from SDL_ttf

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,26 @@ namespace Util {
*/
class Text : public Core::Drawable {
public:
enum class Alignment {
LEFT = TTF_WRAPPED_ALIGN_LEFT,
CENTER = TTF_WRAPPED_ALIGN_CENTER,
RIGHT = TTF_WRAPPED_ALIGN_RIGHT,
};

/**
* @brief Constructor for the Text class.
*
* @param font The font to use for the text.
* @param size The size of the text.
* @param text The actual text content.
* @param color The color of the text (default is gray).
* @param alignment The alignment of the text (default is right-aligned).
* @param wrapLength The maximum length at which text should wrap (default
* is 0, no wrapping).
*/
Text(const std::string &font, int size, const std::string &text,
const Util::Color &color = Color(127, 127, 127));
const Util::Color &color = Color(127, 127, 127),
Alignment alignment = Alignment::LEFT, int wrapLength = 0);

glm::vec2 GetSize() const override { return m_Size; };

Expand All @@ -48,6 +66,25 @@ class Text : public Core::Drawable {
ApplyTexture();
};

/**
* @brief Set the maximum length at which text should wrap.
*
* The text will only wrap on `\n` when set to 0
*
* @param wrapLength The maximum length at which text should wrap.
*/
void SetWrapLength(int wrapLength) {
m_WrapLength = wrapLength;
ApplyTexture();
}

/**
* @brief Set the alignment of the text.
*
* @param alignment The alignment to set.
*/
void SetAlignment(Alignment alignment);

/**
* @brief Draws the text with a given transform and z-index.
*
Expand Down Expand Up @@ -81,6 +118,7 @@ class Text : public Core::Drawable {
std::string m_Text;
Util::Color m_Color;
glm::vec2 m_Size;
int m_WrapLength;
};
} // namespace Util

Expand Down
16 changes: 12 additions & 4 deletions src/Util/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace Util {
Text::Text(const std::string &font, int fontSize, const std::string &text,
const Util::Color &color)
const Util::Color &color, Alignment alignment, int wrapLength)
: m_Text(text),
m_Color(color) {
m_Color(color),
m_WrapLength(wrapLength) {
if (s_Program == nullptr) {
InitProgram();
}
Expand All @@ -30,10 +31,12 @@ Text::Text(const std::string &font, int fontSize, const std::string &text,
LOG_ERROR("{}", TTF_GetError());
}

TTF_SetFontWrappedAlign(m_Font.get(), static_cast<int>(alignment));

auto surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
m_Color.ToSdlColor(), m_WrapLength),
SDL_FreeSurface,
};
if (surface == nullptr) {
Expand All @@ -48,6 +51,11 @@ Text::Text(const std::string &font, int fontSize, const std::string &text,
m_Size = {surface->pitch / surface->format->BytesPerPixel, surface->h};
}

void Text::SetAlignment(Alignment alignment) {
TTF_SetFontWrappedAlign(m_Font.get(), static_cast<int>(alignment));
ApplyTexture();
}

void Text::Draw(const Util::Transform &transform, const float zIndex) {
auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex);
s_UniformBuffer->SetData(0, data);
Expand Down Expand Up @@ -115,7 +123,7 @@ void Text::ApplyTexture() {
auto surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
m_Color.ToSdlColor(), m_WrapLength),
SDL_FreeSurface,
};
if (surface == nullptr) {
Expand Down