Archived

This topic is now archived and is closed to further replies.

sdlprorammer

SDL - display text

Recommended Posts

evillive2    779
Well, without using one of the font alternatives available from other SDL developers, you could always create a bitmap of a font and use it like you would blitting any other image. Load the bitmap onto a surface SDL_Surface *pFontSurface. Keep the character images in order and it will make things much easier on you. Then you can create an array of SDL_Rect array[256] (256 being the number of ascii characters) which holds the source rectangles for each character. When you want to write some text simply loop through the string and draw the character in the appropriate place.

char string[128];
SDL_Rect dest, copy;

strcpy( string, "Some text" );

// assuming you have some form of copyrect, setrect and offsetrect

// and each character is same size (true type fonts)

setrect( &dest, x, y, font_width, font_height );

for ( int x = 0; x<strlen(string); x++ )
{
// copy needs an & infront but it keeps showing © instead

copyrect( copy, &dest );
offsetrect( copy, x*font_width, 0 );
// SDL may alter the destination rect which is why we use a copy

SDL_BlitSurface( pFontSurface, &array[x], screen, copy );
}


Something like that is very generic and not very flexible with lots of room for optimization but should be fine for small amounts of text to be drawn to the screen like updating the score or a timer etc.

Evillive2
E-Mail

[edited by - evillive2 on May 25, 2004 5:52:45 PM]

[edited by - evillive2 on May 25, 2004 5:53:38 PM]

[edited by - evillive2 on May 25, 2004 5:54:48 PM]

[edited by - evillive2 on May 25, 2004 5:56:07 PM]

[edited by - evillive2 on May 25, 2004 5:56:42 PM]

Share this post


Link to post
Share on other sites
sdlprorammer    100
thanks for the help but...

graveyardfilla, in the sdl documentation http://sdldoc.csn.ul.ie/index.php
i don't find any ttf function. What do you means?

evillive2, i know i can do it with a picture, but i don't want to. I don't want to send to someone a picture with letters, when i want to give him my .exe. It is so stupid Is there any other ways? Maybe a function from windows.h> ( especially this one )?

thanks..

[edited by - sdlprorammer on May 26, 2004 7:33:04 AM]

Share this post


Link to post
Share on other sites
TomasH    360
quote:
Original post by sdlprorammer
thanks for the help but...

graveyardfilla, in the sdl documentation http://sdldoc.csn.ul.ie/index.php
i don''t find any ttf function. What do you means?


SDL_ttf is a library for TTF fonts with SDL:
http://www.libsdl.org/projects/SDL_ttf/

Share this post


Link to post
Share on other sites
incubator01    122
SDL_TTF is your best solution, i''m doing this stuff too right now
documentation is somewhere on gamedev.net, i used google but no longer have the url''s since I saved them for later.

I dont know if it is the best way for opengl, but if you plan to make 2d stuff, it is your best bet.
(unless you want to use the Win32 function CreateFont or something or use the glx functions)

Share this post


Link to post
Share on other sites
ff8    134
you can do a Release from your project and put all including file like .bmp

Share this post


Link to post
Share on other sites