How do I automatically center text using SDL_TTF?

How do I automatically center text using SDL_TTF ???

Hi,

I am using “SDL_ttf-2.0.7” (an S.D.L. based text engine).

I would like to automatically center text in the horizontal plane,
any one know how to do this using this SDL_TTF ???

I looked through the SDL_TTF user manual and did not see any centering
functions.

Thanks in advance!

My SDL based PC Windows Tetris game project:
http://www.silentheroproductions.com/tetribattle3_100percentforpcwindows.htm

(http://www.silentheroproductions.com/tetribattle3_100percentforpcwindows.htm)
(TetriBattle 3 100%[TM])

Jesse “JeZ+Lee” Palser, BS, A+, Network+, MCP
SLNTHERO at AOL.com
Silent Hero Productions ®
"Fun, little games for when you don’t have time to save the world.[TM]"
www.SilentHeroProductions.com (http://www.silentheroproductions.com/)

You need to use TTF_SizeText() to figure out how many pixels wide your
output will be, then position your output yourself within the total
space you have for the text. Basically, the starting x coordinate is:

( <target_xsize> - <text_xsize> ) / 2

…and you will probably want to check to be sure the size returned by
TTF_SizeText() ("<text_xsize>" above) is less that the width in pixels
of the area you are drawing to ("<target_xsize>" above).On Wed, Apr 19, 2006 at 08:29:03AM -0400, SlntHero at aol.com wrote:

How do I automatically center text using SDL_TTF ???


Steaphan Greene
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060419/a87dfca4/attachment.pgp

How do I automatically center text using SDL_TTF ???

The render functions from SDL_TTF returns a SDL_Surface. You can use its
width and height to center it on the screen.

TTF_Font* font = TTF_OpenFont(“times.ttf”, 14);
// …
SDL_Color color = { 255, 0, 0 };
SDL_Surface* text = TTF_RenderText_Solid(font, “Hello World!”, color);
SDL_Rect dst = { (screen->w + text->w) / 2, (screen->h + text->h) / 2, 0, 0 };
SDL_BlitSurface(text, NULL, screen, &dst);
SDL_FreeSurface(text);
// …
TTF_CloseFont(font);

You may want to make wrapper functions, especially since you’re using C++.
You’ll have to use your own code to support ASCII flow characters (like ‘\n’)
You should also notice that there are more kinds of fonts, like using an
image and blit parts of it, or drawing font in the code.
Either way, try to spent time investigating how to do stuff yourself. There
are a couple of books and a few good SDL websites that you can find out from
the main website. In the end, you’ll learn so much more.

By the way, you asked people to try out your game… This just isn’t the
right place for it… And besides people here won’t run a binary. Ignoring
the fact that probably most people here don’t even use Windows, with the
ammount of social engineering this days, nobody will run a binary that some
dude sent in an email. Besides, programmers very much prefer to play with the
source code, than with the game itself.

Cheers,
RicardoEm Quarta, 19 de Abril de 2006 13:29, o SlntHero at aol.com escreveu:

Hi,

I am using “SDL_ttf-2.0.7” (an S.D.L. based text engine).

I would like to automatically center text in the horizontal plane,
any one know how to do this using this SDL_TTF ???

I looked through the SDL_TTF user manual and did not see any centering
functions.

Thanks in advance!

My SDL based PC Windows Tetris game project:
http://www.silentheroproductions.com/tetribattle3_100percentforpcwindows.h
tm

(http://www.silentheroproductions.com/tetribattle3_100percentforpcwindows.h
tm) (TetriBattle 3 100%[TM])

Jesse “JeZ+Lee” Palser, BS, A+, Network+, MCP
SLNTHERO at AOL.com
Silent Hero Productions ®
"Fun, little games for when you don’t have time to save the world.[TM]"
www.SilentHeroProductions.com (http://www.silentheroproductions.com/)


The human animal differs from the lesser primates in his passion for
lists of “Ten Best”.
– H. Allen Smith