SFML community forums

Help => Graphics => Topic started by: Mr.Goose on August 03, 2023, 05:00:53 pm

Title: National letters - input text on screen
Post by: Mr.Goose on August 03, 2023, 05:00:53 pm
Hi everyone!

I have one issue, where I need your help with. I made field, where you can write name of your character, when you making it. Everything looks fine, but I can't figure out how can I input my national (polish) letters in (ę, ó, ą, ś, ł, ż, ź, ć, ń).

Here is my code:

(click to show/hide)

I tried everything, for ex. I tried to implement unicode numbers to this "else if", but only two letters works ("Ó, ó"). Another letters are replaced by another.

Can you help me with that issue?
Title: Re: National letters - input text on screen
Post by: fallahn on August 03, 2023, 07:12:05 pm
Assuming name_input_string is a std::string (hard to tell without a namespace) try:

Code: [Select]
char_creation_name_input_txt.setString(sf::String::fromUtf8(name_input_string.begin(), name_input_string.end());
Title: Re: National letters - input text on screen
Post by: Mr.Goose on August 03, 2023, 08:20:13 pm
Unfortunetly, that doesn't work. Characters still doesn't show up or are replaced by other characters, when I delete that "else if".

I tried to change advanced save settings in Visual Studio 2022 from Middleeuropean (codepage 1250) to UTF-8, but that doesn't works too.

Yes, name_input_string is exacly string.
Title: Re: National letters - input text on screen
Post by: Hapax on August 03, 2023, 10:18:38 pm
I have to ask but are the characters you are hoping to see within the range you allow?

unicode 65-90 and 97-122.
Title: Re: National letters - input text on screen
Post by: Mr.Goose on August 05, 2023, 10:09:26 am
Yes, after fallahn suggestions I deleted that "else if" with unicode range. Unfortunetly when I do this, my national letters are replaced by other letters, for ex. "ń" is replaced by "D".

EDIT:
Additional info! When I just draw texts on screen, I just write letter "L" before string. For ex.:

(click to show/hide)

This letter "L" works, but when I want to write text directly on screen, that doesn't works.
Title: Re: National letters - input text on screen
Post by: Hapax on August 06, 2023, 09:45:02 pm
Ah, have you tried using a wide string? i.e. std::w_string

There are 'better' string types for unicode characters but, apparently, SFML doesn't support them yet.

https://www.sfml-dev.org/tutorials/2.6/graphics-text.php#how-to-avoid-problems-with-non-ascii-characters
Title: Re: National letters - input text on screen
Post by: kimci86 on August 06, 2023, 10:26:53 pm
If name_input_string is an std::string and you do:
Code: [Select]
name_input_string += ev->text.unicode;then the 4-bytes unicode character will be narrowed down to only one byte and lose information.
For example "ń" (unicode character U+0144) becomes "D" (unicode character U+0044). Only the least significant byte is added to the string.

You can use sf::String instead of std::string to store unicode characters.
Alternatively you could encode unicode characters to UTF-8 encoding to store them in an std::string.
Title: Re: National letters - input text on screen
Post by: Mr.Goose on August 08, 2023, 09:27:31 am
And that finally works!

I just change string to String, as you said.

Thank you kimci86 and everyone else for help!