Rendering centered text

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 !

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” <jeroen.clarysse at telenet.be>:

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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

the first suggestion will still keep all text left-aligned, just on a smaller width :frowning:

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 !> 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” <@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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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” <jeroen.clarysse at telenet.be>:

the first suggestion will still keep all text left-aligned, just on a
smaller width :frowning:

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 !

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” <
jeroen.clarysse at telenet.be>:
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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Do you want your text to appear like this: http://tinypic.com/view.php?pic=2j1n76c&s=8#.VRVXXuHn5Ms ?