Archived

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

Davaris

This is how I use SDL text

Recommended Posts

I saw a few people were having trouble with it. So until Cone3D writes one of his tutes heres my main file. You need SDL_ttf and you can get it from the library sectin at http://www.libsdl.org/. Thanks for the corrections Drizzt. #include "SDL.h" #include "SDL_ttf.h" SDL_Surface *screen; void DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, screen, &dest); } int main( int argc, char* argv[] ) { TTF_Font* font; //initialize systems if (SDL_Init(SDL_INIT_VIDEO) <0) { printf("Unable to init SDL: %s\n", SDL_GetError()); return 1; } //set our at exit function atexit ( SDL_Quit ); //create a window screen = SDL_SetVideoMode ( 640, 480, 16, SDL_FULLSCREEN); if (screen == NULL) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); return 1; } TTF_Init (); // Need the .ttf file in the same directory as // the .exe or give it the full path. font = TTF_OpenFont("Antqua.ttf", 20); TTF_SetFontStyle (font, TTF_STYLE_NORMAL); SDL_Color fg = {255,255,255,0}; SDL_Color bg = {255,0,255,0}; SDL_Surface *text1; SDL_Surface *text2; text1 = TTF_RenderText_Solid(font, "Hello World 1", fg); text2 = TTF_RenderText_Shaded(font, "Hello World 2", fg, bg); int done = 0; //declare event variable SDL_Event event ; //message pump while(done == 0) { DrawIMG(text1, 50, 100); DrawIMG(text2, 200, 200); //look for an event while ( SDL_PollEvent ( &event ) ) { //an event was found if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } SDL_Flip(screen); }//end of message pump //done TTF_CloseFont(font); // Don't forget to release the two text surfaces. // I forgot the name of the function return ( 0 ) ; } Edited by - Davaris on December 12, 2001 4:15:03 PM

Share this post


Link to post
Share on other sites
Its cool to see people helping here, it is also cool to see that people are realizing how awsome SDL is.

One thing, you fogot to declare SDL_Surface *screen; , and you forgot to add the DrawIMG() function to your file, yet you make 2 calls to it. Heres the function:


void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

Also, don't forget to add the sdl_ttf.lib to your project.

Edited by - Drizzt DoUrden on December 12, 2001 4:12:10 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks a lot guys !!

I''ve been looking for this for days !!

Share this post


Link to post
Share on other sites
Does the text on screen display a little bit "Shaky" to anyone... kinda hurts to look at it a while

*edit* when i run it in a window instead of fullscreen it shows up fine! arg!

Edited by - kajjait on March 5, 2002 8:37:50 PM

Share this post


Link to post
Share on other sites
One note. If you don''t wanna mess with Freetype and SDL_TTF, you could go with SFont.

http://www.linux-games.com/sfont/

You don''t have the size and color flexibility that you do with TrueType, but you can do nice stuff like shaded and textured text. The fonts that they''ve made for it are quite nice.

---
John Hattan
The Code Zone
Sweet software for a saturnine world

Share this post


Link to post
Share on other sites
I use SFont - much easier to deal with and plenty good for most needs.

_________________________________________________________________

Drew Sikora
A.K.A. Gaiiden

ICQ #: 70449988
AOLIM: DarkPylat

Blade Edge Software
Staff Member, GDNet
Public Relations, Game Institute

3-time Contributing author, [i]Game Design Methods [/i] , Charles River Media (coming April/May 2002)
Online column - [i]Design Corner [/i] at Pixelate

NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]

Share this post


Link to post
Share on other sites
There were two methods explained to me:

1. When using SDL_SetVideoMode() use the SDL_OPENGLBLIT flag with the others used for OpenGL. (Exactly how you render the text, I don''t know.)

2. Use the SDL_Surface obtained from TTF_RenderText_*() to create an OpenGL texture and putting that texture onto a quad on the screen. (This, I haven''t been able to get working either)

That''s something to think about.

Share this post


Link to post
Share on other sites