This is how I use SDL text

Started by
10 comments, last by Davaris 20 years ago
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
"I am a pitbull on the pantleg of opportunity."George W. Bush
Advertisement
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
------------------------------Put THAT in your smoke and pipe it
This a a neat method. Now how would you display the value of a variable ?
------------------------------Put THAT in your smoke and pipe it
quote:Original post by Drizzt DoUrden
Now how would you display the value of a variable ?

By using the standard C or C++ libraries. An example in C:
  int SomeValue = 12345;char SomeString[12];sprintf(SomeString,"%d",SomeValue);  


[Resist Windows XP''s Invasive Production Activation Technology!]
Thanks, but darnit Null and Void, why do you have to respond so fast. I was on my way back to edit my post (I just figured out that my integer had to be converted to a char).
------------------------------Put THAT in your smoke and pipe it
Thanks a lot guys !!

I''ve been looking for this for days !!
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
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

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

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, <a href="http://www.charlesriver.com/titles/larameegames.html">Game Design Methods</a> , Charles River Media (coming April/May 2002)
Online column - Design Corner at Pixelate

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

Drew Sikora
Executive Producer
GameDev.net

anyone here knows how to make sdl_ttf work with an opengl window?

This topic is closed to new replies.

Advertisement