Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtext] LoadFont() somehow distorts the memory of TextSplit() #4704

Closed
4 tasks done
AnkurMal opened this issue Jan 18, 2025 · 2 comments
Closed
4 tasks done

[rtext] LoadFont() somehow distorts the memory of TextSplit() #4704

AnkurMal opened this issue Jan 18, 2025 · 2 comments

Comments

@AnkurMal
Copy link

AnkurMal commented Jan 18, 2025

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • I checked the documentation on the wiki
  • My code has no errors or misuse of raylib

Issue description

Essentially, the array of strings (splits) returned by TextSplit(), gets "garbage" value after I use it after loading a font using LoadFont().

Environment

HP 15s, Windows 11 24H2, Intel iRIS Xe

Code Example

#include "include/raylib.h"

int main(void) {
    int count;

    InitWindow(800, 450, "sample");

    char* data = "1.2.3.4";
    const char **spl = TextSplit(data, '.', &count);

    Font font = LoadFont("assets/font/font.ttf");
    for(int i=0; i<count; i++) printf("%s\n", spl[i]);

    SetTargetFPS(60);
    while (!WindowShouldClose()) {
        BeginDrawing();
            ClearBackground(BLACK);
        EndDrawing();
    }
    
    UnloadFont(font);
    CloseWindow();
    return 0;
}

here, if I put the line for(int i=0; i<count; i++) printf("%s\n", spl[i]); before Font font = LoadFont("assets/font/font.ttf");, it prints the output as it should be, i,e,

1
2
3
4

But if I put it after that, it produces garbage:

.ttf
tf
@AnkurMal AnkurMal changed the title [rtext] Loadfont somehow distorts the memory of TextSplit [rtext] Loadfont() somehow distorts the memory of TextSplit() Jan 18, 2025
@AnkurMal AnkurMal changed the title [rtext] Loadfont() somehow distorts the memory of TextSplit() [rtext] LoadFont() somehow distorts the memory of TextSplit() Jan 18, 2025
@raysan5
Copy link
Owner

raysan5 commented Jan 18, 2025

@AnkurMal Interesting issue... not sure what could be the cause...

@asdqwe Just reviewed the implementation of TextSplit() and look good to me, it keeps a copy of the input text in an internal buffer and splits the strings by the delimiter character, keeping the pointers to every piece of text (on the internal buffer)... am I missing something?

@raysan5
Copy link
Owner

raysan5 commented Jan 18, 2025

@asdqwe Oh! I see it now! Ok, that's a design decision, raylib is intended for "immediate-mode" usage of text data (get-use-discard) moving the TextSplit() usage under LoadFont() will fix that.

No plans to redesign the function to support other behaviour but I'm adding some extra documentation on raylib.h for text functions.

@raysan5 raysan5 closed this as completed Jan 18, 2025
ngynkvn pushed a commit to ngynkvn/raylib that referenced this issue Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants