# Rendering centered text

**URL:** <https://discourse.libsdl.org/t/rendering-centered-text/21308>\
**Category:** SDL Development\
**Created:** [March 27, 2015, 12:33pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308 "2015-03-27T12:33:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeroen\_Clarysse](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@Jeroen\_Clarysse](https://discourse.libsdl.org/u/Jeroen_Clarysse)\
**Post date:** [March 27, 2015, 12:33pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308/1 "2015-03-27T12:33:00Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![Bob\_Rubbens](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/bob_rubbens/32/2626_2.png) [@Bob\_Rubbens](https://discourse.libsdl.org/u/Bob_Rubbens)\
**Post date:** [March 27, 2015, 12:53pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308/2 "2015-03-27T12:53:11Z")

</div>

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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Jeroen\_Clarysse](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@Jeroen\_Clarysse](https://discourse.libsdl.org/u/Jeroen_Clarysse)\
**Post date:** [March 27, 2015, 12:59pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308/3 "2015-03-27T12:59:43Z")

</div>

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 !\> 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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Bob\_Rubbens](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/bob_rubbens/32/2626_2.png) [@Bob\_Rubbens](https://discourse.libsdl.org/u/Bob_Rubbens)\
**Post date:** [March 27, 2015, 1:04pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308/4 "2015-03-27T13:04:06Z")

</div>

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 ☹
> 
> 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)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [March 27, 2015, 1:14pm UTC](https://discourse.libsdl.org/t/rendering-centered-text/21308/5 "2015-03-27T13:14:01Z")

</div>

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