Homebrew C2D_GetTextDimensions is not getting the text height properly

  • Thread starter Thread starter pvini07BR
  • Start date Start date
  • Views Views 469
  • Replies Replies 0

pvini07BR

Member
Newcomer
Joined
Sep 15, 2024
Messages
13
Reaction score
47
Trophies
0
Age
20
XP
136
Country
Brazil
I'm trying to code a BlueSky client for the Nintendo 3DS. Currently I'm dealing with the UI.
Currently, I'm making a feed, where there is several posts you can scroll through and see their contents. Each post has its own text.
The text is fetched from the web from a public API using libcurl.

The thing is, I had to write my own code to wrap text when it gets too big:
C++:
Post::Post(std::string text, float text_scale) {
    this->text = text;
    // Check if text exceeded the character limit
    while (this->text.size() > MAX_POST_CHARACTERS) {
        this->text.erase(this->text.size()-1);
    }

    this->text_scale = text_scale;
    this->height = 15.0f;
    this->textBuf = C2D_TextBufNew(MAX_POST_CHARACTERS+1);
   
    // Text wrapping code
    std::string wrapped_text = "";
    C2D_Text tempText;
    for (size_t i = 0; i < text.size(); i++) {
        wrapped_text += this->text[i];

        C2D_TextParse(&tempText, this->textBuf, wrapped_text.c_str());
        C2D_TextOptimize(&tempText);

        float t_width;
        C2D_TextGetDimensions(&tempText, text_scale, text_scale, &t_width, NULL);
        if (t_width > BOTTOM_SCREEN_WIDTH) {
            int n = wrapped_text.rfind(' ', i);
            if (n != -1) {
                wrapped_text[n] = '\n';
            } else {
                wrapped_text.insert(wrapped_text.begin() + (i-1), '\n');
            }
            //this->height += 15.0f;
        }

        C2D_TextBufClear(this->textBuf);
    }
   
    // Create C2D_Text object
    C2D_TextParse(&this->c2d_text, this->textBuf, wrapped_text.c_str());
    C2D_TextOptimize(&this->c2d_text);
   
    // Get text height
    C2D_TextGetDimensions(&this->c2d_text, this->text_scale, this->text_scale, nullptr, &this->height);
}

The code for drawing a post:

C++:
void Post::draw(float x, float y) {
    C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.0, this->text_scale, this->text_scale, C2D_Color32(255, 255, 255, 255));

    C2D_DrawLine(x, y, lineColor, x + BOTTOM_SCREEN_WIDTH, y, lineColor, 1.0f, 0.0f);
    C2D_DrawLine(x, y+this->get_height(), lineColor, x + BOTTOM_SCREEN_WIDTH, y+this->get_height(), lineColor, 1.0f, 0.0f);
}

And for some reason the text gets all jumbled:
1726414027146.png

Post automatically merged:

Nevermind, I realized the issue was that the Feed (the class responsible for drawing the posts) was calculating the Y position of the posts wrongly. I fixed it:

Before:

C++:
void Feed::draw(float h_displacement, float scrollY) {
    for (size_t i = 0; i < this->posts.size(); i++) {
        float height = this->posts[i].get_height();
        float y = i * height;
        float final_y = scrollY + y;
        float end_final_y = final_y + height;
        
        if ((final_y >= 0.0f && final_y < SCREEN_HEIGHT) || (end_final_y >= && end_final_y < SCREEN_HEIGHT)) {
            this->posts[i].draw(h_displacement, final_y);
        }
    }
}

After:

C++:
void Feed::draw(float h_displacement, float scrollY) {
    float add = 0.0f;
    for (size_t i = 0; i < this->posts.size(); i++) {
        float Yorigin = add + scrollY;
        float Yend = Yorigin + this->posts[i].get_height();

        if ((Yorigin >= 0.0 && Yorigin < (float)SCREEN_HEIGHT) || (Yend >= 0.0 && Yend < (float)SCREEN_HEIGHT)) {
            this->posts[i].draw(h_displacement, Yorigin);
        }

        add += this->posts[i].get_height();
    }
}
 
Last edited by pvini07BR,

Site & Scene News

Popular threads in this forum