Hi
I don't have much experience with text encodings, but I'll try answering what you asked - at least partially
#1in 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.
#2As 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
#3As far as I know (and as Bertram said), TTF_RenderUNICODE takes UTF16-encoded characters, since the argument is given as an Uint16 array.
#4This 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...
#5Bertram 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!