Getting text in SDL2

Getting text in SDL2

Postby Sauer2 » 18 Jun 2013, 23:25

Good morning everyone,

i'm experimenting in implementing some kind of textbox for SDL.
In order to do that, I need some advice from you.

So far 5 questions:

1. Which way does one get the input character from an event into a string or textbuffer and how to distinguish it from control keys like Esc, Tab and Enter?
2. SFML has sf::Utf to handle encodings, is there something similar in SDL2? Do I need an additional library like utfcpp?
3. What's the RenderUnicode function in SDL2_ttf?
http://www.libsdl.org/projects/SDL_ttf/ ... html#SEC45
There is already RenderUTF8. Is that UTF16 support?
4. What if only a part of the textbuffer should be shown? Does one render all the single glyphs?
5. How to handle non-monospaced fonts?

Thanks in advance!
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Getting text in SDL2

Postby Bertram » 19 Jun 2013, 10:32

Hi Sauer2, :)

I've got no big experiments so far with SDL2. Yet, I might be able to answer to a few ones.

About question 3: The parameters tell it, it's uint16 text so, as for me, yes it is UTF16 support.
SDL_Surface *TTF_RenderUNICODE_Solid(TTF_Font *font, const Uint16 *text, SDL_Color fg)

About the 4th one: The very function above returns a SDL_surface, which you can render as any image.
You'll have to, either adapted the text passed to the statement to show only pieces of it, or adapt your rendering code,
to show only part of the image returned.

Question 5: TTF (True Type Font) fonts can be non-monospaced. You even got functions in the SDL_TTF API to get the width and height of a glyph if necessary.

I hope I helped you a bit.

Best regards,
User avatar
Bertram
VT Moderator
 
Posts: 1652
Joined: 09 Nov 2012, 12:26

Re: Getting text in SDL2

Postby Buch » 19 Jun 2013, 11:53

Hi

I don't have much experience with text encodings, but I'll try answering what you asked - at least partially :)

#1
in SDL 1.2 you had the SDL_KEYDOWN (or SDL_KEYUP) event. As part of the keyboard event structure, you had the keysym structure, and as part of the keysym structure you had the unicode member, which contained the character representation of the pressed key (taking in account all modifiers - also remember to turn on unicode with SDL_EnableUNICODE). This worked, with some limitations on accented characters (don't really know why: some were correctly read, some were not). In conclusion, you could have done something like this:

{l Code}: {l Select All Code}
int programRunning = 1;
std::string text = "";

SDL_EnableUNICODE(SDL_ENABLE);

while (programRunning){
    while (SDL_PollEvent(&event)){
        if (event.type == SDL_KEYDOWN){
            char c = event.key.keysym.unicode;
            if (isprint(c)) text.push_back(c);
        }
    }
}


This could still have caused some problems with non-latin character sets (not sure about this - never tried -, though sounds very likely to me, since isprint function excludes special characters, like \n, \r, escape etc. but applies to ASCII only characters, probably excluding others).

Fortunately, those guys at SDL decided to pack a more flexible interface for text input with SDL 2. Less fortunately, I haven't had time yet to take a deep look at it, and the documentation about text input methods seems pretty unfinished (you can still take a look at the wiki pages: SDL events (notice text input and text editing events),keyboard support (again, methods relative to text input) and particularly the tutorial about text input). The new interface should take care of all the major issues of text editing for you, including international charsets.

#2
As for UTF, there should not be particular issues if you're reading UTF encoded text from a file and then you pass it straightaway to SDL_ttf. I had the problem of implementing UTF at a late development stage, and I had nothing to do but change the encoding of my text input files (which can be done simply via the text editor) and change the rendering function to TTF_RenderUTF8_Solid.
Something different might happen if you have to manipulate UNICODE text within your code. I'm not an expert about this, but maybe the wchar.h and wctype.h headers in the standard library might be useful :think:

#3
As far as I know (and as Bertram said), TTF_RenderUNICODE takes UTF16-encoded characters, since the argument is given as an Uint16 array.

#4
This question can have two meanings: you might want to print only a part of a single text line or to print only some of several text lines.

To render only a part of a string, you just have to pass only a part of that string to the rendering function. Let's say you have a string like "hello world!" and you want to print only the word "hello", you first create a string with only the required text (in our case, "hello") and then you render the text using one of the TTF_Render functions. This can be achieved either using the strncpy function from the standard library or using the std::string.substr function if you're using C++ and std::string.

If you meant how to print only some lines out of many, remember that SDL_ttf prints only one line at a time. That is: newline characters will just be drawn on the same line as empty rectangles. To handle multiple lines, you have to manually split the single string into lines and then render and print them separately on different y coordinates. For example, a string like "a\nb\nc\n" should be first split into the three strings "a", "b" and "c" and then the three strings should be rendered individually. Once you have done this, you can simply tell your program to print only from line a to line b, without printing the others...

#5
Bertram already answered this: TTF format handles natively both monospaced and non-monospaced fonts, and you don't have to worry about this at all.

Hope this was useful!
User avatar
Buch
 
Posts: 51
Joined: 15 Jan 2013, 14:30
Location: Castel del Piano, Grosseto, Italy

Re: Getting text in SDL2

Postby Sauer2 » 19 Jun 2013, 16:37

Thank you, both of you. That was very helpful.
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Getting text in SDL2

Postby Sauer2 » 21 Jun 2013, 13:56

It seems, there is a new event type textinput for text editing to ease the pain:
http://wiki.libsdl.org/moin.fcg/Tutorials/TextInput
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Who is online

Users browsing this forum: No registered users and 1 guest