rendering centered text |
jeroen.clarysse
|
hi all,
i'm trying to figure out how I can render a given char* as centered text i use this call currently : SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(msg_font, msg, msg_color, screen_width); in which msg_font is ptr to a TTF_Font object, msg is a char* ptr, msg_color is a ptr to an SDL_Color object and screen_width is calculated as the width of the current SDL window msg is usually 3 to 6 lines of text, generated from internal code (so NOT read from an RTF file or something like that) I'm trying to figure out how I can make the text-align set to centered` any help would be greatly appreciated ! _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||
|
rendering centered text |
Bob Rubbens
Guest
|
Just set screen_width to a portion of your screen width, and place the resulting surface more to the right? Or generate each line of the text seperately (using just TTF_RenderText_Blended()) and align it yourself? Op 27 mrt. 2015 13:33 schreef "jeroen clarysse":
|
|||||||||||||
|
rendering centered text |
jeroen.clarysse
|
the first suggestion will still keep all text left-aligned, just on a smaller width
the 2nd suggestion would require the feature to calculate the width of each line, so I can position them properly I was hoping there was a way to do this via SDL_RTF, but that library seems not to be SDL2 compatble (and contains no XCode nor VisualC project file... which I need both) thanks for the reply anyway !
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
rendering centered text |
Bob Rubbens
Guest
|
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.) Op 27 mrt. 2015 13:59 schreef "jeroen clarysse":
|
|||||||||||||||
|
Naith
|
Do you want your text to appear like this: http://tinypic.com/view.php?pic=2j1n76c&s=8#.VRVXXuHn5Ms ?
|
|||||||||||
|
rendering centered text |
jeroen.clarysse
|
sorry for the late reply !
indeed, that is what I want to achieve ! When each line is a "single" line of thext (= no wrapped text), this is simple. However if the text is a big block that gets wrapped over multiple lines, I don't know how to measure each broken-line individually and then shift it to the right
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
rendering centered text |
jeroen.clarysse
|
like I said in my previous reply : this only works if the text isn't wrapped automatically I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ? _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
rendering centered text |
Jonny D
|
You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):https://github.com/grimfang4/nfont
It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory. Jonny D On Sunday, March 29, 2015, jeroen clarysse wrote:
|
|||||||||||||
|
Re: rendering centered text |
rendering centered text |
jeroen.clarysse
|
it is a nice routine indeed, but it suffers from the drawback I described : you can not use wrapping...
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
rendering centered text |
jeroen.clarysse
|
this looks very promising !!!
thanks for the link
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||
|
rendering centered text |
jeroen.clarysse
|
ah, now I see alas that the NFont library is ascii only... no unicode support
does anyone know if SDL_RTF is operational on both windows and mac ? I find no sln or xcode file in the download folder ...
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||
|
rendering centered text |
Pallav Nawani
|
What you can do is:
(a) Pre-Process the text to separate it into lines (b) Center & render each line individually. If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines. Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position. int aX = (total_width - max_text_width)/2; Pallav Nawani IronCode Gaming Private Limited Website:Â http://www.ironcode.com Twitter:Â http://twitter.com/Ironcode_Gaming Facebook: http://www.facebook.com/Ironcode.Gaming Mobile: 9997478768 On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
|
|||||||||||||||||
|
rendering centered text |
jeroen.clarysse
|
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ and then the final text is split up by blitting each of these characters separately from that surface. this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||||
|
rendering centered text |
Pallav Nawani
|
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.
Pallav Nawani IronCode Gaming Private Limited Website:Â http://www.ironcode.com Twitter:Â http://twitter.com/Ironcode_Gaming Facebook: http://www.facebook.com/Ironcode.Gaming Mobile: 9997478768 On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
|
|||||||||||||||||||
|
rendering centered text |
Bob Rubbens
Guest
|
Afaik SDL_ttf supports unicode just fine: http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC53
On 30 March 2015 at 09:59, jeroen clarysse wrote:
|
|||||||||||||||||||
|
rendering centered text |
jeroen.clarysse
|
SDL_TTF does, but the code in NFont doesn't : it generates individual glyphs for each character, and then blits char-per-char to make centered txt. Only the list of chars below is "prepared" as glyps. As a result, other chars won't work
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||||||
|
rendering centered text |
jeroen.clarysse
|
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.
right now I see two options : a) get SDL_RTF working b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||||||
|
rendering centered text |
Bob Rubbens
Guest
|
Just split a long std::string into an array of std::string, render each element of the array with SDL_ttf, store resulting surfaces in another array, and finally render each surface independently. You can center every surface independently by subtracting half the width of each surface from the x position.
Splitting a std::string into an array of std::string can be done by just splitting the string each 50 characters. This can be improved by splitting on the first space before or after the 50-char breakpoint, but that's the basic idea. If your string is in unicode it is probably a bit harder but still doable without any library. It's not perfect but it probably works just fine if you keep in mind your own requirements. Op 30 mrt. 2015 13:16 schreef "jeroen clarysse":
|
|||||||||||||||||||||
|
rendering centered text |
jeroen.clarysse
|
hm.. if eel like reinventing the weel a bit with that routine : SDL_redertext() does more or less the same... only without alignment
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||||||||
|
rendering centered text |
Jonny D
|
NFont does support Unicode. It caches the glyphs as you said, but if you are using SDL_ttf to load a Unicode font, then NFont will dynamically load the glyphs it needs as they are used.
Jonny D On Mon, Mar 30, 2015 at 8:26 AM, jeroen clarysse wrote:
|
|||||||||||||||||||||||
|
rendering centered text |
Pallav Nawani
|
Wouldn't
TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h) or TTF_SizeUNICODE(TTF_Font *font, const Unit16 *text, int *w, int *h) Solve the problem of determining where to split? Pallav Nawani IronCode Gaming Private Limited Website:Â http://www.ironcode.com Twitter:Â http://twitter.com/Ironcode_Gaming Facebook: http://www.facebook.com/Ironcode.Gaming Mobile: 9997478768 On Mon, Mar 30, 2015 at 4:46 PM, jeroen clarysse wrote:
|
|||||||||||||||||||||
|