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

[text] Add DrawText() functions that take a specified length #1527

Closed
schveiguy opened this issue Jan 12, 2021 · 3 comments
Closed

[text] Add DrawText() functions that take a specified length #1527

schveiguy opened this issue Jan 12, 2021 · 3 comments

Comments

@schveiguy
Copy link
Sponsor Contributor

schveiguy commented Jan 12, 2021

Issue description

I am using raylib as a teaching tool for the D programming language. It is a wonderfully thought out and simple library, and I'm enjoying my time with it. One thing that sticks out to me when using from D is the requirement for text to be Null terminated. In most modern programming languages (D included), text consists of an array, which carries a length attribute, which makes null termination awkward and cumbersome.

Indeed, in the first line of DrawText, the code figures out the length of the text by scanning for the null terminator. A set of overloads that would accept pointer + length would be most appreciated, and the existing functions changed to wrappers that first fetch the length by searching for Null.

In D, it is quite cumbersome to work with string data that is null terminated, and generally the requirement is to call toStringz which allocates a new string that is null terminated when needed. Given that raylib doesn't really require null termination (and immediately converts it to a length variable in the first line of code), I think this should be a simple change. I'm willing to generate a PR if you wish.

I would recommend DrawTextN* for the name variant, for precedents like strncopy and snprintf.

I will also note that I think this is only relevant for the DrawText family of functions, because it is the only reasonable place where generated text is needed. Literals for passing to e.g. InitWindow can include the null character in the literal easily enough.

Environment

N/A

Issue Screenshot

N/A

Code Example

In D:

// uses raylib-d binding (https://github.com/onroundit/raylib-d)
string name = "foo";
// existing
DrawText(toStringz(name), 100, 100, 15, Colors.WHITE);

// proposed
DrawTextN(name.ptr, name.length, 100, 100, 15, Colors.WHITE);
@raysan5
Copy link
Owner

raysan5 commented Jan 12, 2021

@schveiguy Sorry, no plans to redesign the library to accept an extra input paramater that, actually, it's not required in C language by design.

In any case, raylib uses a custom TextLength() function, I can consider allowing some option to replace it by a custom implementation (despite requiring recompiling the library with that option yourself), would it work for you?

@raysan5 raysan5 changed the title Enhancement Request: Add DrawText functions that take a specified length [text] Add DrawText() functions that take a specified length Jan 12, 2021
@schveiguy
Copy link
Sponsor Contributor Author

Unfortunately no, because the parameter is still only a char *. The length needs to be orthogonal to the actual string data.

There are plenty of precedents of C functions that do not require null termination for character data, but you are right that the "normal" string type in C is simply a null-terminated char *. It just makes things awkward for other-language bindings that use a pointer/length combination. The toStringz usage above actually requires an extra heap allocation to add the null character.

The more wide-spread use case (even for C) is when you have data that does not contain a null character that you can't modify for some reason. Like if you only wanted to show the first N characters of a string.

And FWIW, I wasn't requesting a redesign, just additional overloads.

@raysan5
Copy link
Owner

raysan5 commented Jan 12, 2021

Sorry then, no plans to add those additional functions for the moment...

@raysan5 raysan5 closed this as completed Jan 12, 2021
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